Test Setup Failed
Push — master ( cc7f7b...40b87c )
by Matthew
04:03
created

BaseJobManagerTest   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 22
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 202
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpBeforeClass() 0 5 1
A testSaveJob() 0 12 1
A getJob() 0 8 1
A testGetJobByWorker() 0 10 1
A testDeleteJob() 0 13 1
B testJobPriority() 0 36 1
A testResetErroneousJobs() 0 4 1
A testResetStalledJobs() 0 4 1
A testPruneStalledJobs() 0 4 1
A testPruneErroneousJobs() 0 4 1
A testPruneExpiredJobs() 0 4 1
A testPruneArchivedJobs() 0 10 2
A expectingException() 0 9 2
C testPerformance() 0 50 7
1
<?php
2
3
namespace Dtc\QueueBundle\Tests\Model;
4
5
use PHPUnit\Framework\TestCase;
6
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()
14
    {
15
        self::$jobClass = self::$worker->getJobClass();
16
        self::$worker->setJobManager(self::$jobManager);
17
    }
18
19
    public function testSaveJob()
20
    {
21
        $job = $this->getJob();
22
        $jobInQueue = self::$jobManager->getJob();
23
        $this->assertNotNull($jobInQueue, 'There should be a job.');
24
        $this->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
        $this->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
        $this->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 = new self::$jobClass(self::$worker, false, null);
55
        $job->fibonacci(1);
56
        $this->assertNotNull($job->getId(), 'Job id should be generated');
57
58
        self::$jobManager->deleteJob($job);
59
60
        $nextJob = self::$jobManager->getJob();
61
        $this->assertNull($nextJob, "Shouldn't be any jobs left in queue");
62
63
        return $job;
64
    }
65
66
    /**
67
     * Not all managers will support job priority.
68
     */
69
    public function testJobPriority()
70
    {
71
        $firstJob = new self::$jobClass(self::$worker, false, 12);
72
        $firstJob->fibonacci(1);
73
        $this->assertNotNull($firstJob->getId(), 'Job id should be generated');
74
75
        $secondJob = new self::$jobClass(self::$worker, false, 1);
76
        $secondJob->fibonacci(1);
77
        $this->assertNotNull($secondJob->getId(), 'Job id should be generated');
78
79
        $thirdJob = new self::$jobClass(self::$worker, false, 5);
80
        $thirdJob->fibonacci(1);
81
        $this->assertNotNull($thirdJob->getId(), 'Job id should be generated');
82
83
        $jobInQueue = self::$jobManager->getJob();
84
        $this->assertNotNull($jobInQueue, 'There should be a job.');
85
        $this->assertEquals(
86
            $secondJob->getId(),
87
            $jobInQueue->getId(),
88
            'Second job id should be returned - lower number unload first'
89
        );
90
91
        $jobInQueue = self::$jobManager->getJob();
92
        $this->assertEquals(
93
            $thirdJob->getId(),
94
            $jobInQueue->getId(),
95
            'Third job id should be returned - lower number unload first'
96
        );
97
98
        $jobInQueue = self::$jobManager->getJob();
99
        $this->assertEquals(
100
            $firstJob->getId(),
101
            $jobInQueue->getId(),
102
            'First job id should be returned - lower number unload first'
103
        );
104
    }
105
106
    public function testResetErroneousJobs()
107
    {
108
        $this->expectingException('resetErroneousJobs');
109
    }
110
111
    public function testResetStalledJobs()
112
    {
113
        $this->expectingException('resetStalledJobs');
114
    }
115
116
    public function testPruneStalledJobs()
117
    {
118
        $this->expectingException('pruneStalledJobs');
119
    }
120
121
    public function testPruneErroneousJobs()
122
    {
123
        $this->expectingException('pruneErroneousJobs');
124
    }
125
126
    public function testPruneExpiredJobs()
127
    {
128
        $this->expectingException('pruneExpiredJobs');
129
    }
130
131
    public function testPruneArchivedJobs()
132
    {
133
        try {
134
            $time = time() - 86400;
135
            self::$jobManager->pruneArchivedJobs(new \DateTime("@$time"));
136
            self::fail('Expected Exception');
137
        } catch (\Exception $exception) {
138
            self::assertTrue(true);
139
        }
140
    }
141
142
    /**
143
     * @param string $method
144
     */
145
    protected function expectingException($method)
146
    {
147
        try {
148
            self::$jobManager->$method();
149
            self::fail('Expected Exception');
150
        } catch (\Exception $exception) {
151
            self::assertTrue(true);
152
        }
153
    }
154
155
    /**
156
     * @outputBuffering disabled
157
     */
158
    public function testPerformance()
159
    {
160
        echo "\n".static::class.": Testing Performance\n";
161
        flush();
162
163
        $start = microtime(true);
164
        $jobsTotal = 100; // have to trim this down as Travis is slow.
165
        self::$jobManager->enableSorting = false; // Ignore priority
166
167
        for ($i = 0; $i < $jobsTotal; ++$i) {
168
            $startLater = microtime(true);
169
            self::$worker->later()->fibonacci(1);
170
            echo "set $i: ".(microtime(true) - $startLater)."\n";
171
            try {
172
                $count = self::$jobManager->getJobCount();
173
                self::assertEquals($i + 1, $count);
174
                echo "\n$count Jobs found\n";
175
            } catch (\Exception $e) {
176
                if ('Unsupported' !== $e->getMessage()) {
177
                    throw $e;
178
                }
179
            }
180
        }
181
182
        $total = microtime(true) - $start;
183
        echo "\nTotal of {$jobsTotal} jobs enqueued in {$total} seconds\n";
184
185
        try {
186
            $count = self::$jobManager->getJobCount();
187
            echo "\n$count Jobs found\n";
188
        } catch (\Exception $e) {
189
            if ('Unsupported' !== $e->getMessage()) {
190
                throw $e;
191
            }
192
        }
193
        $start = microtime(true);
194
        $job = null;
195
        for ($i = 0; $i < $jobsTotal; ++$i) {
196
            $startTime = microtime(true);
197
            $job = self::$jobManager->getJob();
198
            echo "Job {$job->getId()}\n";
199
            echo "get $i: ".(microtime(true) - $startTime)."\n";
200
        }
201
        $total = microtime(true) - $start;
202
        echo "Total of {$jobsTotal} jobs dequeued in {$total} seconds\n";
203
        $this->assertNotNull($job, 'The last job in queue...');
204
205
        $nextJob = self::$jobManager->getJob();
206
        $this->assertNull($nextJob, "Shouldn't be any jobs left in queue");
207
    }
208
}
209