1
|
|
|
<?php |
2
|
|
|
namespace Tavii\SQSJobQueue\Worker; |
3
|
|
|
|
4
|
|
|
use Phake; |
5
|
|
|
use Tavii\SQSJobQueue\Storage\EntityInterface; |
6
|
|
|
use Tavii\SQSJobQueue\Storage\StorageInterface; |
7
|
|
|
|
8
|
|
|
class WorkerTest extends \PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @test |
13
|
|
|
*/ |
14
|
|
|
public function ワーカーを実行する() |
15
|
|
|
{ |
16
|
|
|
$name = 'test'; |
17
|
|
|
|
18
|
|
|
$queue = Phake::mock('Tavii\SQSJobQueue\Queue\Queue'); |
19
|
|
|
$storage = Phake::mock('Tavii\SQSJobQueue\Worker\TestStorage'); |
20
|
|
|
$message = Phake::mock('Tavii\SQSJobQueue\Message\Message'); |
21
|
|
|
$job = Phake::mock('Tavii\SQSJobQueue\Job\Job'); |
22
|
|
|
|
23
|
|
|
Phake::when($queue)->receive($name) |
24
|
|
|
->thenReturn($message); |
25
|
|
|
|
26
|
|
|
Phake::when($message)->getJob() |
27
|
|
|
->thenReturn($job); |
28
|
|
|
|
29
|
|
|
Phake::when($job)->execute() |
30
|
|
|
->thenReturn(true); |
31
|
|
|
|
32
|
|
|
$worker = new Worker($queue, $storage); |
33
|
|
|
$actual = $worker->run($name); |
34
|
|
|
|
35
|
|
|
Phake::verify($queue)->receive ($name); |
36
|
|
|
Phake::verify($message)->getJob(); |
37
|
|
|
Phake::verify($job)->execute(); |
38
|
|
|
Phake::verify($queue)->delete($message); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @test |
43
|
|
|
*/ |
44
|
|
|
public function ワーカーを実行させる() |
45
|
|
|
{ |
46
|
|
|
$name = "test"; |
47
|
|
|
$queue = Phake::mock('Tavii\SQSJobQueue\Queue\Queue'); |
48
|
|
|
$storage = Phake::mock('Tavii\SQSJobQueue\Worker\TestStorage'); |
49
|
|
|
|
50
|
|
|
$worker = new Worker($queue, $storage); |
51
|
|
|
$worker->start($name); |
52
|
|
|
|
53
|
|
|
Phake::verify($storage)->set($name, "test.com", 234); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @test |
58
|
|
|
*/ |
59
|
|
|
public function ワーカーを停止させる() |
60
|
|
|
{ |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
$name = "test"; |
64
|
|
|
$entity = new TestEntity($name, 'test.com', 1234); |
65
|
|
|
|
66
|
|
|
$queue = Phake::mock('Tavii\SQSJobQueue\Queue\Queue'); |
67
|
|
|
$storage = Phake::mock('Tavii\SQSJobQueue\Worker\TestStorage'); |
68
|
|
|
Phake::when($storage)->find($name, 'test.com', null) |
69
|
|
|
->thenReturn(array( |
70
|
|
|
$entity |
71
|
|
|
)); |
72
|
|
|
|
73
|
|
|
$worker = new Worker($queue, $storage); |
74
|
|
|
$worker->stop($name); |
75
|
|
|
|
76
|
|
|
Phake::verify($storage)->find($name, "test.com", null); |
77
|
|
|
Phake::verify($storage)->remove($name, "test.com", 1234); |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @test |
83
|
|
|
*/ |
84
|
|
|
public function ワーカーを強制的に停止させる() |
85
|
|
|
{ |
86
|
|
|
$name = "test"; |
87
|
|
|
$entity = new TestEntity($name, 'test.com', 1234); |
88
|
|
|
|
89
|
|
|
$queue = Phake::mock('Tavii\SQSJobQueue\Queue\Queue'); |
90
|
|
|
$storage = Phake::mock('Tavii\SQSJobQueue\Worker\TestStorage'); |
91
|
|
|
Phake::when($storage)->find($name, 'test.com', null) |
92
|
|
|
->thenReturn(array( |
93
|
|
|
$entity |
94
|
|
|
)); |
95
|
|
|
|
96
|
|
|
$worker = new Worker($queue, $storage); |
97
|
|
|
$worker->stop($name, null, true); |
98
|
|
|
|
99
|
|
|
Phake::verify($storage)->find($name, "test.com", null); |
100
|
|
|
Phake::verify($storage)->remove($name, "test.com", 1234); |
101
|
|
|
Phake::verify($storage)->removeForce($name, "test.com"); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
function pcntl_fork() { |
108
|
|
|
return 234; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
function gethostname() { |
112
|
|
|
return 'test.com'; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
function posix_kill($pid, $num) { |
116
|
|
|
return true; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
class TestStorage implements StorageInterface |
120
|
|
|
{ |
121
|
|
|
/** |
122
|
|
|
* {@inheritdoc} |
123
|
|
|
*/ |
124
|
|
|
public function all() |
125
|
|
|
{ |
126
|
|
|
// TODO: Implement all() method. |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
|
|
public function find($queue, $server = null, $procId = null, $prefix = null) |
133
|
|
|
{ |
134
|
|
|
// TODO: Implement find() method. |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
|
|
public function set($queue, $server, $procId, $prefix) |
141
|
|
|
{ |
142
|
|
|
// TODO: Implement set() method. |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
|
|
public function get($queue, $server, $procId, $prefix) |
149
|
|
|
{ |
150
|
|
|
// TODO: Implement get() method. |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritdoc} |
155
|
|
|
*/ |
156
|
|
|
public function remove($queue, $server, $procId, $prefix) |
157
|
|
|
{ |
158
|
|
|
// TODO: Implement remove() method. |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* {@inheritdoc} |
163
|
|
|
*/ |
164
|
|
|
public function removeForce($queue, $server) {} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
class TestEntity implements EntityInterface |
171
|
|
|
{ |
172
|
|
|
|
173
|
|
|
private $queue; |
174
|
|
|
|
175
|
|
|
private $server; |
176
|
|
|
|
177
|
|
|
private $procId; |
178
|
|
|
|
179
|
|
|
private $prefix; |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* {@inheritdoc} |
183
|
|
|
*/ |
184
|
|
|
public function __construct($queue, $server, $procId) |
185
|
|
|
{ |
186
|
|
|
$this->queue = $queue; |
187
|
|
|
$this->server = $server; |
188
|
|
|
$this->procId = $procId; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* {@inheritdoc} |
193
|
|
|
*/ |
194
|
|
|
public function getQueue() |
195
|
|
|
{ |
196
|
|
|
return $this->queue; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* {@inheritdoc} |
201
|
|
|
*/ |
202
|
|
|
public function getServer() |
203
|
|
|
{ |
204
|
|
|
return $this->server; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* {@inheritdoc} |
209
|
|
|
*/ |
210
|
|
|
public function getProcId() |
211
|
|
|
{ |
212
|
|
|
return $this->procId; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* {@inheritdoc} |
217
|
|
|
*/ |
218
|
|
|
public function getPrefix() |
219
|
|
|
{ |
220
|
|
|
return $this->prefix; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
|
224
|
|
|
} |