|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace React\Tests\Dns\Query; |
| 4 | + |
| 5 | +use React\Dns\Query\TimeoutExecutor; |
| 6 | +use React\Dns\Query\Query; |
| 7 | +use React\Dns\Model\Message; |
| 8 | +use React\Promise\Deferred; |
| 9 | +use React\Dns\Query\CancellationException; |
| 10 | +use React\Tests\Dns\TestCase; |
| 11 | +use React\EventLoop\Factory; |
| 12 | +use React\Promise; |
| 13 | + |
| 14 | +class TimeoutExecutorTest extends TestCase |
| 15 | +{ |
| 16 | + public function setUp() |
| 17 | + { |
| 18 | + $this->loop = Factory::create(); |
| 19 | + |
| 20 | + $this->wrapped = $this->getMock('React\Dns\Query\ExecutorInterface'); |
| 21 | + |
| 22 | + $this->executor = new TimeoutExecutor($this->wrapped, 5.0, $this->loop); |
| 23 | + } |
| 24 | + |
| 25 | + public function testCancellingPromiseWillCancelWrapped() |
| 26 | + { |
| 27 | + $cancelled = 0; |
| 28 | + |
| 29 | + $this->wrapped |
| 30 | + ->expects($this->once()) |
| 31 | + ->method('query') |
| 32 | + ->will($this->returnCallback(function ($domain, $query) use (&$cancelled) { |
| 33 | + $deferred = new Deferred(function ($resolve, $reject) use (&$cancelled) { |
| 34 | + ++$cancelled; |
| 35 | + $reject(new CancellationException('Cancelled')); |
| 36 | + }); |
| 37 | + |
| 38 | + return $deferred->promise(); |
| 39 | + })); |
| 40 | + |
| 41 | + $query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN, 1345656451); |
| 42 | + $promise = $this->executor->query('8.8.8.8:53', $query); |
| 43 | + |
| 44 | + $this->assertEquals(0, $cancelled); |
| 45 | + $promise->cancel(); |
| 46 | + $this->assertEquals(1, $cancelled); |
| 47 | + |
| 48 | + $promise->then($this->expectCallableNever(), $this->expectCallableOnce()); |
| 49 | + } |
| 50 | + |
| 51 | + public function testResolvesPromiseWhenWrappedResolves() |
| 52 | + { |
| 53 | + $this->wrapped |
| 54 | + ->expects($this->once()) |
| 55 | + ->method('query') |
| 56 | + ->willReturn(Promise\resolve('0.0.0.0')); |
| 57 | + |
| 58 | + $query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN, 1345656451); |
| 59 | + $promise = $this->executor->query('8.8.8.8:53', $query); |
| 60 | + |
| 61 | + $promise->then($this->expectCallableOnce(), $this->expectCallableNever()); |
| 62 | + } |
| 63 | + |
| 64 | + public function testRejectsPromiseWhenWrappedRejects() |
| 65 | + { |
| 66 | + $this->wrapped |
| 67 | + ->expects($this->once()) |
| 68 | + ->method('query') |
| 69 | + ->willReturn(Promise\reject(new \RuntimeException())); |
| 70 | + |
| 71 | + $query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN, 1345656451); |
| 72 | + $promise = $this->executor->query('8.8.8.8:53', $query); |
| 73 | + |
| 74 | + $promise->then($this->expectCallableNever(), $this->expectCallableOnceWith(new \RuntimeException())); |
| 75 | + } |
| 76 | + |
| 77 | + public function testWrappedWillBeCancelledOnTimeout() |
| 78 | + { |
| 79 | + $this->executor = new TimeoutExecutor($this->wrapped, 0.001, $this->loop); |
| 80 | + |
| 81 | + $cancelled = 0; |
| 82 | + |
| 83 | + $this->wrapped |
| 84 | + ->expects($this->once()) |
| 85 | + ->method('query') |
| 86 | + ->will($this->returnCallback(function ($domain, $query) use (&$cancelled) { |
| 87 | + $deferred = new Deferred(function ($resolve, $reject) use (&$cancelled) { |
| 88 | + ++$cancelled; |
| 89 | + $reject(new CancellationException('Cancelled')); |
| 90 | + }); |
| 91 | + |
| 92 | + return $deferred->promise(); |
| 93 | + })); |
| 94 | + |
| 95 | + $callback = $this->expectCallableNever(); |
| 96 | + |
| 97 | + $errorback = $this->createCallableMock(); |
| 98 | + $errorback |
| 99 | + ->expects($this->once()) |
| 100 | + ->method('__invoke') |
| 101 | + ->with($this->logicalAnd( |
| 102 | + $this->isInstanceOf('React\Dns\Query\TimeoutException'), |
| 103 | + $this->attribute($this->equalTo('DNS query for igor.io timed out'), 'message') |
| 104 | + )); |
| 105 | + |
| 106 | + $query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN, 1345656451); |
| 107 | + $this->executor->query('8.8.8.8:53', $query)->then($callback, $errorback); |
| 108 | + |
| 109 | + $this->assertEquals(0, $cancelled); |
| 110 | + |
| 111 | + $this->loop->run(); |
| 112 | + |
| 113 | + $this->assertEquals(1, $cancelled); |
| 114 | + } |
| 115 | +} |
0 commit comments