Skip to content

Commit

Permalink
Merge pull request #238 from clue-labs/windows
Browse files Browse the repository at this point in the history
Run test suite on Windows
  • Loading branch information
jsor authored Jun 23, 2020
2 parents 59730c4 + e846bbb commit 31967e9
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 55 deletions.
13 changes: 12 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,27 @@ matrix:
- php: 7.3
- php: 7.4
- php: hhvm-3.18
install: composer require phpunit/phpunit:^5 --dev --no-interaction # requires legacy phpunit
- name: Mac OS X
os: osx
language: generic
before_install:
- curl -s http://getcomposer.org/installer | php
- mv composer.phar /usr/local/bin/composer
- name: Windows
os: windows
language: bash
before_install:
- choco install php
- choco install composer
- export PATH="$(powershell -Command '("Process", "Machine" | % { [Environment]::GetEnvironmentVariable("PATH", $_) -Split ";" -Replace "\\$", "" } | Select -Unique | % { cygpath $_ }) -Join ":"')"
- php -r "file_put_contents(php_ini_loaded_file(),'extension_dir=ext'.PHP_EOL,FILE_APPEND);"
- php -r "file_put_contents(php_ini_loaded_file(),'extension=sockets'.PHP_EOL,FILE_APPEND);"
install:
- composer install
allow_failures:
- php: hhvm-3.18
- os: osx
- os: windows

sudo: false

Expand Down
10 changes: 9 additions & 1 deletion tests/FunctionalSecureServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use React\Socket\SecureConnector;
use React\Socket\ServerInterface;
use React\Socket\SecureServer;
use React\Socket\TcpServer;
use React\Socket\TcpConnector;
use React\Socket\TcpServer;

class FunctionalSecureServerTest extends TestCase
{
Expand Down Expand Up @@ -549,6 +549,10 @@ public function testServerEmitsErrorForClientWithInvalidCertificate()

public function testEmitsErrorForServerWithEncryptedCertificateMissingPassphrase()
{
if (DIRECTORY_SEPARATOR === '\\') {
$this->markTestSkipped('Not supported on Windows');
}

$loop = Factory::create();

$server = new TcpServer(0, $loop);
Expand All @@ -569,6 +573,10 @@ public function testEmitsErrorForServerWithEncryptedCertificateMissingPassphrase

public function testEmitsErrorForServerWithEncryptedCertificateWithInvalidPassphrase()
{
if (DIRECTORY_SEPARATOR === '\\') {
$this->markTestSkipped('Not supported on Windows');
}

$loop = Factory::create();

$server = new TcpServer(0, $loop);
Expand Down
4 changes: 4 additions & 0 deletions tests/FunctionalTcpServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ public function testEmitsConnectionWithLocalIp()

public function testEmitsConnectionWithLocalIpDespiteListeningOnAll()
{
if (DIRECTORY_SEPARATOR === '\\') {
$this->markTestSkipped('Skipping on Windows due to default firewall rules');
}

$loop = Factory::create();

$server = new TcpServer('0.0.0.0:0', $loop);
Expand Down
46 changes: 0 additions & 46 deletions tests/HappyEyeBallsConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,52 +178,6 @@ public function testIpv6DoesntResolvesWhileIpv4DoesFirstSoIpv4Connects(array $ip
$this->loop->run();
}

public function testThatTheIpv4ConnectionWillWait100MilisecondsWhenIpv6AndIpv4ResolveSimultaniously()
{
$timings = array();
$this->resolver->expects($this->at(0))->method('resolveAll')->with('google.com', Message::TYPE_AAAA)->will($this->returnValue(Promise\resolve(array('1:2:3:4'))));
$this->resolver->expects($this->at(1))->method('resolveAll')->with('google.com', Message::TYPE_A)->will($this->returnValue(Promise\resolve(array('1.2.3.4'))));
$this->tcp->expects($this->at(0))->method('connect')->with($this->equalTo('scheme://[1:2:3:4]:80/?hostname=google.com'))->will($this->returnCallback(function () use (&$timings) {
$timings[Message::TYPE_AAAA] = microtime(true);

return new Promise\Promise(function () {});
}));
$this->tcp->expects($this->at(1))->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/?hostname=google.com'))->will($this->returnCallback(function () use (&$timings) {
$timings[Message::TYPE_A] = microtime(true);

return new Promise\Promise(function () {});
}));

$this->connector->connect('scheme://google.com:80/?hostname=google.com');

$this->loop->run();

self::assertGreaterThan(0.01, $timings[Message::TYPE_A] - $timings[Message::TYPE_AAAA]);
}

/**
* @dataProvider provideIpvAddresses
*/
public function testAssert100MilisecondsBetweenConnectionAttempts(array $ipv6, array $ipv4)
{
$timings = array();
$this->resolver->expects($this->at(0))->method('resolveAll')->with('google.com', Message::TYPE_AAAA)->will($this->returnValue(Promise\resolve($ipv6)));
$this->resolver->expects($this->at(1))->method('resolveAll')->with('google.com', Message::TYPE_A)->will($this->returnValue(Promise\resolve($ipv4)));
$this->tcp->expects($this->any())->method('connect')->with($this->stringContains(':80/?hostname=google.com'))->will($this->returnCallback(function () use (&$timings) {
$timings[] = microtime(true);

return new Promise\Promise(function () {});
}));

$this->connector->connect('scheme://google.com:80/?hostname=google.com');

$this->loop->run();

for ($i = 0; $i < (count($timings) - 1); $i++) {
self::assertGreaterThan(0.01, $timings[$i + 1] - $timings[$i]);
}
}

/**
* @dataProvider provideIpvAddresses
*/
Expand Down
5 changes: 4 additions & 1 deletion tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ function ($e) use (&$wait) {
if ($wait) {
Block\sleep(0.2, $loop);
if ($wait) {
$this->fail('Connection attempt did not fail');
Block\sleep(2.0, $loop);
if ($wait) {
$this->fail('Connection attempt did not fail');
}
}
}
unset($promise);
Expand Down
2 changes: 1 addition & 1 deletion tests/TcpConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class TcpConnectorTest extends TestCase
{
const TIMEOUT = 0.1;
const TIMEOUT = 5.0;

/**
* @test
Expand Down
31 changes: 27 additions & 4 deletions tests/TcpServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
use React\EventLoop\Factory;
use React\Socket\TcpServer;
use React\Stream\DuplexResourceStream;
use React\Promise\Promise;

class TcpServerTest extends TestCase
{
const TIMEOUT = 5.0;

private $loop;
private $server;
private $port;
Expand All @@ -33,13 +36,18 @@ public function setUp()
/**
* @covers React\Socket\TcpServer::handleConnection
*/
public function testConnection()
public function testServerEmitsConnectionEventForNewConnection()
{
$client = stream_socket_client('tcp://localhost:'.$this->port);

$this->server->on('connection', $this->expectCallableOnce());
$server = $this->server;
$promise = new Promise(function ($resolve) use ($server) {
$server->on('connection', $resolve);
});

$this->tick();
$connection = Block\await($promise, $this->loop, self::TIMEOUT);

$this->assertInstanceOf('React\Socket\ConnectionInterface', $connection);
}

/**
Expand Down Expand Up @@ -270,7 +278,9 @@ public function testEmitsErrorWhenAcceptListenerFails()
$server->on('error', $this->expectCallableOnceWith($this->isInstanceOf('RuntimeException')));

$this->assertNotNull($listener);
$listener(false);
$socket = stream_socket_server('tcp://127.0.0.1:0');
fclose($socket);
$listener($socket);
}

/**
Expand All @@ -295,8 +305,21 @@ public function tearDown()
}
}

/**
* This methods runs the loop for "one tick"
*
* This is prone to race conditions and as such somewhat unreliable across
* different operating systems. Running the loop until the expected events
* fire is the preferred alternative.
*
* @deprecated
*/
private function tick()
{
if (DIRECTORY_SEPARATOR === '\\') {
$this->markTestSkipped('Not supported on Windows');
}

Block\sleep(0, $this->loop);
}
}
4 changes: 4 additions & 0 deletions tests/UnixConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public function testInvalidScheme()

public function testValid()
{
if (!in_array('unix', stream_get_transports())) {
$this->markTestSkipped('Unix domain sockets (UDS) not supported on your platform (Windows?)');
}

// random unix domain socket path
$path = sys_get_temp_dir() . '/test' . uniqid() . '.sock';

Expand Down
4 changes: 3 additions & 1 deletion tests/UnixServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ public function testEmitsErrorWhenAcceptListenerFails()
$server->on('error', $this->expectCallableOnceWith($this->isInstanceOf('RuntimeException')));

$this->assertNotNull($listener);
$listener(false);
$socket = stream_socket_server('tcp://127.0.0.1:0');
fclose($socket);
$listener($socket);
}

/**
Expand Down

0 comments on commit 31967e9

Please sign in to comment.