diff --git a/.travis.yml b/.travis.yml index 157e9dd..0922767 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: php -sudo: false +sudo: required cache: directories: diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index 50b4fe3..3bc67d2 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -12,6 +12,11 @@ public function startServer($name) { $filename = __DIR__ . '/server/' . $name . '.php'; $pipes = []; + + if (!Semaphore::acquire()) { + $this->fail('Could not connect to server'); + } + $this->servers[$name] = proc_open('php '. $filename, [], $pipes); sleep(1); } @@ -28,5 +33,7 @@ public function tearDown() foreach (array_keys($this->servers) as $name) { $this->stopServer($name); } + + Semaphore::release(); } } diff --git a/tests/Semaphore.php b/tests/Semaphore.php new file mode 100644 index 0000000..ea83010 --- /dev/null +++ b/tests/Semaphore.php @@ -0,0 +1,40 @@ + + */ +final class Semaphore +{ + private static $openConnections = 0; + + /** + * Busy waiting for my turn to go. + * + * @return bool + */ + public static function acquire() + { + $tries = 1; + while (self::$openConnections > 0) { + sleep($tries++); + if ($tries > 5) { + return false; + } + } + self::$openConnections++; + + return true; + } + + /** + * Signal that you are done. + */ + public static function release() + { + // Do no be too quick + usleep(500000); + self::$openConnections--; + } +} diff --git a/tests/StreamTest.php b/tests/StreamTest.php index 910c6e2..2421fb1 100644 --- a/tests/StreamTest.php +++ b/tests/StreamTest.php @@ -2,6 +2,7 @@ namespace Http\Client\Socket\Tests; +use Http\Client\Socket\Exception\TimeoutException; use Http\Client\Socket\Stream; use PHPUnit\Framework\TestCase; @@ -108,18 +109,16 @@ public function testIsReadable() $this->assertTrue($stream->isReadable()); } + /** + * @expectedException \Http\Client\Socket\Exception\TimeoutException + */ public function testTimeout() { $socket = fsockopen("php.net", 80); socket_set_timeout($socket, 0, 100); - $stream = new Stream($socket); - - try { - $stream->getContents(); - } catch (\Exception $e) { - $this->assertInstanceOf('Http\Socket\Exception\TimeoutException', $e); - } + $stream = new Stream($socket, 50); + $stream->getContents(); } public function testMetadatas() diff --git a/tests/server/ssl/file.srl b/tests/server/ssl/file.srl index fe909bb..da51c42 100644 --- a/tests/server/ssl/file.srl +++ b/tests/server/ssl/file.srl @@ -1 +1 @@ -2D +2F diff --git a/tests/server/tcp-bugous-server.php b/tests/server/tcp-bugous-server.php index 72b0946..7f0d91f 100644 --- a/tests/server/tcp-bugous-server.php +++ b/tests/server/tcp-bugous-server.php @@ -1,6 +1,9 @@ [ 'local_cert' => __DIR__ . '/ssl/server-and-key.pem', @@ -44,3 +46,4 @@ while (!@feof($client)) { @fread($client, 1000); } +\Http\Client\Socket\Tests\Semaphore::release(); diff --git a/tests/server/tcp-ssl-server.php b/tests/server/tcp-ssl-server.php index de3af5a..2d5a22e 100644 --- a/tests/server/tcp-ssl-server.php +++ b/tests/server/tcp-ssl-server.php @@ -1,5 +1,7 @@ [ 'local_cert' => __DIR__ . '/ssl/server-and-key.pem' @@ -30,8 +32,8 @@ } - - while (!@feof($client)) { @fread($client, 1000); } + +\Http\Client\Socket\Tests\Semaphore::release(); diff --git a/tests/server/unix-domain-server.php b/tests/server/unix-domain-server.php index d6954b0..6221b98 100644 --- a/tests/server/unix-domain-server.php +++ b/tests/server/unix-domain-server.php @@ -1,5 +1,7 @@