forked from clue/reactphp-mq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueTest.php
More file actions
277 lines (217 loc) · 7.3 KB
/
QueueTest.php
File metadata and controls
277 lines (217 loc) · 7.3 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
namespace Clue\Tests\React\Mq;
use Clue\React\Mq\Queue;
use React\Promise\Promise;
use React\Promise\Deferred;
class QueueTest extends TestCase
{
/**
* @doesNotPerformAssertions
*/
public function testCtorWithNormalLimits()
{
$q = new Queue(1, 2, function () { });
}
/**
* @doesNotPerformAssertions
*/
public function testCtorWithNoHardLimit()
{
$q = new Queue(1, null, function () { });
}
public function testCtorThrowsIfNotCallable()
{
$this->setExpectedException('InvalidArgumentException');
new Queue(1, 2, null);
}
public function testCtorThrowsIfConcurrencyTooLow()
{
$this->setExpectedException('InvalidArgumentException');
new Queue(0, 2, function () { });
}
public function testCtorThrowsIfConcurrencyAboveLiit()
{
$this->setExpectedException('InvalidArgumentException');
new Queue(3, 2, function () { });
}
public function testInvokeOnceWillInvokeHandler()
{
$called = 0;
$q = new Queue(1, 2, function () use (&$called) {
++$called;
return new Promise(function () { });
});
$q();
$this->assertCount(1, $q);
$this->assertEquals(1, $called);
}
public function testInvokeOnceWillInvokeHandlerWithArgument()
{
$args = null;
$q = new Queue(1, 2, function ($param) use (&$args) {
$args = func_get_args();
return new Promise(function () { });
});
$q(42);
$this->assertEquals(array(42), $args);
}
public function testInvokeOnceWillResolveWhenHandlerResolves()
{
$called = 0;
$q = new Queue(1, 2, function () use (&$called) {
return new Promise(function ($resolve) use (&$called) { $resolve(++$called); });
});
$q()->then($this->expectCallableOnceWith(1));
$this->assertCount(0, $q);
$this->assertEquals(1, $called);
}
public function testInvokeOnceWillRejectWhenHandlerRejects()
{
$called = 0;
$q = new Queue(1, 2, function () use (&$called) {
return new Promise(function () use (&$called) {
++$called;
throw new \RuntimeException();
});
});
$q()->then(null, $this->expectCallableOnce());
$this->assertCount(0, $q);
$this->assertEquals(1, $called);
}
public function testInvokeTwiceWillInvokeHandlerTwiceWhenBelowLimit()
{
$called = 0;
$q = new Queue(2, 2, function () use (&$called) {
++$called;
return new Promise(function () { });
});
$q();
$q();
$this->assertCount(2, $q);
$this->assertEquals(2, $called);
}
public function testInvokeTwiceWillInvokeHandlerOnceWhenConcurrencyIsReached()
{
$called = 0;
$q = new Queue(1, 2, function () use (&$called) {
++$called;
return new Promise(function () { });
});
$q();
$q();
$this->assertCount(2, $q);
$this->assertEquals(1, $called);
}
public function testInvokeTwiceWillInvokeSecondHandlerOnlyOnceConcurrencyIsBelowLimit()
{
$deferred = new Deferred();
$called = 0;
$q = new Queue(1, 2, function () use (&$called, $deferred) {
++$called;
return $deferred->promise();
});
$q();
$this->assertEquals(1, $called);
$this->assertCount(1, $q);
$q();
$this->assertEquals(1, $called);
$this->assertCount(2, $q);
$deferred->resolve(1);
$this->assertEquals(2, $called);
$this->assertCount(0, $q);
}
public function testInvokeTwiceWillResolveWhenHandlerResolves()
{
$called = 0;
$q = new Queue(1, 1, function () use (&$called) {
return new Promise(function ($resolve) use (&$called) { $resolve(++$called); });
});
$q()->then($this->expectCallableOnceWith(1));
$q()->then($this->expectCallableOnceWith(2));
$this->assertCount(0, $q);
$this->assertEquals(2, $called);
}
public function testInvokeTwiceWillInvokeHandlerOnceAndRejectWhenLimitIsReached()
{
$called = 0;
$q = new Queue(1, 1, function () use (&$called) {
++$called;
return new Promise(function () { });
});
$pending = $q();
$q()->then(null, $this->expectCallableOnce());
$this->assertEquals(1, $called);
}
public function testCancelOnceWillInvokePendingCancellationHandler()
{
$once = $this->expectCallableOnce();
$q = new Queue(1, 2, function () use ($once) {
return new Promise(function () { }, $once);
});
$q()->cancel();
}
public function testCancelPendingWillRejectPromiseAndRemoveJobFromQueue()
{
$never = $this->expectCallableNever();
$q = new Queue(1, 2, function () use ($never) {
return new Promise(function () { }, $never);
});
$pending = $q();
$this->assertCount(1, $q);
$second = $q();
$this->assertCount(2, $q);
$second->cancel();
$second->then(null, $this->expectCallableOnce());
$this->assertCount(1, $q);
}
public function testCancelPendingOperationThatWasPreviousQueuedShouldInvokeItsCancellationHandler()
{
$q = new Queue(1, null, function ($promise) {
return $promise;
});
$deferred = new Deferred();
$first = $q($deferred->promise());
$second = $q(new Promise(function () { }, $this->expectCallableOnce()));
$deferred->resolve(null);
$second->cancel();
}
public function testCancelPendingOperationThatWasPreviouslyQueuedShouldRejectWithCancellationResult()
{
$q = new Queue(1, null, function ($promise) {
return $promise;
});
$deferred = new Deferred();
$first = $q($deferred->promise());
$second = $q(new Promise(function () { }, function () { throw new \BadMethodCallException(); }));
$deferred->resolve(null);
$second->cancel();
$second->then(null, $this->expectCallableOnceWith($this->isInstanceOf('BadMethodCallException')));
}
public function testCancelPendingOperationThatWasPreviouslyQueuedShouldNotRejectIfCancellationHandlerDoesNotReject()
{
$q = new Queue(1, null, function ($promise) {
return $promise;
});
$deferred = new Deferred();
$first = $q($deferred->promise());
$second = $q(new Promise(function () { }, function () { }));
$deferred->resolve(null);
$second->cancel();
$second->then($this->expectCallableNever(), $this->expectCallableNever());
}
public function testCancelNextOperationFromFirstOperationShouldInvokeCancellationHandler()
{
$q = new Queue(1, null, function () {
return new Promise(function () { }, function () {
throw new \RuntimeException();
});
});
$first = $q();
$second = $q();
$first->then(null, function () use ($second) {
$second->cancel();
});
$first->cancel();
$second->then(null, $this->expectCallableOnce());
}
}