1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\Tests\Redis; |
4
|
|
|
|
5
|
|
|
use Dtc\QueueBundle\Model\BaseJob; |
6
|
|
|
use Dtc\QueueBundle\Model\Job; |
7
|
|
|
use Dtc\QueueBundle\Model\JobTiming; |
8
|
|
|
use Dtc\QueueBundle\Manager\JobTimingManager; |
9
|
|
|
use Dtc\QueueBundle\Model\Run; |
10
|
|
|
use Dtc\QueueBundle\Manager\RunManager; |
11
|
|
|
use Dtc\QueueBundle\Redis\JobManager; |
12
|
|
|
use Dtc\QueueBundle\Redis\Predis; |
13
|
|
|
use Dtc\QueueBundle\Tests\FibonacciWorker; |
14
|
|
|
use Dtc\QueueBundle\Tests\Manager\AutoRetryTrait; |
15
|
|
|
use Dtc\QueueBundle\Tests\Manager\BaseJobManagerTest; |
16
|
|
|
use Dtc\QueueBundle\Tests\Manager\PriorityTestTrait; |
17
|
|
|
use Dtc\QueueBundle\Tests\Manager\RetryableTrait; |
18
|
|
|
use Dtc\QueueBundle\Util\Util; |
19
|
|
|
use Predis\Client; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author David |
23
|
|
|
* |
24
|
|
|
* This test requires local beanstalkd running |
25
|
|
|
*/ |
26
|
|
|
class JobManagerTest extends BaseJobManagerTest |
27
|
|
|
{ |
28
|
|
|
use PriorityTestTrait; |
29
|
|
|
use AutoRetryTrait; |
30
|
|
|
use RetryableTrait; |
31
|
|
|
public static $connection; |
32
|
|
|
|
33
|
|
|
public static function setUpBeforeClass() |
34
|
|
|
{ |
35
|
|
|
$host = getenv('REDIS_HOST'); |
36
|
|
|
$port = getenv('REDIS_PORT') ?: 6379; |
37
|
|
|
$jobTimingClass = JobTiming::class; |
38
|
|
|
$runClass = Run::class; |
39
|
|
|
$predisClient = new Client(['scheme' => 'tcp', 'host' => $host, 'port' => $port]); |
40
|
|
|
$predisClient->flushall(); |
41
|
|
|
$predis = new Predis($predisClient); |
42
|
|
|
|
43
|
|
|
self::$jobTimingManager = new JobTimingManager($jobTimingClass, false); |
44
|
|
|
self::$runManager = new RunManager($runClass); |
45
|
|
|
self::$jobManager = new JobManager(self::$runManager, self::$jobTimingManager, \Dtc\QueueBundle\Redis\Job::class, 'test_cache_key'); |
46
|
|
|
self::$jobManager->setRedis($predis); |
47
|
|
|
self::$jobManager->setMaxPriority(255); |
48
|
|
|
self::$worker = new FibonacciWorker(); |
49
|
|
|
parent::setUpBeforeClass(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
View Code Duplication |
public function testConstructor() |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$test = null; |
55
|
|
|
try { |
56
|
|
|
$test = new JobManager(self::$runManager, self::$jobTimingManager, Job::class, 'something'); |
57
|
|
|
} catch (\Exception $exception) { |
58
|
|
|
self::fail("shouldn't get here"); |
59
|
|
|
} |
60
|
|
|
self::assertNotNull($test); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
public function testGetJobByWorker() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$failed = false; |
66
|
|
|
try { |
67
|
|
|
self::$jobManager->getJob(self::$worker->getName()); |
68
|
|
|
$failed = true; |
69
|
|
|
} catch (\Exception $exception) { |
70
|
|
|
self::assertTrue(true); |
71
|
|
|
} |
72
|
|
|
self::assertFalse($failed); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
public function testExpiredJob() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$job = new self::$jobClass(self::$worker, false, null); |
78
|
|
|
$time = time() - 1; |
79
|
|
|
$job->setExpiresAt(new \DateTime("@$time"))->fibonacci(1); |
80
|
|
|
self::assertNotNull($job->getId(), 'Job id should be generated'); |
81
|
|
|
|
82
|
|
|
$jobInQueue = self::$jobManager->getJob(); |
83
|
|
|
self::assertNull($jobInQueue, 'The job should have been dropped...'); |
84
|
|
|
|
85
|
|
|
$job = new self::$jobClass(self::$worker, false, null); |
86
|
|
|
$time = time() - 1; |
87
|
|
|
$job->setExpiresAt(new \DateTime("@$time"))->fibonacci(1); |
88
|
|
|
|
89
|
|
|
$job = new self::$jobClass(self::$worker, false, null); |
90
|
|
|
$time = time() - 1; |
91
|
|
|
$job->setExpiresAt(new \DateTime("@$time"))->fibonacci(5); |
92
|
|
|
|
93
|
|
|
$job = new self::$jobClass(self::$worker, false, null); |
94
|
|
|
$time = time() - 1; |
95
|
|
|
$job->setExpiresAt(new \DateTime("@$time"))->fibonacci(2); |
96
|
|
|
|
97
|
|
|
$job = new self::$jobClass(self::$worker, false, null); |
98
|
|
|
$job->fibonacci(1); |
99
|
|
|
$jobInQueue = self::$jobManager->getJob(); |
100
|
|
|
self::assertNotNull($jobInQueue, 'There should be a job.'); |
101
|
|
|
self::assertEquals( |
102
|
|
|
$job->getId(), |
103
|
|
|
$jobInQueue->getId(), |
104
|
|
|
'Job id returned by manager should be the same' |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testBatchJobs() |
109
|
|
|
{ |
110
|
|
|
$limit = 10000; |
111
|
|
|
while ($limit && self::$jobManager->getJob()) { |
112
|
|
|
--$limit; |
113
|
|
|
} |
114
|
|
|
self::assertGreaterThan(0, $limit); |
115
|
|
|
|
116
|
|
|
/** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */ |
117
|
|
|
$worker = self::$worker; |
118
|
|
|
$job1 = $worker->later()->fibonacci(1); |
119
|
|
|
$job2 = $worker->batchLater()->fibonacci(1); |
120
|
|
|
|
121
|
|
|
self::assertEquals($job1->getId(), $job2->getId()); |
122
|
|
|
|
123
|
|
|
$job = self::$jobManager->getJob(); |
124
|
|
|
self::assertEquals($job1->getId(), $job->getId()); |
125
|
|
|
self::assertEquals($job1->getPriority(), $job->getPriority()); |
126
|
|
|
|
127
|
|
|
$job = self::$jobManager->getJob(); |
128
|
|
|
self::assertNull($job); |
129
|
|
|
|
130
|
|
|
$job1 = $worker->later()->fibonacci(1); |
131
|
|
|
$job2 = $worker->batchLater()->setPriority(3)->fibonacci(1); |
132
|
|
|
self::assertEquals($job1->getId(), $job2->getId()); |
133
|
|
|
self::assertNotEquals($job1->getPriority(), $job2->getPriority()); |
134
|
|
|
|
135
|
|
|
$job = self::$jobManager->getJob(); |
136
|
|
|
self::assertNotNull($job); |
137
|
|
|
self::assertEquals($job1->getId(), $job->getId()); |
138
|
|
|
self::assertEquals($job->getPriority(), $job2->getPriority()); |
139
|
|
|
|
140
|
|
|
$job = self::$jobManager->getJob(); |
141
|
|
|
self::assertNull($job); |
142
|
|
|
|
143
|
|
|
$job1 = $worker->later(100)->fibonacci(1); |
144
|
|
|
$time1 = new \DateTime(); |
145
|
|
|
$job2 = $worker->batchLater(0)->fibonacci(1); |
146
|
|
|
$time2 = Util::getDateTimeFromDecimalFormat(Util::getMicrotimeDecimal()); |
147
|
|
|
|
148
|
|
|
self::assertEquals($job1->getId(), $job2->getId()); |
149
|
|
|
self::assertGreaterThanOrEqual($time1, $job2->getWhenAt()); |
150
|
|
|
self::assertLessThanOrEqual($time2, $job2->getWhenAt()); |
151
|
|
|
|
152
|
|
|
$job = self::$jobManager->getJob(); |
153
|
|
|
self::assertNotNull($job); |
154
|
|
|
self::assertEquals($job1->getId(), $job->getId()); |
155
|
|
|
self::assertNotNull($job->getPriority()); |
156
|
|
|
self::assertGreaterThanOrEqual($time1, $job->getWhenAt()); |
157
|
|
|
self::assertLessThanOrEqual($time2, $job->getWhenAt()); |
158
|
|
|
|
159
|
|
|
$job1 = $worker->later(100)->setPriority(3)->fibonacci(1); |
160
|
|
|
$priority1 = $job1->getPriority(); |
161
|
|
|
$time1 = Util::getDateTimeFromDecimalFormat(Util::getMicrotimeDecimal()); |
162
|
|
|
$job2 = $worker->batchLater(0)->setPriority(1)->fibonacci(1); |
163
|
|
|
$time2 = Util::getDateTimeFromDecimalFormat(Util::getMicrotimeDecimal()); |
164
|
|
|
self::assertEquals($job1->getId(), $job2->getId()); |
165
|
|
|
self::assertNotEquals($priority1, $job2->getPriority()); |
166
|
|
|
|
167
|
|
|
self::assertGreaterThanOrEqual($time1, $job2->getWhenAt()); |
168
|
|
|
self::assertLessThanOrEqual($time2, $job2->getWhenAt()); |
169
|
|
|
|
170
|
|
|
$limit = 10000; |
171
|
|
|
while ($limit && self::$jobManager->getJob()) { |
172
|
|
|
--$limit; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
View Code Duplication |
public function testSaveJob() |
|
|
|
|
177
|
|
|
{ |
178
|
|
|
// Make sure a job proper type |
179
|
|
|
$failed = false; |
180
|
|
|
try { |
181
|
|
|
$job = new Job(); |
182
|
|
|
self::$jobManager->save($job); |
183
|
|
|
$failed = true; |
184
|
|
|
} catch (\Exception $exception) { |
185
|
|
|
self::assertTrue(true); |
186
|
|
|
} |
187
|
|
|
self::assertFalse($failed); |
188
|
|
|
|
189
|
|
|
$job = new self::$jobClass(self::$worker, false, null); |
190
|
|
|
try { |
191
|
|
|
$job->setPriority(256)->fibonacci(1); |
192
|
|
|
$failed = true; |
193
|
|
|
} catch (\Exception $exception) { |
194
|
|
|
self::assertTrue(true); |
195
|
|
|
} |
196
|
|
|
self::assertFalse($failed); |
197
|
|
|
|
198
|
|
|
$job = new self::$jobClass(self::$worker, false, null); |
199
|
|
|
$job->setPriority(100)->fibonacci(1); |
200
|
|
|
self::assertNotNull($job->getId(), 'Job id should be generated'); |
201
|
|
|
|
202
|
|
|
$jobInQueue = self::$jobManager->getJob(); |
203
|
|
|
self::assertNotNull($jobInQueue, 'There should be a job.'); |
204
|
|
|
self::$jobManager->saveHistory($jobInQueue); |
205
|
|
|
|
206
|
|
|
$job = new self::$jobClass(self::$worker, false, null); |
207
|
|
|
$job->fibonacci(1); |
208
|
|
|
self::assertNotNull($job->getId(), 'Job id should be generated'); |
209
|
|
|
|
210
|
|
|
$failed = false; |
211
|
|
|
try { |
212
|
|
|
self::$jobManager->getJob('fibonacci'); |
213
|
|
|
$failed = true; |
214
|
|
|
} catch (\Exception $exception) { |
215
|
|
|
self::assertTrue(true); |
216
|
|
|
} |
217
|
|
|
self::assertFalse($failed); |
218
|
|
|
|
219
|
|
|
$failed = false; |
220
|
|
|
try { |
221
|
|
|
self::$jobManager->getJob(null, 'fibonacci'); |
222
|
|
|
$failed = true; |
223
|
|
|
} catch (\Exception $exception) { |
224
|
|
|
self::assertTrue(true); |
225
|
|
|
} |
226
|
|
|
self::assertFalse($failed); |
227
|
|
|
|
228
|
|
|
$jobInQueue = self::$jobManager->getJob(); |
229
|
|
|
self::assertNotNull($jobInQueue, 'There should be a job.'); |
230
|
|
|
self::assertEquals( |
231
|
|
|
$job->getId(), |
232
|
|
|
$jobInQueue->getId(), |
233
|
|
|
'Job id returned by manager should be the same' |
234
|
|
|
); |
235
|
|
|
|
236
|
|
|
$job->setStatus(BaseJob::STATUS_SUCCESS); |
237
|
|
|
$badJob = new Job(); |
238
|
|
|
$failed = false; |
239
|
|
|
try { |
240
|
|
|
self::$jobManager->saveHistory($badJob); |
241
|
|
|
$failed = true; |
242
|
|
|
} catch (\Exception $exception) { |
243
|
|
|
self::assertTrue(true); |
244
|
|
|
} |
245
|
|
|
self::assertFalse($failed); |
246
|
|
|
self::$jobManager->saveHistory($jobInQueue); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.