Completed
Push — master ( 8ffdf8...f67228 )
by Matthew
08:43
created

JobManagerTest::testSaveJob()   B

Complexity

Conditions 6
Paths 48

Size

Total Lines 72
Code Lines 55

Duplication

Lines 72
Ratio 100 %

Importance

Changes 0
Metric Value
dl 72
loc 72
rs 8.5164
c 0
b 0
f 0
cc 6
eloc 55
nc 48
nop 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A JobManagerTest::drainQueue() 0 13 3
B JobManagerTest::testGetWaitingJobCount() 0 45 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Dtc\QueueBundle\Tests\RabbitMQ;
4
5
use Dtc\QueueBundle\Exception\UnsupportedException;
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\RabbitMQ\JobManager;
12
use Dtc\QueueBundle\Tests\FibonacciWorker;
13
use Dtc\QueueBundle\Tests\Manager\AutoRetryTrait;
14
use Dtc\QueueBundle\Tests\Manager\BaseJobManagerTest;
15
use Dtc\QueueBundle\Tests\Manager\PriorityTestTrait;
16
use Dtc\QueueBundle\Tests\Manager\RetryableTrait;
17
use Dtc\QueueBundle\Tests\Manager\SaveJobTrait;
18
use PhpAmqpLib\Connection\AMQPStreamConnection;
19
20
/**
21
 * @author David
22
 *
23
 * This test requires local beanstalkd running
24
 */
25
class JobManagerTest extends BaseJobManagerTest
26
{
27
    use PriorityTestTrait;
28
    use AutoRetryTrait;
29
    use RetryableTrait;
30
    use SaveJobTrait;
31
    public static $connection;
32
33
    public static function setUpBeforeClass()
34
    {
35
        $host = getenv('RABBIT_MQ_HOST');
36
        $port = 5672;
37
        $user = 'guest';
38
        $pass = 'guest';
39
        $vhost = '/';
40
        $jobTimingClass = JobTiming::class;
41
        $runClass = Run::class;
42
        self::$connection = new AMQPStreamConnection($host, $port, $user, $pass, $vhost);
43
44
        self::$jobTimingManager = new JobTimingManager($jobTimingClass, false);
45
        self::$runManager = new RunManager($runClass);
46
        self::$jobManager = new JobManager(self::$runManager, self::$jobTimingManager, \Dtc\QueueBundle\RabbitMQ\Job::class);
47
        self::$jobManager->setAMQPConnection(self::$connection);
48
        self::$jobManager->setMaxPriority(255);
49
        self::$jobManager->setQueueArgs('dtc_queue', false, true, false, false);
50
        self::$jobManager->setExchangeArgs('dtc_queue_exchange', 'direct', false, true, false);
51
        $channel = self::$connection->channel();
52
        $channel->queue_delete('dtc_queue');
53
        $channel->close();
54
        self::$worker = new FibonacciWorker();
55
        self::$jobManager->setupChannel();
56
        $channel = self::$jobManager->getChannel();
57
        $drained = self::drainQueue($channel);
58
59
        if ($drained) {
60
            echo "\nRabbitMQ - drained $drained prior to start of test";
61
        }
62
        parent::setUpBeforeClass();
63
    }
64
65 View Code Duplication
    public function testConstructor()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
66
    {
67
        $test = null;
68
        try {
69
            $test = new JobManager(self::$runManager, self::$jobTimingManager, Job::class);
70
        } catch (\Exception $exception) {
71
            self::fail("shouldn't get here");
72
        }
73
        self::assertNotNull($test);
74
    }
75
76
    public function testSetupChannel()
77
    {
78
        $jobManager = new JobManager(self::$runManager, self::$jobTimingManager, Job::class);
79
        $failed = false;
80
        try {
81
            $jobManager->setupChannel();
82
            $failed = true;
83
        } catch (\Exception $exception) {
84
            self::assertTrue(true);
85
        }
86
        self::assertFalse($failed);
87
88
        try {
89
            $jobManager->setQueueArgs('dtc_queue', false, true, false, false);
90
            $failed = true;
91
        } catch (\Exception $exception) {
92
            self::assertTrue(true);
93
        }
94
        self::assertFalse($failed);
95
96
        $jobManager->setMaxPriority('asdf');
97
        try {
98
            $jobManager->setQueueArgs('dtc_queue', false, true, false, false);
99
            $failed = true;
100
        } catch (\Exception $exception) {
101
            self::assertTrue(true);
102
        }
103
        self::assertFalse($failed);
104
105
        $jobManager->setMaxPriority(255);
106
        $jobManager->setQueueArgs('dtc_queue', false, true, false, false);
107
        try {
108
            $jobManager->setupChannel();
109
            $failed = true;
110
        } catch (\Exception $exception) {
111
            self::assertTrue(true);
112
        }
113
        self::assertFalse($failed);
114
        $jobManager->setExchangeArgs('dtc_queue_exchange', 'direct', false, true, false);
115
        $jobManager->setAMQPConnection(self::$connection);
116
        $jobManager->setupChannel();
117
        self::assertTrue(true);
118
    }
119
120
    /**
121
     * RabbitMQ has no ability to delete specific messages off the queue.
122
     */
123
    public function testDeleteJob()
124
    {
125
        $job = new self::$jobClass(self::$worker, false, null);
126
        $job->fibonacci(1);
127
        self::assertNotNull($job->getId(), 'Job id should be generated');
128
129
        try {
130
            self::$jobManager->deleteJob($job);
131
            $this->fail('expected exception');
132
        } catch (\Exception $e) {
133
            self::assertTrue(true);
134
        }
135
        self::$jobManager->getJob();
136
    }
137
138 View Code Duplication
    public function testGetJobByWorker()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
139
    {
140
        $failed = false;
141
        try {
142
            self::$jobManager->getJob(self::$worker->getName());
143
            $failed = true;
144
        } catch (\Exception $exception) {
145
            self::assertTrue(true);
146
        }
147
        self::assertFalse($failed);
148
    }
149
150 View Code Duplication
    public function testExpiredJob()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
151
    {
152
        $job = new self::$jobClass(self::$worker, false, null);
153
        $time = time() - 1;
154
        $job->setExpiresAt(new \DateTime("@$time"))->fibonacci(1);
155
        self::assertNotNull($job->getId(), 'Job id should be generated');
156
157
        $jobInQueue = self::$jobManager->getJob();
158
        self::assertNull($jobInQueue, 'The job should have been dropped...');
159
160
        $job = new self::$jobClass(self::$worker, false, null);
161
        $time = time() - 1;
162
        $job->setExpiresAt(new \DateTime("@$time"))->fibonacci(1);
163
164
        $job = new self::$jobClass(self::$worker, false, null);
165
        $time = time() - 1;
166
        $job->setExpiresAt(new \DateTime("@$time"))->fibonacci(5);
167
168
        $job = new self::$jobClass(self::$worker, false, null);
169
        $time = time() - 1;
170
        $job->setExpiresAt(new \DateTime("@$time"))->fibonacci(2);
171
172
        $job = new self::$jobClass(self::$worker, false, null);
173
        $job->fibonacci(1);
174
        $jobInQueue = self::$jobManager->getJob();
175
        self::assertNotNull($jobInQueue, 'There should be a job.');
176
        self::assertEquals(
177
            $job->getId(),
178
            $jobInQueue->getId(),
179
            'Job id returned by manager should be the same'
180
        );
181
    }
182
183
    protected static function drainQueue($channel)
184
    {
185
        $drained = 0;
186
        do {
187
            $message = $channel->basic_get('dtc_queue');
188
            if ($message) {
189
                $channel->basic_ack($message->delivery_info['delivery_tag']);
190
                ++$drained;
191
            }
192
        } while ($message);
193
194
        return $drained;
195
    }
196
197
    public function testGetWaitingJobCount()
198
    {
199
        /** @var JobManager $jobManager */
200
        $jobManager = self::$jobManager;
201
        self::drainQueue($jobManager->getChannel());
202
203
        $job = new self::$jobClass(self::$worker, false, null);
204
        $job->fibonacci(1);
205
        self::assertNotNull($job->getId(), 'Job id should be generated');
206
207
        self::assertEquals(1, self::getWaitingJobCount(2));
208
209
        $failure = false;
210
        try {
211
            $jobManager->getWaitingJobCount('fibonacci');
212
            $failure = true;
213
        } catch (UnsupportedException $exception) {
214
            self::assertTrue(true);
215
        }
216
        self::assertFalse($failure);
217
218
        $failure = false;
219
        try {
220
            $jobManager->getWaitingJobCount(null, 'fibonacci');
221
            $failure = true;
222
        } catch (UnsupportedException $exception) {
223
            self::assertTrue(true);
224
        }
225
        self::assertFalse($failure);
226
227
        $failure = false;
228
        try {
229
            $jobManager->getWaitingJobCount('fibonacci', 'fibonacci');
230
            $failure = true;
231
        } catch (UnsupportedException $exception) {
232
            self::assertTrue(true);
233
        }
234
        self::assertFalse($failure);
235
236
        $job = new self::$jobClass(self::$worker, false, null);
237
        $job->fibonacci(1);
238
        self::assertNotNull($job->getId(), 'Job id should be generated');
239
240
        self::assertEquals(2, self::getWaitingJobCount(2));
241
    }
242
}
243