@@ -8,47 +8,47 @@ |
||
8 | 8 | */ |
9 | 9 | class QueuedTasksFixture extends TestFixture |
10 | 10 | { |
11 | - /** |
|
12 | - * Fields |
|
13 | - * |
|
14 | - * @var array |
|
15 | - */ |
|
16 | - // @codingStandardsIgnoreStart |
|
17 | - public $fields = [ |
|
18 | - 'id' => ['type' => 'integer', 'length' => 10, 'unsigned' => true, 'null' => false, 'default' => null, 'comment' => '', 'autoIncrement' => true, 'precision' => null], |
|
19 | - 'task' => ['type' => 'string', 'length' => 255, 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null, 'fixed' => null], |
|
20 | - 'data' => ['type' => 'text', 'length' => 16777215, 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null], |
|
21 | - 'not_before' => ['type' => 'timestamp', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null], |
|
22 | - 'fetched' => ['type' => 'timestamp', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null], |
|
23 | - 'completed' => ['type' => 'timestamp', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null], |
|
24 | - 'failed_count' => ['type' => 'integer', 'length' => 10, 'unsigned' => true, 'null' => false, 'default' => '0', 'comment' => '', 'precision' => null, 'autoIncrement' => null], |
|
25 | - 'failure_message' => ['type' => 'text', 'length' => null, 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null], |
|
26 | - 'worker_key' => ['type' => 'string', 'fixed' => true, 'length' => 40, 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null], |
|
27 | - 'created' => ['type' => 'timestamp', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null], |
|
28 | - '_indexes' => [ |
|
29 | - 'completed' => ['type' => 'index', 'columns' => ['completed'], 'length' => []], |
|
30 | - 'worker_key' => ['type' => 'index', 'columns' => ['worker_key'], 'length' => []], |
|
31 | - 'task' => ['type' => 'index', 'columns' => ['task'], 'length' => []], |
|
32 | - ], |
|
33 | - '_constraints' => [ |
|
34 | - 'primary' => ['type' => 'primary', 'columns' => ['id'], 'length' => []], |
|
35 | - ], |
|
36 | - '_options' => [ |
|
37 | - 'engine' => 'InnoDB', |
|
38 | - 'collation' => 'utf8_general_ci' |
|
39 | - ], |
|
40 | - ]; |
|
41 | - // @codingStandardsIgnoreEnd |
|
11 | + /** |
|
12 | + * Fields |
|
13 | + * |
|
14 | + * @var array |
|
15 | + */ |
|
16 | + // @codingStandardsIgnoreStart |
|
17 | + public $fields = [ |
|
18 | + 'id' => ['type' => 'integer', 'length' => 10, 'unsigned' => true, 'null' => false, 'default' => null, 'comment' => '', 'autoIncrement' => true, 'precision' => null], |
|
19 | + 'task' => ['type' => 'string', 'length' => 255, 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null, 'fixed' => null], |
|
20 | + 'data' => ['type' => 'text', 'length' => 16777215, 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null], |
|
21 | + 'not_before' => ['type' => 'timestamp', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null], |
|
22 | + 'fetched' => ['type' => 'timestamp', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null], |
|
23 | + 'completed' => ['type' => 'timestamp', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null], |
|
24 | + 'failed_count' => ['type' => 'integer', 'length' => 10, 'unsigned' => true, 'null' => false, 'default' => '0', 'comment' => '', 'precision' => null, 'autoIncrement' => null], |
|
25 | + 'failure_message' => ['type' => 'text', 'length' => null, 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null], |
|
26 | + 'worker_key' => ['type' => 'string', 'fixed' => true, 'length' => 40, 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null], |
|
27 | + 'created' => ['type' => 'timestamp', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null], |
|
28 | + '_indexes' => [ |
|
29 | + 'completed' => ['type' => 'index', 'columns' => ['completed'], 'length' => []], |
|
30 | + 'worker_key' => ['type' => 'index', 'columns' => ['worker_key'], 'length' => []], |
|
31 | + 'task' => ['type' => 'index', 'columns' => ['task'], 'length' => []], |
|
32 | + ], |
|
33 | + '_constraints' => [ |
|
34 | + 'primary' => ['type' => 'primary', 'columns' => ['id'], 'length' => []], |
|
35 | + ], |
|
36 | + '_options' => [ |
|
37 | + 'engine' => 'InnoDB', |
|
38 | + 'collation' => 'utf8_general_ci' |
|
39 | + ], |
|
40 | + ]; |
|
41 | + // @codingStandardsIgnoreEnd |
|
42 | 42 | |
43 | - /** |
|
44 | - * Init method |
|
45 | - * |
|
46 | - * @return void |
|
47 | - */ |
|
48 | - public function init() |
|
49 | - { |
|
50 | - $this->records = [ |
|
51 | - ]; |
|
52 | - parent::init(); |
|
53 | - } |
|
43 | + /** |
|
44 | + * Init method |
|
45 | + * |
|
46 | + * @return void |
|
47 | + */ |
|
48 | + public function init() |
|
49 | + { |
|
50 | + $this->records = [ |
|
51 | + ]; |
|
52 | + parent::init(); |
|
53 | + } |
|
54 | 54 | } |
@@ -1,18 +1,18 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | return [ |
4 | - 'Queue' => [ |
|
5 | - // time (in seconds) after which a job is requeued if the worker doesn't report back |
|
6 | - 'defaultWorkerTimeout' => 1800, |
|
4 | + 'Queue' => [ |
|
5 | + // time (in seconds) after which a job is requeued if the worker doesn't report back |
|
6 | + 'defaultWorkerTimeout' => 1800, |
|
7 | 7 | |
8 | - // seconds of running time after which the worker will terminate (0 = unlimited) |
|
9 | - 'workerMaxRuntime' => 120, |
|
8 | + // seconds of running time after which the worker will terminate (0 = unlimited) |
|
9 | + 'workerMaxRuntime' => 120, |
|
10 | 10 | |
11 | - // minimum time (in seconds) which a task remains in the database before being cleaned up. |
|
12 | - 'cleanupTimeout' => 2592000, // 30 days |
|
11 | + // minimum time (in seconds) which a task remains in the database before being cleaned up. |
|
12 | + 'cleanupTimeout' => 2592000, // 30 days |
|
13 | 13 | |
14 | - /* Optional */ |
|
14 | + /* Optional */ |
|
15 | 15 | |
16 | - 'isSearchEnabled' => true, |
|
17 | - ], |
|
16 | + 'isSearchEnabled' => true, |
|
17 | + ], |
|
18 | 18 | ]; |
@@ -19,502 +19,502 @@ |
||
19 | 19 | class QueuedTasksTableTest extends TestCase |
20 | 20 | { |
21 | 21 | |
22 | - /** |
|
23 | - * |
|
24 | - * @var \Queue\Model\Table\QueuedTasksTable |
|
25 | - */ |
|
26 | - protected $QueuedTasks; |
|
27 | - |
|
28 | - /** |
|
29 | - * Fixtures |
|
30 | - * |
|
31 | - * @var array |
|
32 | - */ |
|
33 | - public $fixtures = [ |
|
34 | - 'plugin.Queue.QueuedTasks' |
|
35 | - ]; |
|
36 | - |
|
37 | - /** |
|
38 | - * setUp method |
|
39 | - * |
|
40 | - * @return void |
|
41 | - */ |
|
42 | - public function setUp() |
|
43 | - { |
|
44 | - parent::setUp(); |
|
45 | - $config = TableRegistry::getTableLocator()->exists('QueuedTasks') ? [] : [ |
|
46 | - 'className' => QueuedTasksTable::class |
|
47 | - ]; |
|
48 | - $this->QueuedTasks = TableRegistry::getTableLocator()->get('QueuedTasks', $config); |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * Basic Instance test |
|
53 | - * |
|
54 | - * @return void |
|
55 | - */ |
|
56 | - public function testQueueInstance() |
|
57 | - { |
|
58 | - $this->assertInstanceOf(QueuedTasksTable::class, $this->QueuedTasks); |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Test the basic create and length evaluation functions. |
|
63 | - * |
|
64 | - * @return void |
|
65 | - */ |
|
66 | - public function testCreateAndCount() |
|
67 | - { |
|
68 | - // at first, the queue should contain 0 items. |
|
69 | - $this->assertSame(0, $this->QueuedTasks->getLength()); |
|
70 | - |
|
71 | - // create a job |
|
72 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('test1', [ |
|
73 | - 'some' => 'random', |
|
74 | - 'test' => 'data' |
|
75 | - ])); |
|
76 | - |
|
77 | - // test if queue Length is 1 now. |
|
78 | - $this->assertSame(1, $this->QueuedTasks->getLength()); |
|
79 | - |
|
80 | - // create some more jobs |
|
81 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('test2', [ |
|
82 | - 'some' => 'random', |
|
83 | - 'test' => 'data2' |
|
84 | - ])); |
|
85 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('test2', [ |
|
86 | - 'some' => 'random', |
|
87 | - 'test' => 'data3' |
|
88 | - ])); |
|
89 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('test3', [ |
|
90 | - 'some' => 'random', |
|
91 | - 'test' => 'data4' |
|
92 | - ])); |
|
93 | - |
|
94 | - // overall queueLength shpould now be 4 |
|
95 | - $this->assertSame(4, $this->QueuedTasks->getLength()); |
|
96 | - |
|
97 | - // there should be 1 task of type 'test1', one of type 'test3' and 2 of type 'test2' |
|
98 | - $this->assertSame(1, $this->QueuedTasks->getLength('test1')); |
|
99 | - $this->assertSame(2, $this->QueuedTasks->getLength('test2')); |
|
100 | - $this->assertSame(1, $this->QueuedTasks->getLength('test3')); |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Test the basic create and fetch functions. |
|
105 | - * |
|
106 | - * @return void |
|
107 | - */ |
|
108 | - public function testCreateAndFetch() |
|
109 | - { |
|
110 | - $this->_needsConnection(); |
|
111 | - |
|
112 | - // $capabilities is a list of tasks the worker can run. |
|
113 | - $capabilities = [ |
|
114 | - 'task1' => [ |
|
115 | - 'name' => 'task1', |
|
116 | - 'timeout' => 100, |
|
117 | - 'retries' => 2 |
|
118 | - ] |
|
119 | - ]; |
|
120 | - $testData = [ |
|
121 | - 'x1' => 'y1', |
|
122 | - 'x2' => 'y2', |
|
123 | - 'x3' => 'y3', |
|
124 | - 'x4' => 'y4' |
|
125 | - ]; |
|
126 | - |
|
127 | - // start off empty. |
|
128 | - $this->assertSame([], $this->QueuedTasks->find() |
|
129 | - ->toArray()); |
|
130 | - // at first, the queue should contain 0 items. |
|
131 | - $this->assertSame(0, $this->QueuedTasks->getLength()); |
|
132 | - // there are no jobs, so we cant fetch any. |
|
133 | - $this->assertNull($this->QueuedTasks->requestJob($capabilities)); |
|
134 | - // insert one job. |
|
135 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', $testData)); |
|
136 | - |
|
137 | - // fetch and check the first job. |
|
138 | - $job = $this->QueuedTasks->requestJob($capabilities); |
|
139 | - $this->assertSame(1, $job->id); |
|
140 | - $this->assertSame('task1', $job->task); |
|
141 | - $this->assertSame(0, $job->failed); |
|
142 | - $this->assertNull($job->completed); |
|
143 | - $this->assertSame($testData, unserialize($job->data)); |
|
144 | - |
|
145 | - // after this job has been fetched, it may not be reassigned. |
|
146 | - $result = $this->QueuedTasks->requestJob($capabilities); |
|
147 | - $this->assertNull($result); |
|
148 | - |
|
149 | - // queue length is still 1 since the first job did not finish. |
|
150 | - $this->assertSame(1, $this->QueuedTasks->getLength()); |
|
151 | - |
|
152 | - // Now mark Task1 as done |
|
153 | - $this->assertTrue($this->QueuedTasks->markJobDone($job)); |
|
154 | - |
|
155 | - // Should be 0 again. |
|
156 | - $this->assertSame(0, $this->QueuedTasks->getLength()); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Test the delivery of jobs in sequence, skipping fetched but not completed tasks. |
|
161 | - * |
|
162 | - * @return void |
|
163 | - */ |
|
164 | - public function testSequence() |
|
165 | - { |
|
166 | - $this->_needsConnection(); |
|
167 | - |
|
168 | - // $capabilities is a list of tasks the worker can run. |
|
169 | - $capabilities = [ |
|
170 | - 'task1' => [ |
|
171 | - 'name' => 'task1', |
|
172 | - 'timeout' => 100, |
|
173 | - 'retries' => 2 |
|
174 | - ] |
|
175 | - ]; |
|
176 | - // at first, the queue should contain 0 items. |
|
177 | - $this->assertSame(0, $this->QueuedTasks->getLength()); |
|
178 | - // create some more jobs |
|
179 | - foreach (range(0, 9) as $num) { |
|
180 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', [ |
|
181 | - 'tasknum' => $num |
|
182 | - ])); |
|
183 | - } |
|
184 | - // 10 jobs in the queue. |
|
185 | - $this->assertSame(10, $this->QueuedTasks->getLength()); |
|
186 | - |
|
187 | - // jobs should be fetched in the original sequence. |
|
188 | - $array = []; |
|
189 | - foreach (range(0, 4) as $num) { |
|
190 | - $this->QueuedTasks->clearKey(); |
|
191 | - $array[$num] = $this->QueuedTasks->requestJob($capabilities); |
|
192 | - $jobData = unserialize($array[$num]['data']); |
|
193 | - $this->assertSame($num, $jobData['tasknum']); |
|
194 | - } |
|
195 | - // now mark them as done |
|
196 | - foreach (range(0, 4) as $num) { |
|
197 | - $this->assertTrue($this->QueuedTasks->markJobDone($array[$num])); |
|
198 | - $this->assertSame(9 - $num, $this->QueuedTasks->getLength()); |
|
199 | - } |
|
200 | - |
|
201 | - // jobs should be fetched in the original sequence. |
|
202 | - foreach (range(5, 9) as $num) { |
|
203 | - $job = $this->QueuedTasks->requestJob($capabilities); |
|
204 | - $jobData = unserialize($job->data); |
|
205 | - $this->assertSame($num, $jobData['tasknum']); |
|
206 | - $this->assertTrue($this->QueuedTasks->markJobDone($job)); |
|
207 | - $this->assertSame(9 - $num, $this->QueuedTasks->getLength()); |
|
208 | - } |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * Test creating Jobs to run close to a specified time, and strtotime parsing. |
|
213 | - * Using toUnixString() function to convert Time object to timestamp, instead of strtotime |
|
214 | - * |
|
215 | - * @return null |
|
216 | - */ |
|
217 | - public function testNotBefore() |
|
218 | - { |
|
219 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', null, '+ 1 Min')); |
|
220 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', null, '+ 1 Day')); |
|
221 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', null, '2009-07-01 12:00:00')); |
|
222 | - $data = $this->QueuedTasks->find('all')->toArray(); |
|
223 | - $this->assertWithinRange((new Time('+ 1 Min'))->toUnixString(), $data[0]['not_before']->toUnixString(), 60); |
|
224 | - $this->assertWithinRange((new Time('+ 1 Day'))->toUnixString(), $data[1]['not_before']->toUnixString(), 60); |
|
225 | - $this->assertWithinRange((new Time('2009-07-01 12:00:00'))->toUnixString(), $data[2]['not_before']->toUnixString(), 60); |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Test Job reordering depending on 'notBefore' field. |
|
230 | - * Jobs with an expired not_before field should be executed before any other job without specific timing info. |
|
231 | - * |
|
232 | - * @return void |
|
233 | - */ |
|
234 | - public function testNotBeforeOrder() |
|
235 | - { |
|
236 | - $this->_needsConnection(); |
|
237 | - |
|
238 | - $capabilities = [ |
|
239 | - 'task1' => [ |
|
240 | - 'name' => 'task1', |
|
241 | - 'timeout' => 100, |
|
242 | - 'retries' => 2 |
|
243 | - ], |
|
244 | - 'dummytask' => [ |
|
245 | - 'name' => 'dummytask', |
|
246 | - 'timeout' => 100, |
|
247 | - 'retries' => 2 |
|
248 | - ] |
|
249 | - ]; |
|
250 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('dummytask')); |
|
251 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('dummytask')); |
|
252 | - // create a task with it's execution target some seconds in the past, so it should jump to the top of the testCreateAndFetchlist. |
|
253 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', [ |
|
254 | - 'three' |
|
255 | - ], '- 3 Seconds')); |
|
256 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', [ |
|
257 | - 'two' |
|
258 | - ], '- 5 Seconds')); |
|
259 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', [ |
|
260 | - 'one' |
|
261 | - ], '- 7 Seconds')); |
|
262 | - |
|
263 | - // when using requestJob, the jobs we just created should be delivered in this order, NOT the order in which they where created. |
|
264 | - $expected = [ |
|
265 | - [ |
|
266 | - 'name' => 'task1', |
|
267 | - 'data' => [ |
|
268 | - 'one' |
|
269 | - ] |
|
270 | - ], |
|
271 | - [ |
|
272 | - 'name' => 'task1', |
|
273 | - 'data' => [ |
|
274 | - 'two' |
|
275 | - ] |
|
276 | - ], |
|
277 | - [ |
|
278 | - 'name' => 'task1', |
|
279 | - 'data' => [ |
|
280 | - 'three' |
|
281 | - ] |
|
282 | - ], |
|
283 | - [ |
|
284 | - 'name' => 'dummytask', |
|
285 | - 'data' => null |
|
286 | - ], |
|
287 | - [ |
|
288 | - 'name' => 'dummytask', |
|
289 | - 'data' => null |
|
290 | - ] |
|
291 | - ]; |
|
292 | - |
|
293 | - foreach ($expected as $item) { |
|
294 | - $this->QueuedTasks->clearKey(); |
|
295 | - $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
296 | - |
|
297 | - $this->assertSame($item['name'], $tmp['task']); |
|
298 | - $this->assertEquals($item['data'], unserialize($tmp['data'])); |
|
299 | - } |
|
300 | - } |
|
301 | - |
|
302 | - /** |
|
303 | - * Job Rate limiting. |
|
304 | - * Do not execute jobs of a certain type more often than once every X seconds. |
|
305 | - * |
|
306 | - * @return void |
|
307 | - */ |
|
308 | - public function testRateLimit() |
|
309 | - { |
|
310 | - $this->_needsConnection(); |
|
311 | - |
|
312 | - $capabilities = [ |
|
313 | - 'task1' => [ |
|
314 | - 'name' => 'task1', |
|
315 | - 'timeout' => 101, |
|
316 | - 'retries' => 2, |
|
317 | - 'rate' => 2 |
|
318 | - ], |
|
319 | - 'dummytask' => [ |
|
320 | - 'name' => 'dummytask', |
|
321 | - 'timeout' => 101, |
|
322 | - 'retries' => 2 |
|
323 | - ] |
|
324 | - ]; |
|
325 | - |
|
326 | - // clear out the rate history |
|
327 | - $this->QueuedTasks->rateHistory = []; |
|
328 | - |
|
329 | - $data1 = [ |
|
330 | - 'key' => 1 |
|
331 | - ]; |
|
332 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', $data1)); |
|
333 | - $data2 = [ |
|
334 | - 'key' => 2 |
|
335 | - ]; |
|
336 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', $data2)); |
|
337 | - $data3 = [ |
|
338 | - 'key' => 3 |
|
339 | - ]; |
|
340 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', $data3)); |
|
341 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('dummytask')); |
|
342 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('dummytask')); |
|
343 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('dummytask')); |
|
344 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('dummytask')); |
|
345 | - |
|
346 | - // At first we get task1-1. |
|
347 | - $this->QueuedTasks->clearKey(); |
|
348 | - $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
349 | - $this->assertSame('task1', $tmp['task']); |
|
350 | - $this->assertSame($data1, unserialize($tmp['data'])); |
|
351 | - |
|
352 | - // The rate limit should now skip over task1-2 and fetch a dummytask. |
|
353 | - $this->QueuedTasks->clearKey(); |
|
354 | - $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
355 | - $this->assertSame('dummytask', $tmp['task']); |
|
356 | - $this->assertFalse(unserialize($tmp['data'])); |
|
357 | - |
|
358 | - usleep(100000); |
|
359 | - // and again. |
|
360 | - $this->QueuedTasks->clearKey(); |
|
361 | - $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
362 | - $this->assertSame('dummytask', $tmp['task']); |
|
363 | - $this->assertFalse(unserialize($tmp['data'])); |
|
364 | - |
|
365 | - // Then some time passes |
|
366 | - sleep(2); |
|
367 | - |
|
368 | - // Now we should get task1-2 |
|
369 | - $this->QueuedTasks->clearKey(); |
|
370 | - $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
371 | - $this->assertSame('task1', $tmp['task']); |
|
372 | - $this->assertSame($data2, unserialize($tmp['data'])); |
|
373 | - |
|
374 | - // and again rate limit to dummytask. |
|
375 | - $this->QueuedTasks->clearKey(); |
|
376 | - $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
377 | - $this->assertSame('dummytask', $tmp['task']); |
|
378 | - $this->assertFalse(unserialize($tmp['data'])); |
|
379 | - |
|
380 | - // Then some more time passes |
|
381 | - sleep(2); |
|
382 | - |
|
383 | - // Now we should get task1-3 |
|
384 | - $this->QueuedTasks->clearKey(); |
|
385 | - $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
386 | - $this->assertSame('task1', $tmp['task']); |
|
387 | - $this->assertSame($data3, unserialize($tmp['data'])); |
|
388 | - |
|
389 | - // and again rate limit to dummytask. |
|
390 | - $this->QueuedTasks->clearKey(); |
|
391 | - $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
392 | - $this->assertSame('dummytask', $tmp['task']); |
|
393 | - $this->assertFalse(unserialize($tmp['data'])); |
|
394 | - |
|
395 | - // and now the queue is empty |
|
396 | - $this->QueuedTasks->clearKey(); |
|
397 | - $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
398 | - $this->assertNull($tmp); |
|
399 | - } |
|
400 | - |
|
401 | - /** |
|
402 | - * Are those tests still valid? //FIXME |
|
403 | - * |
|
404 | - * @return void |
|
405 | - */ |
|
406 | - public function _testRequeueAfterTimeout() |
|
407 | - { |
|
408 | - $capabilities = [ |
|
409 | - 'task1' => [ |
|
410 | - 'name' => 'task1', |
|
411 | - 'timeout' => 1, |
|
412 | - 'retries' => 2, |
|
413 | - 'rate' => 0 |
|
414 | - ] |
|
415 | - ]; |
|
416 | - |
|
417 | - $data = [ |
|
418 | - 'key' => '1' |
|
419 | - ]; |
|
420 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', $data)); |
|
421 | - |
|
422 | - $this->QueuedTasks->clearKey(); |
|
423 | - $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
424 | - $this->assertSame('task1', $tmp['task']); |
|
425 | - $this->assertSame($data, unserialize($tmp['data'])); |
|
426 | - $this->assertSame('0', $tmp['failed']); |
|
427 | - sleep(2); |
|
428 | - |
|
429 | - $this->QueuedTasks->clearKey(); |
|
430 | - $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
431 | - $this->assertSame('task1', $tmp['task']); |
|
432 | - $this->assertSame($data, unserialize($tmp['data'])); |
|
433 | - $this->assertSame('1', $tmp['failed']); |
|
434 | - $this->assertSame('Restart after timeout', $tmp['failure_message']); |
|
435 | - } |
|
436 | - |
|
437 | - /** |
|
438 | - * Tests whether the timeout of second tasks doesn't interfere with |
|
439 | - * requeue of tasks |
|
440 | - * |
|
441 | - * Are those tests still valid? //FIXME |
|
442 | - * |
|
443 | - * @return void |
|
444 | - */ |
|
445 | - public function _testRequeueAfterTimeout2() |
|
446 | - { |
|
447 | - $capabilities = [ |
|
448 | - 'task1' => [ |
|
449 | - 'name' => 'task1', |
|
450 | - 'timeout' => 1, |
|
451 | - 'retries' => 2, |
|
452 | - 'rate' => 0 |
|
453 | - ], |
|
454 | - 'task2' => [ |
|
455 | - 'name' => 'task2', |
|
456 | - 'timeout' => 100, |
|
457 | - 'retries' => 2, |
|
458 | - 'rate' => 0 |
|
459 | - ] |
|
460 | - ]; |
|
461 | - |
|
462 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', [ |
|
463 | - '1' |
|
464 | - ])); |
|
465 | - |
|
466 | - $this->QueuedTasks->clearKey(); |
|
467 | - $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
468 | - $this->assertSame('task1', $tmp['task']); |
|
469 | - $this->assertSame([ |
|
470 | - '1' |
|
471 | - ], unserialize($tmp['data'])); |
|
472 | - $this->assertSame('0', $tmp['failed']); |
|
473 | - sleep(2); |
|
474 | - |
|
475 | - $this->QueuedTasks->clearKey(); |
|
476 | - $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
477 | - $this->assertSame('task1', $tmp['task']); |
|
478 | - $this->assertSame([ |
|
479 | - '1' |
|
480 | - ], unserialize($tmp['data'])); |
|
481 | - $this->assertSame('1', $tmp['failed']); |
|
482 | - $this->assertSame('Restart after timeout', $tmp['failure_message']); |
|
483 | - } |
|
484 | - |
|
485 | - /** |
|
486 | - * |
|
487 | - * @return void |
|
488 | - */ |
|
489 | - public function testIsQueued() |
|
490 | - { |
|
491 | - $result = $this->QueuedTasks->isQueued('foo-bar'); |
|
492 | - $this->assertFalse($result); |
|
493 | - |
|
494 | - $queuedJob = $this->QueuedTasks->newEntity([ |
|
495 | - 'key' => 'key', |
|
496 | - 'task' => 'FooBar' |
|
497 | - ]); |
|
498 | - $this->QueuedTasks->saveOrFail($queuedJob); |
|
499 | - |
|
500 | - $result = $this->QueuedTasks->isQueued('foo-bar'); |
|
501 | - $this->assertTrue($result); |
|
502 | - |
|
503 | - $queuedJob->completed = new FrozenTime(); |
|
504 | - $this->QueuedTasks->saveOrFail($queuedJob); |
|
505 | - |
|
506 | - $result = $this->QueuedTasks->isQueued('foo-bar'); |
|
507 | - $this->assertFalse($result); |
|
508 | - } |
|
509 | - |
|
510 | - /** |
|
511 | - * Helper method for skipping tests that need a real connection. |
|
512 | - * |
|
513 | - * @return void |
|
514 | - */ |
|
515 | - protected function _needsConnection() |
|
516 | - { |
|
517 | - $config = ConnectionManager::getConfig('test'); |
|
518 | - $this->skipIf(strpos($config['driver'], 'Mysql') === false, 'Only Mysql is working yet for this.'); |
|
519 | - } |
|
22 | + /** |
|
23 | + * |
|
24 | + * @var \Queue\Model\Table\QueuedTasksTable |
|
25 | + */ |
|
26 | + protected $QueuedTasks; |
|
27 | + |
|
28 | + /** |
|
29 | + * Fixtures |
|
30 | + * |
|
31 | + * @var array |
|
32 | + */ |
|
33 | + public $fixtures = [ |
|
34 | + 'plugin.Queue.QueuedTasks' |
|
35 | + ]; |
|
36 | + |
|
37 | + /** |
|
38 | + * setUp method |
|
39 | + * |
|
40 | + * @return void |
|
41 | + */ |
|
42 | + public function setUp() |
|
43 | + { |
|
44 | + parent::setUp(); |
|
45 | + $config = TableRegistry::getTableLocator()->exists('QueuedTasks') ? [] : [ |
|
46 | + 'className' => QueuedTasksTable::class |
|
47 | + ]; |
|
48 | + $this->QueuedTasks = TableRegistry::getTableLocator()->get('QueuedTasks', $config); |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * Basic Instance test |
|
53 | + * |
|
54 | + * @return void |
|
55 | + */ |
|
56 | + public function testQueueInstance() |
|
57 | + { |
|
58 | + $this->assertInstanceOf(QueuedTasksTable::class, $this->QueuedTasks); |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Test the basic create and length evaluation functions. |
|
63 | + * |
|
64 | + * @return void |
|
65 | + */ |
|
66 | + public function testCreateAndCount() |
|
67 | + { |
|
68 | + // at first, the queue should contain 0 items. |
|
69 | + $this->assertSame(0, $this->QueuedTasks->getLength()); |
|
70 | + |
|
71 | + // create a job |
|
72 | + $this->assertTrue((bool) $this->QueuedTasks->createJob('test1', [ |
|
73 | + 'some' => 'random', |
|
74 | + 'test' => 'data' |
|
75 | + ])); |
|
76 | + |
|
77 | + // test if queue Length is 1 now. |
|
78 | + $this->assertSame(1, $this->QueuedTasks->getLength()); |
|
79 | + |
|
80 | + // create some more jobs |
|
81 | + $this->assertTrue((bool) $this->QueuedTasks->createJob('test2', [ |
|
82 | + 'some' => 'random', |
|
83 | + 'test' => 'data2' |
|
84 | + ])); |
|
85 | + $this->assertTrue((bool) $this->QueuedTasks->createJob('test2', [ |
|
86 | + 'some' => 'random', |
|
87 | + 'test' => 'data3' |
|
88 | + ])); |
|
89 | + $this->assertTrue((bool) $this->QueuedTasks->createJob('test3', [ |
|
90 | + 'some' => 'random', |
|
91 | + 'test' => 'data4' |
|
92 | + ])); |
|
93 | + |
|
94 | + // overall queueLength shpould now be 4 |
|
95 | + $this->assertSame(4, $this->QueuedTasks->getLength()); |
|
96 | + |
|
97 | + // there should be 1 task of type 'test1', one of type 'test3' and 2 of type 'test2' |
|
98 | + $this->assertSame(1, $this->QueuedTasks->getLength('test1')); |
|
99 | + $this->assertSame(2, $this->QueuedTasks->getLength('test2')); |
|
100 | + $this->assertSame(1, $this->QueuedTasks->getLength('test3')); |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Test the basic create and fetch functions. |
|
105 | + * |
|
106 | + * @return void |
|
107 | + */ |
|
108 | + public function testCreateAndFetch() |
|
109 | + { |
|
110 | + $this->_needsConnection(); |
|
111 | + |
|
112 | + // $capabilities is a list of tasks the worker can run. |
|
113 | + $capabilities = [ |
|
114 | + 'task1' => [ |
|
115 | + 'name' => 'task1', |
|
116 | + 'timeout' => 100, |
|
117 | + 'retries' => 2 |
|
118 | + ] |
|
119 | + ]; |
|
120 | + $testData = [ |
|
121 | + 'x1' => 'y1', |
|
122 | + 'x2' => 'y2', |
|
123 | + 'x3' => 'y3', |
|
124 | + 'x4' => 'y4' |
|
125 | + ]; |
|
126 | + |
|
127 | + // start off empty. |
|
128 | + $this->assertSame([], $this->QueuedTasks->find() |
|
129 | + ->toArray()); |
|
130 | + // at first, the queue should contain 0 items. |
|
131 | + $this->assertSame(0, $this->QueuedTasks->getLength()); |
|
132 | + // there are no jobs, so we cant fetch any. |
|
133 | + $this->assertNull($this->QueuedTasks->requestJob($capabilities)); |
|
134 | + // insert one job. |
|
135 | + $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', $testData)); |
|
136 | + |
|
137 | + // fetch and check the first job. |
|
138 | + $job = $this->QueuedTasks->requestJob($capabilities); |
|
139 | + $this->assertSame(1, $job->id); |
|
140 | + $this->assertSame('task1', $job->task); |
|
141 | + $this->assertSame(0, $job->failed); |
|
142 | + $this->assertNull($job->completed); |
|
143 | + $this->assertSame($testData, unserialize($job->data)); |
|
144 | + |
|
145 | + // after this job has been fetched, it may not be reassigned. |
|
146 | + $result = $this->QueuedTasks->requestJob($capabilities); |
|
147 | + $this->assertNull($result); |
|
148 | + |
|
149 | + // queue length is still 1 since the first job did not finish. |
|
150 | + $this->assertSame(1, $this->QueuedTasks->getLength()); |
|
151 | + |
|
152 | + // Now mark Task1 as done |
|
153 | + $this->assertTrue($this->QueuedTasks->markJobDone($job)); |
|
154 | + |
|
155 | + // Should be 0 again. |
|
156 | + $this->assertSame(0, $this->QueuedTasks->getLength()); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Test the delivery of jobs in sequence, skipping fetched but not completed tasks. |
|
161 | + * |
|
162 | + * @return void |
|
163 | + */ |
|
164 | + public function testSequence() |
|
165 | + { |
|
166 | + $this->_needsConnection(); |
|
167 | + |
|
168 | + // $capabilities is a list of tasks the worker can run. |
|
169 | + $capabilities = [ |
|
170 | + 'task1' => [ |
|
171 | + 'name' => 'task1', |
|
172 | + 'timeout' => 100, |
|
173 | + 'retries' => 2 |
|
174 | + ] |
|
175 | + ]; |
|
176 | + // at first, the queue should contain 0 items. |
|
177 | + $this->assertSame(0, $this->QueuedTasks->getLength()); |
|
178 | + // create some more jobs |
|
179 | + foreach (range(0, 9) as $num) { |
|
180 | + $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', [ |
|
181 | + 'tasknum' => $num |
|
182 | + ])); |
|
183 | + } |
|
184 | + // 10 jobs in the queue. |
|
185 | + $this->assertSame(10, $this->QueuedTasks->getLength()); |
|
186 | + |
|
187 | + // jobs should be fetched in the original sequence. |
|
188 | + $array = []; |
|
189 | + foreach (range(0, 4) as $num) { |
|
190 | + $this->QueuedTasks->clearKey(); |
|
191 | + $array[$num] = $this->QueuedTasks->requestJob($capabilities); |
|
192 | + $jobData = unserialize($array[$num]['data']); |
|
193 | + $this->assertSame($num, $jobData['tasknum']); |
|
194 | + } |
|
195 | + // now mark them as done |
|
196 | + foreach (range(0, 4) as $num) { |
|
197 | + $this->assertTrue($this->QueuedTasks->markJobDone($array[$num])); |
|
198 | + $this->assertSame(9 - $num, $this->QueuedTasks->getLength()); |
|
199 | + } |
|
200 | + |
|
201 | + // jobs should be fetched in the original sequence. |
|
202 | + foreach (range(5, 9) as $num) { |
|
203 | + $job = $this->QueuedTasks->requestJob($capabilities); |
|
204 | + $jobData = unserialize($job->data); |
|
205 | + $this->assertSame($num, $jobData['tasknum']); |
|
206 | + $this->assertTrue($this->QueuedTasks->markJobDone($job)); |
|
207 | + $this->assertSame(9 - $num, $this->QueuedTasks->getLength()); |
|
208 | + } |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * Test creating Jobs to run close to a specified time, and strtotime parsing. |
|
213 | + * Using toUnixString() function to convert Time object to timestamp, instead of strtotime |
|
214 | + * |
|
215 | + * @return null |
|
216 | + */ |
|
217 | + public function testNotBefore() |
|
218 | + { |
|
219 | + $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', null, '+ 1 Min')); |
|
220 | + $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', null, '+ 1 Day')); |
|
221 | + $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', null, '2009-07-01 12:00:00')); |
|
222 | + $data = $this->QueuedTasks->find('all')->toArray(); |
|
223 | + $this->assertWithinRange((new Time('+ 1 Min'))->toUnixString(), $data[0]['not_before']->toUnixString(), 60); |
|
224 | + $this->assertWithinRange((new Time('+ 1 Day'))->toUnixString(), $data[1]['not_before']->toUnixString(), 60); |
|
225 | + $this->assertWithinRange((new Time('2009-07-01 12:00:00'))->toUnixString(), $data[2]['not_before']->toUnixString(), 60); |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Test Job reordering depending on 'notBefore' field. |
|
230 | + * Jobs with an expired not_before field should be executed before any other job without specific timing info. |
|
231 | + * |
|
232 | + * @return void |
|
233 | + */ |
|
234 | + public function testNotBeforeOrder() |
|
235 | + { |
|
236 | + $this->_needsConnection(); |
|
237 | + |
|
238 | + $capabilities = [ |
|
239 | + 'task1' => [ |
|
240 | + 'name' => 'task1', |
|
241 | + 'timeout' => 100, |
|
242 | + 'retries' => 2 |
|
243 | + ], |
|
244 | + 'dummytask' => [ |
|
245 | + 'name' => 'dummytask', |
|
246 | + 'timeout' => 100, |
|
247 | + 'retries' => 2 |
|
248 | + ] |
|
249 | + ]; |
|
250 | + $this->assertTrue((bool) $this->QueuedTasks->createJob('dummytask')); |
|
251 | + $this->assertTrue((bool) $this->QueuedTasks->createJob('dummytask')); |
|
252 | + // create a task with it's execution target some seconds in the past, so it should jump to the top of the testCreateAndFetchlist. |
|
253 | + $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', [ |
|
254 | + 'three' |
|
255 | + ], '- 3 Seconds')); |
|
256 | + $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', [ |
|
257 | + 'two' |
|
258 | + ], '- 5 Seconds')); |
|
259 | + $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', [ |
|
260 | + 'one' |
|
261 | + ], '- 7 Seconds')); |
|
262 | + |
|
263 | + // when using requestJob, the jobs we just created should be delivered in this order, NOT the order in which they where created. |
|
264 | + $expected = [ |
|
265 | + [ |
|
266 | + 'name' => 'task1', |
|
267 | + 'data' => [ |
|
268 | + 'one' |
|
269 | + ] |
|
270 | + ], |
|
271 | + [ |
|
272 | + 'name' => 'task1', |
|
273 | + 'data' => [ |
|
274 | + 'two' |
|
275 | + ] |
|
276 | + ], |
|
277 | + [ |
|
278 | + 'name' => 'task1', |
|
279 | + 'data' => [ |
|
280 | + 'three' |
|
281 | + ] |
|
282 | + ], |
|
283 | + [ |
|
284 | + 'name' => 'dummytask', |
|
285 | + 'data' => null |
|
286 | + ], |
|
287 | + [ |
|
288 | + 'name' => 'dummytask', |
|
289 | + 'data' => null |
|
290 | + ] |
|
291 | + ]; |
|
292 | + |
|
293 | + foreach ($expected as $item) { |
|
294 | + $this->QueuedTasks->clearKey(); |
|
295 | + $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
296 | + |
|
297 | + $this->assertSame($item['name'], $tmp['task']); |
|
298 | + $this->assertEquals($item['data'], unserialize($tmp['data'])); |
|
299 | + } |
|
300 | + } |
|
301 | + |
|
302 | + /** |
|
303 | + * Job Rate limiting. |
|
304 | + * Do not execute jobs of a certain type more often than once every X seconds. |
|
305 | + * |
|
306 | + * @return void |
|
307 | + */ |
|
308 | + public function testRateLimit() |
|
309 | + { |
|
310 | + $this->_needsConnection(); |
|
311 | + |
|
312 | + $capabilities = [ |
|
313 | + 'task1' => [ |
|
314 | + 'name' => 'task1', |
|
315 | + 'timeout' => 101, |
|
316 | + 'retries' => 2, |
|
317 | + 'rate' => 2 |
|
318 | + ], |
|
319 | + 'dummytask' => [ |
|
320 | + 'name' => 'dummytask', |
|
321 | + 'timeout' => 101, |
|
322 | + 'retries' => 2 |
|
323 | + ] |
|
324 | + ]; |
|
325 | + |
|
326 | + // clear out the rate history |
|
327 | + $this->QueuedTasks->rateHistory = []; |
|
328 | + |
|
329 | + $data1 = [ |
|
330 | + 'key' => 1 |
|
331 | + ]; |
|
332 | + $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', $data1)); |
|
333 | + $data2 = [ |
|
334 | + 'key' => 2 |
|
335 | + ]; |
|
336 | + $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', $data2)); |
|
337 | + $data3 = [ |
|
338 | + 'key' => 3 |
|
339 | + ]; |
|
340 | + $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', $data3)); |
|
341 | + $this->assertTrue((bool) $this->QueuedTasks->createJob('dummytask')); |
|
342 | + $this->assertTrue((bool) $this->QueuedTasks->createJob('dummytask')); |
|
343 | + $this->assertTrue((bool) $this->QueuedTasks->createJob('dummytask')); |
|
344 | + $this->assertTrue((bool) $this->QueuedTasks->createJob('dummytask')); |
|
345 | + |
|
346 | + // At first we get task1-1. |
|
347 | + $this->QueuedTasks->clearKey(); |
|
348 | + $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
349 | + $this->assertSame('task1', $tmp['task']); |
|
350 | + $this->assertSame($data1, unserialize($tmp['data'])); |
|
351 | + |
|
352 | + // The rate limit should now skip over task1-2 and fetch a dummytask. |
|
353 | + $this->QueuedTasks->clearKey(); |
|
354 | + $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
355 | + $this->assertSame('dummytask', $tmp['task']); |
|
356 | + $this->assertFalse(unserialize($tmp['data'])); |
|
357 | + |
|
358 | + usleep(100000); |
|
359 | + // and again. |
|
360 | + $this->QueuedTasks->clearKey(); |
|
361 | + $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
362 | + $this->assertSame('dummytask', $tmp['task']); |
|
363 | + $this->assertFalse(unserialize($tmp['data'])); |
|
364 | + |
|
365 | + // Then some time passes |
|
366 | + sleep(2); |
|
367 | + |
|
368 | + // Now we should get task1-2 |
|
369 | + $this->QueuedTasks->clearKey(); |
|
370 | + $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
371 | + $this->assertSame('task1', $tmp['task']); |
|
372 | + $this->assertSame($data2, unserialize($tmp['data'])); |
|
373 | + |
|
374 | + // and again rate limit to dummytask. |
|
375 | + $this->QueuedTasks->clearKey(); |
|
376 | + $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
377 | + $this->assertSame('dummytask', $tmp['task']); |
|
378 | + $this->assertFalse(unserialize($tmp['data'])); |
|
379 | + |
|
380 | + // Then some more time passes |
|
381 | + sleep(2); |
|
382 | + |
|
383 | + // Now we should get task1-3 |
|
384 | + $this->QueuedTasks->clearKey(); |
|
385 | + $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
386 | + $this->assertSame('task1', $tmp['task']); |
|
387 | + $this->assertSame($data3, unserialize($tmp['data'])); |
|
388 | + |
|
389 | + // and again rate limit to dummytask. |
|
390 | + $this->QueuedTasks->clearKey(); |
|
391 | + $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
392 | + $this->assertSame('dummytask', $tmp['task']); |
|
393 | + $this->assertFalse(unserialize($tmp['data'])); |
|
394 | + |
|
395 | + // and now the queue is empty |
|
396 | + $this->QueuedTasks->clearKey(); |
|
397 | + $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
398 | + $this->assertNull($tmp); |
|
399 | + } |
|
400 | + |
|
401 | + /** |
|
402 | + * Are those tests still valid? //FIXME |
|
403 | + * |
|
404 | + * @return void |
|
405 | + */ |
|
406 | + public function _testRequeueAfterTimeout() |
|
407 | + { |
|
408 | + $capabilities = [ |
|
409 | + 'task1' => [ |
|
410 | + 'name' => 'task1', |
|
411 | + 'timeout' => 1, |
|
412 | + 'retries' => 2, |
|
413 | + 'rate' => 0 |
|
414 | + ] |
|
415 | + ]; |
|
416 | + |
|
417 | + $data = [ |
|
418 | + 'key' => '1' |
|
419 | + ]; |
|
420 | + $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', $data)); |
|
421 | + |
|
422 | + $this->QueuedTasks->clearKey(); |
|
423 | + $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
424 | + $this->assertSame('task1', $tmp['task']); |
|
425 | + $this->assertSame($data, unserialize($tmp['data'])); |
|
426 | + $this->assertSame('0', $tmp['failed']); |
|
427 | + sleep(2); |
|
428 | + |
|
429 | + $this->QueuedTasks->clearKey(); |
|
430 | + $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
431 | + $this->assertSame('task1', $tmp['task']); |
|
432 | + $this->assertSame($data, unserialize($tmp['data'])); |
|
433 | + $this->assertSame('1', $tmp['failed']); |
|
434 | + $this->assertSame('Restart after timeout', $tmp['failure_message']); |
|
435 | + } |
|
436 | + |
|
437 | + /** |
|
438 | + * Tests whether the timeout of second tasks doesn't interfere with |
|
439 | + * requeue of tasks |
|
440 | + * |
|
441 | + * Are those tests still valid? //FIXME |
|
442 | + * |
|
443 | + * @return void |
|
444 | + */ |
|
445 | + public function _testRequeueAfterTimeout2() |
|
446 | + { |
|
447 | + $capabilities = [ |
|
448 | + 'task1' => [ |
|
449 | + 'name' => 'task1', |
|
450 | + 'timeout' => 1, |
|
451 | + 'retries' => 2, |
|
452 | + 'rate' => 0 |
|
453 | + ], |
|
454 | + 'task2' => [ |
|
455 | + 'name' => 'task2', |
|
456 | + 'timeout' => 100, |
|
457 | + 'retries' => 2, |
|
458 | + 'rate' => 0 |
|
459 | + ] |
|
460 | + ]; |
|
461 | + |
|
462 | + $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', [ |
|
463 | + '1' |
|
464 | + ])); |
|
465 | + |
|
466 | + $this->QueuedTasks->clearKey(); |
|
467 | + $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
468 | + $this->assertSame('task1', $tmp['task']); |
|
469 | + $this->assertSame([ |
|
470 | + '1' |
|
471 | + ], unserialize($tmp['data'])); |
|
472 | + $this->assertSame('0', $tmp['failed']); |
|
473 | + sleep(2); |
|
474 | + |
|
475 | + $this->QueuedTasks->clearKey(); |
|
476 | + $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
477 | + $this->assertSame('task1', $tmp['task']); |
|
478 | + $this->assertSame([ |
|
479 | + '1' |
|
480 | + ], unserialize($tmp['data'])); |
|
481 | + $this->assertSame('1', $tmp['failed']); |
|
482 | + $this->assertSame('Restart after timeout', $tmp['failure_message']); |
|
483 | + } |
|
484 | + |
|
485 | + /** |
|
486 | + * |
|
487 | + * @return void |
|
488 | + */ |
|
489 | + public function testIsQueued() |
|
490 | + { |
|
491 | + $result = $this->QueuedTasks->isQueued('foo-bar'); |
|
492 | + $this->assertFalse($result); |
|
493 | + |
|
494 | + $queuedJob = $this->QueuedTasks->newEntity([ |
|
495 | + 'key' => 'key', |
|
496 | + 'task' => 'FooBar' |
|
497 | + ]); |
|
498 | + $this->QueuedTasks->saveOrFail($queuedJob); |
|
499 | + |
|
500 | + $result = $this->QueuedTasks->isQueued('foo-bar'); |
|
501 | + $this->assertTrue($result); |
|
502 | + |
|
503 | + $queuedJob->completed = new FrozenTime(); |
|
504 | + $this->QueuedTasks->saveOrFail($queuedJob); |
|
505 | + |
|
506 | + $result = $this->QueuedTasks->isQueued('foo-bar'); |
|
507 | + $this->assertFalse($result); |
|
508 | + } |
|
509 | + |
|
510 | + /** |
|
511 | + * Helper method for skipping tests that need a real connection. |
|
512 | + * |
|
513 | + * @return void |
|
514 | + */ |
|
515 | + protected function _needsConnection() |
|
516 | + { |
|
517 | + $config = ConnectionManager::getConfig('test'); |
|
518 | + $this->skipIf(strpos($config['driver'], 'Mysql') === false, 'Only Mysql is working yet for this.'); |
|
519 | + } |
|
520 | 520 | } |
521 | 521 | \ No newline at end of file |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | $this->assertSame(0, $this->QueuedTasks->getLength()); |
70 | 70 | |
71 | 71 | // create a job |
72 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('test1', [ |
|
72 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('test1', [ |
|
73 | 73 | 'some' => 'random', |
74 | 74 | 'test' => 'data' |
75 | 75 | ])); |
@@ -78,15 +78,15 @@ discard block |
||
78 | 78 | $this->assertSame(1, $this->QueuedTasks->getLength()); |
79 | 79 | |
80 | 80 | // create some more jobs |
81 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('test2', [ |
|
81 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('test2', [ |
|
82 | 82 | 'some' => 'random', |
83 | 83 | 'test' => 'data2' |
84 | 84 | ])); |
85 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('test2', [ |
|
85 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('test2', [ |
|
86 | 86 | 'some' => 'random', |
87 | 87 | 'test' => 'data3' |
88 | 88 | ])); |
89 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('test3', [ |
|
89 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('test3', [ |
|
90 | 90 | 'some' => 'random', |
91 | 91 | 'test' => 'data4' |
92 | 92 | ])); |
@@ -132,7 +132,7 @@ discard block |
||
132 | 132 | // there are no jobs, so we cant fetch any. |
133 | 133 | $this->assertNull($this->QueuedTasks->requestJob($capabilities)); |
134 | 134 | // insert one job. |
135 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', $testData)); |
|
135 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', $testData)); |
|
136 | 136 | |
137 | 137 | // fetch and check the first job. |
138 | 138 | $job = $this->QueuedTasks->requestJob($capabilities); |
@@ -177,7 +177,7 @@ discard block |
||
177 | 177 | $this->assertSame(0, $this->QueuedTasks->getLength()); |
178 | 178 | // create some more jobs |
179 | 179 | foreach (range(0, 9) as $num) { |
180 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', [ |
|
180 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', [ |
|
181 | 181 | 'tasknum' => $num |
182 | 182 | ])); |
183 | 183 | } |
@@ -216,9 +216,9 @@ discard block |
||
216 | 216 | */ |
217 | 217 | public function testNotBefore() |
218 | 218 | { |
219 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', null, '+ 1 Min')); |
|
220 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', null, '+ 1 Day')); |
|
221 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', null, '2009-07-01 12:00:00')); |
|
219 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', null, '+ 1 Min')); |
|
220 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', null, '+ 1 Day')); |
|
221 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', null, '2009-07-01 12:00:00')); |
|
222 | 222 | $data = $this->QueuedTasks->find('all')->toArray(); |
223 | 223 | $this->assertWithinRange((new Time('+ 1 Min'))->toUnixString(), $data[0]['not_before']->toUnixString(), 60); |
224 | 224 | $this->assertWithinRange((new Time('+ 1 Day'))->toUnixString(), $data[1]['not_before']->toUnixString(), 60); |
@@ -247,16 +247,16 @@ discard block |
||
247 | 247 | 'retries' => 2 |
248 | 248 | ] |
249 | 249 | ]; |
250 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('dummytask')); |
|
251 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('dummytask')); |
|
250 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('dummytask')); |
|
251 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('dummytask')); |
|
252 | 252 | // create a task with it's execution target some seconds in the past, so it should jump to the top of the testCreateAndFetchlist. |
253 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', [ |
|
253 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', [ |
|
254 | 254 | 'three' |
255 | 255 | ], '- 3 Seconds')); |
256 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', [ |
|
256 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', [ |
|
257 | 257 | 'two' |
258 | 258 | ], '- 5 Seconds')); |
259 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', [ |
|
259 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', [ |
|
260 | 260 | 'one' |
261 | 261 | ], '- 7 Seconds')); |
262 | 262 | |
@@ -329,19 +329,19 @@ discard block |
||
329 | 329 | $data1 = [ |
330 | 330 | 'key' => 1 |
331 | 331 | ]; |
332 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', $data1)); |
|
332 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', $data1)); |
|
333 | 333 | $data2 = [ |
334 | 334 | 'key' => 2 |
335 | 335 | ]; |
336 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', $data2)); |
|
336 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', $data2)); |
|
337 | 337 | $data3 = [ |
338 | 338 | 'key' => 3 |
339 | 339 | ]; |
340 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', $data3)); |
|
341 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('dummytask')); |
|
342 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('dummytask')); |
|
343 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('dummytask')); |
|
344 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('dummytask')); |
|
340 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', $data3)); |
|
341 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('dummytask')); |
|
342 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('dummytask')); |
|
343 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('dummytask')); |
|
344 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('dummytask')); |
|
345 | 345 | |
346 | 346 | // At first we get task1-1. |
347 | 347 | $this->QueuedTasks->clearKey(); |
@@ -417,7 +417,7 @@ discard block |
||
417 | 417 | $data = [ |
418 | 418 | 'key' => '1' |
419 | 419 | ]; |
420 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', $data)); |
|
420 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', $data)); |
|
421 | 421 | |
422 | 422 | $this->QueuedTasks->clearKey(); |
423 | 423 | $tmp = $this->QueuedTasks->requestJob($capabilities); |
@@ -459,7 +459,7 @@ discard block |
||
459 | 459 | ] |
460 | 460 | ]; |
461 | 461 | |
462 | - $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', [ |
|
462 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', [ |
|
463 | 463 | '1' |
464 | 464 | ])); |
465 | 465 |