Skip to content

Commit 8a92d91

Browse files
committed
fix queue consumer tests. (not all for now)
1 parent fe1a1df commit 8a92d91

3 files changed

Lines changed: 136 additions & 57 deletions

File tree

‎pkg/enqueue/Consumption/QueueConsumer.php‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
use Enqueue\Consumption\Exception\InvalidArgumentException;
77
use Enqueue\Consumption\Exception\LogicException;
88
use Enqueue\Util\VarExport;
9-
use Interop\Amqp\AmqpConsumer;
109
use Interop\Queue\PsrConsumer;
1110
use Interop\Queue\PsrContext;
1211
use Interop\Queue\PsrMessage;
1312
use Interop\Queue\PsrProcessor;
1413
use Interop\Queue\PsrQueue;
14+
use Interop\Queue\PsrSubscriptionConsumer;
1515
use Interop\Queue\PsrSubscriptionConsumerAwareContext;
1616
use Psr\Log\LoggerInterface;
1717
use Psr\Log\NullLogger;
@@ -57,6 +57,11 @@ final class QueueConsumer
5757
*/
5858
private $logger;
5959

60+
/**
61+
* @var PsrSubscriptionConsumer
62+
*/
63+
private $fallbackSubscriptionConsumer;
64+
6065
/**
6166
* @param PsrContext $psrContext
6267
* @param ExtensionInterface|ChainExtension|null $extension
@@ -76,6 +81,7 @@ public function __construct(
7681

7782
$this->boundProcessors = [];
7883
$this->logger = new NullLogger();
84+
$this->fallbackSubscriptionConsumer = new FallbackSubscriptionConsumer();
7985
}
8086

8187
/**
@@ -186,7 +192,7 @@ public function consume(ExtensionInterface $runtimeExtension = null)
186192

187193
$this->logger->info('Start consuming');
188194

189-
$subscriptionConsumer = new FallbackSubscriptionConsumer();
195+
$subscriptionConsumer = $this->fallbackSubscriptionConsumer;
190196
if ($context instanceof PsrSubscriptionConsumerAwareContext) {
191197
$subscriptionConsumer = $context->createSubscriptionConsumer();
192198
}
@@ -222,7 +228,7 @@ public function consume(ExtensionInterface $runtimeExtension = null)
222228
};
223229

224230
foreach ($consumers as $consumer) {
225-
/* @var AmqpConsumer $consumer */
231+
/* @var PsrConsumer $consumer */
226232

227233
$subscriptionConsumer->subscribe($consumer, $callback);
228234
}
@@ -237,7 +243,7 @@ public function consume(ExtensionInterface $runtimeExtension = null)
237243

238244
$subscriptionConsumer->consume($this->receiveTimeout);
239245

240-
usleep($this->idleTimeout * 1000);
246+
$this->idleTimeout && usleep($this->idleTimeout * 1000);
241247
$this->extension->onIdle($context);
242248
} catch (ConsumptionInterruptedException $e) {
243249
$this->logger->info(sprintf('Consuming interrupted'));
@@ -253,31 +259,25 @@ public function consume(ExtensionInterface $runtimeExtension = null)
253259
$this->extension->onInterrupted($context);
254260

255261
return;
256-
} catch (\Exception $exception) {
262+
} catch (\Throwable $exception) {
257263
$context->setExecutionInterrupted(true);
258264
$context->setException($exception);
259265

260-
try {
261-
$this->onInterruptionByException($this->extension, $context);
262-
} catch (\Exception $e) {
263-
// for some reason finally does not work here on php5.5
266+
$this->onInterruptionByException($this->extension, $context);
264267

265-
throw $e;
266-
}
268+
return;
267269
}
268270
}
269271
}
270272

271273
/**
272-
* @param ExtensionInterface $extension
273-
* @param Context $context
274-
*
275-
* @throws ConsumptionInterruptedException
274+
* @internal
276275
*
277-
* @return bool
276+
* @param PsrSubscriptionConsumer $fallbackSubscriptionConsumer
278277
*/
279-
private function doConsume(ExtensionInterface $extension, Context $context)
278+
public function setFallbackSubscriptionConsumer(PsrSubscriptionConsumer $fallbackSubscriptionConsumer): void
280279
{
280+
$this->fallbackSubscriptionConsumer = $fallbackSubscriptionConsumer;
281281
}
282282

283283
/**
@@ -331,7 +331,7 @@ private function processMessage(PsrConsumer $consumer, PsrProcessor $processor,
331331

332332
$this->extension->onPreReceived($context);
333333
if (!$context->getResult()) {
334-
$result = $processor->process($message, $this->psrContext);
334+
$result = $processor->process($message, $context->getPsrContext());
335335
$context->setResult($result);
336336
}
337337

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Enqueue\Tests\Consumption\Mock;
4+
5+
use Interop\Queue\PsrConsumer;
6+
use Interop\Queue\PsrMessage;
7+
use Interop\Queue\PsrSubscriptionConsumer;
8+
9+
class DummySubscriptionConsumer implements PsrSubscriptionConsumer
10+
{
11+
private $subscriptions = [];
12+
13+
private $messages = [];
14+
15+
/**
16+
* @param float|int $timeout milliseconds 1000 is 1 second, a zero is consume endlessly
17+
*/
18+
public function consume($timeout = 0)
19+
{
20+
foreach ($this->messages as list($message, $queueName)) {
21+
/** @var PsrMessage $message */
22+
/** @var string $queueName */
23+
if (false == call_user_func($this->subscriptions[$queueName][1], $message, $this->subscriptions[$queueName][0])) {
24+
return;
25+
}
26+
}
27+
}
28+
29+
public function subscribe(PsrConsumer $consumer, callable $callback)
30+
{
31+
$this->subscriptions[$consumer->getQueue()->getQueueName()] = [$consumer, $callback];
32+
}
33+
34+
public function unsubscribe(PsrConsumer $consumer)
35+
{
36+
unset($this->subscriptions[$consumer->getQueue()->getQueueName()]);
37+
}
38+
39+
public function unsubscribeAll()
40+
{
41+
$this->subscriptions = [];
42+
}
43+
44+
public function addMessage(PsrMessage $message, string $queueName)
45+
{
46+
$this->messages[] = [$message, $queueName];
47+
}
48+
}

‎pkg/enqueue/Tests/Consumption/QueueConsumerTest.php‎

Lines changed: 70 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
use Enqueue\Consumption\Result;
1212
use Enqueue\Null\NullQueue;
1313
use Enqueue\Tests\Consumption\Mock\BreakCycleExtension;
14+
use Enqueue\Tests\Consumption\Mock\DummySubscriptionConsumer;
1415
use Interop\Queue\PsrConsumer;
1516
use Interop\Queue\PsrContext;
1617
use Interop\Queue\PsrMessage;
1718
use Interop\Queue\PsrProcessor;
1819
use Interop\Queue\PsrQueue;
20+
use Interop\Queue\PsrSubscriptionConsumer;
1921
use PHPUnit\Framework\TestCase;
2022
use Psr\Log\NullLogger;
2123

@@ -171,10 +173,10 @@ public function testShouldSubscribeToGivenQueueWithExpectedTimeout()
171173
{
172174
$expectedQueue = new NullQueue('theQueueName');
173175

174-
$messageConsumerMock = $this->createMock(PsrConsumer::class);
175-
$messageConsumerMock
176+
$subscriptionConsumerMock = $this->createSubscriptionConsumerMock();
177+
$subscriptionConsumerMock
176178
->expects($this->once())
177-
->method('receive')
179+
->method('consume')
178180
->with(12345)
179181
->willReturn(null)
180182
;
@@ -184,7 +186,7 @@ public function testShouldSubscribeToGivenQueueWithExpectedTimeout()
184186
->expects($this->once())
185187
->method('createConsumer')
186188
->with($this->identicalTo($expectedQueue))
187-
->willReturn($messageConsumerMock)
189+
->willReturn($this->createConsumerStub())
188190
;
189191

190192
$processorMock = $this->createProcessorMock();
@@ -194,6 +196,7 @@ public function testShouldSubscribeToGivenQueueWithExpectedTimeout()
194196
;
195197

196198
$queueConsumer = new QueueConsumer($contextMock, new BreakCycleExtension(1), 0, 12345);
199+
$queueConsumer->setFallbackSubscriptionConsumer($subscriptionConsumerMock);
197200
$queueConsumer->bind($expectedQueue, $processorMock);
198201
$queueConsumer->consume();
199202
}
@@ -202,10 +205,10 @@ public function testShouldSubscribeToGivenQueueAndQuitAfterFifthIdleCycle()
202205
{
203206
$expectedQueue = new NullQueue('theQueueName');
204207

205-
$messageConsumerMock = $this->createMock(PsrConsumer::class);
206-
$messageConsumerMock
208+
$subscriptionConsumerMock = $this->createSubscriptionConsumerMock();
209+
$subscriptionConsumerMock
207210
->expects($this->exactly(5))
208-
->method('receive')
211+
->method('consume')
209212
->willReturn(null)
210213
;
211214

@@ -214,7 +217,7 @@ public function testShouldSubscribeToGivenQueueAndQuitAfterFifthIdleCycle()
214217
->expects($this->once())
215218
->method('createConsumer')
216219
->with($this->identicalTo($expectedQueue))
217-
->willReturn($messageConsumerMock)
220+
->willReturn($this->createConsumerStub())
218221
;
219222

220223
$processorMock = $this->createProcessorMock();
@@ -224,16 +227,29 @@ public function testShouldSubscribeToGivenQueueAndQuitAfterFifthIdleCycle()
224227
;
225228

226229
$queueConsumer = new QueueConsumer($contextMock, new BreakCycleExtension(5), 0);
230+
$queueConsumer->setFallbackSubscriptionConsumer($subscriptionConsumerMock);
227231
$queueConsumer->bind($expectedQueue, $processorMock);
228232
$queueConsumer->consume();
229233
}
230234

231235
public function testShouldProcessFiveMessagesAndQuit()
232236
{
233-
$messageMock = $this->createMessageMock();
234-
$messageConsumerStub = $this->createMessageConsumerStub($messageMock);
237+
$fooQueue = new NullQueue('foo_queue');
235238

236-
$contextStub = $this->createPsrContextStub($messageConsumerStub);
239+
$firstMessageMock = $this->createMessageMock();
240+
$secondMessageMock = $this->createMessageMock();
241+
$thirdMessageMock = $this->createMessageMock();
242+
$fourthMessageMock = $this->createMessageMock();
243+
$fifthMessageMock = $this->createMessageMock();
244+
245+
$subscriptionConsumerMock = new DummySubscriptionConsumer();
246+
$subscriptionConsumerMock->addMessage($firstMessageMock, 'foo_queue');
247+
$subscriptionConsumerMock->addMessage($secondMessageMock, 'foo_queue');
248+
$subscriptionConsumerMock->addMessage($thirdMessageMock, 'foo_queue');
249+
$subscriptionConsumerMock->addMessage($fourthMessageMock, 'foo_queue');
250+
$subscriptionConsumerMock->addMessage($fifthMessageMock, 'foo_queue');
251+
252+
$contextStub = $this->createPsrContextStub();
237253

238254
$processorMock = $this->createProcessorMock();
239255
$processorMock
@@ -242,16 +258,21 @@ public function testShouldProcessFiveMessagesAndQuit()
242258
->willReturn(Result::ACK)
243259
;
244260

245-
$queueConsumer = new QueueConsumer($contextStub, new BreakCycleExtension(5), 0);
246-
$queueConsumer->bind(new NullQueue('aQueueName'), $processorMock);
261+
$queueConsumer = new QueueConsumer($contextStub, new BreakCycleExtension(5));
262+
$queueConsumer->setFallbackSubscriptionConsumer($subscriptionConsumerMock);
263+
$queueConsumer->bind($fooQueue, $processorMock);
247264

248265
$queueConsumer->consume();
249266
}
250267

251268
public function testShouldAckMessageIfProcessorReturnSuchStatus()
252269
{
253270
$messageMock = $this->createMessageMock();
254-
$messageConsumerStub = $this->createMessageConsumerStub($messageMock);
271+
272+
$subscriptionConsumerMock = new DummySubscriptionConsumer();
273+
$subscriptionConsumerMock->addMessage($messageMock, 'foo_queue');
274+
275+
$messageConsumerStub = $this->createConsumerStub(new NullQueue('foo_queue'));
255276
$messageConsumerStub
256277
->expects($this->once())
257278
->method('acknowledge')
@@ -269,7 +290,8 @@ public function testShouldAckMessageIfProcessorReturnSuchStatus()
269290
;
270291

271292
$queueConsumer = new QueueConsumer($contextStub, new BreakCycleExtension(1), 0);
272-
$queueConsumer->bind(new NullQueue('aQueueName'), $processorMock);
293+
$queueConsumer->setFallbackSubscriptionConsumer($subscriptionConsumerMock);
294+
$queueConsumer->bind(new NullQueue('foo_queue'), $processorMock);
273295

274296
$queueConsumer->consume();
275297
}
@@ -1112,40 +1134,26 @@ public function testShouldCallEachQueueOneByOne()
11121134
}
11131135

11141136
/**
1115-
* @param null|mixed $message
1116-
*
1117-
* @return \PHPUnit_Framework_MockObject_MockObject|PsrConsumer
1118-
*/
1119-
protected function createMessageConsumerStub($message = null)
1120-
{
1121-
$messageConsumerMock = $this->createMock(PsrConsumer::class);
1122-
$messageConsumerMock
1123-
->expects($this->any())
1124-
->method('receive')
1125-
->willReturn($message)
1126-
;
1127-
1128-
return $messageConsumerMock;
1129-
}
1130-
1131-
/**
1132-
* @param null|mixed $messageConsumer
1133-
*
11341137
* @return \PHPUnit_Framework_MockObject_MockObject|PsrContext
11351138
*/
1136-
protected function createPsrContextStub($messageConsumer = null)
1139+
protected function createPsrContextStub(PsrConsumer $consumer = null)
11371140
{
11381141
$context = $this->createMock(PsrContext::class);
11391142
$context
11401143
->expects($this->any())
1141-
->method('createConsumer')
1142-
->willReturn($messageConsumer)
1144+
->method('createQueue')
1145+
->willReturnCallback(function (string $queueName) {
1146+
return new NullQueue($queueName);
1147+
})
11431148
;
11441149
$context
11451150
->expects($this->any())
1146-
->method('createQueue')
1147-
->willReturn($this->createMock(PsrQueue::class))
1151+
->method('createConsumer')
1152+
->willReturnCallback(function (PsrQueue $queue) use ($consumer) {
1153+
return $consumer ?: $this->createConsumerStub($queue);
1154+
})
11481155
;
1156+
11491157
$context
11501158
->expects($this->any())
11511159
->method('close')
@@ -1192,4 +1200,27 @@ protected function createExtension()
11921200
{
11931201
return $this->createMock(ExtensionInterface::class);
11941202
}
1203+
1204+
/**
1205+
* @return \PHPUnit_Framework_MockObject_MockObject|PsrConsumer
1206+
*/
1207+
protected function createConsumerStub(PsrQueue $queue = null)
1208+
{
1209+
$consumerMock = $this->createMock(PsrConsumer::class);
1210+
$consumerMock
1211+
->expects($this->any())
1212+
->method('getQueue')
1213+
->willReturn($queue)
1214+
;
1215+
1216+
return $consumerMock;
1217+
}
1218+
1219+
/**
1220+
* @return PsrSubscriptionConsumer|\PHPUnit_Framework_MockObject_MockObject
1221+
*/
1222+
private function createSubscriptionConsumerMock()
1223+
{
1224+
return $this->createMock(PsrSubscriptionConsumer::class);
1225+
}
11951226
}

0 commit comments

Comments
 (0)