forked from reactphp/dns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeoutExecutorTest.php
More file actions
195 lines (150 loc) · 6.97 KB
/
TimeoutExecutorTest.php
File metadata and controls
195 lines (150 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
namespace React\Tests\Dns\Query;
use React\Dns\Model\Message;
use React\Dns\Query\CancellationException;
use React\Dns\Query\Query;
use React\Dns\Query\TimeoutException;
use React\Dns\Query\TimeoutExecutor;
use React\Promise;
use React\Promise\Deferred;
use React\Tests\Dns\TestCase;
class TimeoutExecutorTest extends TestCase
{
private $wrapped;
private $executor;
private $loop;
/**
* @before
*/
public function setUpExecutor()
{
$this->wrapped = $this->getMockBuilder('React\Dns\Query\ExecutorInterface')->getMock();
$this->loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$this->executor = new TimeoutExecutor($this->wrapped, 5.0, $this->loop);
}
public function testCtorWithoutLoopShouldAssignDefaultLoop()
{
$executor = new TimeoutExecutor($this->executor, 5.0);
$ref = new \ReflectionProperty($executor, 'loop');
$ref->setAccessible(true);
$loop = $ref->getValue($executor);
$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
}
public function testCancellingPromiseWillCancelWrapped()
{
$timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock();
$this->loop->expects($this->once())->method('addTimer')->with(5.0, $this->anything())->willReturn($timer);
$this->loop->expects($this->once())->method('cancelTimer')->with($timer);
$cancelled = 0;
$this->wrapped
->expects($this->once())
->method('query')
->will($this->returnCallback(function ($query) use (&$cancelled) {
$deferred = new Deferred(function ($resolve, $reject) use (&$cancelled) {
++$cancelled;
$reject(new CancellationException('Cancelled'));
});
return $deferred->promise();
}));
$query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN);
$promise = $this->executor->query($query);
$this->assertEquals(0, $cancelled);
$promise->cancel();
$this->assertEquals(1, $cancelled);
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());
}
public function testResolvesPromiseWithoutStartingTimerWhenWrappedReturnsResolvedPromise()
{
$this->loop->expects($this->never())->method('addTimer');
$this->loop->expects($this->never())->method('cancelTimer');
$this->wrapped
->expects($this->once())
->method('query')
->willReturn(Promise\resolve('0.0.0.0'));
$query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN);
$promise = $this->executor->query($query);
$promise->then($this->expectCallableOnce(), $this->expectCallableNever());
}
public function testResolvesPromiseAfterCancellingTimerWhenWrappedReturnsPendingPromiseThatResolves()
{
$timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock();
$this->loop->expects($this->once())->method('addTimer')->with(5.0, $this->anything())->willReturn($timer);
$this->loop->expects($this->once())->method('cancelTimer')->with($timer);
$deferred = new Deferred();
$this->wrapped
->expects($this->once())
->method('query')
->willReturn($deferred->promise());
$query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN);
$promise = $this->executor->query($query);
$deferred->resolve('0.0.0.0');
$promise->then($this->expectCallableOnce(), $this->expectCallableNever());
}
public function testRejectsPromiseWithoutStartingTimerWhenWrappedReturnsRejectedPromise()
{
$this->loop->expects($this->never())->method('addTimer');
$this->loop->expects($this->never())->method('cancelTimer');
$this->wrapped
->expects($this->once())
->method('query')
->willReturn(Promise\reject(new \RuntimeException()));
$query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN);
$promise = $this->executor->query($query);
$promise->then($this->expectCallableNever(), $this->expectCallableOnceWith(new \RuntimeException()));
}
public function testRejectsPromiseAfterCancellingTimerWhenWrappedReturnsPendingPromiseThatRejects()
{
$timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock();
$this->loop->expects($this->once())->method('addTimer')->with(5.0, $this->anything())->willReturn($timer);
$this->loop->expects($this->once())->method('cancelTimer')->with($timer);
$deferred = new Deferred();
$this->wrapped
->expects($this->once())
->method('query')
->willReturn($deferred->promise());
$query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN);
$promise = $this->executor->query($query);
$deferred->reject(new \RuntimeException());
$promise->then($this->expectCallableNever(), $this->expectCallableOnceWith(new \RuntimeException()));
}
public function testRejectsPromiseAndCancelsPendingQueryWhenTimeoutTriggers()
{
$timerCallback = null;
$timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock();
$this->loop->expects($this->once())->method('addTimer')->with(5.0, $this->callback(function ($callback) use (&$timerCallback) {
$timerCallback = $callback;
return true;
}))->willReturn($timer);
$this->loop->expects($this->once())->method('cancelTimer')->with($timer);
$cancelled = 0;
$this->wrapped
->expects($this->once())
->method('query')
->will($this->returnCallback(function ($query) use (&$cancelled) {
$deferred = new Deferred(function ($resolve, $reject) use (&$cancelled) {
++$cancelled;
$reject(new CancellationException('Cancelled'));
});
return $deferred->promise();
}));
$query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN);
$promise = $this->executor->query($query);
$this->assertEquals(0, $cancelled);
$this->assertNotNull($timerCallback);
$timerCallback();
$this->assertEquals(1, $cancelled);
$exception = null;
$promise->then(null, function ($reason) use (&$exception) {
$exception = $reason;
});
assert($exception instanceof TimeoutException);
$this->assertInstanceOf('React\Dns\Query\TimeoutException', $exception);
$this->assertEquals('DNS query for igor.io (A) timed out' , $exception->getMessage());
}
/** @test */
public function constructorThrowsExceptionForInvalidLoop()
{
$this->setExpectedException('InvalidArgumentException', 'Argument #3 ($loop) expected null|React\EventLoop\LoopInterface');
new TimeoutExecutor($this->executor, 5.0, 'loop');
}
}