Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Query Builder for better support across platforms. #176

Merged
merged 1 commit into from
Aug 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions pkg/dbal/DbalConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,18 @@ protected function receiveMessage()
try {
$now = time();

$sql = sprintf(
'SELECT * FROM %s WHERE queue=:queue AND '.
'(delayed_until IS NULL OR delayed_until<=:delayedUntil) '.
'ORDER BY priority DESC, id ASC LIMIT 1 FOR UPDATE',
$this->context->getTableName()
);
$query = $this->dbal->createQueryBuilder();
$query
->select('*')
->from($this->context->getTableName())
->where('queue = :queue')
->andWhere('(delayed_until IS NULL OR delayed_until <= :delayedUntil)')
->orderBy('priority', 'desc')
->orderBy('id', 'asc')
->setMaxResults(1)
;

$sql = $query->getSQL().' '.$this->dbal->getDatabasePlatform()->getWriteLockSQL();

$dbalMessage = $this->dbal->executeQuery(
$sql,
Expand Down
135 changes: 135 additions & 0 deletions pkg/dbal/Tests/DbalConsumerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Enqueue\Dbal\Tests;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Statement;
use Enqueue\Dbal\DbalConsumer;
use Enqueue\Dbal\DbalContext;
Expand Down Expand Up @@ -148,7 +150,41 @@ public function testShouldReceiveMessage()
->will($this->returnValue($dbalMessage))
;

$queryBuilder = $this->createQueryBuilderMock();
$queryBuilder
->expects($this->once())
->method('select')
->will($this->returnSelf())
;
$queryBuilder
->expects($this->once())
->method('from')
->will($this->returnSelf())
;
$queryBuilder
->expects($this->once())
->method('where')
->will($this->returnSelf())
;
$queryBuilder
->expects($this->once())
->method('andWhere')
->will($this->returnSelf())
;
$queryBuilder
->expects($this->exactly(2))
->method('orderBy')
->will($this->returnSelf())
;

$platform = $this->createPlatformMock();

$dbal = $this->createConnectionMock();
$dbal
->expects($this->once())
->method('createQueryBuilder')
->willReturn($queryBuilder)
;
$dbal
->expects($this->once())
->method('executeQuery')
Expand All @@ -163,6 +199,11 @@ public function testShouldReceiveMessage()
->expects($this->once())
->method('commit')
;
$dbal
->expects($this->once())
->method('getDatabasePlatform')
->willReturn($platform)
;

$context = $this->createContextMock();
$context
Expand Down Expand Up @@ -201,7 +242,41 @@ public function testShouldReturnNullIfThereIsNoNewMessage()
->will($this->returnValue(null))
;

$queryBuilder = $this->createQueryBuilderMock();
$queryBuilder
->expects($this->once())
->method('select')
->will($this->returnSelf())
;
$queryBuilder
->expects($this->once())
->method('from')
->will($this->returnSelf())
;
$queryBuilder
->expects($this->once())
->method('where')
->will($this->returnSelf())
;
$queryBuilder
->expects($this->once())
->method('andWhere')
->will($this->returnSelf())
;
$queryBuilder
->expects($this->exactly(2))
->method('orderBy')
->will($this->returnSelf())
;

$platform = $this->createPlatformMock();

$dbal = $this->createConnectionMock();
$dbal
->expects($this->once())
->method('createQueryBuilder')
->willReturn($queryBuilder)
;
$dbal
->expects($this->once())
->method('executeQuery')
Expand All @@ -216,6 +291,11 @@ public function testShouldReturnNullIfThereIsNoNewMessage()
->expects($this->once())
->method('commit')
;
$dbal
->expects($this->once())
->method('getDatabasePlatform')
->willReturn($platform)
;

$context = $this->createContextMock();
$context
Expand Down Expand Up @@ -250,7 +330,41 @@ public function testShouldThrowIfMessageWasNotRemoved()
->will($this->returnValue(['id' => '2134']))
;

$queryBuilder = $this->createQueryBuilderMock();
$queryBuilder
->expects($this->once())
->method('select')
->will($this->returnSelf())
;
$queryBuilder
->expects($this->once())
->method('from')
->will($this->returnSelf())
;
$queryBuilder
->expects($this->once())
->method('where')
->will($this->returnSelf())
;
$queryBuilder
->expects($this->once())
->method('andWhere')
->will($this->returnSelf())
;
$queryBuilder
->expects($this->exactly(2))
->method('orderBy')
->will($this->returnSelf())
;

$platform = $this->createPlatformMock();

$dbal = $this->createConnectionMock();
$dbal
->expects($this->once())
->method('createQueryBuilder')
->willReturn($queryBuilder)
;
$dbal
->expects($this->once())
->method('executeQuery')
Expand All @@ -269,6 +383,11 @@ public function testShouldThrowIfMessageWasNotRemoved()
->expects($this->once())
->method('rollBack')
;
$dbal
->expects($this->once())
->method('getDatabasePlatform')
->willReturn($platform)
;

$context = $this->createContextMock();
$context
Expand Down Expand Up @@ -318,6 +437,22 @@ private function createContextMock()
{
return $this->createMock(DbalContext::class);
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject|QueryBuilder
*/
private function createQueryBuilderMock()
{
return $this->createMock(QueryBuilder::class);
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject|AbstractPlatform
*/
private function createPlatformMock()
{
return $this->createMock(AbstractPlatform::class);
}
}

class InvalidMessage implements PsrMessage
Expand Down
4 changes: 2 additions & 2 deletions pkg/redis/RedisConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public function receive($timeout = 0)
$timeout = (int) ($timeout / 1000);
if (empty($timeout)) {
// Caused by
// Predis\Response\ServerException: ERR timeout is not an integer or out of range
// /mqdev/vendor/predis/predis/src/Client.php:370
// Predis\Response\ServerException: ERR timeout is not an integer or out of range
// /mqdev/vendor/predis/predis/src/Client.php:370

return $this->receiveNoWait();
}
Expand Down