Skip to content

Commit

Permalink
Merge pull request #263 from Solodkiy/update-popMessage-for-amqp
Browse files Browse the repository at this point in the history
fix inconsistent popMessage interface popMessage in amqp driver
  • Loading branch information
henrikbjorn authored Dec 18, 2016
2 parents 8e5884c + 2812c80 commit c3652b6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
23 changes: 14 additions & 9 deletions src/Driver/PhpAmqpDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,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];
}

/**
Expand Down
10 changes: 8 additions & 2 deletions tests/Driver/PhpAmqpDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c3652b6

Please sign in to comment.