TimeoutExecutorTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace React\Tests\Dns\Query;
  3. use React\Dns\Model\Message;
  4. use React\Dns\Query\CancellationException;
  5. use React\Dns\Query\Query;
  6. use React\Dns\Query\TimeoutException;
  7. use React\Dns\Query\TimeoutExecutor;
  8. use React\Promise;
  9. use React\Promise\Deferred;
  10. use React\Tests\Dns\TestCase;
  11. class TimeoutExecutorTest extends TestCase
  12. {
  13. private $wrapped;
  14. private $executor;
  15. private $loop;
  16. /**
  17. * @before
  18. */
  19. public function setUpExecutor()
  20. {
  21. $this->wrapped = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock();
  22. $this->loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
  23. $this->executor = new TimeoutExecutor($this->wrapped, 5.0, $this->loop);
  24. }
  25. public function testCtorWithoutLoopShouldAssignDefaultLoop()
  26. {
  27. $executor = new TimeoutExecutor($this->executor, 5.0);
  28. $ref = new \ReflectionProperty($executor, 'loop');
  29. $ref->setAccessible(true);
  30. $loop = $ref->getValue($executor);
  31. $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
  32. }
  33. public function testCancellingPromiseWillCancelWrapped()
  34. {
  35. $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock();
  36. $this->loop->expects($this->once())->method('addTimer')->with(5.0, $this->anything())->willReturn($timer);
  37. $this->loop->expects($this->once())->method('cancelTimer')->with($timer);
  38. $cancelled = 0;
  39. $this->wrapped
  40. ->expects($this->once())
  41. ->method('query')
  42. ->will($this->returnCallback(function ($query) use (&$cancelled) {
  43. $deferred = new Deferred(function ($resolve, $reject) use (&$cancelled) {
  44. ++$cancelled;
  45. $reject(new CancellationException('Cancelled'));
  46. });
  47. return $deferred->promise();
  48. }));
  49. $query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN);
  50. $promise = $this->executor->query($query);
  51. $this->assertEquals(0, $cancelled);
  52. $promise->cancel();
  53. $this->assertEquals(1, $cancelled);
  54. $promise->then($this->expectCallableNever(), $this->expectCallableOnce());
  55. }
  56. public function testResolvesPromiseWithoutStartingTimerWhenWrappedReturnsResolvedPromise()
  57. {
  58. $this->loop->expects($this->never())->method('addTimer');
  59. $this->loop->expects($this->never())->method('cancelTimer');
  60. $this->wrapped
  61. ->expects($this->once())
  62. ->method('query')
  63. ->willReturn(Promise\resolve('0.0.0.0'));
  64. $query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN);
  65. $promise = $this->executor->query($query);
  66. $promise->then($this->expectCallableOnce(), $this->expectCallableNever());
  67. }
  68. public function testResolvesPromiseAfterCancellingTimerWhenWrappedReturnsPendingPromiseThatResolves()
  69. {
  70. $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock();
  71. $this->loop->expects($this->once())->method('addTimer')->with(5.0, $this->anything())->willReturn($timer);
  72. $this->loop->expects($this->once())->method('cancelTimer')->with($timer);
  73. $deferred = new Deferred();
  74. $this->wrapped
  75. ->expects($this->once())
  76. ->method('query')
  77. ->willReturn($deferred->promise());
  78. $query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN);
  79. $promise = $this->executor->query($query);
  80. $deferred->resolve('0.0.0.0');
  81. $promise->then($this->expectCallableOnce(), $this->expectCallableNever());
  82. }
  83. public function testRejectsPromiseWithoutStartingTimerWhenWrappedReturnsRejectedPromise()
  84. {
  85. $this->loop->expects($this->never())->method('addTimer');
  86. $this->loop->expects($this->never())->method('cancelTimer');
  87. $this->wrapped
  88. ->expects($this->once())
  89. ->method('query')
  90. ->willReturn(Promise\reject(new \RuntimeException()));
  91. $query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN);
  92. $promise = $this->executor->query($query);
  93. $promise->then($this->expectCallableNever(), $this->expectCallableOnceWith(new \RuntimeException()));
  94. }
  95. public function testRejectsPromiseAfterCancellingTimerWhenWrappedReturnsPendingPromiseThatRejects()
  96. {
  97. $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock();
  98. $this->loop->expects($this->once())->method('addTimer')->with(5.0, $this->anything())->willReturn($timer);
  99. $this->loop->expects($this->once())->method('cancelTimer')->with($timer);
  100. $deferred = new Deferred();
  101. $this->wrapped
  102. ->expects($this->once())
  103. ->method('query')
  104. ->willReturn($deferred->promise());
  105. $query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN);
  106. $promise = $this->executor->query($query);
  107. $deferred->reject(new \RuntimeException());
  108. $promise->then($this->expectCallableNever(), $this->expectCallableOnceWith(new \RuntimeException()));
  109. }
  110. public function testRejectsPromiseAndCancelsPendingQueryWhenTimeoutTriggers()
  111. {
  112. $timerCallback = null;
  113. $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock();
  114. $this->loop->expects($this->once())->method('addTimer')->with(5.0, $this->callback(function ($callback) use (&$timerCallback) {
  115. $timerCallback = $callback;
  116. return true;
  117. }))->willReturn($timer);
  118. $this->loop->expects($this->once())->method('cancelTimer')->with($timer);
  119. $cancelled = 0;
  120. $this->wrapped
  121. ->expects($this->once())
  122. ->method('query')
  123. ->will($this->returnCallback(function ($query) use (&$cancelled) {
  124. $deferred = new Deferred(function ($resolve, $reject) use (&$cancelled) {
  125. ++$cancelled;
  126. $reject(new CancellationException('Cancelled'));
  127. });
  128. return $deferred->promise();
  129. }));
  130. $query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN);
  131. $promise = $this->executor->query($query);
  132. $this->assertEquals(0, $cancelled);
  133. $this->assertNotNull($timerCallback);
  134. $timerCallback();
  135. $this->assertEquals(1, $cancelled);
  136. $exception = null;
  137. $promise->then(null, function ($reason) use (&$exception) {
  138. $exception = $reason;
  139. });
  140. assert($exception instanceof TimeoutException);
  141. $this->assertInstanceOf('React\Dns\Query\TimeoutException', $exception);
  142. $this->assertEquals('DNS query for igor.io (A) timed out' , $exception->getMessage());
  143. }
  144. }