|
3 | 3 | namespace React\Tests\Socket; |
4 | 4 |
|
5 | 5 | use React\Promise; |
| 6 | +use React\Promise\Deferred; |
6 | 7 | use React\Socket\SecureConnector; |
7 | 8 |
|
8 | 9 | class SecureConnectorTest extends TestCase |
@@ -49,6 +50,15 @@ public function testConnectionToInvalidSchemeWillReject() |
49 | 50 | $promise->then(null, $this->expectCallableOnce()); |
50 | 51 | } |
51 | 52 |
|
| 53 | + public function testCancelDuringTcpConnectionCancelsTcpConnection() |
| 54 | + { |
| 55 | + $pending = new Promise\Promise(function () { }, $this->expectCallableOnce()); |
| 56 | + $this->tcp->expects($this->once())->method('connect')->with('example.com:80')->willReturn($pending); |
| 57 | + |
| 58 | + $promise = $this->connector->connect('example.com:80'); |
| 59 | + $promise->cancel(); |
| 60 | + } |
| 61 | + |
52 | 62 | /** |
53 | 63 | * @expectedException RuntimeException |
54 | 64 | * @expectedExceptionMessage Connection cancelled |
@@ -121,6 +131,80 @@ public function testConnectionWillBeRejectedIfStreamEncryptionFailsAndClosesConn |
121 | 131 | $this->throwRejection($promise); |
122 | 132 | } |
123 | 133 |
|
| 134 | + /** |
| 135 | + * @expectedException RuntimeException |
| 136 | + * @expectedExceptionMessage Connection to example.com:80 cancelled during TLS handshake |
| 137 | + */ |
| 138 | + public function testCancelDuringStreamEncryptionCancelsEncryptionAndClosesConnection() |
| 139 | + { |
| 140 | + $connection = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->getMock(); |
| 141 | + $connection->expects($this->once())->method('close'); |
| 142 | + |
| 143 | + $pending = new Promise\Promise(function () { }, function () { |
| 144 | + throw new \Exception('Ignored'); |
| 145 | + }); |
| 146 | + $encryption = $this->getMockBuilder('React\Socket\StreamEncryption')->disableOriginalConstructor()->getMock(); |
| 147 | + $encryption->expects($this->once())->method('enable')->willReturn($pending); |
| 148 | + |
| 149 | + $ref = new \ReflectionProperty($this->connector, 'streamEncryption'); |
| 150 | + $ref->setAccessible(true); |
| 151 | + $ref->setValue($this->connector, $encryption); |
| 152 | + |
| 153 | + $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('example.com:80'))->willReturn(Promise\resolve($connection)); |
| 154 | + |
| 155 | + $promise = $this->connector->connect('example.com:80'); |
| 156 | + $promise->cancel(); |
| 157 | + |
| 158 | + $this->throwRejection($promise); |
| 159 | + } |
| 160 | + |
| 161 | + public function testRejectionDuringConnectionShouldNotCreateAnyGarbageReferences() |
| 162 | + { |
| 163 | + if (class_exists('React\Promise\When')) { |
| 164 | + $this->markTestSkipped('Not supported on legacy Promise v1 API'); |
| 165 | + } |
| 166 | + |
| 167 | + gc_collect_cycles(); |
| 168 | + |
| 169 | + $tcp = new Deferred(); |
| 170 | + $this->tcp->expects($this->once())->method('connect')->willReturn($tcp->promise()); |
| 171 | + |
| 172 | + $promise = $this->connector->connect('example.com:80'); |
| 173 | + $tcp->reject(new \RuntimeException()); |
| 174 | + unset($promise, $tcp); |
| 175 | + |
| 176 | + $this->assertEquals(0, gc_collect_cycles()); |
| 177 | + } |
| 178 | + |
| 179 | + public function testRejectionDuringTlsHandshakeShouldNotCreateAnyGarbageReferences() |
| 180 | + { |
| 181 | + if (class_exists('React\Promise\When')) { |
| 182 | + $this->markTestSkipped('Not supported on legacy Promise v1 API'); |
| 183 | + } |
| 184 | + |
| 185 | + gc_collect_cycles(); |
| 186 | + |
| 187 | + $connection = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->getMock(); |
| 188 | + |
| 189 | + $tcp = new Deferred(); |
| 190 | + $this->tcp->expects($this->once())->method('connect')->willReturn($tcp->promise()); |
| 191 | + |
| 192 | + $tls = new Deferred(); |
| 193 | + $encryption = $this->getMockBuilder('React\Socket\StreamEncryption')->disableOriginalConstructor()->getMock(); |
| 194 | + $encryption->expects($this->once())->method('enable')->willReturn($tls->promise()); |
| 195 | + |
| 196 | + $ref = new \ReflectionProperty($this->connector, 'streamEncryption'); |
| 197 | + $ref->setAccessible(true); |
| 198 | + $ref->setValue($this->connector, $encryption); |
| 199 | + |
| 200 | + $promise = $this->connector->connect('example.com:80'); |
| 201 | + $tcp->resolve($connection); |
| 202 | + $tls->reject(new \RuntimeException()); |
| 203 | + unset($promise, $tcp, $tls); |
| 204 | + |
| 205 | + $this->assertEquals(0, gc_collect_cycles()); |
| 206 | + } |
| 207 | + |
124 | 208 | private function throwRejection($promise) |
125 | 209 | { |
126 | 210 | $ex = null; |
|
0 commit comments