| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- <?php
- namespace React\Tests\Dns\Query;
- use React\Dns\Model\Message;
- use React\Dns\Query\FallbackExecutor;
- use React\Dns\Query\Query;
- use React\Promise\Promise;
- use React\Tests\Dns\TestCase;
- class FallbackExecutorTest extends TestCase
- {
- public function testQueryWillReturnPendingPromiseWhenPrimaryExecutorIsStillPending()
- {
- $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN);
- $primary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock();
- $primary->expects($this->once())->method('query')->with($query)->willReturn(new Promise(function () { }));
- $secondary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock();
- $executor = new FallbackExecutor($primary, $secondary);
- $promise = $executor->query($query);
- $this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
- $promise->then($this->expectCallableNever(), $this->expectCallableNever());
- }
- public function testQueryWillResolveWithMessageWhenPrimaryExecutorResolvesWithMessage()
- {
- $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN);
- $primary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock();
- $primary->expects($this->once())->method('query')->with($query)->willReturn(\React\Promise\resolve(new Message()));
- $secondary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock();
- $executor = new FallbackExecutor($primary, $secondary);
- $promise = $executor->query($query);
- $this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
- $promise->then($this->expectCallableOnceWith($this->isInstanceOf('React\Dns\Model\Message')), $this->expectCallableNever());
- }
- public function testQueryWillReturnPendingPromiseWhenPrimaryExecutorRejectsPromiseAndSecondaryExecutorIsStillPending()
- {
- $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN);
- $primary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock();
- $primary->expects($this->once())->method('query')->with($query)->willReturn(\React\Promise\reject(new \RuntimeException()));
- $secondary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock();
- $secondary->expects($this->once())->method('query')->with($query)->willReturn(new Promise(function () { }));
- $executor = new FallbackExecutor($primary, $secondary);
- $promise = $executor->query($query);
- $this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
- $promise->then($this->expectCallableNever(), $this->expectCallableNever());
- }
- public function testQueryWillResolveWithMessageWhenPrimaryExecutorRejectsPromiseAndSecondaryExecutorResolvesWithMessage()
- {
- $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN);
- $primary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock();
- $primary->expects($this->once())->method('query')->with($query)->willReturn(\React\Promise\reject(new \RuntimeException()));
- $secondary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock();
- $secondary->expects($this->once())->method('query')->with($query)->willReturn(\React\Promise\resolve(new Message()));
- $executor = new FallbackExecutor($primary, $secondary);
- $promise = $executor->query($query);
- $this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
- $promise->then($this->expectCallableOnceWith($this->isInstanceOf('React\Dns\Model\Message')), $this->expectCallableNever());
- }
- public function testQueryWillRejectWithExceptionMessagesConcatenatedAfterColonWhenPrimaryExecutorRejectsPromiseAndSecondaryExecutorRejectsPromiseWithMessageWithColon()
- {
- $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN);
- $primary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock();
- $primary->expects($this->once())->method('query')->with($query)->willReturn(\React\Promise\reject(new \RuntimeException('DNS query for reactphp.org (A) failed: Unable to connect to DNS server A')));
- $secondary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock();
- $secondary->expects($this->once())->method('query')->with($query)->willReturn(\React\Promise\reject(new \RuntimeException('DNS query for reactphp.org (A) failed: Unable to connect to DNS server B')));
- $executor = new FallbackExecutor($primary, $secondary);
- $promise = $executor->query($query);
- $this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
- $promise->then($this->expectCallableNever(), $this->expectCallableOnce($this->isInstanceOf('Exception')));
- $exception = null;
- $promise->then(null, function ($reason) use (&$exception) {
- $exception = $reason;
- });
- $this->assertInstanceOf('RuntimeException', $exception);
- $this->assertEquals('DNS query for reactphp.org (A) failed: Unable to connect to DNS server A. Unable to connect to DNS server B', $exception->getMessage());
- }
- public function testQueryWillRejectWithExceptionMessagesConcatenatedInFullWhenPrimaryExecutorRejectsPromiseAndSecondaryExecutorRejectsPromiseWithMessageWithNoColon()
- {
- $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN);
- $primary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock();
- $primary->expects($this->once())->method('query')->with($query)->willReturn(\React\Promise\reject(new \RuntimeException('Reason A')));
- $secondary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock();
- $secondary->expects($this->once())->method('query')->with($query)->willReturn(\React\Promise\reject(new \RuntimeException('Reason B')));
- $executor = new FallbackExecutor($primary, $secondary);
- $promise = $executor->query($query);
- $this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
- $promise->then($this->expectCallableNever(), $this->expectCallableOnce($this->isInstanceOf('Exception')));
- $exception = null;
- $promise->then(null, function ($reason) use (&$exception) {
- $exception = $reason;
- });
- $this->assertInstanceOf('RuntimeException', $exception);
- $this->assertEquals('Reason A. Reason B', $exception->getMessage());
- }
- public function testCancelQueryWillReturnRejectedPromiseWithoutCallingSecondaryExecutorWhenPrimaryExecutorIsStillPending()
- {
- $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN);
- $primary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock();
- $primary->expects($this->once())->method('query')->with($query)->willReturn(new Promise(function () { }, function () { throw new \RuntimeException(); }));
- $secondary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock();
- $secondary->expects($this->never())->method('query');
- $executor = new FallbackExecutor($primary, $secondary);
- $promise = $executor->query($query);
- $promise->cancel();
- $this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
- $promise->then($this->expectCallableNever(), $this->expectCallableOnce());
- }
- public function testCancelQueryWillReturnRejectedPromiseWhenPrimaryExecutorRejectsAndSecondaryExecutorIsStillPending()
- {
- $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN);
- $primary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock();
- $primary->expects($this->once())->method('query')->with($query)->willReturn(\React\Promise\reject(new \RuntimeException()));
- $secondary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock();
- $secondary->expects($this->once())->method('query')->with($query)->willReturn(new Promise(function () { }, function () { throw new \RuntimeException(); }));
- $executor = new FallbackExecutor($primary, $secondary);
- $promise = $executor->query($query);
- $promise->cancel();
- $this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
- $promise->then($this->expectCallableNever(), $this->expectCallableOnce());
- }
- public function testCancelQueryShouldNotCauseGarbageReferencesWhenCancellingPrimaryExecutor()
- {
- if (class_exists('React\Promise\When')) {
- $this->markTestSkipped('Not supported on legacy Promise v1 API');
- }
- $primary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock();
- $primary->expects($this->once())->method('query')->willReturn(new Promise(function () { }, function () { throw new \RuntimeException(); }));
- $secondary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock();
- $secondary->expects($this->never())->method('query');
- $executor = new FallbackExecutor($primary, $secondary);
- while (gc_collect_cycles()) {
- // collect all garbage cycles
- }
- $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN);
- $promise = $executor->query($query);
- $promise->cancel();
- $promise = null;
- $this->assertEquals(0, gc_collect_cycles());
- }
- public function testCancelQueryShouldNotCauseGarbageReferencesWhenCancellingSecondaryExecutor()
- {
- if (class_exists('React\Promise\When')) {
- $this->markTestSkipped('Not supported on legacy Promise v1 API');
- }
- $primary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock();
- $primary->expects($this->once())->method('query')->willReturn(\React\Promise\reject(new \RuntimeException()));
- $secondary = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock();
- $secondary->expects($this->once())->method('query')->willReturn(new Promise(function () { }, function () { throw new \RuntimeException(); }));
- $executor = new FallbackExecutor($primary, $secondary);
- while (gc_collect_cycles()) {
- // collect all garbage cycles
- }
- $query = new Query('reactphp.org', Message::TYPE_A, Message::CLASS_IN);
- $promise = $executor->query($query);
- $promise->cancel();
- $promise = null;
- $this->assertEquals(0, gc_collect_cycles());
- }
- }
|