Skip to content

Commit 274abeb

Browse files
committed
Merge pull request #74 from AlexMax/fix-unit-tests
Fix unit tests
2 parents 2171cc5 + 3d9e69a commit 274abeb

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

src/Model/Table/QueuedTasksTable.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Cake\Core\Configure;
55
use Cake\ORM\Table;
66
use Cake\Utility\Hash;
7+
use Cake\I18n\Time;
78

89
/**
910
* QueuedTask for queued tasks.
@@ -73,7 +74,7 @@ public function createJob($jobName, $data = null, $notBefore = null, $group = nu
7374
'reference' => $reference,
7475
];
7576
if ($notBefore !== null) {
76-
$data['notbefore'] = strtotime($notBefore);
77+
$data['notbefore'] = new Time($notBefore);
7778
}
7879
$queuedTask = $this->newEntity($data);
7980
if ($queuedTask->errors()) {
@@ -203,13 +204,13 @@ public function requestJob(array $capabilities, $group = null) {
203204
'AND' => [
204205
[
205206
'OR' => [
206-
'notbefore <' => time(),
207+
'notbefore <' => new Time(),
207208
'notbefore IS' => null,
208209
],
209210
],
210211
[
211212
'OR' => [
212-
'fetched <' => time() - $task['timeout'],
213+
'fetched <' => (new Time())->modify(sprintf('-%d seconds', $task['timeout'])),
213214
'fetched IS' => null,
214215
],
215216
],
@@ -265,7 +266,7 @@ public function requestJob(array $capabilities, $group = null) {
265266
$this->save($data, ['fieldList' => ['id', 'failed', 'failure_message']]);
266267
}
267268
//save last fetch by type for Rate Limiting.
268-
$this->rateHistory[$data['jobtype']] = time();
269+
$this->rateHistory[$data['jobtype']] = (new Time())->toUnixString() ;
269270
return $data;
270271
}
271272

tests/TestCase/Model/Table/QueuedTasksTableTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Cake\Core\Configure;
1010
use Cake\ORM\TableRegistry;
1111
use Cake\TestSuite\TestCase;
12+
use Cake\I18n\Time;
1213

1314
/**
1415
* Queue\Model\Table\QueuedTasksTable Test Case
@@ -215,9 +216,9 @@ public function testNotBefore() {
215216
$this->assertTrue((bool)$this->QueuedTasks->createJob('task1', [], '+ 1 Day'));
216217
$this->assertTrue((bool)$this->QueuedTasks->createJob('task1', [], '2009-07-01 12:00:00'));
217218
$data = $this->QueuedTasks->find('all')->toArray();
218-
$this->assertWithinRange(strtotime('+ 1 Min'), $data[0]['notbefore']->toUnixString(), 60);
219-
$this->assertWithinRange(strtotime('+ 1 Day'), $data[1]['notbefore']->toUnixString(), 60);
220-
$this->assertWithinRange(strtotime('2009-07-01 12:00:00'), $data[2]['notbefore']->toUnixString(), 60);
219+
$this->assertWithinRange((new Time('+ 1 Min'))->toUnixString(), $data[0]['notbefore']->toUnixString(), 60);
220+
$this->assertWithinRange((new Time('+ 1 Day'))->toUnixString(), $data[1]['notbefore']->toUnixString(), 60);
221+
$this->assertWithinRange((new Time('2009-07-01 12:00:00'))->toUnixString(), $data[2]['notbefore']->toUnixString(), 60);
221222
}
222223

223224
/**
@@ -291,7 +292,7 @@ public function testRateLimit() {
291292
'name' => 'task1',
292293
'timeout' => 101,
293294
'retries' => 2,
294-
'rate' => 1,
295+
'rate' => 2,
295296
],
296297
'dummytask' => [
297298
'name' => 'dummytask',
@@ -323,15 +324,15 @@ public function testRateLimit() {
323324
$this->assertEquals('dummytask', $tmp['jobtype']);
324325
$this->assertNull(unserialize($tmp['data']));
325326

326-
usleep(500000);
327+
usleep(100000);
327328
//and again.
328329
$this->QueuedTasks->clearKey();
329330
$tmp = $this->QueuedTasks->requestJob($capabilities);
330331
$this->assertEquals('dummytask', $tmp['jobtype']);
331332
$this->assertNull(unserialize($tmp['data']));
332333

333334
//Then some time passes
334-
sleep(1);
335+
sleep(2);
335336

336337
//Now we should get task1-2
337338
$this->QueuedTasks->clearKey();
@@ -346,7 +347,7 @@ public function testRateLimit() {
346347
$this->assertNull(unserialize($tmp['data']));
347348

348349
//Then some more time passes
349-
sleep(1);
350+
sleep(2);
350351

351352
//Now we should get task1-3
352353
$this->QueuedTasks->clearKey();

0 commit comments

Comments
 (0)