|
4 | 4 | use Ratchet\Client\Connector;
|
5 | 5 | use React\EventLoop\Factory;
|
6 | 6 | use React\Promise\RejectedPromise;
|
| 7 | +use React\Promise\Promise; |
7 | 8 |
|
8 | 9 | class ConnectorTest extends TestCase
|
9 | 10 | {
|
@@ -36,4 +37,70 @@ public function testSecureConnectionUsesTlsScheme($uri, $expectedConnectorUri) {
|
36 | 37 |
|
37 | 38 | $pawlConnector($uri);
|
38 | 39 | }
|
| 40 | + |
| 41 | + public function testConnectorRejectsWhenUnderlyingSocketConnectorRejects() |
| 42 | + { |
| 43 | + $exception = new RuntimeException('Connection failed'); |
| 44 | + |
| 45 | + $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); |
| 46 | + $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); |
| 47 | + $connector->expects($this->once())->method('connect')->willReturn(\React\Promise\reject($exception)); |
| 48 | + |
| 49 | + $pawlConnector = new Connector($loop, $connector); |
| 50 | + |
| 51 | + $promise = $pawlConnector('ws://localhost'); |
| 52 | + |
| 53 | + $actual = null; |
| 54 | + $promise->then(null, function ($reason) use (&$actual) { |
| 55 | + $actual = $reason; |
| 56 | + }); |
| 57 | + $this->assertSame($exception, $actual); |
| 58 | + } |
| 59 | + |
| 60 | + public function testCancelConnectorShouldCancelUnderlyingSocketConnectorWhenSocketConnectionIsPending() |
| 61 | + { |
| 62 | + $promise = new Promise(function () { }, function () use (&$cancelled) { |
| 63 | + ++$cancelled; |
| 64 | + }); |
| 65 | + |
| 66 | + $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); |
| 67 | + $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); |
| 68 | + $connector->expects($this->once())->method('connect')->willReturn($promise); |
| 69 | + |
| 70 | + $pawlConnector = new Connector($loop, $connector); |
| 71 | + |
| 72 | + $promise = $pawlConnector('ws://localhost'); |
| 73 | + |
| 74 | + $this->assertNull($cancelled); |
| 75 | + $promise->cancel(); |
| 76 | + $this->assertEquals(1, $cancelled); |
| 77 | + |
| 78 | + $message = null; |
| 79 | + $promise->then(null, function ($reason) use (&$message) { |
| 80 | + $message = $reason->getMessage(); |
| 81 | + }); |
| 82 | + $this->assertEquals('Connection to ws://localhost cancelled during handshake', $message); |
| 83 | + } |
| 84 | + |
| 85 | + public function testCancelConnectorShouldCloseUnderlyingSocketConnectionWhenHandshakeIsPending() |
| 86 | + { |
| 87 | + $connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock(); |
| 88 | + $connection->expects($this->once())->method('close'); |
| 89 | + |
| 90 | + $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); |
| 91 | + $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); |
| 92 | + $connector->expects($this->once())->method('connect')->willReturn(\React\Promise\resolve($connection)); |
| 93 | + |
| 94 | + $pawlConnector = new Connector($loop, $connector); |
| 95 | + |
| 96 | + $promise = $pawlConnector('ws://localhost'); |
| 97 | + |
| 98 | + $promise->cancel(); |
| 99 | + |
| 100 | + $message = null; |
| 101 | + $promise->then(null, function ($reason) use (&$message) { |
| 102 | + $message = $reason->getMessage(); |
| 103 | + }); |
| 104 | + $this->assertEquals('Connection to ws://localhost cancelled during handshake', $message); |
| 105 | + } |
39 | 106 | }
|
0 commit comments