|
2 | 2 |
|
3 | 3 | namespace React\Tests\Socket; |
4 | 4 |
|
5 | | -use React\Socket\DnsConnector; |
6 | 5 | use React\Promise; |
| 6 | +use React\Promise\Deferred; |
| 7 | +use React\Socket\DnsConnector; |
7 | 8 |
|
8 | 9 | class DnsConnectorTest extends TestCase |
9 | 10 | { |
@@ -77,6 +78,38 @@ public function testRejectsImmediatelyIfUriIsInvalid() |
77 | 78 | $promise->then($this->expectCallableNever(), $this->expectCallableOnce()); |
78 | 79 | } |
79 | 80 |
|
| 81 | + /** |
| 82 | + * @expectedException RuntimeException |
| 83 | + * @expectedExceptionMessage Connection failed |
| 84 | + */ |
| 85 | + public function testRejectsWithTcpConnectorRejectionIfGivenIp() |
| 86 | + { |
| 87 | + $promise = Promise\reject(new \RuntimeException('Connection failed')); |
| 88 | + $this->resolver->expects($this->never())->method('resolve'); |
| 89 | + $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80'))->willReturn($promise); |
| 90 | + |
| 91 | + $promise = $this->connector->connect('1.2.3.4:80'); |
| 92 | + $promise->cancel(); |
| 93 | + |
| 94 | + $this->throwRejection($promise); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @expectedException RuntimeException |
| 99 | + * @expectedExceptionMessage Connection failed |
| 100 | + */ |
| 101 | + public function testRejectsWithTcpConnectorRejectionAfterDnsIsResolved() |
| 102 | + { |
| 103 | + $promise = Promise\reject(new \RuntimeException('Connection failed')); |
| 104 | + $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn(Promise\resolve('1.2.3.4')); |
| 105 | + $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($promise); |
| 106 | + |
| 107 | + $promise = $this->connector->connect('example.com:80'); |
| 108 | + $promise->cancel(); |
| 109 | + |
| 110 | + $this->throwRejection($promise); |
| 111 | + } |
| 112 | + |
80 | 113 | /** |
81 | 114 | * @expectedException RuntimeException |
82 | 115 | * @expectedExceptionMessage Connection to example.invalid:80 failed during DNS lookup: DNS error |
@@ -121,16 +154,144 @@ public function testCancelDuringDnsCancelsDnsAndDoesNotStartTcpConnection() |
121 | 154 | $this->throwRejection($promise); |
122 | 155 | } |
123 | 156 |
|
124 | | - public function testCancelDuringTcpConnectionCancelsTcpConnection() |
| 157 | + public function testCancelDuringTcpConnectionCancelsTcpConnectionIfGivenIp() |
125 | 158 | { |
126 | | - $pending = new Promise\Promise(function () { }, function () { throw new \Exception(); }); |
127 | | - $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->will($this->returnValue(Promise\resolve('1.2.3.4'))); |
128 | | - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->will($this->returnValue($pending)); |
| 159 | + $pending = new Promise\Promise(function () { }, $this->expectCallableOnce()); |
| 160 | + $this->resolver->expects($this->never())->method('resolve'); |
| 161 | + $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80'))->willReturn($pending); |
| 162 | + |
| 163 | + $promise = $this->connector->connect('1.2.3.4:80'); |
| 164 | + $promise->cancel(); |
| 165 | + } |
| 166 | + |
| 167 | + public function testCancelDuringTcpConnectionCancelsTcpConnectionAfterDnsIsResolved() |
| 168 | + { |
| 169 | + $pending = new Promise\Promise(function () { }, $this->expectCallableOnce()); |
| 170 | + $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn(Promise\resolve('1.2.3.4')); |
| 171 | + $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($pending); |
129 | 172 |
|
130 | 173 | $promise = $this->connector->connect('example.com:80'); |
131 | 174 | $promise->cancel(); |
| 175 | + } |
132 | 176 |
|
133 | | - $promise->then($this->expectCallableNever(), $this->expectCallableOnce()); |
| 177 | + /** |
| 178 | + * @expectedException RuntimeException |
| 179 | + * @expectedExceptionMessage Connection cancelled |
| 180 | + */ |
| 181 | + public function testCancelDuringTcpConnectionCancelsTcpConnectionWithTcpRejectionAfterDnsIsResolved() |
| 182 | + { |
| 183 | + $first = new Deferred(); |
| 184 | + $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($first->promise()); |
| 185 | + $pending = new Promise\Promise(function () { }, function () { |
| 186 | + throw new \RuntimeException('Connection cancelled'); |
| 187 | + }); |
| 188 | + $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($pending); |
| 189 | + |
| 190 | + $promise = $this->connector->connect('example.com:80'); |
| 191 | + $first->resolve('1.2.3.4'); |
| 192 | + |
| 193 | + $promise->cancel(); |
| 194 | + |
| 195 | + $this->throwRejection($promise); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * @requires PHP 7 |
| 200 | + */ |
| 201 | + public function testRejectionDuringDnsLookupShouldNotCreateAnyGarbageReferences() |
| 202 | + { |
| 203 | + if (class_exists('React\Promise\When')) { |
| 204 | + $this->markTestSkipped('Not supported on legacy Promise v1 API'); |
| 205 | + } |
| 206 | + |
| 207 | + gc_collect_cycles(); |
| 208 | + |
| 209 | + $dns = new Deferred(); |
| 210 | + $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise()); |
| 211 | + $this->tcp->expects($this->never())->method('connect'); |
| 212 | + |
| 213 | + $promise = $this->connector->connect('example.com:80'); |
| 214 | + $dns->reject(new \RuntimeException('DNS failed')); |
| 215 | + unset($promise, $dns); |
| 216 | + |
| 217 | + $this->assertEquals(0, gc_collect_cycles()); |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * @requires PHP 7 |
| 222 | + */ |
| 223 | + public function testRejectionAfterDnsLookupShouldNotCreateAnyGarbageReferences() |
| 224 | + { |
| 225 | + if (class_exists('React\Promise\When')) { |
| 226 | + $this->markTestSkipped('Not supported on legacy Promise v1 API'); |
| 227 | + } |
| 228 | + |
| 229 | + gc_collect_cycles(); |
| 230 | + |
| 231 | + $dns = new Deferred(); |
| 232 | + $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise()); |
| 233 | + |
| 234 | + $tcp = new Deferred(); |
| 235 | + $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($tcp->promise()); |
| 236 | + |
| 237 | + $promise = $this->connector->connect('example.com:80'); |
| 238 | + $dns->resolve('1.2.3.4'); |
| 239 | + $tcp->reject(new \RuntimeException('Connection failed')); |
| 240 | + unset($promise, $dns, $tcp); |
| 241 | + |
| 242 | + $this->assertEquals(0, gc_collect_cycles()); |
| 243 | + } |
| 244 | + |
| 245 | + /** |
| 246 | + * @requires PHP 7 |
| 247 | + */ |
| 248 | + public function testCancelDuringDnsLookupShouldNotCreateAnyGarbageReferences() |
| 249 | + { |
| 250 | + if (class_exists('React\Promise\When')) { |
| 251 | + $this->markTestSkipped('Not supported on legacy Promise v1 API'); |
| 252 | + } |
| 253 | + |
| 254 | + gc_collect_cycles(); |
| 255 | + |
| 256 | + $dns = new Deferred(function () { |
| 257 | + throw new \RuntimeException(); |
| 258 | + }); |
| 259 | + $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise()); |
| 260 | + $this->tcp->expects($this->never())->method('connect'); |
| 261 | + |
| 262 | + $promise = $this->connector->connect('example.com:80'); |
| 263 | + |
| 264 | + $promise->cancel(); |
| 265 | + unset($promise, $dns); |
| 266 | + |
| 267 | + $this->assertEquals(0, gc_collect_cycles()); |
| 268 | + } |
| 269 | + |
| 270 | + /** |
| 271 | + * @requires PHP 7 |
| 272 | + */ |
| 273 | + public function testCancelDuringTcpConnectionShouldNotCreateAnyGarbageReferences() |
| 274 | + { |
| 275 | + if (class_exists('React\Promise\When')) { |
| 276 | + $this->markTestSkipped('Not supported on legacy Promise v1 API'); |
| 277 | + } |
| 278 | + |
| 279 | + gc_collect_cycles(); |
| 280 | + |
| 281 | + $dns = new Deferred(); |
| 282 | + $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($dns->promise()); |
| 283 | + $tcp = new Promise\Promise(function () { }, function () { |
| 284 | + throw new \RuntimeException('Connection cancelled'); |
| 285 | + }); |
| 286 | + $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($tcp); |
| 287 | + |
| 288 | + $promise = $this->connector->connect('example.com:80'); |
| 289 | + $dns->resolve('1.2.3.4'); |
| 290 | + |
| 291 | + $promise->cancel(); |
| 292 | + unset($promise, $dns); |
| 293 | + |
| 294 | + $this->assertEquals(0, gc_collect_cycles()); |
134 | 295 | } |
135 | 296 |
|
136 | 297 | private function throwRejection($promise) |
|
0 commit comments