FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cakephp_queue/tests/cases/models/queued_task.test.php at master · keycoder/cakephp_queue · GitHub
keycoder
/
cakephp_queue
Public
forked from
MSeven/cakephp_queue
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
cakephp_queue
/
tests
/
cases
/
models
/
queued_task.test.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
417 lines (367 loc) · 13 KB
Breadcrumbs
cakephp_queue
/
tests
/
cases
/
models
/
queued_task.test.php
Copy path
File metadata and controls
417 lines (367 loc) · 13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
/**
* @author MGriesbach@gmail.com
* @package QueuePlugin
* @subpackage QueuePlugin.Tests.Models
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @link http://github.com/MSeven/cakephp_queue
*/
App::
import
(
'
Model
'
,
'
Queue.QueuedTask
'
);
class
TestQueuedTask
extends
QueuedTask {
public
$
name
=
'
TestQueuedTask
'
;
public
$
useTable
=
'
queued_tasks
'
;
public
$
cacheSources
=
false
;
public
$
useDbConfig
=
'
test_suite
'
;
}
class
QueuedTaskTestCase
extends
CakeTestCase {
/**
* ZendStudio Codehint
*
* @var TestQueuedTask
*/
public
$
QueuedTask
;
/**
* Fixtures to load
*
* @var array
*/
public
$
fixtures
=
array
(
'
plugin.queue.queued_task
'
);
/**
* Initialize the Testcase
*
*/
public
function
start
() {
parent
::
start
();
$
this
->
QueuedTask
= & ClassRegistry::
init
(
'
TestQueuedTask
'
);
}
/**
* Basic Instance test
*/
public
function
testQueueInstance
() {
$
this
->
assertTrue
(
is_a
(
$
this
->
QueuedTask
,
'
TestQueuedTask
'
));
}
/**
* Test the basic create and length evaluation functions.
*/
public
function
testCreateAndCount
() {
// at first, the queue should contain 0 items.
$
this
->
assertEqual
(
0
,
$
this
->
QueuedTask
->
getLength
());
// create a job
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
test1
'
,
array
(
'
some
'
=>
'
random
'
,
'
test
'
=>
'
data
'
)));
// test if queue Length is 1 now.
$
this
->
assertEqual
(
1
,
$
this
->
QueuedTask
->
getLength
());
//create some more jobs
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
test2
'
,
array
(
'
some
'
=>
'
random
'
,
'
test
'
=>
'
data2
'
)));
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
test2
'
,
array
(
'
some
'
=>
'
random
'
,
'
test
'
=>
'
data3
'
)));
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
test3
'
,
array
(
'
some
'
=>
'
random
'
,
'
test
'
=>
'
data4
'
)));
//overall queueLength shpould now be 4
$
this
->
assertEqual
(
4
,
$
this
->
QueuedTask
->
getLength
());
// there should be 1 task of type 'test1', one of type 'test3' and 2 of type 'test2'
$
this
->
assertEqual
(
1
,
$
this
->
QueuedTask
->
getLength
(
'
test1
'
));
$
this
->
assertEqual
(
2
,
$
this
->
QueuedTask
->
getLength
(
'
test2
'
));
$
this
->
assertEqual
(
1
,
$
this
->
QueuedTask
->
getLength
(
'
test3
'
));
}
public
function
testCreateAndFetch
() {
//$capabilities is a list of tasks the worker can run.
$
capabilities
=
array
(
'
task1
'
=>
array
(
'
name
'
=>
'
task1
'
,
'
timeout
'
=>
100
,
'
retries
'
=>
2
)
);
$
testData
=
array
(
'
x1
'
=>
'
y1
'
,
'
x2
'
=>
'
y2
'
,
'
x3
'
=>
'
y3
'
,
'
x4
'
=>
'
y4
'
);
// start off empty.
$
this
->
assertEqual
(
array
(),
$
this
->
QueuedTask
->
find
(
'
all
'
));
// at first, the queue should contain 0 items.
$
this
->
assertEqual
(
0
,
$
this
->
QueuedTask
->
getLength
());
// there are no jobs, so we cant fetch any.
$
this
->
assertFalse
(
$
this
->
QueuedTask
->
requestJob
(
$
capabilities
));
// insert one job.
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
task1
'
,
$
testData
));
// fetch and check the first job.
$
data
=
$
this
->
QueuedTask
->
requestJob
(
$
capabilities
);
$
this
->
assertEqual
(
1
,
$
data
[
'
id
'
]);
$
this
->
assertEqual
(
'
task1
'
,
$
data
[
'
jobtype
'
]);
$
this
->
assertEqual
(
0
,
$
data
[
'
failed
'
]);
$
this
->
assertNull
(
$
data
[
'
completed
'
]);
$
this
->
assertEqual
(
$
testData
,
unserialize
(
$
data
[
'
data
'
]));
// after this job has been fetched, it may not be reassigned.
$
this
->
assertEqual
(
array
(),
$
this
->
QueuedTask
->
requestJob
(
$
capabilities
));
// queue length is still 1 since the first job did not finish.
$
this
->
assertEqual
(
1
,
$
this
->
QueuedTask
->
getLength
());
// Now mark Task1 as done
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
markJobDone
(
1
));
// Should be 0 again.
$
this
->
assertEqual
(
0
,
$
this
->
QueuedTask
->
getLength
());
}
/**
* Test the delivery of jobs in sequence, skipping fetched but not completed tasks.
*
*/
public
function
testSequence
() {
//$capabilities is a list of tasks the worker can run.
$
capabilities
=
array
(
'
task1
'
=>
array
(
'
name
'
=>
'
task1
'
,
'
timeout
'
=>
100
,
'
retries
'
=>
2
)
);
// at first, the queue should contain 0 items.
$
this
->
assertEqual
(
0
,
$
this
->
QueuedTask
->
getLength
());
// create some more jobs
foreach
(
range
(
0
,
9
)
as
$
num
) {
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
task1
'
,
array
(
'
tasknum
'
=>
$
num
)));
}
// 10 jobs in the queue.
$
this
->
assertEqual
(
10
,
$
this
->
QueuedTask
->
getLength
());
// jobs should be fetched in the original sequence.
foreach
(
range
(
0
,
4
)
as
$
num
) {
$
job
=
$
this
->
QueuedTask
->
requestJob
(
$
capabilities
);
$
jobData
=
unserialize
(
$
job
[
'
data
'
]);
$
this
->
assertEqual
(
$
num
,
$
jobData
[
'
tasknum
'
]);
}
// now mark them as done
foreach
(
range
(
0
,
4
)
as
$
num
) {
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
markJobDone
(
$
num
+
1
));
$
this
->
assertEqual
(
9
-
$
num
,
$
this
->
QueuedTask
->
getLength
());
}
// jobs should be fetched in the original sequence.
foreach
(
range
(
5
,
9
)
as
$
num
) {
$
job
=
$
this
->
QueuedTask
->
requestJob
(
$
capabilities
);
$
jobData
=
unserialize
(
$
job
[
'
data
'
]);
$
this
->
assertEqual
(
$
num
,
$
jobData
[
'
tasknum
'
]);
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
markJobDone
(
$
job
[
'
id
'
]));
$
this
->
assertEqual
(
9
-
$
num
,
$
this
->
QueuedTask
->
getLength
());
}
}
/**
* Test creating Jobs to run close to a specified time, and strtotime parsing.
* @return null
*/
public
function
testNotBefore
() {
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
task1
'
,
null
,
'
+ 1 Min
'
));
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
task1
'
,
null
,
'
+ 1 Day
'
));
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
task1
'
,
null
,
'
2009-07-01 12:00:00
'
));
$
data
=
$
this
->
QueuedTask
->
find
(
'
all
'
);
$
this
->
assertEqual
(
$
data
[
0
][
'
TestQueuedTask
'
][
'
notbefore
'
],
date
(
'
Y-m-d H:i:s
'
,
strtotime
(
'
+ 1 Min
'
)));
$
this
->
assertEqual
(
$
data
[
1
][
'
TestQueuedTask
'
][
'
notbefore
'
],
date
(
'
Y-m-d H:i:s
'
,
strtotime
(
'
+ 1 Day
'
)));
$
this
->
assertEqual
(
$
data
[
2
][
'
TestQueuedTask
'
][
'
notbefore
'
],
'
2009-07-01 12:00:00
'
);
}
/**
* Test Job reordering depending on 'notBefore' field.
* Jobs with an expired notbefore field should be executed before any other job without specific timing info.
* @return null
*/
public
function
testNotBeforeOrder
() {
$
capabilities
=
array
(
'
task1
'
=>
array
(
'
name
'
=>
'
task1
'
,
'
timeout
'
=>
100
,
'
retries
'
=>
2
),
'
dummytask
'
=>
array
(
'
name
'
=>
'
dummytask
'
,
'
timeout
'
=>
100
,
'
retries
'
=>
2
)
);
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
dummytask
'
,
null
));
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
dummytask
'
,
null
));
// create a task with it's execution target some seconds in the past, so it should jump to the top of the list.
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
task1
'
,
'
three
'
,
'
- 3 Seconds
'
));
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
task1
'
,
'
two
'
,
'
- 4 Seconds
'
));
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
task1
'
,
'
one
'
,
'
- 5 Seconds
'
));
// when usin requestJob, the jobs we just created should be delivered in this order, NOT the order in which they where created.
$
expected
=
array
(
array
(
'
name
'
=>
'
task1
'
,
'
data
'
=>
'
one
'
),
array
(
'
name
'
=>
'
task1
'
,
'
data
'
=>
'
two
'
),
array
(
'
name
'
=>
'
task1
'
,
'
data
'
=>
'
three
'
),
array
(
'
name
'
=>
'
dummytask
'
,
'
data
'
=>
''
),
array
(
'
name
'
=>
'
dummytask
'
,
'
data
'
=>
''
)
);
foreach
(
$
expected
as
$
item
) {
$
tmp
=
$
this
->
QueuedTask
->
requestJob
(
$
capabilities
);
$
this
->
assertEqual
(
$
item
[
'
name
'
],
$
tmp
[
'
jobtype
'
]);
$
this
->
assertEqual
(
$
item
[
'
data
'
],
unserialize
(
$
tmp
[
'
data
'
]));
}
}
/**
* Job Rate limiting.
* Do not execute jobs of a certain type more often than once every X seconds.
*/
public
function
testRateLimit
() {
$
capabilities
=
array
(
'
task1
'
=>
array
(
'
name
'
=>
'
task1
'
,
'
timeout
'
=>
100
,
'
retries
'
=>
2
,
'
rate
'
=>
1
),
'
dummytask
'
=>
array
(
'
name
'
=>
'
dummytask
'
,
'
timeout
'
=>
100
,
'
retries
'
=>
2
)
);
// clear out the rate history
$
this
->
QueuedTask
->
rateHistory
=
array
();
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
task1
'
,
'
1
'
));
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
task1
'
,
'
2
'
));
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
task1
'
,
'
3
'
));
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
dummytask
'
,
null
));
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
dummytask
'
,
null
));
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
dummytask
'
,
null
));
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
dummytask
'
,
null
));
//At first we get task1-1.
$
tmp
=
$
this
->
QueuedTask
->
requestJob
(
$
capabilities
);
$
this
->
assertEqual
(
$
tmp
[
'
jobtype
'
],
'
task1
'
);
$
this
->
assertEqual
(
unserialize
(
$
tmp
[
'
data
'
]),
'
1
'
);
//The rate limit should now skip over task1-2 and fetch a dummytask.
$
tmp
=
$
this
->
QueuedTask
->
requestJob
(
$
capabilities
);
$
this
->
assertEqual
(
$
tmp
[
'
jobtype
'
],
'
dummytask
'
);
$
this
->
assertEqual
(
unserialize
(
$
tmp
[
'
data
'
]),
null
);
//and again.
$
tmp
=
$
this
->
QueuedTask
->
requestJob
(
$
capabilities
);
$
this
->
assertEqual
(
$
tmp
[
'
jobtype
'
],
'
dummytask
'
);
$
this
->
assertEqual
(
unserialize
(
$
tmp
[
'
data
'
]),
null
);
//Then some time passes
sleep
(
1
);
//Now we should get task1-2
$
tmp
=
$
this
->
QueuedTask
->
requestJob
(
$
capabilities
);
$
this
->
assertEqual
(
$
tmp
[
'
jobtype
'
],
'
task1
'
);
$
this
->
assertEqual
(
unserialize
(
$
tmp
[
'
data
'
]),
'
2
'
);
//and again rate limit to dummytask.
$
tmp
=
$
this
->
QueuedTask
->
requestJob
(
$
capabilities
);
$
this
->
assertEqual
(
$
tmp
[
'
jobtype
'
],
'
dummytask
'
);
$
this
->
assertEqual
(
unserialize
(
$
tmp
[
'
data
'
]),
null
);
//Then some more time passes
sleep
(
1
);
//Now we should get task1-3
$
tmp
=
$
this
->
QueuedTask
->
requestJob
(
$
capabilities
);
$
this
->
assertEqual
(
$
tmp
[
'
jobtype
'
],
'
task1
'
);
$
this
->
assertEqual
(
unserialize
(
$
tmp
[
'
data
'
]),
'
3
'
);
//and again rate limit to dummytask.
$
tmp
=
$
this
->
QueuedTask
->
requestJob
(
$
capabilities
);
$
this
->
assertEqual
(
$
tmp
[
'
jobtype
'
],
'
dummytask
'
);
$
this
->
assertEqual
(
unserialize
(
$
tmp
[
'
data
'
]),
null
);
//and now the queue is empty
$
tmp
=
$
this
->
QueuedTask
->
requestJob
(
$
capabilities
);
$
this
->
assertEqual
(
$
tmp
,
null
);
}
public
function
testRequeueAfterTimeout
() {
$
capabilities
=
array
(
'
task1
'
=>
array
(
'
name
'
=>
'
task1
'
,
'
timeout
'
=>
1
,
'
retries
'
=>
2
,
'
rate
'
=>
0
)
);
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
task1
'
,
'
1
'
));
$
tmp
=
$
this
->
QueuedTask
->
requestJob
(
$
capabilities
);
$
this
->
assertEqual
(
$
tmp
[
'
jobtype
'
],
'
task1
'
);
$
this
->
assertEqual
(
unserialize
(
$
tmp
[
'
data
'
]),
'
1
'
);
$
this
->
assertEqual
(
$
tmp
[
'
failed
'
],
'
0
'
);
sleep
(
2
);
$
tmp
=
$
this
->
QueuedTask
->
requestJob
(
$
capabilities
);
$
this
->
assertEqual
(
$
tmp
[
'
jobtype
'
],
'
task1
'
);
$
this
->
assertEqual
(
unserialize
(
$
tmp
[
'
data
'
]),
'
1
'
);
$
this
->
assertEqual
(
$
tmp
[
'
failed
'
],
'
1
'
);
$
this
->
assertEqual
(
$
tmp
[
'
failure_message
'
],
'
Restart after timeout
'
);
}
public
function
testRequestGroup
() {
$
capabilities
=
array
(
'
task1
'
=>
array
(
'
name
'
=>
'
task1
'
,
'
timeout
'
=>
1
,
'
retries
'
=>
2
,
'
rate
'
=>
0
)
);
// create an ungrouped task
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
task1
'
,
1
));
//create a Grouped Task
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
task1
'
,
2
,
null
,
'
testgroup
'
));
// Fetching without group should completely ignore the Group field.
$
tmp
=
$
this
->
QueuedTask
->
requestJob
(
$
capabilities
);
$
this
->
assertEqual
(
$
tmp
[
'
jobtype
'
],
'
task1
'
);
$
this
->
assertEqual
(
unserialize
(
$
tmp
[
'
data
'
]),
1
);
$
tmp
=
$
this
->
QueuedTask
->
requestJob
(
$
capabilities
);
$
this
->
assertEqual
(
$
tmp
[
'
jobtype
'
],
'
task1
'
);
$
this
->
assertEqual
(
unserialize
(
$
tmp
[
'
data
'
]),
2
);
// well, lets tra that Again, while limiting by Group
// create an ungrouped task
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
task1
'
,
3
));
//create a Grouped Task
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
task1
'
,
4
,
null
,
'
testgroup
'
,
'
Job number 4
'
));
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
task1
'
,
5
,
null
,
null
,
'
Job number 5
'
));
$
this
->
assertTrue
(
$
this
->
QueuedTask
->
createJob
(
'
task1
'
,
6
,
null
,
'
testgroup
'
,
'
Job number 6
'
));
// we should only get tasks 4 and 6, in that order, when requesting inside the group
$
tmp
=
$
this
->
QueuedTask
->
requestJob
(
$
capabilities
,
'
testgroup
'
);
$
this
->
assertEqual
(
$
tmp
[
'
jobtype
'
],
'
task1
'
);
$
this
->
assertEqual
(
unserialize
(
$
tmp
[
'
data
'
]),
4
);
$
tmp
=
$
this
->
QueuedTask
->
requestJob
(
$
capabilities
,
'
testgroup
'
);
$
this
->
assertEqual
(
$
tmp
[
'
jobtype
'
],
'
task1
'
);
$
this
->
assertEqual
(
unserialize
(
$
tmp
[
'
data
'
]),
6
);
// use FindProgress on the testgroup:
$
progress
=
$
this
->
QueuedTask
->
find
(
'
progress
'
,
array
(
'
conditions
'
=>
array
(
'
group
'
=>
'
testgroup
'
)
));
$
this
->
assertEqual
(
count
(
$
progress
),
3
);
$
this
->
assertNull
(
$
progress
[
0
][
'
reference
'
]);
$
this
->
assertEqual
(
$
progress
[
0
][
'
status
'
],
'
IN_PROGRESS
'
);
$
this
->
assertEqual
(
$
progress
[
1
][
'
reference
'
],
'
Job number 4
'
);
$
this
->
assertEqual
(
$
progress
[
1
][
'
status
'
],
'
IN_PROGRESS
'
);
$
this
->
assertEqual
(
$
progress
[
2
][
'
reference
'
],
'
Job number 6
'
);
$
this
->
assertEqual
(
$
progress
[
2
][
'
status
'
],
'
IN_PROGRESS
'
);
}
public
function
testMarkJobFailed
() {
$
this
->
QueuedTask
->
createJob
(
'
dummytask
'
,
null
);
$
id
=
$
this
->
QueuedTask
->
id
;
$
expected
=
'
Timeout: 100
'
;
$
this
->
QueuedTask
->
markJobFailed
(
$
id
,
$
expected
);
$
result
=
$
this
->
QueuedTask
->
field
(
'
failure_message
'
);
$
this
->
assertEqual
(
$
result
,
$
expected
);
}
}
?>
Back
|
FazBrowse Home
|
New Git URL