From 2812c8025889278a1c54fd964c40392866ca3837 Mon Sep 17 00:00:00 2001 From: Alexey Solodkiy Date: Sun, 6 Nov 2016 22:05:46 +0300 Subject: [PATCH] fix inconsistent popMessage interface popMessage in amqp driver --- src/Driver/PhpAmqpDriver.php | 23 ++++++++++++++--------- tests/Driver/PhpAmqpDriverTest.php | 10 ++++++++-- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/Driver/PhpAmqpDriver.php b/src/Driver/PhpAmqpDriver.php index fac7e6cb..5627fbfe 100644 --- a/src/Driver/PhpAmqpDriver.php +++ b/src/Driver/PhpAmqpDriver.php @@ -85,24 +85,29 @@ public function pushMessage($queueName, $message) /** * Remove the next message in line. And if no message is available - * wait $interval seconds. + * wait $duration seconds. * * @param string $queueName - * @param int $interval + * @param int $duration * * @return array An array like array($message, $receipt); */ - public function popMessage($queueName, $interval = 10000) + public function popMessage($queueName, $duration = 5) { - $message = $this->getChannel()->basic_get($queueName); - if (!$message) { - // sleep for 10 ms to prevent hammering CPU - usleep($interval); + $runtime = microtime(true) + $duration; + + while (microtime(true) < $runtime) { + $message = $this->getChannel()->basic_get($queueName); - return [null, null]; + if ($message) { + return [$message->body, $message->get('delivery_tag')]; + } + + // sleep for 10 ms to prevent hammering CPU + usleep(10000); } - return [$message->body, $message->get('delivery_tag')]; + return [null, null]; } /** diff --git a/tests/Driver/PhpAmqpDriverTest.php b/tests/Driver/PhpAmqpDriverTest.php index 4ac7aff1..a04cb51b 100644 --- a/tests/Driver/PhpAmqpDriverTest.php +++ b/tests/Driver/PhpAmqpDriverTest.php @@ -137,13 +137,19 @@ public function testItPopsMessages() public function testItPopsArrayWithNullsWhenThereAreNoMessages() { + $startTime = microtime(true); + $this->phpAmqpChannel - ->expects($this->once()) + ->expects($this->any()) ->method('basic_get') ->with($this->equalTo('foo-queue')) ->willReturn(null); - $this->assertEquals([null, null], $this->driver->popMessage('foo-queue')); + $result = $this->driver->popMessage('foo-queue', 0.1); + $duration = microtime(true) - $startTime; + + $this->assertEquals([null, null], $result); + $this->assertGreaterThan(0.1, $duration); } public function testItAcknowledgesMessage()