diff --git a/README.md b/README.md index fd21f72..58e0c6c 100644 --- a/README.md +++ b/README.md @@ -65,18 +65,34 @@ the [examples](examples) to get started. ### Factory The `Factory` is responsible for creating your [`Client`](#client) instance. -It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage). ```php -$loop = \React\EventLoop\Factory::create(); -$factory = new Factory($loop); +$factory = new Clue\React\Quassel\Factory(); ``` -If you need custom DNS, proxy or TLS settings, you can explicitly pass a -custom instance of the [`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface): +This class takes an optional `LoopInterface|null $loop` parameter that can be used to +pass the event loop instance to use for this object. You can use a `null` value +here in order to use the [default loop](https://github.com/reactphp/event-loop#loop). +This value SHOULD NOT be given unless you're sure you want to explicitly use a +given event loop instance. + +If you need custom connector settings (DNS resolution, TLS parameters, timeouts, +proxy servers etc.), you can explicitly pass a custom instance of the +[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface): ```php -$factory = new Factory($loop, $connector); +$connector = new React\Socket\Connector(null, array( + 'dns' => '127.0.0.1', + 'tcp' => array( + 'bindto' => '192.168.10.1:0' + ), + 'tls' => array( + 'verify_peer' => false, + 'verify_peer_name' => false + ) +)); + +$factory = new Clue\React\Quassel\Factory(null, $connector); ``` #### createClient() diff --git a/composer.json b/composer.json index 1181511..3ffa239 100644 --- a/composer.json +++ b/composer.json @@ -19,10 +19,10 @@ "require": { "php": ">=5.3", "clue/qdatastream": "^0.8", - "react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3", + "react/event-loop": "^1.2", "react/promise": "~2.0|~1.1", - "react/socket": "^1.0 || ^0.8 || ^0.7", - "react/stream": "^1.0 || ^0.7 || ^0.6 || ^0.5 || ^0.4.6" + "react/socket": "^1.8", + "react/stream": "^1.2" }, "require-dev": { "clue/block-react": "^1.1", diff --git a/examples/01-channels.php b/examples/01-channels.php index 2d23324..f0ba8bc 100644 --- a/examples/01-channels.php +++ b/examples/01-channels.php @@ -17,8 +17,7 @@ echo 'Password: '; $pass = trim(fgets(STDIN)); -$loop = \React\EventLoop\Factory::create(); -$factory = new Factory($loop); +$factory = new Factory(); $uri = rawurlencode($user) . ':' . rawurlencode($pass) . '@' . $host; @@ -90,5 +89,3 @@ })->then(null, function ($e) { echo $e; }); - -$loop->run(); diff --git a/examples/02-chatbot.php b/examples/02-chatbot.php index 7aa3ba2..92ea8b9 100644 --- a/examples/02-chatbot.php +++ b/examples/02-chatbot.php @@ -25,8 +25,7 @@ die('Keyword MUST contain at least 3 characters to avoid excessive spam'); } -$loop = \React\EventLoop\Factory::create(); -$factory = new Factory($loop); +$factory = new Factory(); $uri = rawurlencode($user) . ':' . rawurlencode($pass) . '@' . $host; @@ -61,5 +60,3 @@ })->then(null, function ($e) { echo $e; }); - -$loop->run(); diff --git a/examples/03-pingbot.php b/examples/03-pingbot.php index 9eaf929..0e399c6 100644 --- a/examples/03-pingbot.php +++ b/examples/03-pingbot.php @@ -19,8 +19,7 @@ echo 'Password: '; $pass = trim(fgets(STDIN)); -$loop = \React\EventLoop\Factory::create(); -$factory = new Factory($loop); +$factory = new Factory(); $uri = rawurlencode($user) . ':' . rawurlencode($pass) . '@' . $host; @@ -127,5 +126,3 @@ })->then(null, function ($e) { echo $e; }); - -$loop->run(); diff --git a/examples/04-connect.php b/examples/04-connect.php index d603575..8b27ca5 100644 --- a/examples/04-connect.php +++ b/examples/04-connect.php @@ -19,8 +19,7 @@ echo 'Password: '; $pass = trim(fgets(STDIN)); -$loop = \React\EventLoop\Factory::create(); -$factory = new Factory($loop); +$factory = new Factory(); $uri = rawurlencode($user) . ':' . rawurlencode($pass) . '@' . $host; @@ -88,5 +87,3 @@ })->then(null, function ($e) { echo $e; }); - -$loop->run(); diff --git a/examples/05-backlog.php b/examples/05-backlog.php index e5339f3..8d0f72c 100644 --- a/examples/05-backlog.php +++ b/examples/05-backlog.php @@ -22,8 +22,7 @@ echo 'Channel to export (empty=all): '; $channel = trim(fgets(STDIN)); -$loop = \React\EventLoop\Factory::create(); -$factory = new Factory($loop); +$factory = new Factory(); $uri = rawurlencode($user) . ':' . rawurlencode($pass) . '@' . $host; @@ -96,5 +95,3 @@ })->then(null, function ($e) { echo $e; }); - -$loop->run(); diff --git a/examples/11-debug.php b/examples/11-debug.php index a7249cd..0ea3914 100644 --- a/examples/11-debug.php +++ b/examples/11-debug.php @@ -2,7 +2,6 @@ use Clue\React\Quassel\Factory; use Clue\React\Quassel\Client; -use Clue\React\Quassel\Io\Protocol; use Clue\React\Quassel\Models\BufferInfo; require __DIR__ . '/../vendor/autoload.php'; @@ -19,8 +18,7 @@ echo 'Password: '; $user['password'] = trim(fgets(STDIN)); -$loop = \React\EventLoop\Factory::create(); -$factory = new Factory($loop); +$factory = new Factory(); echo '[1/5] Connecting' . PHP_EOL; $factory->createClient($host)->then(function (Client $client) use ($user) { @@ -110,5 +108,3 @@ })->then(null, function ($e) { echo $e; }); - -$loop->run(); diff --git a/src/Factory.php b/src/Factory.php index 648ae81..6adb0b8 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -4,6 +4,7 @@ use Clue\React\Quassel\Io\Prober; use Clue\React\Quassel\Io\Protocol; +use React\EventLoop\Loop; use React\EventLoop\LoopInterface; use React\Promise; use React\Socket\ConnectorInterface; @@ -13,15 +14,24 @@ class Factory { - public function __construct(LoopInterface $loop, ConnectorInterface $connector = null, Prober $prober = null) + /** @var LoopInterface */ + private $loop; + + /** @var ConnectorInterface */ + private $connector; + + /** @var Prober */ + private $prober; + + public function __construct(LoopInterface $loop = null, ConnectorInterface $connector = null, Prober $prober = null) { + $this->loop = $loop ?: Loop::get(); if ($connector === null) { $connector = new Connector($loop); } if ($prober === null) { $prober = new Prober(); } - $this->loop = $loop; $this->connector = $connector; $this->prober = $prober; } diff --git a/tests/FactoryIntegrationTest.php b/tests/FactoryIntegrationTest.php index 7f32f78..d003bf0 100644 --- a/tests/FactoryIntegrationTest.php +++ b/tests/FactoryIntegrationTest.php @@ -5,32 +5,28 @@ use Clue\React\Block; use Clue\React\Quassel\Client; use Clue\React\Quassel\Factory; -use React\EventLoop\Factory as LoopFactory; +use Clue\React\Quassel\Io\Protocol; +use React\EventLoop\Loop; use React\Socket\Server; use React\Socket\ConnectionInterface; -use Clue\React\Quassel\Io\Protocol; class FactoryIntegrationTest extends TestCase { public function testCreateClientCreatesConnection() { - $loop = LoopFactory::create(); - - $server = new Server(0, $loop); + $server = new Server(0); $server->on('connection', $this->expectCallableOnce()); $uri = str_replace('tcp://', '', $server->getAddress()); - $factory = new Factory($loop); + $factory = new Factory(); $promise = $factory->createClient($uri); - Block\sleep(0.1, $loop); + Block\sleep(0.1, Loop::get()); } public function testCreateClientSendsProbeOverConnection() { - $loop = LoopFactory::create(); - - $server = new Server(0, $loop); + $server = new Server(0); $data = $this->expectCallableOnceWith("\x42\xb3\x3f\x00" . "\x00\x00\x00\x02" . "\x80\x00\x00\x01"); $server->on('connection', function (ConnectionInterface $conn) use ($data) { @@ -38,17 +34,15 @@ public function testCreateClientSendsProbeOverConnection() }); $uri = str_replace('tcp://', '', $server->getAddress()); - $factory = new Factory($loop); + $factory = new Factory(); $promise = $factory->createClient($uri); - Block\sleep(0.1, $loop); + Block\sleep(0.1, Loop::get()); } public function testCreateClientResolvesIfServerRespondsWithProbeResponse() { - $loop = LoopFactory::create(); - - $server = new Server(0, $loop); + $server = new Server(0); $server->on('connection', function (ConnectionInterface $conn) { $conn->on('data', function () use ($conn) { @@ -57,10 +51,10 @@ public function testCreateClientResolvesIfServerRespondsWithProbeResponse() }); $uri = str_replace('tcp://', '', $server->getAddress()); - $factory = new Factory($loop); + $factory = new Factory(); $promise = $factory->createClient($uri); - $client = Block\await($promise, $loop, 10.0); + $client = Block\await($promise, Loop::get(), 10.0); $this->assertTrue($client instanceof Client); $client->close(); @@ -68,8 +62,7 @@ public function testCreateClientResolvesIfServerRespondsWithProbeResponse() public function testCreateClientCreatesSecondConnectionWithoutProbeIfConnectionClosesDuringProbe() { - $loop = LoopFactory::create(); - $server = new Server(0, $loop); + $server = new Server(0); $once = $this->expectCallableOnce(); $server->on('connection', function (ConnectionInterface $conn) use ($once) { @@ -80,16 +73,15 @@ public function testCreateClientCreatesSecondConnectionWithoutProbeIfConnectionC }); $uri = str_replace('tcp://', '', $server->getAddress()); - $factory = new Factory($loop); + $factory = new Factory(); $promise = $factory->createClient($uri); - Block\sleep(0.1, $loop); + Block\sleep(0.1, Loop::get()); } public function testCreateClientRejectsIfServerRespondsWithInvalidData() { - $loop = LoopFactory::create(); - $server = new Server(0, $loop); + $server = new Server(0); $server->on('connection', function (ConnectionInterface $conn) { $conn->on('data', function () use ($conn) { @@ -98,17 +90,16 @@ public function testCreateClientRejectsIfServerRespondsWithInvalidData() }); $uri = str_replace('tcp://', '', $server->getAddress()); - $factory = new Factory($loop); + $factory = new Factory(); $promise = $factory->createClient($uri); $this->setExpectedException('RuntimeException'); - Block\await($promise, $loop, 10.0); + Block\await($promise, Loop::get(), 10.0); } public function testCreateClientWithAuthSendsClientInitAfterProbe() { - $loop = LoopFactory::create(); - $server = new Server(0, $loop); + $server = new Server(0); $data = $this->expectCallableOnceWith($this->callback(function ($packet) { $data = FactoryIntegrationTest::decode($packet); @@ -124,16 +115,15 @@ public function testCreateClientWithAuthSendsClientInitAfterProbe() }); $uri = str_replace('tcp://', '', $server->getAddress()); - $factory = new Factory($loop); + $factory = new Factory(); $promise = $factory->createClient('user:pass@' . $uri); - Block\sleep(0.1, $loop); + Block\sleep(0.1, Loop::get()); } public function testCreateClientWithAuthRejectsIfServerClosesAfterClientInit() { - $loop = LoopFactory::create(); - $server = new Server(0, $loop); + $server = new Server(0); $server->on('connection', function (ConnectionInterface $conn) { $conn->on('data', function () use ($conn) { @@ -145,17 +135,16 @@ public function testCreateClientWithAuthRejectsIfServerClosesAfterClientInit() }); $uri = str_replace('tcp://', '', $server->getAddress()); - $factory = new Factory($loop); + $factory = new Factory(); $promise = $factory->createClient('user:pass@' . $uri); $this->setExpectedException('RuntimeException'); - Block\await($promise, $loop, 10.0); + Block\await($promise, Loop::get(), 10.0); } public function testCreateClientWithAuthRejectsIfServerSendsClientInitRejectAfterClientInit() { - $loop = LoopFactory::create(); - $server = new Server(0, $loop); + $server = new Server(0); $server->on('connection', function (ConnectionInterface $conn) { $conn->once('data', function () use ($conn) { @@ -172,17 +161,16 @@ public function testCreateClientWithAuthRejectsIfServerSendsClientInitRejectAfte }); $uri = str_replace('tcp://', '', $server->getAddress()); - $factory = new Factory($loop); + $factory = new Factory(); $promise = $factory->createClient('user:pass@' . $uri); $this->setExpectedException('RuntimeException'); - Block\await($promise, $loop, 10.0); + Block\await($promise, Loop::get(), 10.0); } public function testCreateClientWithAuthRejectsIfServerSendsUnknownMessageAfterClientInit() { - $loop = LoopFactory::create(); - $server = new Server(0, $loop); + $server = new Server(0); $server->on('connection', function (ConnectionInterface $conn) { $conn->once('data', function () use ($conn) { @@ -199,17 +187,16 @@ public function testCreateClientWithAuthRejectsIfServerSendsUnknownMessageAfterC }); $uri = str_replace('tcp://', '', $server->getAddress()); - $factory = new Factory($loop); + $factory = new Factory(); $promise = $factory->createClient('user:pass@' . $uri); $this->setExpectedException('RuntimeException'); - Block\await($promise, $loop, 10.0); + Block\await($promise, Loop::get(), 10.0); } public function testCreateClientWithAuthRejectsIfServerSendsInvalidTruncatedResponseAfterClientInit() { - $loop = LoopFactory::create(); - $server = new Server(0, $loop); + $server = new Server(0); $server->on('connection', function (ConnectionInterface $conn) { $conn->once('data', function () use ($conn) { @@ -221,17 +208,16 @@ public function testCreateClientWithAuthRejectsIfServerSendsInvalidTruncatedResp }); $uri = str_replace('tcp://', '', $server->getAddress()); - $factory = new Factory($loop); + $factory = new Factory(); $promise = $factory->createClient('user:pass@' . $uri); $this->setExpectedException('RuntimeException'); - Block\await($promise, $loop, 10.0); + Block\await($promise, Loop::get(), 10.0); } public function testCreateClientWithAuthRejectsIfServerSendsClientInitAckNotConfigured() { - $loop = LoopFactory::create(); - $server = new Server(0, $loop); + $server = new Server(0); $server->on('connection', function (ConnectionInterface $conn) { $conn->once('data', function () use ($conn) { @@ -247,17 +233,16 @@ public function testCreateClientWithAuthRejectsIfServerSendsClientInitAckNotConf }); $uri = str_replace('tcp://', '', $server->getAddress()); - $factory = new Factory($loop); + $factory = new Factory(); $promise = $factory->createClient('user:pass@' . $uri); $this->setExpectedException('RuntimeException'); - Block\await($promise, $loop, 10.0); + Block\await($promise, Loop::get(), 10.0); } public function testCreateClientWithAuthSendsClientLoginAfterClientInit() { - $loop = LoopFactory::create(); - $server = new Server(0, $loop); + $server = new Server(0); // expect login packet $data = $this->expectCallableOnceWith($this->callback(function ($packet) { @@ -284,16 +269,15 @@ public function testCreateClientWithAuthSendsClientLoginAfterClientInit() }); $uri = str_replace('tcp://', '', $server->getAddress()); - $factory = new Factory($loop); + $factory = new Factory(); $promise = $factory->createClient('user:pass@' . $uri); - Block\sleep(0.1, $loop); + Block\sleep(0.1, Loop::get()); } public function testCreateClientRespondsWithHeartBeatResponseAfterHeartBeatRequest() { - $loop = LoopFactory::create(); - $server = new Server(0, $loop); + $server = new Server(0); // expect heartbeat response packet $data = $this->expectCallableOnceWith($this->callback(function ($packet) { @@ -303,14 +287,14 @@ public function testCreateClientRespondsWithHeartBeatResponseAfterHeartBeatReque return (isset($data[0]) && $data[0] === Protocol::REQUEST_HEARTBEATREPLY); })); - $server->on('connection', function (ConnectionInterface $conn) use ($data, $loop) { - $conn->once('data', function () use ($conn, $data, $loop) { + $server->on('connection', function (ConnectionInterface $conn) use ($data) { + $conn->once('data', function () use ($conn, $data) { $conn->write("\x00\x00\x00\x02"); // expect heartbeat response next $conn->on('data', $data); - $loop->addTimer(0.01, function() use ($conn) { + Loop::addTimer(0.01, function() use ($conn) { // response with successful init $conn->write(FactoryIntegrationTest::encode(array( Protocol::REQUEST_HEARTBEAT, @@ -321,31 +305,30 @@ public function testCreateClientRespondsWithHeartBeatResponseAfterHeartBeatReque }); $uri = str_replace('tcp://', '', $server->getAddress()); - $factory = new Factory($loop); + $factory = new Factory(); $promise = $factory->createClient($uri); - Block\sleep(0.1, $loop); + Block\sleep(0.1, Loop::get()); - $client = Block\await($promise, $loop); + $client = Block\await($promise, Loop::get()); $client->close(); } public function testCreateClientDoesNotRespondWithHeartBeatResponseIfPongIsDisabled() { - $loop = LoopFactory::create(); - $server = new Server(0, $loop); + $server = new Server(0); // expect no message in response $data = $this->expectCallableNever(); - $server->on('connection', function (ConnectionInterface $conn) use ($data, $loop) { - $conn->once('data', function () use ($conn, $data, $loop) { + $server->on('connection', function (ConnectionInterface $conn) use ($data) { + $conn->once('data', function () use ($conn, $data) { $conn->write("\x00\x00\x00\x02"); // expect no message in response $conn->on('data', $data); - $loop->addTimer(0.01, function() use ($conn) { + Loop::addTimer(0.01, function() use ($conn) { // response with successful init $conn->write(FactoryIntegrationTest::encode(array( Protocol::REQUEST_HEARTBEAT, @@ -356,19 +339,18 @@ public function testCreateClientDoesNotRespondWithHeartBeatResponseIfPongIsDisab }); $uri = str_replace('tcp://', '', $server->getAddress()); - $factory = new Factory($loop); + $factory = new Factory(); $promise = $factory->createClient($uri . '?pong=0'); - Block\sleep(0.1, $loop); + Block\sleep(0.1, Loop::get()); - $client = Block\await($promise, $loop); + $client = Block\await($promise, Loop::get()); $client->close(); } public function testCreateClientSendsHeartBeatRequestAtInterval() { - $loop = LoopFactory::create(); - $server = new Server(0, $loop); + $server = new Server(0); // expect heartbeat response packet $data = $this->expectCallableOnceWith($this->callback(function ($packet) { @@ -388,51 +370,49 @@ public function testCreateClientSendsHeartBeatRequestAtInterval() }); $uri = str_replace('tcp://', '', $server->getAddress()); - $factory = new Factory($loop); + $factory = new Factory(); $promise = $factory->createClient($uri . '?ping=0.05'); - Block\sleep(0.1, $loop); + Block\sleep(0.1, Loop::get()); - $client = Block\await($promise, $loop); + $client = Block\await($promise, Loop::get()); $client->close(); } public function testCreateClientSendsNoHeartBeatRequestIfServerKeepsSendingMessages() { - $loop = LoopFactory::create(); - $server = new Server(0, $loop); + $server = new Server(0); // expect heartbeat response packet $data = $this->expectCallableNever(); - $server->on('connection', function (ConnectionInterface $conn) use ($data, $loop) { - $conn->once('data', function () use ($conn, $data, $loop) { + $server->on('connection', function (ConnectionInterface $conn) use ($data) { + $conn->once('data', function () use ($conn, $data) { $conn->write("\x00\x00\x00\x02"); // expect no heartbeat request $conn->on('data', $data); // periodically send some dummy messages - $loop->addPeriodicTimer(0.01, function() use ($conn) { + Loop::addPeriodicTimer(0.01, function() use ($conn) { $conn->write(FactoryIntegrationTest::encode(array(0))); }); }); }); $uri = str_replace('tcp://', '', $server->getAddress()); - $factory = new Factory($loop); + $factory = new Factory(); $promise = $factory->createClient($uri . '?ping=0.05&pong=0'); - Block\sleep(0.1, $loop); + Block\sleep(0.1, Loop::get()); - $client = Block\await($promise, $loop); + $client = Block\await($promise, Loop::get()); $client->close(); } public function testCreateClientClosesWithErrorIfServerDoesNotRespondToHeartBeatRequests() { - $loop = LoopFactory::create(); - $server = new Server(0, $loop); + $server = new Server(0); $server->on('connection', function (ConnectionInterface $conn) { $conn->once('data', function () use ($conn) { @@ -441,20 +421,19 @@ public function testCreateClientClosesWithErrorIfServerDoesNotRespondToHeartBeat }); $uri = str_replace('tcp://', '', $server->getAddress()); - $factory = new Factory($loop); + $factory = new Factory(); $promise = $factory->createClient($uri . '?ping=0.03'); - $client = Block\await($promise, $loop, 0.1); + $client = Block\await($promise, Loop::get(), 0.1); $client->on('error', $this->expectCallableOnce()); $client->on('close', $this->expectCallableOnce()); - Block\sleep(0.1, $loop); + Block\sleep(0.1, Loop::get()); } public function testCreateClientSendsNoHeartBeatRequestIfPingIsDisabled() { - $loop = LoopFactory::create(); - $server = new Server(0, $loop); + $server = new Server(0); // expect heartbeat response packet $data = $this->expectCallableNever(); @@ -469,12 +448,12 @@ public function testCreateClientSendsNoHeartBeatRequestIfPingIsDisabled() }); $uri = str_replace('tcp://', '', $server->getAddress()); - $factory = new Factory($loop); + $factory = new Factory(); $promise = $factory->createClient($uri . '?ping=0'); - Block\sleep(0.1, $loop); + Block\sleep(0.1, Loop::get()); - $client = Block\await($promise, $loop); + $client = Block\await($promise, Loop::get()); $client->close(); } diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index 7bb43d0..35b444d 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -31,7 +31,18 @@ public function setUpFactory() */ public function testCtorOptionalArgs() { - new Factory($this->loop); + new Factory(); + } + + public function testConstructWithoutLoopAssignsLoopAutomatically() + { + $factory = new Factory(); + + $ref = new \ReflectionProperty($factory, 'loop'); + $ref->setAccessible(true); + $loop = $ref->getValue($factory); + + $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); } public function testPassHostnameAndDefaultPortToConnector() diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index 43f89a8..121ae23 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -2,14 +2,14 @@ namespace Clue\Tests\React\Quassel; -use React\EventLoop\Factory as LoopFactory; -use Clue\React\Quassel\Factory; use Clue\React\Block; use Clue\React\Quassel\Client; -use React\Promise\Promise; +use Clue\React\Quassel\Factory; use Clue\React\Quassel\Io\Protocol; use Clue\React\Quassel\Models\BufferInfo; use Clue\React\Quassel\Models\Message; +use React\EventLoop\Loop; +use React\Promise\Promise; class FunctionalTest extends TestCase { @@ -17,7 +17,6 @@ class FunctionalTest extends TestCase private static $username; private static $password; - private static $loop; private static $blocker; /** @@ -39,8 +38,6 @@ public static function setUpEnvironmentAndLoop() if (!self::$password) { self::$password = 'quassel'; } - - self::$loop = LoopFactory::create(); } /** @@ -58,10 +55,10 @@ public function setUpSkipOnMissingEnvironment() */ public function testCreateClient() { - $factory = new Factory(self::$loop); + $factory = new Factory(); $promise = $factory->createClient(self::$host); - $client = Block\await($promise, self::$loop, 10.0); + $client = Block\await($promise, Loop::get(), 10.0); return $client; } @@ -115,11 +112,7 @@ public function testWriteClientLogin(Client $client, \stdClass $message) $message = $this->awaitMessage($client); $this->assertEquals('ClientLoginAck', $message->MsgType); - try { - $message = $this->awaitMessage($client); - } catch (\React\Promise\Timer\TimeoutException $e) { - $this->markTestIncomplete('Unhandled race condition, please retry'); - } + $message = $this->awaitMessage($client); $this->assertEquals('SessionInit', $message->MsgType); return $message; @@ -148,7 +141,7 @@ public function testWriteHeartBeat(Client $client) $client->writeHeartBeatRequest($time); - $received = Block\await($promise, self::$loop, 10.0); + $received = Block\await($promise, Loop::get(), 10.0); $this->assertEquals($time, $received); } @@ -173,7 +166,7 @@ public function testWriteHeartBeatDefaultsToCurrentTime(Client $client) $client->writeHeartBeatRequest(); - $received = Block\await($promise, self::$loop, 10.0); + $received = Block\await($promise, Loop::get(), 10.0); $this->assertTrue($received instanceof \DateTime); $this->assertEqualsDelta(microtime(true), $received->getTimestamp(), 2.0); @@ -191,16 +184,16 @@ public function testClose(Client $client) $client->close(); - return Block\await($promise, self::$loop, 10.0); + return Block\await($promise, Loop::get(), 10.0); } public function testCreateClientWithAuthUrlReceivesSessionInit() { - $factory = new Factory(self::$loop); + $factory = new Factory(); $url = rawurlencode(self::$username) . ':' . rawurlencode(self::$password) . '@' . self::$host; $promise = $factory->createClient($url); - $client = Block\await($promise, self::$loop, 10.0); + $client = Block\await($promise, Loop::get(), 10.0); $message = $this->awaitMessage($client); $this->assertEquals('SessionInit', $message->MsgType); @@ -210,11 +203,11 @@ public function testCreateClientWithAuthUrlReceivesSessionInit() public function testRequestBacklogReceivesBacklog() { - $factory = new Factory(self::$loop); + $factory = new Factory(); $url = rawurlencode(self::$username) . ':' . rawurlencode(self::$password) . '@' . self::$host; $promise = $factory->createClient($url); - $client = Block\await($promise, self::$loop, 10.0); + $client = Block\await($promise, Loop::get(), 10.0); /* @var $client Client */ $message = $this->awaitMessage($client); @@ -282,22 +275,26 @@ public function testRequestBacklogReceivesBacklog() public function testCreateClientWithInvalidAuthUrlRejects() { - $factory = new Factory(self::$loop); + $factory = new Factory(); $url = rawurlencode(self::$username) . ':@' . self::$host; $promise = $factory->createClient($url); $this->setExpectedException('RuntimeException'); - Block\await($promise, self::$loop, 10.0); + Block\await($promise, Loop::get(), 10.0); } private function awaitMessage(Client $client) { - return Block\await(new Promise(function ($resolve, $reject) use ($client) { - $client->once('data', $resolve); + try { + return Block\await(new Promise(function ($resolve, $reject) use ($client) { + $client->once('data', $resolve); - $client->once('error', $reject); - $client->once('close', $reject); - }), self::$loop, 10.0); + $client->once('error', $reject); + $client->once('close', $reject); + }), Loop::get(), 10.0); + } catch (\React\Promise\Timer\TimeoutException $e) { + $this->markTestIncomplete('Unhandled race condition, please retry'); + } } }