1 | <?php |
||
7 | abstract class BaseJobManagerTest extends TestCase |
||
8 | { |
||
9 | public static $worker; |
||
10 | public static $jobClass; |
||
11 | public static $jobManager; |
||
12 | |||
13 | public static function setUpBeforeClass() |
||
18 | |||
19 | public function testSaveJob() |
||
20 | { |
||
21 | $job = $this->getJob(); |
||
22 | $jobInQueue = self::$jobManager->getJob(); |
||
23 | self::assertNotNull($jobInQueue, 'There should be a job.'); |
||
24 | self::assertEquals( |
||
25 | $job->getId(), |
||
26 | $jobInQueue->getId(), |
||
27 | 'Job id returned by manager should be the same' |
||
28 | ); |
||
29 | self::$jobManager->deleteJob($job); |
||
30 | } |
||
31 | |||
32 | protected function getJob() |
||
33 | { |
||
34 | $job = new self::$jobClass(self::$worker, false, null); |
||
35 | $job->fibonacci(1); |
||
36 | self::assertNotNull($job->getId(), 'Job id should be generated'); |
||
37 | |||
38 | return $job; |
||
39 | } |
||
40 | |||
41 | public function testGetJobByWorker() |
||
42 | { |
||
43 | $job = $this->getJob(); |
||
44 | $jobInQueue = self::$jobManager->getJob(self::$worker->getName()); |
||
45 | self::assertEquals( |
||
46 | $job->getId(), |
||
47 | $jobInQueue->getId(), |
||
48 | 'Job id returned by manager should be the same' |
||
49 | ); |
||
50 | } |
||
51 | |||
52 | public function testDeleteJob() |
||
53 | { |
||
54 | $job = $this->getJob(); |
||
55 | self::$jobManager->deleteJob($job); |
||
56 | |||
57 | $nextJob = self::$jobManager->getJob(); |
||
58 | self::assertNull($nextJob, "Shouldn't be any jobs left in queue"); |
||
59 | |||
60 | return $job; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Not all managers will support job priority. |
||
65 | */ |
||
66 | public function testJobPriority() |
||
67 | { |
||
68 | $firstJob = new self::$jobClass(self::$worker, false, 12); |
||
69 | $firstJob->fibonacci(1); |
||
70 | self::assertNotNull($firstJob->getId(), 'Job id should be generated'); |
||
71 | |||
72 | $secondJob = new self::$jobClass(self::$worker, false, 1); |
||
73 | $secondJob->fibonacci(1); |
||
74 | self::assertNotNull($secondJob->getId(), 'Job id should be generated'); |
||
75 | |||
76 | $thirdJob = new self::$jobClass(self::$worker, false, 5); |
||
77 | $thirdJob->fibonacci(1); |
||
78 | self::assertNotNull($thirdJob->getId(), 'Job id should be generated'); |
||
79 | |||
80 | $jobInQueue = self::$jobManager->getJob(); |
||
81 | self::assertNotNull($jobInQueue, 'There should be a job.'); |
||
82 | self::assertEquals( |
||
83 | $secondJob->getId(), |
||
84 | $jobInQueue->getId(), |
||
85 | 'Second job id should be returned - lower number unload first' |
||
86 | ); |
||
87 | |||
88 | $jobInQueue = self::$jobManager->getJob(); |
||
89 | self::assertEquals( |
||
90 | $thirdJob->getId(), |
||
91 | $jobInQueue->getId(), |
||
92 | 'Third job id should be returned - lower number unload first' |
||
93 | ); |
||
94 | |||
95 | $jobInQueue = self::$jobManager->getJob(); |
||
96 | self::assertEquals( |
||
97 | $firstJob->getId(), |
||
98 | $jobInQueue->getId(), |
||
99 | 'First job id should be returned - lower number unload first' |
||
100 | ); |
||
101 | } |
||
102 | |||
103 | public function testResetErroneousJobs() |
||
107 | |||
108 | public function testResetStalledJobs() |
||
112 | |||
113 | public function testPruneStalledJobs() |
||
117 | |||
118 | public function testPruneErroneousJobs() |
||
122 | |||
123 | public function testPruneExpiredJobs() |
||
127 | |||
128 | public function testGetStatus() |
||
129 | { |
||
130 | $this->expectingException('getStatus'); |
||
131 | } |
||
132 | |||
133 | public function testPruneArchivedJobs() |
||
134 | { |
||
135 | $failed = false; |
||
136 | try { |
||
137 | $time = time() - 86400; |
||
138 | self::$jobManager->pruneArchivedJobs(new \DateTime("@$time")); |
||
139 | $failed = true; |
||
140 | } catch (\Exception $exception) { |
||
141 | self::assertTrue(true); |
||
142 | } |
||
143 | self::assertFalse($failed); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @param string $method |
||
148 | */ |
||
149 | protected function expectingException($method) |
||
150 | { |
||
151 | $failed = false; |
||
152 | try { |
||
153 | self::$jobManager->$method(); |
||
154 | $failed = true; |
||
155 | } catch (\Exception $exception) { |
||
156 | self::assertTrue(true); |
||
157 | } |
||
158 | self::assertFalse($failed); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @outputBuffering disabled |
||
163 | */ |
||
164 | public function testPerformance() |
||
201 | } |
||
202 |