forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueWorkerTest.php
More file actions
executable file
·120 lines (92 loc) · 4.69 KB
/
QueueWorkerTest.php
File metadata and controls
executable file
·120 lines (92 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
use Mockery as m;
use Illuminate\Queue\Worker;
class QueueWorkerTest extends PHPUnit_Framework_TestCase {
public function tearDown()
{
m::close();
}
public function testJobIsPoppedOffQueueAndProcessed()
{
$worker = $this->getMock('Illuminate\Queue\Worker', array('process'), array($manager = m::mock('Illuminate\Queue\QueueManager')));
$manager->shouldReceive('connection')->once()->with('connection')->andReturn($connection = m::mock('StdClass'));
$manager->shouldReceive('getName')->andReturn('connection');
$job = m::mock('Illuminate\Queue\Jobs\Job');
$connection->shouldReceive('pop')->once()->with('queue')->andReturn($job);
$worker->expects($this->once())->method('process')->with($this->equalTo('connection'), $this->equalTo($job), $this->equalTo(0), $this->equalTo(0));
$worker->pop('connection', 'queue');
}
public function testJobIsPoppedOffFirstQueueInListAndProcessed()
{
$worker = $this->getMock('Illuminate\Queue\Worker', array('process'), array($manager = m::mock('Illuminate\Queue\QueueManager')));
$manager->shouldReceive('connection')->once()->with('connection')->andReturn($connection = m::mock('StdClass'));
$manager->shouldReceive('getName')->andReturn('connection');
$job = m::mock('Illuminate\Queue\Jobs\Job');
$connection->shouldReceive('pop')->once()->with('queue1')->andReturn(null);
$connection->shouldReceive('pop')->once()->with('queue2')->andReturn($job);
$worker->expects($this->once())->method('process')->with($this->equalTo('connection'), $this->equalTo($job), $this->equalTo(0), $this->equalTo(0));
$worker->pop('connection', 'queue1,queue2');
}
public function testWorkerSleepsIfNoJobIsPresentAndSleepIsEnabled()
{
$worker = $this->getMock('Illuminate\Queue\Worker', array('process', 'sleep'), array($manager = m::mock('Illuminate\Queue\QueueManager')));
$manager->shouldReceive('connection')->once()->with('connection')->andReturn($connection = m::mock('StdClass'));
$connection->shouldReceive('pop')->once()->with('queue')->andReturn(null);
$worker->expects($this->never())->method('process');
$worker->expects($this->once())->method('sleep')->with($this->equalTo(1));
$worker->pop('connection', 'queue', 0, 128, true);
}
public function testWorkerLogsJobToFailedQueueIfMaxTriesHasBeenExceeded()
{
$worker = new Illuminate\Queue\Worker(m::mock('Illuminate\Queue\QueueManager'), $failer = m::mock('Illuminate\Queue\Failed\FailedJobProviderInterface'));
$job = m::mock('Illuminate\Queue\Jobs\Job');
$job->shouldReceive('attempts')->once()->andReturn(10);
$job->shouldReceive('getQueue')->once()->andReturn('queue');
$job->shouldReceive('getRawBody')->once()->andReturn('body');
$job->shouldReceive('delete')->once();
$failer->shouldReceive('log')->once()->with('connection', 'queue', 'body');
$worker->process('connection', $job, 3, 0);
}
public function testProcessFiresJobAndAutoDeletesIfTrue()
{
$worker = new Illuminate\Queue\Worker(m::mock('Illuminate\Queue\QueueManager'));
$job = m::mock('Illuminate\Queue\Jobs\Job');
$job->shouldReceive('fire')->once();
$job->shouldReceive('autoDelete')->once()->andReturn(true);
$job->shouldReceive('delete')->once();
$worker->process('connection', $job, 0, 0);
}
public function testProcessFiresJobAndDoesntCallDeleteIfJobDoesntAutoDelete()
{
$worker = new Illuminate\Queue\Worker(m::mock('Illuminate\Queue\QueueManager'));
$job = m::mock('Illuminate\Queue\Jobs\Job');
$job->shouldReceive('fire')->once();
$job->shouldReceive('autoDelete')->once()->andReturn(false);
$job->shouldReceive('delete')->never();
$worker->process('connection', $job, 0, 0);
}
/**
* @expectedException RuntimeException
*/
public function testJobIsReleasedWhenExceptionIsThrown()
{
$worker = new Illuminate\Queue\Worker(m::mock('Illuminate\Queue\QueueManager'));
$job = m::mock('Illuminate\Queue\Jobs\Job');
$job->shouldReceive('fire')->once()->andReturnUsing(function() { throw new RuntimeException; });
$job->shouldReceive('isDeleted')->once()->andReturn(false);
$job->shouldReceive('release')->once()->with(5);
$worker->process('connection', $job, 0, 5);
}
/**
* @expectedException RuntimeException
*/
public function testJobIsNotReleasedWhenExceptionIsThrownButJobIsDeleted()
{
$worker = new Illuminate\Queue\Worker(m::mock('Illuminate\Queue\QueueManager'));
$job = m::mock('Illuminate\Queue\Jobs\Job');
$job->shouldReceive('fire')->once()->andReturnUsing(function() { throw new RuntimeException; });
$job->shouldReceive('isDeleted')->once()->andReturn(true);
$job->shouldReceive('release')->never();
$worker->process('connection', $job, 0, 5);
}
}