From fee56d3ce16152e9d4f10f3112cb7b877c66a9b9 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Tue, 20 Jun 2017 16:23:43 +0300 Subject: [PATCH 01/20] [doc] fix missing result --- docs/client/quick_tour.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/client/quick_tour.md b/docs/client/quick_tour.md index 3d751a7ce..ad861dfda 100644 --- a/docs/client/quick_tour.md +++ b/docs/client/quick_tour.md @@ -52,11 +52,14 @@ $client->send('a_bar_topic', new class() implements \JsonSerializable { bind('a_bar_topic', 'a_processor_name', function(PsrMessage $psrMessage) { // processing logic here + + return PsrProcessor::ACK; }); $client->consume(); From f2ae99b5aa06bef7cd2357ccb9e5c835b9ecb304 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Tue, 20 Jun 2017 22:36:12 +0300 Subject: [PATCH 02/20] master is 0.5-dev now --- pkg/amqp-ext/composer.json | 10 +++++----- pkg/dbal/composer.json | 10 +++++----- pkg/enqueue-bundle/composer.json | 22 +++++++++++----------- pkg/enqueue/composer.json | 16 ++++++++-------- pkg/fs/composer.json | 10 +++++----- pkg/job-queue/composer.json | 8 ++++---- pkg/null/composer.json | 8 ++++---- pkg/psr-queue/composer.json | 4 ++-- pkg/redis/composer.json | 10 +++++----- pkg/simple-client/composer.json | 12 ++++++------ pkg/sqs/composer.json | 8 ++++---- pkg/stomp/composer.json | 10 +++++----- pkg/test/composer.json | 2 +- 13 files changed, 65 insertions(+), 65 deletions(-) diff --git a/pkg/amqp-ext/composer.json b/pkg/amqp-ext/composer.json index 3db593332..2341234bd 100644 --- a/pkg/amqp-ext/composer.json +++ b/pkg/amqp-ext/composer.json @@ -13,14 +13,14 @@ "require": { "php": ">=5.6", "ext-amqp": "^1.6", - "enqueue/psr-queue": "^0.4", + "enqueue/psr-queue": "^0.5@dev", "psr/log": "^1" }, "require-dev": { "phpunit/phpunit": "~5.4.0", - "enqueue/test": "^0.4", - "enqueue/enqueue": "^0.4", - "enqueue/null": "^0.4", + "enqueue/test": "^0.5@dev", + "enqueue/enqueue": "^0.5@dev", + "enqueue/null": "^0.5@dev", "empi89/php-amqp-stubs": "*@dev", "symfony/dependency-injection": "^2.8|^3", "symfony/config": "^2.8|^3" @@ -37,7 +37,7 @@ "minimum-stability": "dev", "extra": { "branch-alias": { - "dev-master": "0.4.x-dev" + "dev-master": "0.5.x-dev" } } } diff --git a/pkg/dbal/composer.json b/pkg/dbal/composer.json index 7fce27936..fe056d0e5 100644 --- a/pkg/dbal/composer.json +++ b/pkg/dbal/composer.json @@ -12,15 +12,15 @@ ], "require": { "php": ">=5.6", - "enqueue/psr-queue": "^0.4", + "enqueue/psr-queue": "^0.5@dev", "doctrine/dbal": "~2.5", "psr/log": "^1" }, "require-dev": { "phpunit/phpunit": "~5.4.0", - "enqueue/test": "^0.4", - "enqueue/enqueue": "^0.4", - "enqueue/null": "^0.4", + "enqueue/test": "^0.5@dev", + "enqueue/enqueue": "^0.5@dev", + "enqueue/null": "^0.5@dev", "symfony/dependency-injection": "^2.8|^3", "symfony/config": "^2.8|^3" }, @@ -36,7 +36,7 @@ "minimum-stability": "dev", "extra": { "branch-alias": { - "dev-master": "0.4.x-dev" + "dev-master": "0.5.x-dev" } } } diff --git a/pkg/enqueue-bundle/composer.json b/pkg/enqueue-bundle/composer.json index 8103da8ba..3e120ece2 100644 --- a/pkg/enqueue-bundle/composer.json +++ b/pkg/enqueue-bundle/composer.json @@ -13,19 +13,19 @@ "require": { "php": ">=5.6", "symfony/framework-bundle": "^2.8|^3", - "enqueue/enqueue": "^0.4.18", - "enqueue/null": "^0.4" + "enqueue/enqueue": "^0.5@dev", + "enqueue/null": "^0.5@dev" }, "require-dev": { "phpunit/phpunit": "~5.5", - "enqueue/stomp": "^0.3@dev|^0.4@dev", - "enqueue/amqp-ext": "^0.3@dev|^0.4@dev", - "enqueue/job-queue": "^0.3@dev|^0.4@dev", - "enqueue/fs": "^0.4", - "enqueue/redis": "^0.4", - "enqueue/dbal": "^0.4", - "enqueue/sqs": "^0.4", - "enqueue/test": "^0.3@dev|^0.4@dev", + "enqueue/stomp": "^0.5@dev", + "enqueue/amqp-ext": "^0.5@dev", + "enqueue/job-queue": "^0.5@dev", + "enqueue/fs": "^0.5@dev", + "enqueue/redis": "^0.5@dev", + "enqueue/dbal": "^0.5@dev", + "enqueue/sqs": "^0.5@dev", + "enqueue/test": "^0.5@dev", "doctrine/doctrine-bundle": "~1.2", "symfony/monolog-bundle": "^2.8|^3", "symfony/browser-kit": "^2.8|^3", @@ -39,7 +39,7 @@ }, "extra": { "branch-alias": { - "dev-master": "0.4.x-dev" + "dev-master": "0.5.x-dev" } } } diff --git a/pkg/enqueue/composer.json b/pkg/enqueue/composer.json index 1f888eaeb..d7d2b7c4e 100644 --- a/pkg/enqueue/composer.json +++ b/pkg/enqueue/composer.json @@ -12,8 +12,8 @@ ], "require": { "php": ">=5.6", - "enqueue/psr-queue": "^0.4", - "enqueue/null": "^0.4", + "enqueue/psr-queue": "^0.5@dev", + "enqueue/null": "^0.5@dev", "ramsey/uuid": "^2|^3.5" }, "require-dev": { @@ -23,11 +23,11 @@ "symfony/config": "^2.8|^3", "symfony/event-dispatcher": "^2.8|^3", "symfony/http-kernel": "^2.8|^3", - "enqueue/amqp-ext": "^0.4", - "enqueue/dbal": "^0.4", - "enqueue/fs": "^0.4", - "enqueue/test": "^0.4", - "enqueue/simple-client": "^0.4", + "enqueue/amqp-ext": "^0.5@dev", + "enqueue/dbal": "^0.5@dev", + "enqueue/fs": "^0.5@dev", + "enqueue/test": "^0.5@dev", + "enqueue/simple-client": "^0.5@dev", "empi89/php-amqp-stubs": "*@dev" }, "suggest": { @@ -51,7 +51,7 @@ "minimum-stability": "dev", "extra": { "branch-alias": { - "dev-master": "0.4.x-dev" + "dev-master": "0.5.x-dev" } } } diff --git a/pkg/fs/composer.json b/pkg/fs/composer.json index 42dcc6866..54e528c7f 100644 --- a/pkg/fs/composer.json +++ b/pkg/fs/composer.json @@ -12,16 +12,16 @@ ], "require": { "php": ">=5.6", - "enqueue/psr-queue": "^0.4", + "enqueue/psr-queue": "^0.5@dev", "symfony/filesystem": "^2.8|^3", "makasim/temp-file": "^0.2", "psr/log": "^1" }, "require-dev": { "phpunit/phpunit": "~5.5", - "enqueue/enqueue": "^0.4", - "enqueue/null": "^0.4", - "enqueue/test": "^0.4", + "enqueue/enqueue": "^0.5@dev", + "enqueue/null": "^0.5@dev", + "enqueue/test": "^0.5@dev", "symfony/dependency-injection": "^2.8|^3", "symfony/config": "^2.8|^3" }, @@ -34,7 +34,7 @@ "minimum-stability": "dev", "extra": { "branch-alias": { - "dev-master": "0.4.x-dev" + "dev-master": "0.5.x-dev" } } } diff --git a/pkg/job-queue/composer.json b/pkg/job-queue/composer.json index dbf0abf75..b541b1ddb 100644 --- a/pkg/job-queue/composer.json +++ b/pkg/job-queue/composer.json @@ -13,13 +13,13 @@ "require": { "php": ">=5.6", "symfony/framework-bundle": "^2.8|^3", - "enqueue/enqueue": "^0.4", - "enqueue/null": "^0.4", + "enqueue/enqueue": "^0.5@dev", + "enqueue/null": "^0.5@dev", "doctrine/orm": "~2.4" }, "require-dev": { "phpunit/phpunit": "~5.5", - "enqueue/test": "^0.4", + "enqueue/test": "^0.5@dev", "doctrine/doctrine-bundle": "~1.2", "symfony/browser-kit": "^2.8|^3", "symfony/expression-language": "^2.8|^3" @@ -33,7 +33,7 @@ "minimum-stability": "dev", "extra": { "branch-alias": { - "dev-master": "0.4.x-dev" + "dev-master": "0.5.x-dev" } } } diff --git a/pkg/null/composer.json b/pkg/null/composer.json index 6045e0b98..317f11f74 100644 --- a/pkg/null/composer.json +++ b/pkg/null/composer.json @@ -12,13 +12,13 @@ ], "require": { "php": ">=5.6", - "enqueue/psr-queue": "^0.4", + "enqueue/psr-queue": "^0.5@dev", "psr/log": "^1" }, "require-dev": { "phpunit/phpunit": "~5.5", - "enqueue/enqueue": "^0.4", - "enqueue/test": "^0.4", + "enqueue/enqueue": "^0.5@dev", + "enqueue/test": "^0.5@dev", "symfony/dependency-injection": "^2.8|^3", "symfony/config": "^2.8|^3" }, @@ -31,7 +31,7 @@ "minimum-stability": "dev", "extra": { "branch-alias": { - "dev-master": "0.4.x-dev" + "dev-master": "0.5.x-dev" } } } diff --git a/pkg/psr-queue/composer.json b/pkg/psr-queue/composer.json index 3fc6aa30d..398cd7d29 100644 --- a/pkg/psr-queue/composer.json +++ b/pkg/psr-queue/composer.json @@ -15,7 +15,7 @@ }, "require-dev": { "phpunit/phpunit": "~5.5", - "enqueue/test": "^0.4" + "enqueue/test": "^0.5@dev" }, "autoload": { "psr-4": { "Enqueue\\Psr\\": "" }, @@ -26,7 +26,7 @@ "minimum-stability": "dev", "extra": { "branch-alias": { - "dev-master": "0.4.x-dev" + "dev-master": "0.5.x-dev" } } } diff --git a/pkg/redis/composer.json b/pkg/redis/composer.json index 5db8a055a..947d92336 100644 --- a/pkg/redis/composer.json +++ b/pkg/redis/composer.json @@ -12,15 +12,15 @@ ], "require": { "php": ">=5.6", - "enqueue/psr-queue": "^0.4", + "enqueue/psr-queue": "^0.5@dev", "psr/log": "^1" }, "require-dev": { "phpunit/phpunit": "~5.4.0", "predis/predis": "^1.1", - "enqueue/test": "^0.4", - "enqueue/enqueue": "^0.4", - "enqueue/null": "^0.4", + "enqueue/test": "^0.5@dev", + "enqueue/enqueue": "^0.5@dev", + "enqueue/null": "^0.5@dev", "symfony/dependency-injection": "^2.8|^3", "symfony/config": "^2.8|^3" }, @@ -38,7 +38,7 @@ "minimum-stability": "dev", "extra": { "branch-alias": { - "dev-master": "0.4.x-dev" + "dev-master": "0.5.x-dev" } } } diff --git a/pkg/simple-client/composer.json b/pkg/simple-client/composer.json index 58523d95c..f1f3d75b5 100644 --- a/pkg/simple-client/composer.json +++ b/pkg/simple-client/composer.json @@ -12,17 +12,17 @@ ], "require": { "php": ">=5.6", - "enqueue/enqueue": "^0.4", + "enqueue/enqueue": "^0.5@dev", "symfony/dependency-injection": "^2.8|^3", "symfony/config": "^2.8|^3", "symfony/console": "^2.8|^3" }, "require-dev": { "phpunit/phpunit": "~5.5", - "enqueue/test": "^0.4", - "enqueue/amqp-ext": "^0.4", - "enqueue/fs": "^0.4", - "enqueue/null": "^0.4" + "enqueue/test": "^0.5@dev", + "enqueue/amqp-ext": "^0.5@dev", + "enqueue/fs": "^0.5@dev", + "enqueue/null": "^0.5@dev" }, "autoload": { "psr-4": { "Enqueue\\SimpleClient\\": "" }, @@ -33,7 +33,7 @@ "minimum-stability": "dev", "extra": { "branch-alias": { - "dev-master": "0.4.x-dev" + "dev-master": "0.5.x-dev" } } } diff --git a/pkg/sqs/composer.json b/pkg/sqs/composer.json index 7fa7f879b..21e9c9d7f 100644 --- a/pkg/sqs/composer.json +++ b/pkg/sqs/composer.json @@ -12,14 +12,14 @@ ], "require": { "php": ">=5.6", - "enqueue/psr-queue": "^0.4", + "enqueue/psr-queue": "^0.5@dev", "aws/aws-sdk-php": "~3.26", "psr/log": "^1" }, "require-dev": { "phpunit/phpunit": "~5.4.0", - "enqueue/test": "^0.4", - "enqueue/enqueue": "^0.4", + "enqueue/test": "^0.5@dev", + "enqueue/enqueue": "^0.5@dev", "symfony/dependency-injection": "^2.8|^3", "symfony/config": "^2.8|^3" }, @@ -35,7 +35,7 @@ "minimum-stability": "dev", "extra": { "branch-alias": { - "dev-master": "0.4.x-dev" + "dev-master": "0.5.x-dev" } } } diff --git a/pkg/stomp/composer.json b/pkg/stomp/composer.json index 7fe723f52..1089131c9 100644 --- a/pkg/stomp/composer.json +++ b/pkg/stomp/composer.json @@ -13,16 +13,16 @@ "require": { "php": ">=5.6", "stomp-php/stomp-php": "^4", - "enqueue/psr-queue": "^0.4", + "enqueue/psr-queue": "^0.5@dev", "php-http/guzzle6-adapter": "^1.1", "richardfullmer/rabbitmq-management-api": "^2.0", "psr/log": "^1" }, "require-dev": { "phpunit/phpunit": "~5.4.0", - "enqueue/test": "^0.4", - "enqueue/enqueue": "^0.4", - "enqueue/null": "^0.4", + "enqueue/test": "^0.5@dev", + "enqueue/enqueue": "^0.5@dev", + "enqueue/null": "^0.5@dev", "symfony/dependency-injection": "^2.8|^3", "symfony/config": "^2.8|^3" }, @@ -38,7 +38,7 @@ "minimum-stability": "dev", "extra": { "branch-alias": { - "dev-master": "0.4.x-dev" + "dev-master": "0.5.x-dev" } } } diff --git a/pkg/test/composer.json b/pkg/test/composer.json index 1a2ed3cef..a6f50b01f 100644 --- a/pkg/test/composer.json +++ b/pkg/test/composer.json @@ -7,7 +7,7 @@ "minimum-stability": "dev", "extra": { "branch-alias": { - "dev-master": "0.4.x-dev" + "dev-master": "0.5.x-dev" } } } From 108ff95730b90b8db999e54e8b78db1e34977b6e Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Tue, 20 Jun 2017 22:40:21 +0300 Subject: [PATCH 03/20] [rpc] remove deprecated method. --- .../Tests/Functional/AmqpRpcUseCasesTest.php | 4 ++-- pkg/enqueue/Rpc/Promise.php | 14 -------------- pkg/enqueue/Tests/Rpc/PromiseTest.php | 16 ---------------- pkg/fs/Tests/Functional/FsRpcUseCasesTest.php | 4 ++-- .../Tests/Functional/StompRpcUseCasesTest.php | 4 ++-- 5 files changed, 6 insertions(+), 36 deletions(-) diff --git a/pkg/amqp-ext/Tests/Functional/AmqpRpcUseCasesTest.php b/pkg/amqp-ext/Tests/Functional/AmqpRpcUseCasesTest.php index 498e8e96e..197afa836 100644 --- a/pkg/amqp-ext/Tests/Functional/AmqpRpcUseCasesTest.php +++ b/pkg/amqp-ext/Tests/Functional/AmqpRpcUseCasesTest.php @@ -65,7 +65,7 @@ public function testDoAsyncRpcCallWithCustomReplyQueue() $this->amqpContext->createProducer()->send($replyQueue, $replyMessage); - $actualReplyMessage = $promise->getMessage(); + $actualReplyMessage = $promise->receive(); $this->assertInstanceOf(AmqpMessage::class, $actualReplyMessage); } @@ -95,7 +95,7 @@ public function testDoAsyncRecCallWithCastInternallyCreatedTemporaryReplyQueue() $this->amqpContext->createProducer()->send($replyQueue, $replyMessage); - $actualReplyMessage = $promise->getMessage(); + $actualReplyMessage = $promise->receive(); $this->assertInstanceOf(AmqpMessage::class, $actualReplyMessage); } } diff --git a/pkg/enqueue/Rpc/Promise.php b/pkg/enqueue/Rpc/Promise.php index f716f3950..fca4a43d8 100644 --- a/pkg/enqueue/Rpc/Promise.php +++ b/pkg/enqueue/Rpc/Promise.php @@ -45,20 +45,6 @@ public function __construct(\Closure $receiveCallback, \Closure $receiveNoWaitCa $this->deleteReplyQueue = true; } - /** - * Blocks until message received or timeout expired. - * - * @deprecated use "receive" instead - * - * @throws TimeoutException if the wait timeout is reached - * - * @return PsrMessage - */ - public function getMessage() - { - return $this->receive(); - } - /** * Blocks until message received or timeout expired. * diff --git a/pkg/enqueue/Tests/Rpc/PromiseTest.php b/pkg/enqueue/Tests/Rpc/PromiseTest.php index 2018bc27a..9c747e709 100644 --- a/pkg/enqueue/Tests/Rpc/PromiseTest.php +++ b/pkg/enqueue/Tests/Rpc/PromiseTest.php @@ -234,20 +234,4 @@ public function testOnReceiveNoWaitShouldNotCallCallbackIfMessageReceivedByRecei $this->assertFalse($invokedReceiveNoWait); $this->assertFalse($invokedReceive); } - - public function testDeprecatedGetMessageShouldCallReceiveMethod() - { - $promise = $this->getMockBuilder(Promise::class) - ->disableOriginalConstructor() - ->setMethods(['receive']) - ->getMock() - ; - - $promise - ->expects($this->once()) - ->method('receive') - ; - - $promise->getMessage(); - } } diff --git a/pkg/fs/Tests/Functional/FsRpcUseCasesTest.php b/pkg/fs/Tests/Functional/FsRpcUseCasesTest.php index 2c1bf6393..91c40f450 100644 --- a/pkg/fs/Tests/Functional/FsRpcUseCasesTest.php +++ b/pkg/fs/Tests/Functional/FsRpcUseCasesTest.php @@ -60,7 +60,7 @@ public function testDoAsyncRpcCallWithCustomReplyQueue() $this->fsContext->createProducer()->send($replyQueue, $replyMessage); - $actualReplyMessage = $promise->getMessage(); + $actualReplyMessage = $promise->receive(); $this->assertInstanceOf(FsMessage::class, $actualReplyMessage); } @@ -89,7 +89,7 @@ public function testDoAsyncRecCallWithCastInternallyCreatedTemporaryReplyQueue() $this->fsContext->createProducer()->send($replyQueue, $replyMessage); - $actualReplyMessage = $promise->getMessage(); + $actualReplyMessage = $promise->receive(); $this->assertInstanceOf(FsMessage::class, $actualReplyMessage); } } diff --git a/pkg/stomp/Tests/Functional/StompRpcUseCasesTest.php b/pkg/stomp/Tests/Functional/StompRpcUseCasesTest.php index 5a3e979c8..3ece5e486 100644 --- a/pkg/stomp/Tests/Functional/StompRpcUseCasesTest.php +++ b/pkg/stomp/Tests/Functional/StompRpcUseCasesTest.php @@ -63,7 +63,7 @@ public function testDoAsyncRpcCallWithCustomReplyQueue() $this->stompContext->createProducer()->send($replyQueue, $replyMessage); - $actualReplyMessage = $promise->getMessage(); + $actualReplyMessage = $promise->receive(); $this->assertInstanceOf(StompMessage::class, $actualReplyMessage); } @@ -94,7 +94,7 @@ public function testDoAsyncRecCallWithCastInternallyCreatedTemporaryReplyQueue() $this->stompContext->createProducer()->send($replyQueue, $replyMessage); - $actualReplyMessage = $promise->getMessage(); + $actualReplyMessage = $promise->receive(); $this->assertInstanceOf(StompMessage::class, $actualReplyMessage); } } From d9035ce00b86d2cb108bc9cee3d120855ab4e194 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Tue, 20 Jun 2017 22:53:10 +0300 Subject: [PATCH 04/20] [consumption] Print the queue name on message receive only. --- pkg/enqueue/Consumption/QueueConsumer.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/pkg/enqueue/Consumption/QueueConsumer.php b/pkg/enqueue/Consumption/QueueConsumer.php index 4a3b39aac..ccf17d96a 100644 --- a/pkg/enqueue/Consumption/QueueConsumer.php +++ b/pkg/enqueue/Consumption/QueueConsumer.php @@ -121,17 +121,10 @@ public function consume(ExtensionInterface $runtimeExtension = null) $logger = $context->getLogger() ?: new NullLogger(); $logger->info('Start consuming'); - /** @var PsrQueue|null $previousQueue */ - $previousQueue = null; - while (true) { try { /** @var PsrQueue $queue */ foreach ($this->boundProcessors as list($queue, $processor)) { - if (false == $previousQueue || $previousQueue->getQueueName() != $queue->getQueueName()) { - $logger->debug(sprintf('Switch to a queue %s', $queue->getQueueName())); - } - $consumer = $consumers[$queue->getQueueName()]; $context = new Context($this->psrContext); @@ -141,8 +134,6 @@ public function consume(ExtensionInterface $runtimeExtension = null) $context->setPsrProcessor($processor); $this->doConsume($extension, $context); - - $previousQueue = $queue; } } catch (ConsumptionInterruptedException $e) { $logger->info(sprintf('Consuming interrupted')); @@ -191,7 +182,7 @@ protected function doConsume(ExtensionInterface $extension, Context $context) } if ($message = $consumer->receive($timeout = 5000)) { - $logger->info('Message received'); + $logger->info('Message received from the queue: '.$context->getPsrQueue()->getQueueName()); $logger->debug('Headers: {headers}', ['headers' => new VarExport($message->getHeaders())]); $logger->debug('Properties: {properties}', ['properties' => new VarExport($message->getProperties())]); $logger->debug('Payload: {payload}', ['payload' => new VarExport($message->getBody())]); From f0292cab75b5977ae57ac4df0283375cfc9dddf5 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Tue, 20 Jun 2017 22:53:39 +0300 Subject: [PATCH 05/20] [consumption] add ability to change reply status --- pkg/enqueue/Consumption/Result.php | 7 +++-- pkg/enqueue/Tests/Consumption/ResultTest.php | 28 ++++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/pkg/enqueue/Consumption/Result.php b/pkg/enqueue/Consumption/Result.php index c25a12230..0ed319bd8 100644 --- a/pkg/enqueue/Consumption/Result.php +++ b/pkg/enqueue/Consumption/Result.php @@ -119,13 +119,16 @@ public static function requeue($reason = '') /** * @param PsrMessage $replyMessage + * @param string $status * @param string|null $reason * * @return Result */ - public static function reply(PsrMessage $replyMessage, $reason = '') + public static function reply(PsrMessage $replyMessage, $status = self::ACK, $reason = null) { - $result = static::ack($reason); + $status = null === $status ? self::ACK : $status; + + $result = new static($status, $reason); $result->setReply($replyMessage); return $result; diff --git a/pkg/enqueue/Tests/Consumption/ResultTest.php b/pkg/enqueue/Tests/Consumption/ResultTest.php index 19d43b564..94abb9e14 100644 --- a/pkg/enqueue/Tests/Consumption/ResultTest.php +++ b/pkg/enqueue/Tests/Consumption/ResultTest.php @@ -53,11 +53,35 @@ public function testCouldConstructedWithRequeueFactoryMethod() $this->assertSame(null, $result->getReply()); } - public function testCouldConstructedWithReplyFactoryMethod() + public function testCouldConstructedWithReplyFactoryMethodAndAckStatusByDefault() { $reply = new NullMessage(); - $result = Result::reply($reply, 'theReason'); + $result = Result::reply($reply); + + $this->assertInstanceOf(Result::class, $result); + $this->assertSame(Result::ACK, $result->getStatus()); + $this->assertSame('', $result->getReason()); + $this->assertSame($reply, $result->getReply()); + } + + public function testCouldConstructedWithReplyFactoryMethodAndRejectStatusExplicitly() + { + $reply = new NullMessage(); + + $result = Result::reply($reply, Result::REJECT); + + $this->assertInstanceOf(Result::class, $result); + $this->assertSame(Result::REJECT, $result->getStatus()); + $this->assertSame('', $result->getReason()); + $this->assertSame($reply, $result->getReply()); + } + + public function testCouldConstructedWithReplyFactoryMethodAndReasonSet() + { + $reply = new NullMessage(); + + $result = Result::reply($reply, null, 'theReason'); $this->assertInstanceOf(Result::class, $result); $this->assertSame(Result::ACK, $result->getStatus()); From 974de6a9601f7e918fe297868cb05c47a446a492 Mon Sep 17 00:00:00 2001 From: Alexander Kozienko Date: Thu, 22 Jun 2017 14:31:24 +0300 Subject: [PATCH 06/20] fix dbal polling interval configuration option --- pkg/dbal/DbalContext.php | 4 ++-- pkg/dbal/Tests/DbalContextTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/dbal/DbalContext.php b/pkg/dbal/DbalContext.php index e6a7b5d17..fa1129e48 100644 --- a/pkg/dbal/DbalContext.php +++ b/pkg/dbal/DbalContext.php @@ -115,8 +115,8 @@ public function createConsumer(PsrDestination $destination) $consumer = new DbalConsumer($this, $destination); - if (isset($this->config['pollingInterval'])) { - $consumer->setPollingInterval($this->config['pollingInterval']); + if (isset($this->config['polling_interval'])) { + $consumer->setPollingInterval($this->config['polling_interval']); } return $consumer; diff --git a/pkg/dbal/Tests/DbalContextTest.php b/pkg/dbal/Tests/DbalContextTest.php index 61e73c7bf..cfa17293c 100644 --- a/pkg/dbal/Tests/DbalContextTest.php +++ b/pkg/dbal/Tests/DbalContextTest.php @@ -98,7 +98,7 @@ public function testShouldCreateConsumer() public function testShouldCreateMessageConsumerAndSetPollingInterval() { $context = new DbalContext($this->createConnectionMock(), [ - 'pollingInterval' => 123456, + 'polling_interval' => 123456, ]); $consumer = $context->createConsumer(new DbalDestination('')); From 961d99a3e37db589308b2568fe0684ce3ee45b99 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Fri, 23 Jun 2017 11:36:04 +0300 Subject: [PATCH 07/20] Update supported_brokers.md --- docs/client/supported_brokers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/client/supported_brokers.md b/docs/client/supported_brokers.md index cff70a7ed..47a8848fa 100644 --- a/docs/client/supported_brokers.md +++ b/docs/client/supported_brokers.md @@ -11,7 +11,7 @@ Here's the list of protocols and Client features supported by them | Filesystem | No | No | No | Yes | No | | Redis | No | No | No | Not needed | No | | Doctrine DBAL | Yes | Yes | No | Yes | No | -| AWS SQS | No | Yes | No | Yes | No | +| AWS SQS | No | Yes | No | Yes | Not impl | * \* Possible if a RabbitMQ delay plugin is installed. * \*\* Possible if topics (exchanges) are configured on broker side manually. From 4a128f3c36090e77666a3f57cc85c8da9c29dafd Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Fri, 23 Jun 2017 16:27:05 +0300 Subject: [PATCH 08/20] [beanstalk] Add transport for beanstalkd --- bin/test | 1 + composer.json | 5 + docker-compose.yml | 11 + phpunit.xml.dist | 4 + pkg/pheanstalk/LICENSE | 20 ++ .../PheanstalkConnectionFactory.php | 126 ++++++++ pkg/pheanstalk/PheanstalkConsumer.php | 109 +++++++ pkg/pheanstalk/PheanstalkContext.php | 93 ++++++ pkg/pheanstalk/PheanstalkDestination.php | 46 +++ pkg/pheanstalk/PheanstalkMessage.php | 305 ++++++++++++++++++ pkg/pheanstalk/PheanstalkProducer.php | 54 ++++ pkg/pheanstalk/README.md | 26 ++ .../PheanstalkConnectionFactoryConfigTest.php | 137 ++++++++ .../Spec/PheanstalkConnectionFactoryTest.php | 17 + .../Tests/Spec/PheanstalkContextTest.php | 18 ++ .../Tests/Spec/PheanstalkMessageTest.php | 17 + .../Tests/Spec/PheanstalkQueueTest.php | 17 + ...heanstalkSendToAndReceiveFromQueueTest.php | 34 ++ ...heanstalkSendToAndReceiveFromTopicTest.php | 34 ++ ...alkSendToAndReceiveNoWaitFromQueueTest.php | 30 ++ ...alkSendToAndReceiveNoWaitFromTopicTest.php | 34 ++ ...talkSendToTopicAndReceiveFromQueueTest.php | 45 +++ ...ndToTopicAndReceiveNoWaitFromQueueTest.php | 45 +++ .../Tests/Spec/PheanstalkTopicTest.php | 17 + pkg/pheanstalk/composer.json | 41 +++ pkg/pheanstalk/phpunit.xml.dist | 30 ++ .../Spec/PsrConnectionFactorySpec.php | 27 ++ pkg/psr-queue/Spec/PsrContextSpec.php | 86 +++++ pkg/psr-queue/Spec/PsrQueueSpec.php | 28 ++ pkg/psr-queue/Spec/PsrTopicSpec.php | 28 ++ .../Spec/SendToAndReceiveFromQueueSpec.php | 51 +++ .../Spec/SendToAndReceiveFromTopicSpec.php | 51 +++ .../SendToAndReceiveNoWaitFromQueueSpec.php | 54 ++++ .../SendToAndReceiveNoWaitFromTopicSpec.php | 54 ++++ .../SendToTopicAndReceiveFromQueueSpec.php | 64 ++++ ...ndToTopicAndReceiveNoWaitFromQueueSpec.php | 67 ++++ 36 files changed, 1826 insertions(+) create mode 100644 pkg/pheanstalk/LICENSE create mode 100644 pkg/pheanstalk/PheanstalkConnectionFactory.php create mode 100644 pkg/pheanstalk/PheanstalkConsumer.php create mode 100644 pkg/pheanstalk/PheanstalkContext.php create mode 100644 pkg/pheanstalk/PheanstalkDestination.php create mode 100644 pkg/pheanstalk/PheanstalkMessage.php create mode 100644 pkg/pheanstalk/PheanstalkProducer.php create mode 100644 pkg/pheanstalk/README.md create mode 100644 pkg/pheanstalk/Tests/PheanstalkConnectionFactoryConfigTest.php create mode 100644 pkg/pheanstalk/Tests/Spec/PheanstalkConnectionFactoryTest.php create mode 100644 pkg/pheanstalk/Tests/Spec/PheanstalkContextTest.php create mode 100644 pkg/pheanstalk/Tests/Spec/PheanstalkMessageTest.php create mode 100644 pkg/pheanstalk/Tests/Spec/PheanstalkQueueTest.php create mode 100644 pkg/pheanstalk/Tests/Spec/PheanstalkSendToAndReceiveFromQueueTest.php create mode 100644 pkg/pheanstalk/Tests/Spec/PheanstalkSendToAndReceiveFromTopicTest.php create mode 100644 pkg/pheanstalk/Tests/Spec/PheanstalkSendToAndReceiveNoWaitFromQueueTest.php create mode 100644 pkg/pheanstalk/Tests/Spec/PheanstalkSendToAndReceiveNoWaitFromTopicTest.php create mode 100644 pkg/pheanstalk/Tests/Spec/PheanstalkSendToTopicAndReceiveFromQueueTest.php create mode 100644 pkg/pheanstalk/Tests/Spec/PheanstalkSendToTopicAndReceiveNoWaitFromQueueTest.php create mode 100644 pkg/pheanstalk/Tests/Spec/PheanstalkTopicTest.php create mode 100644 pkg/pheanstalk/composer.json create mode 100644 pkg/pheanstalk/phpunit.xml.dist create mode 100644 pkg/psr-queue/Spec/PsrConnectionFactorySpec.php create mode 100644 pkg/psr-queue/Spec/PsrContextSpec.php create mode 100644 pkg/psr-queue/Spec/PsrQueueSpec.php create mode 100644 pkg/psr-queue/Spec/PsrTopicSpec.php create mode 100644 pkg/psr-queue/Spec/SendToAndReceiveFromQueueSpec.php create mode 100644 pkg/psr-queue/Spec/SendToAndReceiveFromTopicSpec.php create mode 100644 pkg/psr-queue/Spec/SendToAndReceiveNoWaitFromQueueSpec.php create mode 100644 pkg/psr-queue/Spec/SendToAndReceiveNoWaitFromTopicSpec.php create mode 100644 pkg/psr-queue/Spec/SendToTopicAndReceiveFromQueueSpec.php create mode 100644 pkg/psr-queue/Spec/SendToTopicAndReceiveNoWaitFromQueueSpec.php diff --git a/bin/test b/bin/test index 1bd264a4c..c3d2930bb 100755 --- a/bin/test +++ b/bin/test @@ -23,6 +23,7 @@ function waitForService() waitForService rabbitmq 5672 50 waitForService mysql 3306 50 waitForService redis 6379 50 +waitForService beanstalkd 11300 php pkg/job-queue/Tests/Functional/app/console doctrine:database:create php pkg/job-queue/Tests/Functional/app/console doctrine:schema:update --force diff --git a/composer.json b/composer.json index 3aa91097d..5cc11dfd3 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,7 @@ "enqueue/null": "*@dev", "enqueue/dbal": "*@dev", "enqueue/sqs": "*@dev", + "enqueue/pheanstalk": "*@dev", "enqueue/enqueue-bundle": "*@dev", "enqueue/job-queue": "*@dev", "enqueue/simple-client": "*@dev", @@ -85,6 +86,10 @@ "type": "path", "url": "pkg/sqs" }, + { + "type": "path", + "url": "pkg/pheanstalk" + }, { "type": "path", "url": "pkg/simple-client" diff --git a/docker-compose.yml b/docker-compose.yml index 16c6d946c..fba4ac60f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,5 @@ version: '2' + services: dev: image: enqueue/dev:latest @@ -7,6 +8,7 @@ services: - rabbitmq - mysql - redis + - beanstalkd volumes: - './:/mqdev' environment: @@ -29,6 +31,9 @@ services: - AWS__SQS__KEY=$ENQUEUE_AWS__SQS__KEY - AWS__SQS__SECRET=$ENQUEUE_AWS__SQS__SECRET - AWS__SQS__REGION=$ENQUEUE_AWS__SQS__REGION + - BEANSTALKD_HOST=beanstalkd + - BEANSTALKD_PORT=11300 + - BEANSTALKD_DSN=beanstalk://beanstalkd:11300 rabbitmq: image: enqueue/rabbitmq:latest @@ -37,6 +42,12 @@ services: - RABBITMQ_DEFAULT_USER=guest - RABBITMQ_DEFAULT_PASS=guest - RABBITMQ_DEFAULT_VHOST=mqdev + ports: + - "15677:15672" + + beanstalkd: + image: 'schickling/beanstalkd' + redis: image: 'redis:3' ports: diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 429ead9d2..420daae7f 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -49,6 +49,10 @@ pkg/sqs/Tests + + pkg/pheanstalk/Tests + + pkg/enqueue-bundle/Tests diff --git a/pkg/pheanstalk/LICENSE b/pkg/pheanstalk/LICENSE new file mode 100644 index 000000000..d9736f8bf --- /dev/null +++ b/pkg/pheanstalk/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) +Copyright (c) 2017 Kotliar Maksym + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/pkg/pheanstalk/PheanstalkConnectionFactory.php b/pkg/pheanstalk/PheanstalkConnectionFactory.php new file mode 100644 index 000000000..f1967bd61 --- /dev/null +++ b/pkg/pheanstalk/PheanstalkConnectionFactory.php @@ -0,0 +1,126 @@ + 'localhost', + * 'port' => 11300, + * 'timeout' => null, + * 'persisted' => true, + * ] + * + * or + * + * beanstalk://host:port + * + * @param array|string $config + */ + public function __construct($config = 'beanstalk://') + { + if (empty($config) || 'beanstalk://' === $config) { + $config = []; + } elseif (is_string($config)) { + $config = $this->parseDsn($config); + } elseif (is_array($config)) { + } else { + throw new \LogicException('The config must be either an array of options, a DSN string or null'); + } + + $this->config = array_replace($this->defaultConfig(), $config); + } + + /** + * {@inheritdoc} + * + * @return PheanstalkContext + */ + public function createContext() + { + return new PheanstalkContext($this->establishConnection()); + } + + /** + * @return Pheanstalk + */ + private function establishConnection() + { + if (false == $this->connection) { + $this->connection = new Pheanstalk( + $this->config['host'], + $this->config['port'], + $this->config['timeout'], + $this->config['persisted'] + ); + } + + return $this->connection; + } + + /** + * @param string $dsn + * + * @return array + */ + private function parseDsn($dsn) + { + $dsnConfig = parse_url($dsn); + if (false === $dsnConfig) { + throw new \LogicException(sprintf('Failed to parse DSN "%s"', $dsn)); + } + + $dsnConfig = array_replace([ + 'scheme' => null, + 'host' => null, + 'port' => null, + 'user' => null, + 'pass' => null, + 'path' => null, + 'query' => null, + ], $dsnConfig); + + if ('beanstalk' !== $dsnConfig['scheme']) { + throw new \LogicException(sprintf('The given DSN scheme "%s" is not supported. Could be "beanstalk" only.', $dsnConfig['scheme'])); + } + + $query = []; + if ($dsnConfig['query']) { + parse_str($dsnConfig['query'], $query); + } + + return array_replace($query, [ + 'port' => $dsnConfig['port'], + 'host' => $dsnConfig['host'], + ]); + } + + /** + * @return array + */ + private function defaultConfig() + { + return [ + 'host' => 'localhost', + 'port' => Pheanstalk::DEFAULT_PORT, + 'timeout' => null, + 'persisted' => true, + ]; + } +} diff --git a/pkg/pheanstalk/PheanstalkConsumer.php b/pkg/pheanstalk/PheanstalkConsumer.php new file mode 100644 index 000000000..3a143c103 --- /dev/null +++ b/pkg/pheanstalk/PheanstalkConsumer.php @@ -0,0 +1,109 @@ +destination = $destination; + $this->pheanstalk = $pheanstalk; + } + + /** + * {@inheritdoc} + * + * @return PheanstalkDestination + */ + public function getQueue() + { + return $this->destination; + } + + /** + * {@inheritdoc} + * + * @return PheanstalkMessage|null + */ + public function receive($timeout = 0) + { + if ($job = $this->pheanstalk->reserveFromTube($this->destination->getName(), $timeout / 1000)) { + return $this->convertJobToMessage($job); + } + } + + /** + * {@inheritdoc} + * + * @return PheanstalkMessage|null + */ + public function receiveNoWait() + { + if ($job = $this->pheanstalk->reserveFromTube($this->destination->getName(), 0)) { + return $this->convertJobToMessage($job); + } + } + + /** + * {@inheritdoc} + * + * @param PheanstalkMessage $message + */ + public function acknowledge(PsrMessage $message) + { + InvalidMessageException::assertMessageInstanceOf($message, PheanstalkMessage::class); + + if (false == $message->getJob()) { + throw new \LogicException('The message could not be acknowledged because it does not have job set.'); + } + + $this->pheanstalk->delete($message->getJob()); + } + + /** + * {@inheritdoc} + * + * @param PheanstalkMessage $message + */ + public function reject(PsrMessage $message, $requeue = false) + { + $this->acknowledge($message); + + if ($requeue) { + $this->pheanstalk->release($message->getJob(), $message->getPriority(), $message->getDelay()); + } + } + + /** + * @param Job $job + * + * @return PheanstalkMessage + */ + private function convertJobToMessage(Job $job) + { + $message = PheanstalkMessage::jsonUnserialize($job->getData()); + $message->setJob($job); + + return $message; + } +} diff --git a/pkg/pheanstalk/PheanstalkContext.php b/pkg/pheanstalk/PheanstalkContext.php new file mode 100644 index 000000000..b40a72ede --- /dev/null +++ b/pkg/pheanstalk/PheanstalkContext.php @@ -0,0 +1,93 @@ +pheanstalk = $pheanstalk; + } + + /** + * {@inheritdoc} + */ + public function createMessage($body = '', array $properties = [], array $headers = []) + { + return new PheanstalkMessage($body, $properties, $headers); + } + + /** + * {@inheritdoc} + */ + public function createTopic($topicName) + { + return new PheanstalkDestination($topicName); + } + + /** + * {@inheritdoc} + */ + public function createQueue($queueName) + { + return new PheanstalkDestination($queueName); + } + + /** + * {@inheritdoc} + */ + public function createTemporaryQueue() + { + throw new \LogicException('Not implemented'); + } + + /** + * {@inheritdoc} + * + * @return PheanstalkProducer + */ + public function createProducer() + { + return new PheanstalkProducer($this->pheanstalk); + } + + /** + * {@inheritdoc} + * + * @param PheanstalkDestination $destination + * + * @return PheanstalkConsumer + */ + public function createConsumer(PsrDestination $destination) + { + InvalidDestinationException::assertDestinationInstanceOf($destination, PheanstalkDestination::class); + + return new PheanstalkConsumer($destination, $this->pheanstalk); + } + + public function close() + { + $this->pheanstalk->getConnection()->disconnect(); + } + + /** + * @return Pheanstalk + */ + public function getPheanstalk() + { + return $this->pheanstalk; + } +} diff --git a/pkg/pheanstalk/PheanstalkDestination.php b/pkg/pheanstalk/PheanstalkDestination.php new file mode 100644 index 000000000..1ed29508f --- /dev/null +++ b/pkg/pheanstalk/PheanstalkDestination.php @@ -0,0 +1,46 @@ +destinationName = $destinationName; + } + + /** + * @return string + */ + public function getName() + { + return $this->destinationName; + } + + /** + * {@inheritdoc} + */ + public function getQueueName() + { + return $this->getName(); + } + + /** + * {@inheritdoc} + */ + public function getTopicName() + { + return $this->getName(); + } +} diff --git a/pkg/pheanstalk/PheanstalkMessage.php b/pkg/pheanstalk/PheanstalkMessage.php new file mode 100644 index 000000000..2ce6d0685 --- /dev/null +++ b/pkg/pheanstalk/PheanstalkMessage.php @@ -0,0 +1,305 @@ +body = $body; + $this->properties = $properties; + $this->headers = $headers; + $this->redelivered = false; + } + + /** + * @param string $body + */ + public function setBody($body) + { + $this->body = $body; + } + + /** + * {@inheritdoc} + */ + public function getBody() + { + return $this->body; + } + + /** + * @param array $properties + */ + public function setProperties(array $properties) + { + $this->properties = $properties; + } + + /** + * {@inheritdoc} + */ + public function getProperties() + { + return $this->properties; + } + + /** + * {@inheritdoc} + */ + public function setProperty($name, $value) + { + $this->properties[$name] = $value; + } + + /** + * {@inheritdoc} + */ + public function getProperty($name, $default = null) + { + return array_key_exists($name, $this->properties) ? $this->properties[$name] : $default; + } + + /** + * @param array $headers + */ + public function setHeaders(array $headers) + { + $this->headers = $headers; + } + + /** + * {@inheritdoc} + */ + public function getHeaders() + { + return $this->headers; + } + + /** + * {@inheritdoc} + */ + public function setHeader($name, $value) + { + $this->headers[$name] = $value; + } + + /** + * {@inheritdoc} + */ + public function getHeader($name, $default = null) + { + return array_key_exists($name, $this->headers) ? $this->headers[$name] : $default; + } + + /** + * @return bool + */ + public function isRedelivered() + { + return $this->redelivered; + } + + /** + * @param bool $redelivered + */ + public function setRedelivered($redelivered) + { + $this->redelivered = $redelivered; + } + + /** + * {@inheritdoc} + */ + public function setCorrelationId($correlationId) + { + $this->setHeader('correlation_id', (string) $correlationId); + } + + /** + * {@inheritdoc} + */ + public function getCorrelationId() + { + return $this->getHeader('correlation_id'); + } + + /** + * {@inheritdoc} + */ + public function setMessageId($messageId) + { + $this->setHeader('message_id', (string) $messageId); + } + + /** + * {@inheritdoc} + */ + public function getMessageId() + { + return $this->getHeader('message_id'); + } + + /** + * {@inheritdoc} + */ + public function getTimestamp() + { + $value = $this->getHeader('timestamp'); + + return $value === null ? null : (int) $value; + } + + /** + * {@inheritdoc} + */ + public function setTimestamp($timestamp) + { + $this->setHeader('timestamp', $timestamp); + } + + /** + * @param string|null $replyTo + */ + public function setReplyTo($replyTo) + { + $this->setHeader('reply_to', $replyTo); + } + + /** + * @return string|null + */ + public function getReplyTo() + { + return $this->getHeader('reply_to'); + } + + /** + * @param int $time + */ + public function setTimeToRun($time) + { + $this->setHeader('ttr', $time); + } + + /** + * @return int + */ + public function getTimeToRun() + { + return $this->getHeader('ttr', Pheanstalk::DEFAULT_TTR); + } + + /** + * @param int $priority + */ + public function setPriority($priority) + { + $this->setHeader('priority', $priority); + } + + /** + * @return int + */ + public function getPriority() + { + return $this->getHeader('priority', Pheanstalk::DEFAULT_PRIORITY); + } + + /** + * @param int $delay + */ + public function setDelay($delay) + { + $this->setHeader('delay', $delay); + } + + /** + * @return int + */ + public function getDelay() + { + return $this->getHeader('delay', Pheanstalk::DEFAULT_DELAY); + } + + /** + * {@inheritdoc} + */ + public function jsonSerialize() + { + return [ + 'body' => $this->getBody(), + 'properties' => $this->getProperties(), + 'headers' => $this->getHeaders(), + ]; + } + + /** + * @param string $json + * + * @return PheanstalkMessage + */ + public static function jsonUnserialize($json) + { + $data = json_decode($json, true); + if (JSON_ERROR_NONE !== json_last_error()) { + throw new \InvalidArgumentException(sprintf( + 'The malformed json given. Error %s and message %s', + json_last_error(), + json_last_error_msg() + )); + } + + return new self($data['body'], $data['properties'], $data['headers']); + } + + /** + * @return Job + */ + public function getJob() + { + return $this->job; + } + + /** + * @param Job $job + */ + public function setJob(Job $job) + { + $this->job = $job; + } +} diff --git a/pkg/pheanstalk/PheanstalkProducer.php b/pkg/pheanstalk/PheanstalkProducer.php new file mode 100644 index 000000000..7920ae9c2 --- /dev/null +++ b/pkg/pheanstalk/PheanstalkProducer.php @@ -0,0 +1,54 @@ +pheanstalk = $pheanstalk; + } + + /** + * {@inheritdoc} + * + * @param PheanstalkDestination $destination + * @param PheanstalkMessage $message + */ + public function send(PsrDestination $destination, PsrMessage $message) + { + InvalidDestinationException::assertDestinationInstanceOf($destination, PheanstalkDestination::class); + InvalidMessageException::assertMessageInstanceOf($message, PheanstalkMessage::class); + + $rawMessage = json_encode($message); + if (JSON_ERROR_NONE !== json_last_error()) { + throw new \InvalidArgumentException(sprintf( + 'Could not encode value into json. Error %s and message %s', + json_last_error(), + json_last_error_msg() + )); + } + + $this->pheanstalk->useTube($destination->getName())->put( + $rawMessage, + $message->getPriority(), + $message->getDelay(), + $message->getTimeToRun() + ); + } +} diff --git a/pkg/pheanstalk/README.md b/pkg/pheanstalk/README.md new file mode 100644 index 000000000..48a83505f --- /dev/null +++ b/pkg/pheanstalk/README.md @@ -0,0 +1,26 @@ +# Beanstalk Transport + +[![Gitter](https://badges.gitter.im/php-enqueue/Lobby.svg)](https://gitter.im/php-enqueue/Lobby) +[![Build Status](https://travis-ci.org/php-enqueue/pheanstalk.png?branch=master)](https://travis-ci.org/php-enqueue/pheanstalk) +[![Total Downloads](https://poser.pugx.org/enqueue/pheanstalk/d/total.png)](https://packagist.org/packages/enqueue/pheanstalk) +[![Latest Stable Version](https://poser.pugx.org/enqueue/pheanstalk/version.png)](https://packagist.org/packages/enqueue/pheanstalk) + +This is an implementation of the queue specification. It allows you to send and consume message from Beanstalkd broker. + +## Resources + +* [Documentation](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/index.md) +* [Questions](https://gitter.im/php-enqueue/Lobby) +* [Issue Tracker](https://github.com/php-enqueue/enqueue-dev/issues) + +## Developed by Forma-Pro + +Forma-Pro is a full stack development company which interests also spread to open source development. +Being a team of strong professionals we have an aim an ability to help community by developing cutting edge solutions in the areas of e-commerce, docker & microservice oriented architecture where we have accumulated a huge many-years experience. +Our main specialization is Symfony framework based solution, but we are always looking to the technologies that allow us to do our job the best way. We are committed to creating solutions that revolutionize the way how things are developed in aspects of architecture & scalability. + +If you have any questions and inquires about our open source development, this product particularly or any other matter feel free to contact at opensource@forma-pro.com + +## License + +It is released under the [MIT License](LICENSE). \ No newline at end of file diff --git a/pkg/pheanstalk/Tests/PheanstalkConnectionFactoryConfigTest.php b/pkg/pheanstalk/Tests/PheanstalkConnectionFactoryConfigTest.php new file mode 100644 index 000000000..0859a2386 --- /dev/null +++ b/pkg/pheanstalk/Tests/PheanstalkConnectionFactoryConfigTest.php @@ -0,0 +1,137 @@ +expectException(\LogicException::class); + $this->expectExceptionMessage('The config must be either an array of options, a DSN string or null'); + + new PheanstalkConnectionFactory(new \stdClass()); + } + + public function testThrowIfSchemeIsNotBeanstalkAmqp() + { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('The given DSN scheme "http" is not supported. Could be "beanstalk" only.'); + + new PheanstalkConnectionFactory('http://example.com'); + } + + public function testThrowIfDsnCouldNotBeParsed() + { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Failed to parse DSN "beanstalk://:@/"'); + + new PheanstalkConnectionFactory('beanstalk://:@/'); + } + + /** + * @dataProvider provideConfigs + * + * @param mixed $config + * @param mixed $expectedConfig + */ + public function testShouldParseConfigurationAsExpected($config, $expectedConfig) + { + $factory = new PheanstalkConnectionFactory($config); + + $this->assertAttributeEquals($expectedConfig, 'config', $factory); + } + + public static function provideConfigs() + { + yield [ + null, + [ + 'host' => 'localhost', + 'port' => 11300, + 'timeout' => null, + 'persisted' => true, + ], + ]; + + // some examples from Appendix A: Examples (https://www.rabbitmq.com/uri-spec.html) + + yield [ + 'beanstalk://', + [ + 'host' => 'localhost', + 'port' => 11300, + 'timeout' => null, + 'persisted' => true, + ], + ]; + + yield [ + [], + [ + 'host' => 'localhost', + 'port' => 11300, + 'timeout' => null, + 'persisted' => true, + ], + ]; + + yield [ + 'beanstalk://theHost:1234', + [ + 'host' => 'theHost', + 'port' => 1234, + 'timeout' => null, + 'persisted' => true, + ], + ]; + + yield [ + ['host' => 'theHost', 'port' => 1234], + [ + 'host' => 'theHost', + 'port' => 1234, + 'timeout' => null, + 'persisted' => true, + ], + ]; + + yield [ + ['host' => 'theHost'], + [ + 'host' => 'theHost', + 'port' => 11300, + 'timeout' => null, + 'persisted' => true, + ], + ]; + + yield [ + ['host' => 'theHost', 'timeout' => 123], + [ + 'host' => 'theHost', + 'port' => 11300, + 'timeout' => 123, + 'persisted' => true, + ], + ]; + + yield [ + 'beanstalk://theHost:1234?timeout=123&persisted=1', + [ + 'host' => 'theHost', + 'port' => 1234, + 'timeout' => 123, + 'persisted' => 1, + ], + ]; + } +} diff --git a/pkg/pheanstalk/Tests/Spec/PheanstalkConnectionFactoryTest.php b/pkg/pheanstalk/Tests/Spec/PheanstalkConnectionFactoryTest.php new file mode 100644 index 000000000..525ed38aa --- /dev/null +++ b/pkg/pheanstalk/Tests/Spec/PheanstalkConnectionFactoryTest.php @@ -0,0 +1,17 @@ +createMock(Pheanstalk::class)); + } +} diff --git a/pkg/pheanstalk/Tests/Spec/PheanstalkMessageTest.php b/pkg/pheanstalk/Tests/Spec/PheanstalkMessageTest.php new file mode 100644 index 000000000..ac65e26c3 --- /dev/null +++ b/pkg/pheanstalk/Tests/Spec/PheanstalkMessageTest.php @@ -0,0 +1,17 @@ +createContext(); + } + + /** + * @param PsrContext $context + * @param string $queueName + * + * @return PsrQueue + */ + protected function createQueue(PsrContext $context, $queueName) + { + return $context->createQueue($queueName.time()); + } +} diff --git a/pkg/pheanstalk/Tests/Spec/PheanstalkSendToAndReceiveFromTopicTest.php b/pkg/pheanstalk/Tests/Spec/PheanstalkSendToAndReceiveFromTopicTest.php new file mode 100644 index 000000000..2714a6d53 --- /dev/null +++ b/pkg/pheanstalk/Tests/Spec/PheanstalkSendToAndReceiveFromTopicTest.php @@ -0,0 +1,34 @@ +createContext(); + } + + /** + * @param PsrContext $context + * @param string $topicName + * + * @return PsrTopic + */ + protected function createTopic(PsrContext $context, $topicName) + { + return $context->createTopic($topicName.time()); + } +} diff --git a/pkg/pheanstalk/Tests/Spec/PheanstalkSendToAndReceiveNoWaitFromQueueTest.php b/pkg/pheanstalk/Tests/Spec/PheanstalkSendToAndReceiveNoWaitFromQueueTest.php new file mode 100644 index 000000000..072b74230 --- /dev/null +++ b/pkg/pheanstalk/Tests/Spec/PheanstalkSendToAndReceiveNoWaitFromQueueTest.php @@ -0,0 +1,30 @@ +createContext(); + } + + /** + * {@inheritdoc} + */ + protected function createQueue(PsrContext $context, $queueName) + { + return $context->createQueue($queueName.time()); + } +} diff --git a/pkg/pheanstalk/Tests/Spec/PheanstalkSendToAndReceiveNoWaitFromTopicTest.php b/pkg/pheanstalk/Tests/Spec/PheanstalkSendToAndReceiveNoWaitFromTopicTest.php new file mode 100644 index 000000000..9f4b35a6d --- /dev/null +++ b/pkg/pheanstalk/Tests/Spec/PheanstalkSendToAndReceiveNoWaitFromTopicTest.php @@ -0,0 +1,34 @@ +createContext(); + } + + /** + * @param PsrContext $context + * @param string $topicName + * + * @return PsrTopic + */ + protected function createTopic(PsrContext $context, $topicName) + { + return $context->createTopic($topicName.time()); + } +} diff --git a/pkg/pheanstalk/Tests/Spec/PheanstalkSendToTopicAndReceiveFromQueueTest.php b/pkg/pheanstalk/Tests/Spec/PheanstalkSendToTopicAndReceiveFromQueueTest.php new file mode 100644 index 000000000..a81aa4344 --- /dev/null +++ b/pkg/pheanstalk/Tests/Spec/PheanstalkSendToTopicAndReceiveFromQueueTest.php @@ -0,0 +1,45 @@ +time = time(); + } + + /** + * {@inheritdoc} + */ + protected function createContext() + { + $factory = new PheanstalkConnectionFactory(getenv('BEANSTALKD_DSN')); + + return $factory->createContext(); + } + + /** + * {@inheritdoc} + */ + protected function createQueue(PsrContext $context, $queueName) + { + return $context->createQueue($queueName.$this->time); + } + + /** + * {@inheritdoc} + */ + protected function createTopic(PsrContext $context, $topicName) + { + return $context->createTopic($topicName.$this->time); + } +} diff --git a/pkg/pheanstalk/Tests/Spec/PheanstalkSendToTopicAndReceiveNoWaitFromQueueTest.php b/pkg/pheanstalk/Tests/Spec/PheanstalkSendToTopicAndReceiveNoWaitFromQueueTest.php new file mode 100644 index 000000000..efbe93e1d --- /dev/null +++ b/pkg/pheanstalk/Tests/Spec/PheanstalkSendToTopicAndReceiveNoWaitFromQueueTest.php @@ -0,0 +1,45 @@ +time = time(); + } + + /** + * {@inheritdoc} + */ + protected function createContext() + { + $factory = new PheanstalkConnectionFactory(getenv('BEANSTALKD_DSN')); + + return $factory->createContext(); + } + + /** + * {@inheritdoc} + */ + protected function createQueue(PsrContext $context, $queueName) + { + return $context->createQueue($queueName.$this->time); + } + + /** + * {@inheritdoc} + */ + protected function createTopic(PsrContext $context, $topicName) + { + return $context->createTopic($topicName.$this->time); + } +} diff --git a/pkg/pheanstalk/Tests/Spec/PheanstalkTopicTest.php b/pkg/pheanstalk/Tests/Spec/PheanstalkTopicTest.php new file mode 100644 index 000000000..407e22f67 --- /dev/null +++ b/pkg/pheanstalk/Tests/Spec/PheanstalkTopicTest.php @@ -0,0 +1,17 @@ +=5.6", + "pda/pheanstalk": "^3", + "enqueue/psr-queue": "^0.5@dev" + }, + "require-dev": { + "phpunit/phpunit": "~5.4.0", + "enqueue/test": "^0.5@dev", + "enqueue/enqueue": "^0.5@dev", + "enqueue/null": "^0.5@dev", + "symfony/dependency-injection": "^2.8|^3", + "symfony/config": "^2.8|^3" + }, + "autoload": { + "psr-4": { "Enqueue\\Pheanstalk\\": "" }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "suggest": { + "enqueue/enqueue": "If you'd like to use advanced features like Client abstract layer or Symfony integration features" + }, + "minimum-stability": "dev", + "extra": { + "branch-alias": { + "dev-master": "0.5.x-dev" + } + } +} diff --git a/pkg/pheanstalk/phpunit.xml.dist b/pkg/pheanstalk/phpunit.xml.dist new file mode 100644 index 000000000..4dca142e1 --- /dev/null +++ b/pkg/pheanstalk/phpunit.xml.dist @@ -0,0 +1,30 @@ + + + + + + + ./Tests + + + + + + . + + ./vendor + ./Tests + + + + diff --git a/pkg/psr-queue/Spec/PsrConnectionFactorySpec.php b/pkg/psr-queue/Spec/PsrConnectionFactorySpec.php new file mode 100644 index 000000000..db37389c8 --- /dev/null +++ b/pkg/psr-queue/Spec/PsrConnectionFactorySpec.php @@ -0,0 +1,27 @@ +assertInstanceOf(PsrConnectionFactory::class, $this->createConnectionFactory()); + } + + public function testShouldReturnContextOnCreateContextMethodCall() + { + $factory = $this->createConnectionFactory(); + + $this->assertInstanceOf(PsrContext::class, $factory->createContext()); + } + + /** + * @return PsrConnectionFactory + */ + abstract protected function createConnectionFactory(); +} diff --git a/pkg/psr-queue/Spec/PsrContextSpec.php b/pkg/psr-queue/Spec/PsrContextSpec.php new file mode 100644 index 000000000..40a69553d --- /dev/null +++ b/pkg/psr-queue/Spec/PsrContextSpec.php @@ -0,0 +1,86 @@ +assertInstanceOf(PsrContext::class, $this->createContext()); + } + + public function testShouldCreateEmptyMessageOnCreateMessageMethodCallWithoutArguments() + { + $context = $this->createContext(); + + $message = $context->createMessage(); + + $this->assertInstanceOf(PsrMessage::class, $message); + $this->assertSame('', $message->getBody()); + $this->assertSame([], $message->getHeaders()); + $this->assertSame([], $message->getProperties()); + } + + public function testShouldCreateMessageOnCreateMessageMethodCallWithArguments() + { + $context = $this->createContext(); + + $message = $context->createMessage('theBody', ['foo' => 'fooVal'], ['bar' => 'barVal']); + + $this->assertInstanceOf(PsrMessage::class, $message); + $this->assertSame('theBody', $message->getBody()); + $this->assertSame(['bar' => 'barVal'], $message->getHeaders()); + $this->assertSame(['foo' => 'fooVal'], $message->getProperties()); + } + + public function testShouldCreateTopicWithGivenName() + { + $context = $this->createContext(); + + $topic = $context->createTopic('theName'); + + $this->assertInstanceOf(PsrTopic::class, $topic); + $this->assertSame('theName', $topic->getTopicName()); + } + + public function testShouldCreateQueueWithGivenName() + { + $context = $this->createContext(); + + $topic = $context->createTopic('theName'); + + $this->assertInstanceOf(PsrQueue::class, $topic); + $this->assertSame('theName', $topic->getTopicName()); + } + + public function testShouldCreateProducerOnCreateProducerMethodCall() + { + $context = $this->createContext(); + + $producer = $context->createProducer(); + + $this->assertInstanceOf(PsrProducer::class, $producer); + } + + public function testShouldCreateConsumerOnCreateConsumerMethodCall() + { + $context = $this->createContext(); + + $consumer = $context->createConsumer($context->createQueue('aQueue')); + + $this->assertInstanceOf(PsrConsumer::class, $consumer); + } + + /** + * @return PsrContext + */ + abstract protected function createContext(); +} diff --git a/pkg/psr-queue/Spec/PsrQueueSpec.php b/pkg/psr-queue/Spec/PsrQueueSpec.php new file mode 100644 index 000000000..ad98a986d --- /dev/null +++ b/pkg/psr-queue/Spec/PsrQueueSpec.php @@ -0,0 +1,28 @@ +assertInstanceOf(PsrQueue::class, $this->createQueue()); + } + + public function testShouldReturnQueueName() + { + $queue = $this->createQueue(); + + $this->assertSame(self::EXPECTED_QUEUE_NAME, $queue->getQueueName()); + } + + /** + * @return PsrQueue + */ + abstract protected function createQueue(); +} diff --git a/pkg/psr-queue/Spec/PsrTopicSpec.php b/pkg/psr-queue/Spec/PsrTopicSpec.php new file mode 100644 index 000000000..1ac1780c6 --- /dev/null +++ b/pkg/psr-queue/Spec/PsrTopicSpec.php @@ -0,0 +1,28 @@ +assertInstanceOf(PsrTopic::class, $this->createTopic()); + } + + public function testShouldReturnTopicName() + { + $topic = $this->createTopic(); + + $this->assertSame(self::EXPECTED_TOPIC_NAME, $topic->getTopicName()); + } + + /** + * @return PsrTopic + */ + abstract protected function createTopic(); +} diff --git a/pkg/psr-queue/Spec/SendToAndReceiveFromQueueSpec.php b/pkg/psr-queue/Spec/SendToAndReceiveFromQueueSpec.php new file mode 100644 index 000000000..8708dc15f --- /dev/null +++ b/pkg/psr-queue/Spec/SendToAndReceiveFromQueueSpec.php @@ -0,0 +1,51 @@ +createContext(); + $queue = $this->createQueue($context, 'send_to_and_receive_from_queue_spec'); + + $expectedBody = __CLASS__.time(); + + $context->createProducer()->send($queue, $context->createMessage($expectedBody)); + $context->close(); + + $context = $this->createContext(); + $consumer = $context->createConsumer($queue); + + $message = $consumer->receive(2000); // 2 sec + + $this->assertInstanceOf(PsrMessage::class, $message); + $consumer->acknowledge($message); + + $this->assertSame($expectedBody, $message->getBody()); + } + + /** + * @return PsrContext + */ + abstract protected function createContext(); + + /** + * @param PsrContext $context + * @param string $queueName + * + * @return PsrQueue + */ + protected function createQueue(PsrContext $context, $queueName) + { + return $context->createQueue($queueName); + } +} diff --git a/pkg/psr-queue/Spec/SendToAndReceiveFromTopicSpec.php b/pkg/psr-queue/Spec/SendToAndReceiveFromTopicSpec.php new file mode 100644 index 000000000..b830eaf9d --- /dev/null +++ b/pkg/psr-queue/Spec/SendToAndReceiveFromTopicSpec.php @@ -0,0 +1,51 @@ +createContext(); + $topic = $this->createTopic($context, 'send_to_and_receive_from_topic_spec'); + + $expectedBody = __CLASS__.time(); + + $context->createProducer()->send($topic, $context->createMessage($expectedBody)); +// $context->close(); +// +// $context = $this->createContext(); + $consumer = $context->createConsumer($topic); + + $message = $consumer->receive(2000); // 2 sec + + $this->assertInstanceOf(PsrMessage::class, $message); + $consumer->acknowledge($message); + + $this->assertSame($expectedBody, $message->getBody()); + } + + /** + * @return PsrContext + */ + abstract protected function createContext(); + + /** + * @param PsrContext $context + * @param string $topicName + * + * @return PsrTopic + */ + protected function createTopic(PsrContext $context, $topicName) + { + return $context->createTopic($topicName); + } +} diff --git a/pkg/psr-queue/Spec/SendToAndReceiveNoWaitFromQueueSpec.php b/pkg/psr-queue/Spec/SendToAndReceiveNoWaitFromQueueSpec.php new file mode 100644 index 000000000..01490df34 --- /dev/null +++ b/pkg/psr-queue/Spec/SendToAndReceiveNoWaitFromQueueSpec.php @@ -0,0 +1,54 @@ +createContext(); + $queue = $this->createQueue($context, 'send_to_and_receive_no_wait_from_queue_spec'); + + $expectedBody = __CLASS__.time(); + + $context->createProducer()->send($queue, $context->createMessage($expectedBody)); +// $context->close(); +// +// $context = $this->createContext(); + $consumer = $context->createConsumer($queue); + + $startTime = microtime(true); + $message = $consumer->receiveNoWait(); + + $this->assertLessThan(2, microtime(true) - $startTime); + + $this->assertInstanceOf(PsrMessage::class, $message); + $consumer->acknowledge($message); + + $this->assertSame($expectedBody, $message->getBody()); + } + + /** + * @return PsrContext + */ + abstract protected function createContext(); + + /** + * @param PsrContext $context + * @param string $queueName + * + * @return PsrQueue + */ + protected function createQueue(PsrContext $context, $queueName) + { + return $context->createQueue($queueName); + } +} diff --git a/pkg/psr-queue/Spec/SendToAndReceiveNoWaitFromTopicSpec.php b/pkg/psr-queue/Spec/SendToAndReceiveNoWaitFromTopicSpec.php new file mode 100644 index 000000000..f56d4d997 --- /dev/null +++ b/pkg/psr-queue/Spec/SendToAndReceiveNoWaitFromTopicSpec.php @@ -0,0 +1,54 @@ +createContext(); + $topic = $this->createTopic($context, 'send_to_and_receive_no_wait_from_topic_spec'); + + $expectedBody = __CLASS__.time(); + + $context->createProducer()->send($topic, $context->createMessage($expectedBody)); +// $context->close(); +// +// $context = $this->createContext(); + $consumer = $context->createConsumer($topic); + + $startTime = microtime(true); + $message = $consumer->receiveNoWait(); + + $this->assertLessThan(2, microtime(true) - $startTime); + + $this->assertInstanceOf(PsrMessage::class, $message); + $consumer->acknowledge($message); + + $this->assertSame($expectedBody, $message->getBody()); + } + + /** + * @return PsrContext + */ + abstract protected function createContext(); + + /** + * @param PsrContext $context + * @param string $topicName + * + * @return PsrTopic + */ + protected function createTopic(PsrContext $context, $topicName) + { + return $context->createTopic($topicName); + } +} diff --git a/pkg/psr-queue/Spec/SendToTopicAndReceiveFromQueueSpec.php b/pkg/psr-queue/Spec/SendToTopicAndReceiveFromQueueSpec.php new file mode 100644 index 000000000..c7155ec04 --- /dev/null +++ b/pkg/psr-queue/Spec/SendToTopicAndReceiveFromQueueSpec.php @@ -0,0 +1,64 @@ +createContext(); + $topic = $this->createTopic($context, 'send_to_topic_and_receive_from_queue_spec'); + $queue = $this->createQueue($context, 'send_to_topic_and_receive_from_queue_spec'); + + $expectedBody = __CLASS__.time(); + + $context->createProducer()->send($topic, $context->createMessage($expectedBody)); +// $context->close(); +// +// $context = $this->createContext(); + $consumer = $context->createConsumer($queue); + + $message = $consumer->receive(2000); // 2 sec + + $this->assertInstanceOf(PsrMessage::class, $message); + $consumer->acknowledge($message); + + $this->assertSame($expectedBody, $message->getBody()); + } + + /** + * @return PsrContext + */ + abstract protected function createContext(); + + /** + * @param PsrContext $context + * @param string $queueName + * + * @return PsrQueue + */ + protected function createQueue(PsrContext $context, $queueName) + { + return $context->createQueue($queueName); + } + + /** + * @param PsrContext $context + * @param string $topicName + * + * @return PsrTopic + */ + protected function createTopic(PsrContext $context, $topicName) + { + return $context->createTopic($topicName); + } +} diff --git a/pkg/psr-queue/Spec/SendToTopicAndReceiveNoWaitFromQueueSpec.php b/pkg/psr-queue/Spec/SendToTopicAndReceiveNoWaitFromQueueSpec.php new file mode 100644 index 000000000..44cab4516 --- /dev/null +++ b/pkg/psr-queue/Spec/SendToTopicAndReceiveNoWaitFromQueueSpec.php @@ -0,0 +1,67 @@ +createContext(); + $topic = $this->createTopic($context, 'send_to_topic_and_receive_from_queue_spec'); + $queue = $this->createQueue($context, 'send_to_topic_and_receive_from_queue_spec'); + + $expectedBody = __CLASS__.time(); + + $context->createProducer()->send($topic, $context->createMessage($expectedBody)); +// $context->close(); +// +// $context = $this->createContext(); + $consumer = $context->createConsumer($queue); + + $startTime = microtime(true); + $message = $consumer->receiveNoWait(); + + $this->assertLessThan(2, microtime(true) - $startTime); + + $this->assertInstanceOf(PsrMessage::class, $message); + $consumer->acknowledge($message); + + $this->assertSame($expectedBody, $message->getBody()); + } + + /** + * @return PsrContext + */ + abstract protected function createContext(); + + /** + * @param PsrContext $context + * @param string $queueName + * + * @return PsrQueue + */ + protected function createQueue(PsrContext $context, $queueName) + { + return $context->createQueue($queueName); + } + + /** + * @param PsrContext $context + * @param string $topicName + * + * @return PsrTopic + */ + protected function createTopic(PsrContext $context, $topicName) + { + return $context->createTopic($topicName); + } +} From 455ac47dc4089395d6d570ff401c146e4bda3c11 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Fri, 23 Jun 2017 20:35:51 +0300 Subject: [PATCH 09/20] [beanstalk] improve tests. --- ...heanstalkSendToAndReceiveFromTopicTest.php | 34 ------------------- ...alkSendToAndReceiveNoWaitFromTopicTest.php | 34 ------------------- .../Spec/SendToAndReceiveFromQueueSpec.php | 9 ++--- .../Spec/SendToAndReceiveFromTopicSpec.php | 9 ++--- .../SendToAndReceiveNoWaitFromQueueSpec.php | 9 ++--- .../SendToAndReceiveNoWaitFromTopicSpec.php | 9 ++--- .../SendToTopicAndReceiveFromQueueSpec.php | 9 ++--- ...ndToTopicAndReceiveNoWaitFromQueueSpec.php | 9 ++--- 8 files changed, 30 insertions(+), 92 deletions(-) delete mode 100644 pkg/pheanstalk/Tests/Spec/PheanstalkSendToAndReceiveFromTopicTest.php delete mode 100644 pkg/pheanstalk/Tests/Spec/PheanstalkSendToAndReceiveNoWaitFromTopicTest.php diff --git a/pkg/pheanstalk/Tests/Spec/PheanstalkSendToAndReceiveFromTopicTest.php b/pkg/pheanstalk/Tests/Spec/PheanstalkSendToAndReceiveFromTopicTest.php deleted file mode 100644 index 2714a6d53..000000000 --- a/pkg/pheanstalk/Tests/Spec/PheanstalkSendToAndReceiveFromTopicTest.php +++ /dev/null @@ -1,34 +0,0 @@ -createContext(); - } - - /** - * @param PsrContext $context - * @param string $topicName - * - * @return PsrTopic - */ - protected function createTopic(PsrContext $context, $topicName) - { - return $context->createTopic($topicName.time()); - } -} diff --git a/pkg/pheanstalk/Tests/Spec/PheanstalkSendToAndReceiveNoWaitFromTopicTest.php b/pkg/pheanstalk/Tests/Spec/PheanstalkSendToAndReceiveNoWaitFromTopicTest.php deleted file mode 100644 index 9f4b35a6d..000000000 --- a/pkg/pheanstalk/Tests/Spec/PheanstalkSendToAndReceiveNoWaitFromTopicTest.php +++ /dev/null @@ -1,34 +0,0 @@ -createContext(); - } - - /** - * @param PsrContext $context - * @param string $topicName - * - * @return PsrTopic - */ - protected function createTopic(PsrContext $context, $topicName) - { - return $context->createTopic($topicName.time()); - } -} diff --git a/pkg/psr-queue/Spec/SendToAndReceiveFromQueueSpec.php b/pkg/psr-queue/Spec/SendToAndReceiveFromQueueSpec.php index 8708dc15f..b6366de73 100644 --- a/pkg/psr-queue/Spec/SendToAndReceiveFromQueueSpec.php +++ b/pkg/psr-queue/Spec/SendToAndReceiveFromQueueSpec.php @@ -17,13 +17,14 @@ public function test() $context = $this->createContext(); $queue = $this->createQueue($context, 'send_to_and_receive_from_queue_spec'); + $consumer = $context->createConsumer($queue); + + // guard + $this->assertNull($consumer->receiveNoWait()); + $expectedBody = __CLASS__.time(); $context->createProducer()->send($queue, $context->createMessage($expectedBody)); - $context->close(); - - $context = $this->createContext(); - $consumer = $context->createConsumer($queue); $message = $consumer->receive(2000); // 2 sec diff --git a/pkg/psr-queue/Spec/SendToAndReceiveFromTopicSpec.php b/pkg/psr-queue/Spec/SendToAndReceiveFromTopicSpec.php index b830eaf9d..ccc4dfa45 100644 --- a/pkg/psr-queue/Spec/SendToAndReceiveFromTopicSpec.php +++ b/pkg/psr-queue/Spec/SendToAndReceiveFromTopicSpec.php @@ -17,13 +17,14 @@ public function test() $context = $this->createContext(); $topic = $this->createTopic($context, 'send_to_and_receive_from_topic_spec'); + $consumer = $context->createConsumer($topic); + + // guard + $this->assertNull($consumer->receiveNoWait()); + $expectedBody = __CLASS__.time(); $context->createProducer()->send($topic, $context->createMessage($expectedBody)); -// $context->close(); -// -// $context = $this->createContext(); - $consumer = $context->createConsumer($topic); $message = $consumer->receive(2000); // 2 sec diff --git a/pkg/psr-queue/Spec/SendToAndReceiveNoWaitFromQueueSpec.php b/pkg/psr-queue/Spec/SendToAndReceiveNoWaitFromQueueSpec.php index 01490df34..ee27b3df7 100644 --- a/pkg/psr-queue/Spec/SendToAndReceiveNoWaitFromQueueSpec.php +++ b/pkg/psr-queue/Spec/SendToAndReceiveNoWaitFromQueueSpec.php @@ -17,13 +17,14 @@ public function test() $context = $this->createContext(); $queue = $this->createQueue($context, 'send_to_and_receive_no_wait_from_queue_spec'); + $consumer = $context->createConsumer($queue); + + // guard + $this->assertNull($consumer->receiveNoWait()); + $expectedBody = __CLASS__.time(); $context->createProducer()->send($queue, $context->createMessage($expectedBody)); -// $context->close(); -// -// $context = $this->createContext(); - $consumer = $context->createConsumer($queue); $startTime = microtime(true); $message = $consumer->receiveNoWait(); diff --git a/pkg/psr-queue/Spec/SendToAndReceiveNoWaitFromTopicSpec.php b/pkg/psr-queue/Spec/SendToAndReceiveNoWaitFromTopicSpec.php index f56d4d997..10af61596 100644 --- a/pkg/psr-queue/Spec/SendToAndReceiveNoWaitFromTopicSpec.php +++ b/pkg/psr-queue/Spec/SendToAndReceiveNoWaitFromTopicSpec.php @@ -17,13 +17,14 @@ public function test() $context = $this->createContext(); $topic = $this->createTopic($context, 'send_to_and_receive_no_wait_from_topic_spec'); + $consumer = $context->createConsumer($topic); + + // guard + $this->assertNull($consumer->receiveNoWait()); + $expectedBody = __CLASS__.time(); $context->createProducer()->send($topic, $context->createMessage($expectedBody)); -// $context->close(); -// -// $context = $this->createContext(); - $consumer = $context->createConsumer($topic); $startTime = microtime(true); $message = $consumer->receiveNoWait(); diff --git a/pkg/psr-queue/Spec/SendToTopicAndReceiveFromQueueSpec.php b/pkg/psr-queue/Spec/SendToTopicAndReceiveFromQueueSpec.php index c7155ec04..851857693 100644 --- a/pkg/psr-queue/Spec/SendToTopicAndReceiveFromQueueSpec.php +++ b/pkg/psr-queue/Spec/SendToTopicAndReceiveFromQueueSpec.php @@ -19,13 +19,14 @@ public function test() $topic = $this->createTopic($context, 'send_to_topic_and_receive_from_queue_spec'); $queue = $this->createQueue($context, 'send_to_topic_and_receive_from_queue_spec'); + $consumer = $context->createConsumer($queue); + + // guard + $this->assertNull($consumer->receiveNoWait()); + $expectedBody = __CLASS__.time(); $context->createProducer()->send($topic, $context->createMessage($expectedBody)); -// $context->close(); -// -// $context = $this->createContext(); - $consumer = $context->createConsumer($queue); $message = $consumer->receive(2000); // 2 sec diff --git a/pkg/psr-queue/Spec/SendToTopicAndReceiveNoWaitFromQueueSpec.php b/pkg/psr-queue/Spec/SendToTopicAndReceiveNoWaitFromQueueSpec.php index 44cab4516..e0670c77c 100644 --- a/pkg/psr-queue/Spec/SendToTopicAndReceiveNoWaitFromQueueSpec.php +++ b/pkg/psr-queue/Spec/SendToTopicAndReceiveNoWaitFromQueueSpec.php @@ -19,13 +19,14 @@ public function test() $topic = $this->createTopic($context, 'send_to_topic_and_receive_from_queue_spec'); $queue = $this->createQueue($context, 'send_to_topic_and_receive_from_queue_spec'); + $consumer = $context->createConsumer($queue); + + // guard + $this->assertNull($consumer->receiveNoWait()); + $expectedBody = __CLASS__.time(); $context->createProducer()->send($topic, $context->createMessage($expectedBody)); -// $context->close(); -// -// $context = $this->createContext(); - $consumer = $context->createConsumer($queue); $startTime = microtime(true); $message = $consumer->receiveNoWait(); From 2c32585b6ffdf78c58ccbe105012ee9920939873 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Fri, 23 Jun 2017 21:01:00 +0300 Subject: [PATCH 10/20] [pheanstalk] add tests for context and destination. --- .../Tests/PheanstalkContextTest.php | 81 +++++++++++++++++++ .../Tests/PheanstalkDestinationTest.php | 31 +++++++ 2 files changed, 112 insertions(+) create mode 100644 pkg/pheanstalk/Tests/PheanstalkContextTest.php create mode 100644 pkg/pheanstalk/Tests/PheanstalkDestinationTest.php diff --git a/pkg/pheanstalk/Tests/PheanstalkContextTest.php b/pkg/pheanstalk/Tests/PheanstalkContextTest.php new file mode 100644 index 000000000..fa9ee7692 --- /dev/null +++ b/pkg/pheanstalk/Tests/PheanstalkContextTest.php @@ -0,0 +1,81 @@ +assertClassImplements(PsrContext::class, PheanstalkContext::class); + } + + public function testCouldBeConstructedWithPheanstalkAsFirstArgument() + { + new PheanstalkContext($this->createPheanstalkMock()); + } + + public function testThrowNotImplementedOnCreateTemporaryQueue() + { + $context = new PheanstalkContext($this->createPheanstalkMock()); + + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Not implemented'); + $context->createTemporaryQueue(); + } + + public function testThrowInvalidDestinationIfInvalidDestinationGivenOnCreateConsumer() + { + $context = new PheanstalkContext($this->createPheanstalkMock()); + + $this->expectException(InvalidDestinationException::class); + $context->createConsumer(new NullQueue('aQueue')); + } + + public function testShouldAllowGetPheanstalkSetInConstructor() + { + $pheanstalk = $this->createPheanstalkMock(); + + $context = new PheanstalkContext($pheanstalk); + + $this->assertSame($pheanstalk, $context->getPheanstalk()); + } + + public function testShouldDoConnectionDisconnectOnContextClose() + { + $connection = $this->createMock(Connection::class); + $connection + ->expects($this->once()) + ->method('disconnect') + ; + + $pheanstalk = $this->createPheanstalkMock(); + $pheanstalk + ->expects($this->once()) + ->method('getConnection') + ->willReturn($connection) + ; + + $context = new PheanstalkContext($pheanstalk); + + $context->close(); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|Pheanstalk + */ + private function createPheanstalkMock() + { + return $this->createMock(Pheanstalk::class); + } +} diff --git a/pkg/pheanstalk/Tests/PheanstalkDestinationTest.php b/pkg/pheanstalk/Tests/PheanstalkDestinationTest.php new file mode 100644 index 000000000..bd52ef577 --- /dev/null +++ b/pkg/pheanstalk/Tests/PheanstalkDestinationTest.php @@ -0,0 +1,31 @@ +assertClassImplements(PsrQueue::class, PheanstalkDestination::class); + } + + public function testShouldImplementPsrTopicInterface() + { + $this->assertClassImplements(PsrTopic::class, PheanstalkDestination::class); + } + + public function testShouldAllowGetNameSetInConstructor() + { + $destionation = new PheanstalkDestination('theDestinationName'); + + $this->assertSame('theDestinationName', $destionation->getName()); + } +} From 46f2a23f5e20123f848f0f79f9cc4fb0f3f8709b Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Fri, 23 Jun 2017 21:22:32 +0300 Subject: [PATCH 11/20] [pheanstalk] Add tests for message and producer. --- .../Tests/PheanstalkMessageTest.php | 23 ++++++ .../Tests/PheanstalkProducerTest.php | 75 +++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 pkg/pheanstalk/Tests/PheanstalkMessageTest.php create mode 100644 pkg/pheanstalk/Tests/PheanstalkProducerTest.php diff --git a/pkg/pheanstalk/Tests/PheanstalkMessageTest.php b/pkg/pheanstalk/Tests/PheanstalkMessageTest.php new file mode 100644 index 000000000..183cbb435 --- /dev/null +++ b/pkg/pheanstalk/Tests/PheanstalkMessageTest.php @@ -0,0 +1,23 @@ +setJob($job); + + $this->assertSame($job, $message->getJob()); + } +} diff --git a/pkg/pheanstalk/Tests/PheanstalkProducerTest.php b/pkg/pheanstalk/Tests/PheanstalkProducerTest.php new file mode 100644 index 000000000..d9f00c617 --- /dev/null +++ b/pkg/pheanstalk/Tests/PheanstalkProducerTest.php @@ -0,0 +1,75 @@ +createPheanstalkMock()); + } + + public function testThrowIfDestinationInvalid() + { + $producer = new PheanstalkProducer($this->createPheanstalkMock()); + + $this->expectException(InvalidDestinationException::class); + $this->expectExceptionMessage('The destination must be an instance of Enqueue\Pheanstalk\PheanstalkDestination but got Enqueue\Null\NullQueue.'); + $producer->send(new NullQueue('aQueue'), new PheanstalkMessage()); + } + + public function testThrowIfMessageInvalid() + { + $producer = new PheanstalkProducer($this->createPheanstalkMock()); + + $this->expectException(InvalidMessageException::class); + $this->expectExceptionMessage('The message must be an instance of Enqueue\Pheanstalk\PheanstalkMessage but it is Enqueue\Null\NullMessage.'); + $producer->send(new PheanstalkDestination('aQueue'), new NullMessage()); + } + + public function testShouldJsonEncodeMessageAndPutToExpectedTube() + { + $message = new PheanstalkMessage('theBody', ['foo' => 'fooVal'], ['bar' => 'barVal']); + + $pheanstalk = $this->createPheanstalkMock(); + $pheanstalk + ->expects($this->once()) + ->method('useTube') + ->with('theQueueName') + ->willReturnSelf() + ; + $pheanstalk + ->expects($this->once()) + ->method('put') + ->with('{"body":"theBody","properties":{"foo":"fooVal"},"headers":{"bar":"barVal"}}') + ; + + $producer = new PheanstalkProducer($pheanstalk); + + $producer->send( + new PheanstalkDestination('theQueueName'), + $message + ); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|Pheanstalk + */ + private function createPheanstalkMock() + { + return $this->createMock(Pheanstalk::class); + } +} From dd4dceb37e65f37e95285f07b9a224b222d6cec8 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Mon, 26 Jun 2017 11:26:05 +0300 Subject: [PATCH 12/20] [beanstalk] Add tests for consumer --- .../Tests/PheanstalkConsumerTest.php | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 pkg/pheanstalk/Tests/PheanstalkConsumerTest.php diff --git a/pkg/pheanstalk/Tests/PheanstalkConsumerTest.php b/pkg/pheanstalk/Tests/PheanstalkConsumerTest.php new file mode 100644 index 000000000..60015dc99 --- /dev/null +++ b/pkg/pheanstalk/Tests/PheanstalkConsumerTest.php @@ -0,0 +1,128 @@ +createPheanstalkMock() + ); + } + + public function testShouldReturnQueueSetInConstructor() + { + $destination = new PheanstalkDestination('aQueueName'); + + $consumer = new PheanstalkConsumer( + $destination, + $this->createPheanstalkMock() + ); + + $this->assertSame($destination, $consumer->getQueue()); + } + + public function testShouldReceiveFromQueueAndReturnNullIfNoMessageInQueue() + { + $destination = new PheanstalkDestination('theQueueName'); + + $pheanstalk = $this->createPheanstalkMock(); + $pheanstalk + ->expects($this->once()) + ->method('reserveFromTube') + ->with('theQueueName', 1) + ->willReturn(null) + ; + + $consumer = new PheanstalkConsumer($destination, $pheanstalk); + + $this->assertNull($consumer->receive(1000)); + } + + public function testShouldReceiveFromQueueAndReturnMessageIfMessageInQueue() + { + $destination = new PheanstalkDestination('theQueueName'); + $message = new PheanstalkMessage('theBody', ['foo' => 'fooVal'], ['bar' => 'barVal']); + + $job = new Job('theJobId', json_encode($message)); + + $pheanstalk = $this->createPheanstalkMock(); + $pheanstalk + ->expects($this->once()) + ->method('reserveFromTube') + ->with('theQueueName', 1) + ->willReturn($job) + ; + + $consumer = new PheanstalkConsumer($destination, $pheanstalk); + + $actualMessage = $consumer->receive(1000); + + $this->assertSame('theBody', $actualMessage->getBody()); + $this->assertSame(['foo' => 'fooVal'], $actualMessage->getProperties()); + $this->assertSame(['bar' => 'barVal'], $actualMessage->getHeaders()); + $this->assertSame($job, $actualMessage->getJob()); + } + + public function testShouldReceiveNoWaitFromQueueAndReturnNullIfNoMessageInQueue() + { + $destination = new PheanstalkDestination('theQueueName'); + + $pheanstalk = $this->createPheanstalkMock(); + $pheanstalk + ->expects($this->once()) + ->method('reserveFromTube') + ->with('theQueueName', 0) + ->willReturn(null) + ; + + $consumer = new PheanstalkConsumer($destination, $pheanstalk); + + $this->assertNull($consumer->receiveNoWait()); + } + + public function testShouldReceiveNoWaitFromQueueAndReturnMessageIfMessageInQueue() + { + $destination = new PheanstalkDestination('theQueueName'); + $message = new PheanstalkMessage('theBody', ['foo' => 'fooVal'], ['bar' => 'barVal']); + + $job = new Job('theJobId', json_encode($message)); + + $pheanstalk = $this->createPheanstalkMock(); + $pheanstalk + ->expects($this->once()) + ->method('reserveFromTube') + ->with('theQueueName', 0) + ->willReturn($job) + ; + + $consumer = new PheanstalkConsumer($destination, $pheanstalk); + + $actualMessage = $consumer->receiveNoWait(); + + $this->assertSame('theBody', $actualMessage->getBody()); + $this->assertSame(['foo' => 'fooVal'], $actualMessage->getProperties()); + $this->assertSame(['bar' => 'barVal'], $actualMessage->getHeaders()); + $this->assertSame($job, $actualMessage->getJob()); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|Pheanstalk + */ + private function createPheanstalkMock() + { + return $this->createMock(Pheanstalk::class); + } +} From a00d619cbb51290f98ffd42ba6b1936a9bc2d9a5 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Mon, 26 Jun 2017 11:26:38 +0300 Subject: [PATCH 13/20] [amqp] add spec tests --- pkg/amqp-ext/AmqpConnectionFactory.php | 2 +- .../Tests/AmqpConnectionFactoryConfigTest.php | 2 +- .../AmqpSendToAndReceiveFromQueueTest.php | 38 +++++++++++++ .../AmqpSendToAndReceiveFromTopicTest.php | 39 +++++++++++++ ...mqpSendToAndReceiveNoWaitFromQueueTest.php | 38 +++++++++++++ ...mqpSendToAndReceiveNoWaitFromTopicTest.php | 39 +++++++++++++ ...AmqpSendToTopicAndReceiveFromQueueTest.php | 55 +++++++++++++++++++ ...ndToTopicAndReceiveNoWaitFromQueueTest.php | 55 +++++++++++++++++++ 8 files changed, 266 insertions(+), 2 deletions(-) create mode 100644 pkg/amqp-ext/Tests/Spec/AmqpSendToAndReceiveFromQueueTest.php create mode 100644 pkg/amqp-ext/Tests/Spec/AmqpSendToAndReceiveFromTopicTest.php create mode 100644 pkg/amqp-ext/Tests/Spec/AmqpSendToAndReceiveNoWaitFromQueueTest.php create mode 100644 pkg/amqp-ext/Tests/Spec/AmqpSendToAndReceiveNoWaitFromTopicTest.php create mode 100644 pkg/amqp-ext/Tests/Spec/AmqpSendToTopicAndReceiveFromQueueTest.php create mode 100644 pkg/amqp-ext/Tests/Spec/AmqpSendToTopicAndReceiveNoWaitFromQueueTest.php diff --git a/pkg/amqp-ext/AmqpConnectionFactory.php b/pkg/amqp-ext/AmqpConnectionFactory.php index ed220fbf6..90e8f64fc 100644 --- a/pkg/amqp-ext/AmqpConnectionFactory.php +++ b/pkg/amqp-ext/AmqpConnectionFactory.php @@ -150,7 +150,7 @@ private function parseDsn($dsn) ], $dsnConfig); if ('amqp' !== $dsnConfig['scheme']) { - throw new \LogicException('The given DSN scheme "%s" is not supported. Could be "amqp" only.'); + throw new \LogicException(sprintf('The given DSN scheme "%s" is not supported. Could be "amqp" only.', $dsnConfig['scheme'])); } if ($dsnConfig['query']) { diff --git a/pkg/amqp-ext/Tests/AmqpConnectionFactoryConfigTest.php b/pkg/amqp-ext/Tests/AmqpConnectionFactoryConfigTest.php index 5e67ee529..b2132f704 100644 --- a/pkg/amqp-ext/Tests/AmqpConnectionFactoryConfigTest.php +++ b/pkg/amqp-ext/Tests/AmqpConnectionFactoryConfigTest.php @@ -24,7 +24,7 @@ public function testThrowNeitherArrayStringNorNullGivenAsConfig() public function testThrowIfSchemeIsNotAmqp() { $this->expectException(\LogicException::class); - $this->expectExceptionMessage('The given DSN scheme "%s" is not supported. Could be "amqp" only.'); + $this->expectExceptionMessage('The given DSN scheme "http" is not supported. Could be "amqp" only.'); new AmqpConnectionFactory('http://example.com'); } diff --git a/pkg/amqp-ext/Tests/Spec/AmqpSendToAndReceiveFromQueueTest.php b/pkg/amqp-ext/Tests/Spec/AmqpSendToAndReceiveFromQueueTest.php new file mode 100644 index 000000000..e98bb9078 --- /dev/null +++ b/pkg/amqp-ext/Tests/Spec/AmqpSendToAndReceiveFromQueueTest.php @@ -0,0 +1,38 @@ +createContext(); + } + + /** + * {@inheritdoc} + * + * @param AmqpContext $context + */ + protected function createQueue(PsrContext $context, $queueName) + { + $queue = $context->createQueue($queueName); + $context->declareQueue($queue); + $context->purge($queue); + + return $queue; + } +} diff --git a/pkg/amqp-ext/Tests/Spec/AmqpSendToAndReceiveFromTopicTest.php b/pkg/amqp-ext/Tests/Spec/AmqpSendToAndReceiveFromTopicTest.php new file mode 100644 index 000000000..72ebe9221 --- /dev/null +++ b/pkg/amqp-ext/Tests/Spec/AmqpSendToAndReceiveFromTopicTest.php @@ -0,0 +1,39 @@ +createContext(); + } + + /** + * {@inheritdoc} + * + * @param AmqpContext $context + */ + protected function createTopic(PsrContext $context, $topicName) + { + $topic = $context->createTopic($topicName); + $topic->setType(\AMQP_EX_TYPE_FANOUT); + $topic->addFlag(\AMQP_DURABLE); + $context->declareTopic($topic); + + return $topic; + } +} diff --git a/pkg/amqp-ext/Tests/Spec/AmqpSendToAndReceiveNoWaitFromQueueTest.php b/pkg/amqp-ext/Tests/Spec/AmqpSendToAndReceiveNoWaitFromQueueTest.php new file mode 100644 index 000000000..ca78e82f9 --- /dev/null +++ b/pkg/amqp-ext/Tests/Spec/AmqpSendToAndReceiveNoWaitFromQueueTest.php @@ -0,0 +1,38 @@ +createContext(); + } + + /** + * {@inheritdoc} + * + * @param AmqpContext $context + */ + protected function createQueue(PsrContext $context, $queueName) + { + $queue = $context->createQueue($queueName); + $context->declareQueue($queue); + $context->purge($queue); + + return $queue; + } +} diff --git a/pkg/amqp-ext/Tests/Spec/AmqpSendToAndReceiveNoWaitFromTopicTest.php b/pkg/amqp-ext/Tests/Spec/AmqpSendToAndReceiveNoWaitFromTopicTest.php new file mode 100644 index 000000000..d0721d34b --- /dev/null +++ b/pkg/amqp-ext/Tests/Spec/AmqpSendToAndReceiveNoWaitFromTopicTest.php @@ -0,0 +1,39 @@ +createContext(); + } + + /** + * {@inheritdoc} + * + * @param AmqpContext $context + */ + protected function createTopic(PsrContext $context, $topicName) + { + $topic = $context->createTopic($topicName); + $topic->setType(\AMQP_EX_TYPE_FANOUT); + $topic->addFlag(\AMQP_DURABLE); + $context->declareTopic($topic); + + return $topic; + } +} diff --git a/pkg/amqp-ext/Tests/Spec/AmqpSendToTopicAndReceiveFromQueueTest.php b/pkg/amqp-ext/Tests/Spec/AmqpSendToTopicAndReceiveFromQueueTest.php new file mode 100644 index 000000000..fff7d4f81 --- /dev/null +++ b/pkg/amqp-ext/Tests/Spec/AmqpSendToTopicAndReceiveFromQueueTest.php @@ -0,0 +1,55 @@ +createContext(); + } + + /** + * {@inheritdoc} + * + * @param AmqpContext $context + */ + protected function createQueue(PsrContext $context, $queueName) + { + $queue = $context->createQueue($queueName); + $context->declareQueue($queue); + $context->purge($queue); + + $context->bind($context->createTopic($queueName), $queue); + + return $queue; + } + + /** + * {@inheritdoc} + * + * @param AmqpContext $context + */ + protected function createTopic(PsrContext $context, $topicName) + { + $topic = $context->createTopic($topicName); + $topic->setType(\AMQP_EX_TYPE_FANOUT); + $topic->addFlag(\AMQP_DURABLE); + $context->declareTopic($topic); + + return $topic; + } +} diff --git a/pkg/amqp-ext/Tests/Spec/AmqpSendToTopicAndReceiveNoWaitFromQueueTest.php b/pkg/amqp-ext/Tests/Spec/AmqpSendToTopicAndReceiveNoWaitFromQueueTest.php new file mode 100644 index 000000000..18dae26c0 --- /dev/null +++ b/pkg/amqp-ext/Tests/Spec/AmqpSendToTopicAndReceiveNoWaitFromQueueTest.php @@ -0,0 +1,55 @@ +createContext(); + } + + /** + * {@inheritdoc} + * + * @param AmqpContext $context + */ + protected function createQueue(PsrContext $context, $queueName) + { + $queue = $context->createQueue($queueName); + $context->declareQueue($queue); + $context->purge($queue); + + $context->bind($context->createTopic($queueName), $queue); + + return $queue; + } + + /** + * {@inheritdoc} + * + * @param AmqpContext $context + */ + protected function createTopic(PsrContext $context, $topicName) + { + $topic = $context->createTopic($topicName); + $topic->setType(\AMQP_EX_TYPE_FANOUT); + $topic->addFlag(\AMQP_DURABLE); + $context->declareTopic($topic); + + return $topic; + } +} From 23c7c4f8cde54f7892890e79fab092026f8b92b2 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Mon, 26 Jun 2017 11:35:42 +0300 Subject: [PATCH 14/20] [docs] add basic docs for beanstalk --- README.md | 2 +- docs/index.md | 1 + docs/transport/pheanstalk.md | 84 ++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 docs/transport/pheanstalk.md diff --git a/README.md b/README.md index c982e1653..7e0769bfa 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Features: * [Feature rich](docs/quick_tour.md). * [JMS](https://docs.oracle.com/javaee/7/api/javax/jms/package-summary.html) like transport [abstraction](https://github.com/php-enqueue/psr-queue). -* Supports transports [AMQP (RabbitMQ, ActiveMQ)](docs/transport/amqp.md), [STOMP](docs/transport/stomp.md), [Amazon SQS](docs/transport/sqs.md), [Redis](docs/transport/redis.md), [Doctrine DBAL](docs/transport/dbal.md), [Filesystem](docs/transport/filesystem.md), [Null](docs/transport/null.md). +* Supports transports [AMQP (RabbitMQ, ActiveMQ)](docs/transport/amqp.md), [Beanstalk (Pheanstalk)](docs/transport/pheanstalk.md) [STOMP](docs/transport/stomp.md), [Amazon SQS](docs/transport/sqs.md), [Redis](docs/transport/redis.md), [Doctrine DBAL](docs/transport/dbal.md), [Filesystem](docs/transport/filesystem.md), [Null](docs/transport/null.md). * [Symfony bundle](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/bundle/quick_tour.md) * [Magento1 extension](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/magento/quick_tour.md) * [Message bus](http://www.enterpriseintegrationpatterns.com/patterns/messaging/MessageBus.html) support. diff --git a/docs/index.md b/docs/index.md index 0795e1ca0..875c0e3c0 100644 --- a/docs/index.md +++ b/docs/index.md @@ -4,6 +4,7 @@ * Transports - [Amqp (RabbitMQ, ActiveMQ)](transport/amqp.md) - [Amazon SQS](transport/sqs.md) + - [Beanstalk (Pheanstalk)](transport/pheanstalk.md) - [Stomp](transport/stomp.md) - [Redis](transport/redis.md) - [Doctrine DBAL](transport/dbal.md) diff --git a/docs/transport/pheanstalk.md b/docs/transport/pheanstalk.md new file mode 100644 index 000000000..559517c00 --- /dev/null +++ b/docs/transport/pheanstalk.md @@ -0,0 +1,84 @@ +# Beanstalk (Pheanstalk) transport + +The transport uses [Beanstalkd](http://kr.github.io/beanstalkd/) job manager. +The transport uses [Pheanstalk](https://github.com/pda/pheanstalk) library internally. + +* [Installation](#installation) +* [Create context](#create-context) +* [Send message to topic](#send-message-to-topic) +* [Send message to queue](#send-message-to-queue) +* [Consume message](#consume-message) + +## Installation + +```bash +$ composer require enqueue/pheanstalk +``` + + +## Create context + +```php + 'example', + 'port' => 5555 +]); +``` + +## Send message to topic + +```php +createTopic('aTopic'); +$message = $psrContext->createMessage('Hello world!'); + +$psrContext->createProducer()->send($fooTopic, $message); +``` + +## Send message to queue + +```php +createQueue('aQueue'); +$message = $psrContext->createMessage('Hello world!'); + +$psrContext->createProducer()->send($fooQueue, $message); +``` + +## Consume message: + +```php +createQueue('aQueue'); +$consumer = $psrContext->createConsumer($fooQueue); + +$message = $consumer->receive(2000); // wait for 2 seconds + +$message = $consumer->receiveNoWait(); // fetch message or return null immediately + +// process a message + +$consumer->acknowledge($message); +// $consumer->reject($message); +``` + +[back to index](../index.md) \ No newline at end of file From f0974dd1fbc93700efb23a3986dc7ca1d870118f Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Mon, 26 Jun 2017 11:39:51 +0300 Subject: [PATCH 15/20] Add pheanstalk to subtree split. --- README.md | 2 +- bin/release | 2 +- bin/subtree-split | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7e0769bfa..5543efb70 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Features: * [Feature rich](docs/quick_tour.md). * [JMS](https://docs.oracle.com/javaee/7/api/javax/jms/package-summary.html) like transport [abstraction](https://github.com/php-enqueue/psr-queue). -* Supports transports [AMQP (RabbitMQ, ActiveMQ)](docs/transport/amqp.md), [Beanstalk (Pheanstalk)](docs/transport/pheanstalk.md) [STOMP](docs/transport/stomp.md), [Amazon SQS](docs/transport/sqs.md), [Redis](docs/transport/redis.md), [Doctrine DBAL](docs/transport/dbal.md), [Filesystem](docs/transport/filesystem.md), [Null](docs/transport/null.md). +* Supports transports [AMQP (RabbitMQ, ActiveMQ)](docs/transport/amqp.md), [Beanstalk (Pheanstalk)](docs/transport/pheanstalk.md), [STOMP](docs/transport/stomp.md), [Amazon SQS](docs/transport/sqs.md), [Redis](docs/transport/redis.md), [Doctrine DBAL](docs/transport/dbal.md), [Filesystem](docs/transport/filesystem.md), [Null](docs/transport/null.md). * [Symfony bundle](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/bundle/quick_tour.md) * [Magento1 extension](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/magento/quick_tour.md) * [Message bus](http://www.enterpriseintegrationpatterns.com/patterns/messaging/MessageBus.html) support. diff --git a/bin/release b/bin/release index 995494da6..236be3c35 100755 --- a/bin/release +++ b/bin/release @@ -14,7 +14,7 @@ fi CURRENT_BRANCH=`git rev-parse --abbrev-ref HEAD` -for REMOTE in origin psr-queue stomp amqp-ext sqs fs redis dbal null enqueue simple-client enqueue-bundle job-queue test +for REMOTE in origin psr-queue stomp amqp-ext pheanstalk sqs fs redis dbal null enqueue simple-client enqueue-bundle job-queue test do echo "" echo "" diff --git a/bin/subtree-split b/bin/subtree-split index 6dbdc0193..65bd32eb2 100755 --- a/bin/subtree-split +++ b/bin/subtree-split @@ -48,6 +48,7 @@ remote enqueue git@github.com:php-enqueue/enqueue.git remote simple-client git@github.com:php-enqueue/simple-client.git remote stomp git@github.com:php-enqueue/stomp.git remote amqp-ext git@github.com:php-enqueue/amqp-ext.git +remote pheanstalk git@github.com:php-enqueue/pheanstalk.git remote fs git@github.com:php-enqueue/fs.git remote redis git@github.com:php-enqueue/redis.git remote dbal git@github.com:php-enqueue/dbal.git @@ -62,6 +63,7 @@ split 'pkg/enqueue' enqueue split 'pkg/simple-client' simple-client split 'pkg/stomp' stomp split 'pkg/amqp-ext' amqp-ext +split 'pkg/pheanstalk' pheanstalk split 'pkg/fs' fs split 'pkg/redis' redis split 'pkg/dbal' dbal From 131ff9ed5eb66e97345593da404894ba3307f7f1 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Mon, 26 Jun 2017 11:43:19 +0300 Subject: [PATCH 16/20] [pheanstalk] Add travis.yml --- pkg/pheanstalk/.travis.yml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 pkg/pheanstalk/.travis.yml diff --git a/pkg/pheanstalk/.travis.yml b/pkg/pheanstalk/.travis.yml new file mode 100644 index 000000000..42374ddc7 --- /dev/null +++ b/pkg/pheanstalk/.travis.yml @@ -0,0 +1,21 @@ +sudo: false + +git: + depth: 1 + +language: php + +php: + - '5.6' + - '7.0' + +cache: + directories: + - $HOME/.composer/cache + +install: + - composer self-update + - composer install --prefer-source + +script: + - vendor/bin/phpunit --exclude-group=functional From 26a91d8056fa0aa8cbb8e6460f6eb48bafcf7a3b Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Mon, 26 Jun 2017 15:41:57 +0300 Subject: [PATCH 17/20] [client] Merge producerv2 methods to producer interface. --- docs/bundle/debuging.md | 6 +- docs/bundle/job_queue.md | 2 +- docs/bundle/message_producer.md | 35 +- docs/bundle/quick_tour.md | 7 +- docs/client/message_examples.md | 8 +- docs/job_queue/run_sub_job.md | 2 +- pkg/enqueue-bundle/Events/AsyncListener.php | 2 +- .../Resources/config/client.yml | 12 +- .../Resources/config/services.yml | 7 + .../Tests/Functional/Client/ProducerTest.php | 113 ++++++ .../Functional/Client/ProducerV2Test.php | 97 ----- .../Tests/Functional/RpcClientTest.php | 11 +- .../Tests/Functional/UseCasesTest.php | 6 +- .../Tests/Unit/Events/AsyncListenerTest.php | 4 +- pkg/enqueue/Client/Producer.php | 69 +++- pkg/enqueue/Client/ProducerInterface.php | 19 + pkg/enqueue/Client/ProducerV2.php | 57 --- pkg/enqueue/Client/ProducerV2Interface.php | 20 +- pkg/enqueue/Client/RpcClient.php | 130 ------- pkg/enqueue/Client/SpoolProducer.php | 44 ++- pkg/enqueue/Client/TraceableProducer.php | 100 +++-- pkg/enqueue/Rpc/RpcClient.php | 68 +--- pkg/enqueue/Rpc/RpcFactory.php | 87 +++++ .../Symfony/Client/ProduceMessageCommand.php | 2 +- pkg/enqueue/Tests/Client/ProducerTest.php | 121 +++--- pkg/enqueue/Tests/Client/RpcClientTest.php | 363 ------------------ .../Tests/Client/SpoolProducerTest.php | 18 +- .../Tests/Client/TraceableProducerTest.php | 31 +- .../Tests/Functional/Client/RpcClientTest.php | 85 ---- pkg/enqueue/Tests/Rpc/RpcClientTest.php | 6 +- .../Client/ProduceMessageCommandTest.php | 2 +- .../CalculateRootJobStatusProcessor.php | 2 +- pkg/job-queue/DependentJobProcessor.php | 2 +- pkg/job-queue/JobProcessor.php | 10 +- .../CalculateRootJobStatusProcessorTest.php | 6 +- .../Tests/DependentJobProcessorTest.php | 10 +- pkg/job-queue/Tests/JobProcessorTest.php | 13 +- pkg/simple-client/SimpleClient.php | 37 +- .../SimpleClientContainerExtension.php | 21 +- 39 files changed, 630 insertions(+), 1005 deletions(-) delete mode 100644 pkg/enqueue-bundle/Tests/Functional/Client/ProducerV2Test.php delete mode 100644 pkg/enqueue/Client/ProducerV2.php delete mode 100644 pkg/enqueue/Client/RpcClient.php create mode 100644 pkg/enqueue/Rpc/RpcFactory.php delete mode 100644 pkg/enqueue/Tests/Client/RpcClientTest.php delete mode 100644 pkg/enqueue/Tests/Functional/Client/RpcClientTest.php diff --git a/docs/bundle/debuging.md b/docs/bundle/debuging.md index ac83f9882..72aea01e4 100644 --- a/docs/bundle/debuging.md +++ b/docs/bundle/debuging.md @@ -35,13 +35,13 @@ class DefaultController extends Controller /** @var ProducerInterface $producer */ $producer = $this->get('enqueue.producer'); - $producer->send('foo_topic', 'Hello world'); + $producer->sendEvent('foo_topic', 'Hello world'); - $producer->send('bar_topic', ['bar' => 'val']); + $producer->sendEvent('bar_topic', ['bar' => 'val']); $message = new Message(); $message->setBody('baz'); - $producer->send('baz_topic', $message); + $producer->sendEvent('baz_topic', $message); // ... } diff --git a/docs/bundle/job_queue.md b/docs/bundle/job_queue.md index 77dd6ee6a..7ca15042d 100644 --- a/docs/bundle/job_queue.md +++ b/docs/bundle/job_queue.md @@ -89,7 +89,7 @@ class Step1Processor implements PsrProcessor $runner->createDelayed( $jobName, function (JobRunner $runner, Job $childJob) use ($entity) { - $this->producer->send('search:index:index-single-entity', [ + $this->producer->sendCommand('search:index:index-single-entity', [ 'entityId' => $entity->getId(), 'jobId' => $childJob->getId(), ]); diff --git a/docs/bundle/message_producer.md b/docs/bundle/message_producer.md index 6efad8296..181363815 100644 --- a/docs/bundle/message_producer.md +++ b/docs/bundle/message_producer.md @@ -24,8 +24,13 @@ $context->createProducer()->send( The client is shipped with two types of producers. The first one sends messages immediately where another one (it is called spool producer) collects them in memory and sends them `onTerminate` event (the response is already sent). +The producer has two types on send methods: +* `sendEvent` - Message is sent to topic and many consumers can subscriber to it. It is "fire and forget" strategy. +* `sendCommand` - Message is to ONE exact consumer. It could be used as "fire and forget" or as RPC. +### Send event + ```php get('enqueue.producer'); // message is being sent right now -$producer->send('a_topic', 'Hello there!'); +$producer->sendEvent('a_topic', 'Hello there!'); + + +/** @var \Enqueue\Client\SpoolProducer $spoolProducer */ +$spoolProducer = $container->get('enqueue.spool_producer'); + +// message is being sent on console.terminate or kernel.terminate event +$spoolProducer->sendEvent('a_topic', 'Hello there!'); + +// you could send queued messages manually by calling flush method +$spoolProducer->flush(); +``` + +### Send command + +```php +get('enqueue.producer'); + +// message is being sent right now, we use it as RPC +$promise = $producer->sendCommand('a_processor_name', 'Hello there!', $needReply = true); + +$replyMessage = $promise->receive(); /** @var \Enqueue\Client\SpoolProducer $spoolProducer */ $spoolProducer = $container->get('enqueue.spool_producer'); // message is being sent on console.terminate or kernel.terminate event -$spoolProducer->send('a_topic', 'Hello there!'); +$spoolProducer->sendCommand('a_processor_name', 'Hello there!'); // you could send queued messages manually by calling flush method $spoolProducer->flush(); diff --git a/docs/bundle/quick_tour.md b/docs/bundle/quick_tour.md index d67cd306d..b6d8c9036 100644 --- a/docs/bundle/quick_tour.md +++ b/docs/bundle/quick_tour.md @@ -60,7 +60,12 @@ use Enqueue\Client\Producer; /** @var Producer $producer **/ $producer = $container->get('enqueue.producer'); -$producer->send('aFooTopic', 'Something has happened'); + +// send event to many consumers +$producer->sendEvent('aFooTopic', 'Something has happened'); + +// send command to ONE consumer +$producer->sendCommand('aProcessorName', 'Something has happened'); ``` To consume messages you have to first create a message processor: diff --git a/docs/client/message_examples.md b/docs/client/message_examples.md index ff7201fad..5bd627cad 100644 --- a/docs/client/message_examples.md +++ b/docs/client/message_examples.md @@ -15,7 +15,7 @@ $message = new Message(); $message->setDelay(60); // seconds /** @var \Enqueue\Client\ProducerInterface $producer */ -$producer->send('aTopic', $message); +$producer->sendEvent('aTopic', $message); ``` ## Expiration (TTL) @@ -33,7 +33,7 @@ $message = new Message(); $message->setExpire(60); // seconds /** @var \Enqueue\Client\ProducerInterface $producer */ -$producer->send('aTopic', $message); +$producer->sendEvent('aTopic', $message); ``` ## Priority @@ -52,7 +52,7 @@ $message = new Message(); $message->setPriority(MessagePriority::HIGH); /** @var \Enqueue\Client\ProducerInterface $producer */ -$producer->send('aTopic', $message); +$producer->sendEvent('aTopic', $message); ``` ## Timestamp, Content type, Message id @@ -72,7 +72,7 @@ $message->setTimestamp(time()); $message->setContentType('text/plain'); /** @var \Enqueue\Client\ProducerInterface $producer */ -$producer->send('aTopic', $message); +$producer->sendEvent('aTopic', $message); ``` [back to index](../index.md) diff --git a/docs/job_queue/run_sub_job.md b/docs/job_queue/run_sub_job.md index 3e946ce8b..0c819e889 100644 --- a/docs/job_queue/run_sub_job.md +++ b/docs/job_queue/run_sub_job.md @@ -26,7 +26,7 @@ class RootJobProcessor implements PsrProcessor { $result = $this->jobRunner->runUnique($message->getMessageId(), 'aJobName', function (JobRunner $runner) { $runner->createDelayed('aSubJobName1', function (JobRunner $runner, Job $childJob) { - $this->producer->send('aJobTopic', [ + $this->producer->sendEvent('aJobTopic', [ 'jobId' => $childJob->getId(), // other data required by sub job ]); diff --git a/pkg/enqueue-bundle/Events/AsyncListener.php b/pkg/enqueue-bundle/Events/AsyncListener.php index 78f964dce..ae4f5b242 100644 --- a/pkg/enqueue-bundle/Events/AsyncListener.php +++ b/pkg/enqueue-bundle/Events/AsyncListener.php @@ -60,7 +60,7 @@ public function onEvent(Event $event = null, $eventName) $message->setProperty('event_name', $eventName); $message->setProperty('transformer_name', $transformerName); - $this->producer->send('event.'.$eventName, $message); + $this->producer->sendEvent('event.'.$eventName, $message); } } } diff --git a/pkg/enqueue-bundle/Resources/config/client.yml b/pkg/enqueue-bundle/Resources/config/client.yml index 637b9d98a..cc2b30f73 100644 --- a/pkg/enqueue-bundle/Resources/config/client.yml +++ b/pkg/enqueue-bundle/Resources/config/client.yml @@ -7,6 +7,7 @@ services: class: 'Enqueue\Client\Producer' arguments: - '@enqueue.client.driver' + - '@enqueue.client.rpc_factory' - '@enqueue.client.extensions' enqueue.client.spool_producer: @@ -24,18 +25,15 @@ services: alias: 'enqueue.client.producer' enqueue.client.producer_v2: - class: 'Enqueue\Client\ProducerV2' - arguments: - - '@enqueue.client.producer' - - '@enqueue.client.rpc_client' + alias: 'enqueue.client.producer' enqueue.spool_producer: alias: 'enqueue.client.spool_producer' - enqueue.client.rpc_client: - class: 'Enqueue\Client\RpcClient' + enqueue.client.rpc_factory: + class: 'Enqueue\Rpc\RpcFactory' + public: false arguments: - - '@enqueue.client.producer' - '@enqueue.transport.context' enqueue.client.router_processor: diff --git a/pkg/enqueue-bundle/Resources/config/services.yml b/pkg/enqueue-bundle/Resources/config/services.yml index 6a6d7799a..66dc0d1a8 100644 --- a/pkg/enqueue-bundle/Resources/config/services.yml +++ b/pkg/enqueue-bundle/Resources/config/services.yml @@ -18,7 +18,14 @@ services: tags: - { name: 'console.command' } + enqueue.transport.rpc_factory: + class: 'Enqueue\Rpc\RpcFactory' + public: false + arguments: + - '@enqueue.transport.context' + enqueue.transport.rpc_client: class: 'Enqueue\Rpc\RpcClient' arguments: - '@enqueue.transport.context' + - '@enqueue.transport.rpc_factory' diff --git a/pkg/enqueue-bundle/Tests/Functional/Client/ProducerTest.php b/pkg/enqueue-bundle/Tests/Functional/Client/ProducerTest.php index 94e56ff14..c77f6a677 100644 --- a/pkg/enqueue-bundle/Tests/Functional/Client/ProducerTest.php +++ b/pkg/enqueue-bundle/Tests/Functional/Client/ProducerTest.php @@ -3,13 +3,31 @@ namespace Enqueue\Bundle\Tests\Functional\Client; use Enqueue\Bundle\Tests\Functional\WebTestCase; +use Enqueue\Client\Config; +use Enqueue\Client\Message; use Enqueue\Client\ProducerInterface; +use Enqueue\Client\TraceableProducer; +use Enqueue\Rpc\Promise; /** * @group functional */ class ProducerTest extends WebTestCase { + public function setUp() + { + parent::setUp(); + + $this->container->get('enqueue.client.producer')->clearTraces(); + } + + public function tearDown() + { + parent::tearDown(); + + $this->container->get('enqueue.client.producer')->clearTraces(); + } + public function testCouldBeGetFromContainerAsService() { $messageProducer = $this->container->get('enqueue.client.producer'); @@ -24,4 +42,99 @@ public function testCouldBeGetFromContainerAsShortenAlias() $this->assertSame($messageProducer, $aliasMessageProducer); } + + public function testShouldSendEvent() + { + /** @var ProducerInterface $producer */ + $producer = $this->container->get('enqueue.client.producer'); + + $producer->sendEvent('theTopic', 'theMessage'); + + $traces = $this->getTraceableProducer()->getTopicTraces('theTopic'); + + $this->assertCount(1, $traces); + $this->assertEquals('theMessage', $traces[0]['body']); + } + + public function testShouldSendCommandWithoutNeedForReply() + { + /** @var ProducerInterface $producer */ + $producer = $this->container->get('enqueue.client.producer'); + + $result = $producer->sendCommand('theCommand', 'theMessage', false); + + $this->assertNull($result); + + $traces = $this->getTraceableProducer()->getTopicTraces(Config::COMMAND_TOPIC); + + $this->assertCount(1, $traces); + $this->assertEquals('theMessage', $traces[0]['body']); + } + + public function testShouldSendMessageInstanceAsCommandWithoutNeedForReply() + { + /** @var ProducerInterface $producer */ + $producer = $this->container->get('enqueue.client.producer'); + + $message = new Message('theMessage'); + + $result = $producer->sendCommand('theCommand', $message, false); + + $this->assertNull($result); + + $traces = $this->getTraceableProducer()->getTopicTraces(Config::COMMAND_TOPIC); + + $this->assertCount(1, $traces); + $this->assertEquals('theMessage', $traces[0]['body']); + $this->assertEquals([ + 'enqueue.topic_name' => Config::COMMAND_TOPIC, + 'enqueue.processor_name' => 'theCommand', + 'enqueue.processor_queue_name' => 'default', + ], $traces[0]['properties']); + } + + public function testShouldSendCommandWithNeedForReply() + { + /** @var ProducerInterface $producer */ + $producer = $this->container->get('enqueue.client.producer'); + + $result = $producer->sendCommand('theCommand', 'theMessage', true); + + $this->assertInstanceOf(Promise::class, $result); + + $traces = $this->getTraceableProducer()->getCommandTraces('theCommand'); + + $this->assertCount(1, $traces); + $this->assertEquals('theMessage', $traces[0]['body']); + } + + public function testShouldSendMessageInstanceCommandWithNeedForReply() + { + /** @var ProducerInterface $producer */ + $producer = $this->container->get('enqueue.client.producer'); + + $message = new Message('theMessage'); + + $result = $producer->sendCommand('theCommand', $message, true); + + $this->assertInstanceOf(Promise::class, $result); + + $traces = $this->getTraceableProducer()->getCommandTraces('theCommand'); + + $this->assertCount(1, $traces); + $this->assertEquals('theMessage', $traces[0]['body']); + $this->assertEquals([ + 'enqueue.topic_name' => Config::COMMAND_TOPIC, + 'enqueue.processor_name' => 'theCommand', + 'enqueue.processor_queue_name' => 'default', + ], $traces[0]['properties']); + } + + /** + * @return TraceableProducer|object + */ + private function getTraceableProducer() + { + return $this->container->get('enqueue.client.producer'); + } } diff --git a/pkg/enqueue-bundle/Tests/Functional/Client/ProducerV2Test.php b/pkg/enqueue-bundle/Tests/Functional/Client/ProducerV2Test.php deleted file mode 100644 index 58bf1aeeb..000000000 --- a/pkg/enqueue-bundle/Tests/Functional/Client/ProducerV2Test.php +++ /dev/null @@ -1,97 +0,0 @@ -container->get('enqueue.client.producer')->clearTraces(); - } - - public function tearDown() - { - parent::tearDown(); - - $this->container->get('enqueue.client.producer')->clearTraces(); - } - - public function testCouldBeGetFromContainerAsService() - { - $producer = $this->container->get('enqueue.client.producer_v2'); - - $this->assertInstanceOf(ProducerV2Interface::class, $producer); - } - - public function testShouldSendEvent() - { - /** @var ProducerV2Interface $producer */ - $producer = $this->container->get('enqueue.client.producer_v2'); - - $producer->sendEvent('theTopic', 'theMessage'); - - $traces = $this->getTraceableProducer()->getTopicTraces('theTopic'); - - $this->assertCount(1, $traces); - $this->assertEquals('theMessage', $traces[0]['body']); - } - - public function testShouldSendCommandWithoutNeedForReply() - { - /** @var ProducerV2Interface $producer */ - $producer = $this->container->get('enqueue.client.producer_v2'); - - $result = $producer->sendCommand('theCommand', 'theMessage', false); - - $this->assertNull($result); - - $traces = $this->getTraceableProducer()->getTopicTraces(Config::COMMAND_TOPIC); - - $this->assertCount(1, $traces); - $this->assertEquals('theMessage', $traces[0]['body']); - $this->assertEquals([ - 'enqueue.topic_name' => Config::COMMAND_TOPIC, - 'enqueue.processor_name' => 'theCommand', - 'enqueue.processor_queue_name' => 'default', - ], $traces[0]['properties']); - } - - public function testShouldSendCommandWithNeedForReply() - { - /** @var ProducerV2Interface $producer */ - $producer = $this->container->get('enqueue.client.producer_v2'); - - $result = $producer->sendCommand('theCommand', 'theMessage', true); - - $this->assertInstanceOf(Promise::class, $result); - - $traces = $this->getTraceableProducer()->getTopicTraces(Config::COMMAND_TOPIC); - - $this->assertCount(1, $traces); - $this->assertEquals('theMessage', $traces[0]['body']); - $this->assertEquals([ - 'enqueue.topic_name' => Config::COMMAND_TOPIC, - 'enqueue.processor_name' => 'theCommand', - 'enqueue.processor_queue_name' => 'default', - ], $traces[0]['properties']); - } - - /** - * @return TraceableProducer|object - */ - private function getTraceableProducer() - { - return $this->container->get('enqueue.client.producer'); - } -} diff --git a/pkg/enqueue-bundle/Tests/Functional/RpcClientTest.php b/pkg/enqueue-bundle/Tests/Functional/RpcClientTest.php index 1a9b9b95c..f9c9fcb41 100644 --- a/pkg/enqueue-bundle/Tests/Functional/RpcClientTest.php +++ b/pkg/enqueue-bundle/Tests/Functional/RpcClientTest.php @@ -2,6 +2,8 @@ namespace Enqueue\Bundle\Tests\Functional; +use Enqueue\Rpc\RpcClient; + /** * @group functional */ @@ -11,13 +13,6 @@ public function testTransportRpcClientCouldBeGetFromContainerAsService() { $connection = $this->container->get('enqueue.transport.rpc_client'); - $this->assertInstanceOf(\Enqueue\Rpc\RpcClient::class, $connection); - } - - public function testClientRpcClientCouldBeGetFromContainerAsService() - { - $connection = $this->container->get('enqueue.client.rpc_client'); - - $this->assertInstanceOf(\Enqueue\Client\RpcClient::class, $connection); + $this->assertInstanceOf(RpcClient::class, $connection); } } diff --git a/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php b/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php index 38fca16f7..c4a298af9 100644 --- a/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php +++ b/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php @@ -167,7 +167,7 @@ public function testProducerSendsMessage(array $enqueueConfig) { $this->customSetUp($enqueueConfig); - $this->getMessageProducer()->send(TestProcessor::TOPIC, 'test message body'); + $this->getMessageProducer()->sendEvent(TestProcessor::TOPIC, 'test message body'); $queue = $this->getPsrContext()->createQueue('enqueue.test'); @@ -189,7 +189,7 @@ public function testClientConsumeMessagesFromExplicitlySetQueue(array $enqueueCo $command = $this->container->get('enqueue.client.consume_messages_command'); $processor = $this->container->get('test.message.processor'); - $this->getMessageProducer()->send(TestProcessor::TOPIC, 'test message body'); + $this->getMessageProducer()->sendEvent(TestProcessor::TOPIC, 'test message body'); $tester = new CommandTester($command); $tester->execute([ @@ -213,7 +213,7 @@ public function testTransportConsumeMessagesCommandShouldConsumeMessage(array $e $command->setContainer($this->container); $processor = $this->container->get('test.message.processor'); - $this->getMessageProducer()->send(TestProcessor::TOPIC, 'test message body'); + $this->getMessageProducer()->sendEvent(TestProcessor::TOPIC, 'test message body'); $tester = new CommandTester($command); $tester->execute([ diff --git a/pkg/enqueue-bundle/Tests/Unit/Events/AsyncListenerTest.php b/pkg/enqueue-bundle/Tests/Unit/Events/AsyncListenerTest.php index 19879031e..0b0176156 100644 --- a/pkg/enqueue-bundle/Tests/Unit/Events/AsyncListenerTest.php +++ b/pkg/enqueue-bundle/Tests/Unit/Events/AsyncListenerTest.php @@ -25,7 +25,7 @@ public function testShouldDoNothingIfSyncModeOn() $producer = $this->createProducerMock(); $producer ->expects($this->never()) - ->method('send') + ->method('sendEvent') ; $registry = $this->createRegistryMock(); @@ -74,7 +74,7 @@ public function testShouldSendMessageIfSyncModeOff() $producer = $this->createProducerMock(); $producer ->expects($this->once()) - ->method('send') + ->method('sendEvent') ->with('event.fooEvent', $this->identicalTo($message)) ; diff --git a/pkg/enqueue/Client/Producer.php b/pkg/enqueue/Client/Producer.php index ade90049f..7f066b794 100644 --- a/pkg/enqueue/Client/Producer.php +++ b/pkg/enqueue/Client/Producer.php @@ -2,10 +2,11 @@ namespace Enqueue\Client; +use Enqueue\Rpc\RpcFactory; use Enqueue\Util\JSON; use Enqueue\Util\UUID; -class Producer implements ProducerInterface +class Producer implements ProducerInterface, ProducerV2Interface { /** * @var DriverInterface @@ -18,18 +19,31 @@ class Producer implements ProducerInterface private $extension; /** - * @param DriverInterface $driver + * @var RpcFactory */ - public function __construct(DriverInterface $driver, ExtensionInterface $extension = null) - { + private $rpcFactory; + + /** + * @param DriverInterface $driver + * @param ExtensionInterface|null $extension + * @param RpcFactory $rpcFactory + * + * @internal param RpcClient $rpcClient + */ + public function __construct( + DriverInterface $driver, + RpcFactory $rpcFactory, + ExtensionInterface $extension = null + ) { $this->driver = $driver; + $this->rpcFactory = $rpcFactory; $this->extension = $extension ?: new ChainExtension([]); } /** * {@inheritdoc} */ - public function send($topic, $message) + public function sendEvent($topic, $message) { if (false == $message instanceof Message) { $body = $message; @@ -80,6 +94,51 @@ public function send($topic, $message) } } + /** + * {@inheritdoc} + */ + public function sendCommand($command, $message, $needReply = false) + { + if (false == $message instanceof Message) { + $message = new Message($message); + } + + if ($needReply) { + $deleteReplyQueue = false; + $replyTo = $message->getReplyTo(); + + if (false == $replyTo) { + $message->setReplyTo($replyTo = $this->rpcFactory->createReplyTo()); + $deleteReplyQueue = true; + } + + if (false == $message->getCorrelationId()) { + $message->setCorrelationId(UUID::generate()); + } + } + + $message->setProperty(Config::PARAMETER_TOPIC_NAME, Config::COMMAND_TOPIC); + $message->setProperty(Config::PARAMETER_PROCESSOR_NAME, $command); + $message->setScope(Message::SCOPE_APP); + + $this->sendEvent(Config::COMMAND_TOPIC, $message); + + if ($needReply) { + $promise = $this->rpcFactory->createPromise($replyTo, $message->getCorrelationId(), 60000); + $promise->setDeleteReplyQueue($deleteReplyQueue); + + return $promise; + } + } + + /** + * {@inheritdoc} + */ + public function send($topic, $message) + { + $this->sendEvent($topic, $message); + } + /** * @param Message $message */ diff --git a/pkg/enqueue/Client/ProducerInterface.php b/pkg/enqueue/Client/ProducerInterface.php index 67bfaf033..bc8426704 100644 --- a/pkg/enqueue/Client/ProducerInterface.php +++ b/pkg/enqueue/Client/ProducerInterface.php @@ -2,9 +2,28 @@ namespace Enqueue\Client; +use Enqueue\Rpc\Promise; + interface ProducerInterface { /** + * @param string $topic + * @param string|array|Message $message + */ + public function sendEvent($topic, $message); + + /** + * @param string $command + * @param string|array|Message $message + * @param bool $needReply + * + * @return Promise|null the promise is returned if needReply argument is true + */ + public function sendCommand($command, $message, $needReply = false); + + /** + * @deprecated use sendEvent method. + * * Sends a message to a topic. There are some message processor may be subscribed to a topic. * * @param string $topic diff --git a/pkg/enqueue/Client/ProducerV2.php b/pkg/enqueue/Client/ProducerV2.php deleted file mode 100644 index 8a69dfd27..000000000 --- a/pkg/enqueue/Client/ProducerV2.php +++ /dev/null @@ -1,57 +0,0 @@ -realProducer = $realProducer; - $this->rpcClient = $rpcClient; - } - - /** - * {@inheritdoc} - */ - public function sendEvent($topic, $message) - { - $this->realProducer->send($topic, $message); - } - - /** - * {@inheritdoc} - */ - public function sendCommand($command, $message, $needReply = false) - { - if (false == $message instanceof Message) { - $message = new Message($message); - } - - $message->setProperty(Config::PARAMETER_TOPIC_NAME, Config::COMMAND_TOPIC); - $message->setProperty(Config::PARAMETER_PROCESSOR_NAME, $command); - $message->setScope(Message::SCOPE_APP); - - if ($needReply) { - return $this->rpcClient->callAsync(Config::COMMAND_TOPIC, $message, 60000); - } - - $this->realProducer->send(Config::COMMAND_TOPIC, $message); - } -} diff --git a/pkg/enqueue/Client/ProducerV2Interface.php b/pkg/enqueue/Client/ProducerV2Interface.php index 4013bdbe3..c661f0c47 100644 --- a/pkg/enqueue/Client/ProducerV2Interface.php +++ b/pkg/enqueue/Client/ProducerV2Interface.php @@ -2,25 +2,11 @@ namespace Enqueue\Client; -use Enqueue\Rpc\Promise; - /** * @experimental + * + * @deprecated */ -interface ProducerV2Interface +interface ProducerV2Interface extends ProducerInterface { - /** - * @param string $topic - * @param string|array|Message $message - */ - public function sendEvent($topic, $message); - - /** - * @param string $command - * @param string|array|Message $message - * @param bool $needReply - * - * @return Promise|null the promise is returned if needReply argument is true - */ - public function sendCommand($command, $message, $needReply = false); } diff --git a/pkg/enqueue/Client/RpcClient.php b/pkg/enqueue/Client/RpcClient.php deleted file mode 100644 index 6ca123a6a..000000000 --- a/pkg/enqueue/Client/RpcClient.php +++ /dev/null @@ -1,130 +0,0 @@ -context = $context; - $this->producer = $producer; - } - - /** - * @param string $topic - * @param string|array|Message $message - * @param int $timeout - * - * @return PsrMessage - */ - public function call($topic, $message, $timeout) - { - return $this->callAsync($topic, $message, $timeout)->receive(); - } - - /** - * @param string $topic - * @param string|array|Message $message $message - * @param int $timeout - * - * @return Promise - */ - public function callAsync($topic, $message, $timeout) - { - if ($timeout < 1) { - throw new \InvalidArgumentException(sprintf('Timeout must be positive not zero integer. Got %s', $timeout)); - } - - if (false == $message instanceof Message) { - $body = $message; - $message = new Message(); - $message->setBody($body); - } - - if ($message->getReplyTo()) { - $replyQueue = $this->context->createQueue($message->getReplyTo()); - $deleteReplyQueue = false; - } else { - $replyQueue = $this->context->createTemporaryQueue(); - $message->setReplyTo($replyQueue->getQueueName()); - $deleteReplyQueue = true; - } - - if (false == $message->getCorrelationId()) { - $message->setCorrelationId(UUID::generate()); - } - - $this->producer->send($topic, $message); - - $correlationId = $message->getCorrelationId(); - - $receive = function (Promise $promise, $promiseTimeout) use ($replyQueue, $timeout, $correlationId) { - $runTimeout = $promiseTimeout ?: $timeout; - $endTime = time() + ((int) ($runTimeout / 1000)); - $consumer = $this->context->createConsumer($replyQueue); - - do { - if ($message = $consumer->receive($runTimeout)) { - if ($message->getCorrelationId() === $correlationId) { - $consumer->acknowledge($message); - - return $message; - } - - $consumer->reject($message, true); - } - } while (time() < $endTime); - - throw TimeoutException::create($runTimeout, $correlationId); - }; - - $receiveNoWait = function () use ($replyQueue, $correlationId) { - static $consumer; - - if (null === $consumer) { - $consumer = $this->context->createConsumer($replyQueue); - } - - if ($message = $consumer->receiveNoWait()) { - if ($message->getCorrelationId() === $correlationId) { - $consumer->acknowledge($message); - - return $message; - } - - $consumer->reject($message, true); - } - }; - - $finally = function (Promise $promise) use ($replyQueue) { - if ($promise->isDeleteReplyQueue() && method_exists($this->context, 'deleteQueue')) { - $this->context->deleteQueue($replyQueue); - } - }; - - $promise = new Promise($receive, $receiveNoWait, $finally); - $promise->setDeleteReplyQueue($deleteReplyQueue); - - return $promise; - } -} diff --git a/pkg/enqueue/Client/SpoolProducer.php b/pkg/enqueue/Client/SpoolProducer.php index 43e9a4e4d..c5388770f 100644 --- a/pkg/enqueue/Client/SpoolProducer.php +++ b/pkg/enqueue/Client/SpoolProducer.php @@ -12,7 +12,12 @@ class SpoolProducer implements ProducerInterface /** * @var array */ - private $queue; + private $events; + + /** + * @var array + */ + private $commands; /** * @param ProducerInterface $realProducer @@ -21,7 +26,28 @@ public function __construct(ProducerInterface $realProducer) { $this->realProducer = $realProducer; - $this->queue = new \SplQueue(); + $this->events = new \SplQueue(); + $this->commands = new \SplQueue(); + } + + /** + * {@inheritdoc} + */ + public function sendCommand($command, $message, $needReply = false) + { + if ($needReply) { + return $this->realProducer->sendCommand($command, $message, $needReply); + } + + $this->commands->enqueue([$command, $message]); + } + + /** + * {@inheritdoc} + */ + public function sendEvent($topic, $message) + { + $this->events->enqueue([$topic, $message]); } /** @@ -29,7 +55,7 @@ public function __construct(ProducerInterface $realProducer) */ public function send($topic, $message) { - $this->queue->enqueue([$topic, $message]); + $this->sendEvent($topic, $message); } /** @@ -37,10 +63,16 @@ public function send($topic, $message) */ public function flush() { - while (false == $this->queue->isEmpty()) { - list($topic, $message) = $this->queue->dequeue(); + while (false == $this->events->isEmpty()) { + list($topic, $message) = $this->events->dequeue(); + + $this->realProducer->sendEvent($topic, $message); + } + + while (false == $this->commands->isEmpty()) { + list($command, $message) = $this->commands->dequeue(); - $this->realProducer->send($topic, $message); + $this->realProducer->sendCommand($command, $message); } } } diff --git a/pkg/enqueue/Client/TraceableProducer.php b/pkg/enqueue/Client/TraceableProducer.php index 20108248c..cd55a91d6 100644 --- a/pkg/enqueue/Client/TraceableProducer.php +++ b/pkg/enqueue/Client/TraceableProducer.php @@ -24,35 +24,31 @@ public function __construct(ProducerInterface $producer) /** * {@inheritdoc} */ - public function send($topic, $message) + public function sendEvent($topic, $message) { - $this->producer->send($topic, $message); + $this->producer->sendEvent($topic, $message); - $trace = [ - 'topic' => $topic, - 'body' => $message, - 'headers' => [], - 'properties' => [], - 'priority' => null, - 'expire' => null, - 'delay' => null, - 'timestamp' => null, - 'contentType' => null, - 'messageId' => null, - ]; - if ($message instanceof Message) { - $trace['body'] = $message->getBody(); - $trace['headers'] = $message->getHeaders(); - $trace['properties'] = $message->getProperties(); - $trace['priority'] = $message->getPriority(); - $trace['expire'] = $message->getExpire(); - $trace['delay'] = $message->getDelay(); - $trace['timestamp'] = $message->getTimestamp(); - $trace['contentType'] = $message->getContentType(); - $trace['messageId'] = $message->getMessageId(); - } + $this->collectTrace($topic, null, $message); + } - $this->traces[] = $trace; + /** + * {@inheritdoc} + */ + public function sendCommand($command, $message, $needReply = false) + { + $result = $this->producer->sendCommand($command, $message, $needReply); + + $this->collectTrace(Config::COMMAND_TOPIC, $command, $message); + + return $result; + } + + /** + * {@inheritdoc} + */ + public function send($topic, $message) + { + $this->sendEvent($topic, $message); } /** @@ -72,6 +68,23 @@ public function getTopicTraces($topic) return $topicTraces; } + /** + * @param string $command + * + * @return array + */ + public function getCommandTraces($command) + { + $commandTraces = []; + foreach ($this->traces as $trace) { + if ($command == $trace['command']) { + $commandTraces[] = $trace; + } + } + + return $commandTraces; + } + /** * @return array */ @@ -84,4 +97,39 @@ public function clearTraces() { $this->traces = []; } + + /** + * @param string|null $topic + * @param string|null $command + * @param mixed $message + */ + private function collectTrace($topic, $command, $message) + { + $trace = [ + 'topic' => $topic, + 'command' => $command, + 'body' => $message, + 'headers' => [], + 'properties' => [], + 'priority' => null, + 'expire' => null, + 'delay' => null, + 'timestamp' => null, + 'contentType' => null, + 'messageId' => null, + ]; + if ($message instanceof Message) { + $trace['body'] = $message->getBody(); + $trace['headers'] = $message->getHeaders(); + $trace['properties'] = $message->getProperties(); + $trace['priority'] = $message->getPriority(); + $trace['expire'] = $message->getExpire(); + $trace['delay'] = $message->getDelay(); + $trace['timestamp'] = $message->getTimestamp(); + $trace['contentType'] = $message->getContentType(); + $trace['messageId'] = $message->getMessageId(); + } + + $this->traces[] = $trace; + } } diff --git a/pkg/enqueue/Rpc/RpcClient.php b/pkg/enqueue/Rpc/RpcClient.php index 578ca289c..66d9e5cb2 100644 --- a/pkg/enqueue/Rpc/RpcClient.php +++ b/pkg/enqueue/Rpc/RpcClient.php @@ -14,12 +14,19 @@ class RpcClient */ private $context; + /** + * @var RpcFactory + */ + private $rpcFactory; + /** * @param PsrContext $context + * @param RpcFactory $promiseFactory */ - public function __construct(PsrContext $context) + public function __construct(PsrContext $context, RpcFactory $promiseFactory = null) { $this->context = $context; + $this->rpcFactory = $promiseFactory ?: new RpcFactory($context); } /** @@ -49,12 +56,11 @@ public function callAsync(PsrDestination $destination, PsrMessage $message, $tim throw new \InvalidArgumentException(sprintf('Timeout must be positive not zero integer. Got %s', $timeout)); } - if ($message->getReplyTo()) { - $replyQueue = $this->context->createQueue($message->getReplyTo()); - $deleteReplyQueue = false; - } else { - $replyQueue = $this->context->createTemporaryQueue(); - $message->setReplyTo($replyQueue->getQueueName()); + $deleteReplyQueue = false; + $replyTo = $message->getReplyTo(); + + if (false == $replyTo) { + $message->setReplyTo($replyTo = $this->rpcFactory->createReplyTo()); $deleteReplyQueue = true; } @@ -64,53 +70,7 @@ public function callAsync(PsrDestination $destination, PsrMessage $message, $tim $this->context->createProducer()->send($destination, $message); - $correlationId = $message->getCorrelationId(); - - $receive = function (Promise $promise, $promiseTimeout) use ($replyQueue, $timeout, $correlationId) { - $runTimeout = $promiseTimeout ?: $timeout; - $endTime = time() + ((int) ($runTimeout / 1000)); - $consumer = $this->context->createConsumer($replyQueue); - - do { - if ($message = $consumer->receive($runTimeout)) { - if ($message->getCorrelationId() === $correlationId) { - $consumer->acknowledge($message); - - return $message; - } - - $consumer->reject($message, true); - } - } while (time() < $endTime); - - throw TimeoutException::create($runTimeout, $correlationId); - }; - - $receiveNoWait = function () use ($replyQueue, $correlationId) { - static $consumer; - - if (null === $consumer) { - $consumer = $this->context->createConsumer($replyQueue); - } - - if ($message = $consumer->receiveNoWait()) { - if ($message->getCorrelationId() === $correlationId) { - $consumer->acknowledge($message); - - return $message; - } - - $consumer->reject($message, true); - } - }; - - $finally = function (Promise $promise) use ($replyQueue) { - if ($promise->isDeleteReplyQueue() && method_exists($this->context, 'deleteQueue')) { - $this->context->deleteQueue($replyQueue); - } - }; - - $promise = new Promise($receive, $receiveNoWait, $finally); + $promise = $this->rpcFactory->createPromise($replyTo, $message->getCorrelationId(), $timeout); $promise->setDeleteReplyQueue($deleteReplyQueue); return $promise; diff --git a/pkg/enqueue/Rpc/RpcFactory.php b/pkg/enqueue/Rpc/RpcFactory.php new file mode 100644 index 000000000..9c1e0efb5 --- /dev/null +++ b/pkg/enqueue/Rpc/RpcFactory.php @@ -0,0 +1,87 @@ +context = $context; + } + + /** + * @param string $replyTo + * @param string $correlationId + * @param int $timeout + * + * @return Promise + */ + public function createPromise($replyTo, $correlationId, $timeout) + { + $replyQueue = $this->context->createQueue($replyTo); + + $receive = function (Promise $promise, $promiseTimeout) use ($replyQueue, $timeout, $correlationId) { + $runTimeout = $promiseTimeout ?: $timeout; + $endTime = time() + ((int) ($runTimeout / 1000)); + $consumer = $this->context->createConsumer($replyQueue); + + do { + if ($message = $consumer->receive($runTimeout)) { + if ($message->getCorrelationId() === $correlationId) { + $consumer->acknowledge($message); + + return $message; + } + + $consumer->reject($message, true); + } + } while (time() < $endTime); + + throw TimeoutException::create($runTimeout, $correlationId); + }; + + $receiveNoWait = function () use ($replyQueue, $correlationId) { + static $consumer; + + if (null === $consumer) { + $consumer = $this->context->createConsumer($replyQueue); + } + + if ($message = $consumer->receiveNoWait()) { + if ($message->getCorrelationId() === $correlationId) { + $consumer->acknowledge($message); + + return $message; + } + + $consumer->reject($message, true); + } + }; + + $finally = function (Promise $promise) use ($replyQueue) { + if ($promise->isDeleteReplyQueue() && method_exists($this->context, 'deleteQueue')) { + $this->context->deleteQueue($replyQueue); + } + }; + + return new Promise($receive, $receiveNoWait, $finally); + } + + /** + * @return string + */ + public function createReplyTo() + { + return $this->context->createTemporaryQueue()->getQueueName(); + } +} diff --git a/pkg/enqueue/Symfony/Client/ProduceMessageCommand.php b/pkg/enqueue/Symfony/Client/ProduceMessageCommand.php index 1d84a9cb1..a9f9ef90a 100644 --- a/pkg/enqueue/Symfony/Client/ProduceMessageCommand.php +++ b/pkg/enqueue/Symfony/Client/ProduceMessageCommand.php @@ -44,7 +44,7 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { - $this->producer->send( + $this->producer->sendEvent( $input->getArgument('topic'), $input->getArgument('message') ); diff --git a/pkg/enqueue/Tests/Client/ProducerTest.php b/pkg/enqueue/Tests/Client/ProducerTest.php index f0edb98bf..c9a30fcde 100644 --- a/pkg/enqueue/Tests/Client/ProducerTest.php +++ b/pkg/enqueue/Tests/Client/ProducerTest.php @@ -10,6 +10,7 @@ use Enqueue\Client\Producer; use Enqueue\Client\ProducerInterface; use Enqueue\Null\NullQueue; +use Enqueue\Rpc\RpcFactory; use Enqueue\Test\ClassExtensionTrait; use PHPUnit\Framework\TestCase; @@ -24,7 +25,7 @@ public function testShouldImplementProducerInterface() public function testCouldBeConstructedWithDriverAsFirstArgument() { - new Producer($this->createDriverStub()); + new Producer($this->createDriverStub(), $this->createRpcFactory()); } public function testShouldSendMessageToRouter() @@ -38,8 +39,8 @@ public function testShouldSendMessageToRouter() ->with(self::identicalTo($message)) ; - $producer = new Producer($driver); - $producer->send('topic', $message); + $producer = new Producer($driver, $this->createRpcFactory()); + $producer->sendEvent('topic', $message); $expectedProperties = [ 'enqueue.topic_name' => 'topic', @@ -59,8 +60,8 @@ public function testShouldSendMessageWithNormalPriorityByDefault() ->with(self::identicalTo($message)) ; - $producer = new Producer($driver); - $producer->send('topic', $message); + $producer = new Producer($driver, $this->createRpcFactory()); + $producer->sendEvent('topic', $message); self::assertSame(MessagePriority::NORMAL, $message->getPriority()); } @@ -77,8 +78,8 @@ public function testShouldSendMessageWithCustomPriority() ->with(self::identicalTo($message)) ; - $producer = new Producer($driver); - $producer->send('topic', $message); + $producer = new Producer($driver, $this->createRpcFactory()); + $producer->sendEvent('topic', $message); self::assertSame(MessagePriority::HIGH, $message->getPriority()); } @@ -94,8 +95,8 @@ public function testShouldSendMessageWithGeneratedMessageId() ->with(self::identicalTo($message)) ; - $producer = new Producer($driver); - $producer->send('topic', $message); + $producer = new Producer($driver, $this->createRpcFactory()); + $producer->sendEvent('topic', $message); self::assertNotEmpty($message->getMessageId()); } @@ -112,8 +113,8 @@ public function testShouldSendMessageWithCustomMessageId() ->with(self::identicalTo($message)) ; - $producer = new Producer($driver); - $producer->send('topic', $message); + $producer = new Producer($driver, $this->createRpcFactory()); + $producer->sendEvent('topic', $message); self::assertSame('theCustomMessageId', $message->getMessageId()); } @@ -129,8 +130,8 @@ public function testShouldSendMessageWithGeneratedTimestamp() ->with(self::identicalTo($message)) ; - $producer = new Producer($driver); - $producer->send('topic', $message); + $producer = new Producer($driver, $this->createRpcFactory()); + $producer->sendEvent('topic', $message); self::assertNotEmpty($message->getTimestamp()); } @@ -147,8 +148,8 @@ public function testShouldSendMessageWithCustomTimestamp() ->with(self::identicalTo($message)) ; - $producer = new Producer($driver); - $producer->send('topic', $message); + $producer = new Producer($driver, $this->createRpcFactory()); + $producer->sendEvent('topic', $message); self::assertSame('theCustomTimestamp', $message->getTimestamp()); } @@ -165,8 +166,8 @@ public function testShouldSendStringAsPlainText() }) ; - $producer = new Producer($driver); - $producer->send('topic', 'theStringMessage'); + $producer = new Producer($driver, $this->createRpcFactory()); + $producer->sendEvent('topic', 'theStringMessage'); } public function testShouldSendArrayAsJsonString() @@ -181,8 +182,8 @@ public function testShouldSendArrayAsJsonString() }) ; - $producer = new Producer($driver); - $producer->send('topic', ['foo' => 'fooVal']); + $producer = new Producer($driver, $this->createRpcFactory()); + $producer->sendEvent('topic', ['foo' => 'fooVal']); } public function testShouldConvertMessageArrayBodyJsonString() @@ -200,8 +201,8 @@ public function testShouldConvertMessageArrayBodyJsonString() }) ; - $producer = new Producer($driver); - $producer->send('topic', $message); + $producer = new Producer($driver, $this->createRpcFactory()); + $producer->sendEvent('topic', $message); } public function testSendShouldForceScalarsToStringAndSetTextContentType() @@ -220,8 +221,8 @@ public function testSendShouldForceScalarsToStringAndSetTextContentType() }) ; - $producer = new Producer($driver); - $producer->send($queue, 12345); + $producer = new Producer($driver, $this->createRpcFactory()); + $producer->sendEvent($queue, 12345); } public function testSendShouldForceMessageScalarsBodyToStringAndSetTextContentType() @@ -243,8 +244,8 @@ public function testSendShouldForceMessageScalarsBodyToStringAndSetTextContentTy }) ; - $producer = new Producer($driver); - $producer->send($queue, $message); + $producer = new Producer($driver, $this->createRpcFactory()); + $producer->sendEvent($queue, $message); } public function testSendShouldForceNullToEmptyStringAndSetTextContentType() @@ -263,8 +264,8 @@ public function testSendShouldForceNullToEmptyStringAndSetTextContentType() }) ; - $producer = new Producer($driver); - $producer->send($queue, null); + $producer = new Producer($driver, $this->createRpcFactory()); + $producer->sendEvent($queue, null); } public function testSendShouldForceNullBodyToEmptyStringAndSetTextContentType() @@ -286,8 +287,8 @@ public function testSendShouldForceNullBodyToEmptyStringAndSetTextContentType() }) ; - $producer = new Producer($driver); - $producer->send($queue, $message); + $producer = new Producer($driver, $this->createRpcFactory()); + $producer->sendEvent($queue, $message); } public function testShouldThrowExceptionIfBodyIsObjectOnSend() @@ -302,12 +303,12 @@ public function testShouldThrowExceptionIfBodyIsObjectOnSend() ->method('sendToProcessor') ; - $producer = new Producer($driver); + $producer = new Producer($driver, $this->createRpcFactory()); $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('The message\'s body must be either null, scalar, array or object (implements \JsonSerializable). Got: stdClass'); - $producer->send('topic', new \stdClass()); + $producer->sendEvent('topic', new \stdClass()); } public function testShouldThrowExceptionIfBodyIsArrayWithObjectsInsideOnSend() @@ -324,12 +325,12 @@ public function testShouldThrowExceptionIfBodyIsArrayWithObjectsInsideOnSend() ->method('sendToProcessor') ; - $producer = new Producer($driver); + $producer = new Producer($driver, $this->createRpcFactory()); $this->expectException(\LogicException::class); $this->expectExceptionMessage('The message\'s body must be an array of scalars. Found not scalar in the array: stdClass'); - $producer->send($queue, ['foo' => new \stdClass()]); + $producer->sendEvent($queue, ['foo' => new \stdClass()]); } public function testShouldThrowExceptionIfBodyIsArrayWithObjectsInSubArraysInsideOnSend() @@ -346,12 +347,12 @@ public function testShouldThrowExceptionIfBodyIsArrayWithObjectsInSubArraysInsid ->method('sendToProcessor') ; - $producer = new Producer($driver); + $producer = new Producer($driver, $this->createRpcFactory()); $this->expectException(\LogicException::class); $this->expectExceptionMessage('The message\'s body must be an array of scalars. Found not scalar in the array: stdClass'); - $producer->send($queue, ['foo' => ['bar' => new \stdClass()]]); + $producer->sendEvent($queue, ['foo' => ['bar' => new \stdClass()]]); } public function testShouldSendJsonSerializableObjectAsJsonStringToMessageBus() @@ -368,8 +369,8 @@ public function testShouldSendJsonSerializableObjectAsJsonStringToMessageBus() }) ; - $producer = new Producer($driver); - $producer->send('topic', $object); + $producer = new Producer($driver, $this->createRpcFactory()); + $producer->sendEvent('topic', $object); } public function testShouldSendMessageJsonSerializableBodyAsJsonStringToMessageBus() @@ -389,8 +390,8 @@ public function testShouldSendMessageJsonSerializableBodyAsJsonStringToMessageBu }) ; - $producer = new Producer($driver); - $producer->send('topic', $message); + $producer = new Producer($driver, $this->createRpcFactory()); + $producer->sendEvent('topic', $message); } public function testThrowIfTryToSendMessageToMessageBusWithProcessorNamePropertySet() @@ -411,11 +412,11 @@ public function testThrowIfTryToSendMessageToMessageBusWithProcessorNameProperty ->method('sendToProcessor') ; - $producer = new Producer($driver); + $producer = new Producer($driver, $this->createRpcFactory()); $this->expectException(\LogicException::class); $this->expectExceptionMessage('The enqueue.processor_name property must not be set for messages that are sent to message bus.'); - $producer->send('topic', $message); + $producer->sendEvent('topic', $message); } public function testThrowIfTryToSendMessageToMessageBusWithProcessorQueueNamePropertySet() @@ -436,11 +437,11 @@ public function testThrowIfTryToSendMessageToMessageBusWithProcessorQueueNamePro ->method('sendToProcessor') ; - $producer = new Producer($driver); + $producer = new Producer($driver, $this->createRpcFactory()); $this->expectException(\LogicException::class); $this->expectExceptionMessage('The enqueue.processor_queue_name property must not be set for messages that are sent to message bus.'); - $producer->send('topic', $message); + $producer->sendEvent('topic', $message); } public function testThrowIfNotApplicationJsonContentTypeSetWithJsonSerializableBody() @@ -464,8 +465,8 @@ public function testThrowIfNotApplicationJsonContentTypeSetWithJsonSerializableB $this->expectException(\LogicException::class); $this->expectExceptionMessage('Content type "application/json" only allowed when body is array'); - $producer = new Producer($driver); - $producer->send('topic', $message); + $producer = new Producer($driver, $this->createRpcFactory()); + $producer->sendEvent('topic', $message); } public function testShouldSendMessageToApplicationRouter() @@ -489,8 +490,8 @@ public function testShouldSendMessageToApplicationRouter() }) ; - $producer = new Producer($driver); - $producer->send('topic', $message); + $producer = new Producer($driver, $this->createRpcFactory()); + $producer->sendEvent('topic', $message); } public function testShouldSendToCustomMessageToApplicationRouter() @@ -516,8 +517,8 @@ public function testShouldSendToCustomMessageToApplicationRouter() }) ; - $producer = new Producer($driver); - $producer->send('topic', $message); + $producer = new Producer($driver, $this->createRpcFactory()); + $producer->sendEvent('topic', $message); } public function testThrowIfUnSupportedScopeGivenOnSend() @@ -535,11 +536,11 @@ public function testThrowIfUnSupportedScopeGivenOnSend() ->method('sendToProcessor') ; - $producer = new Producer($driver); + $producer = new Producer($driver, $this->createRpcFactory()); $this->expectException(\LogicException::class); $this->expectExceptionMessage('The message scope "iDontKnowScope" is not supported.'); - $producer->send('topic', $message); + $producer->sendEvent('topic', $message); } public function testShouldCallPreSendPostSendExtensionMethodsWhenSendToRouter() @@ -566,8 +567,8 @@ public function testShouldCallPreSendPostSendExtensionMethodsWhenSendToRouter() ->method('sendToRouter') ; - $producer = new Producer($driver, $extension); - $producer->send('topic', $message); + $producer = new Producer($driver, $this->createRpcFactory(), $extension); + $producer->sendEvent('topic', $message); } public function testShouldCallPreSendPostSendExtensionMethodsWhenSendToProcessor() @@ -594,14 +595,22 @@ public function testShouldCallPreSendPostSendExtensionMethodsWhenSendToProcessor ->method('sendToProcessor') ; - $producer = new Producer($driver, $extension); - $producer->send('topic', $message); + $producer = new Producer($driver, $this->createRpcFactory(), $extension); + $producer->sendEvent('topic', $message); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|RpcFactory + */ + private function createRpcFactory() + { + return $this->createMock(RpcFactory::class); } /** * @return \PHPUnit_Framework_MockObject_MockObject|DriverInterface */ - protected function createDriverStub() + private function createDriverStub() { $config = new Config( 'a_prefix', diff --git a/pkg/enqueue/Tests/Client/RpcClientTest.php b/pkg/enqueue/Tests/Client/RpcClientTest.php deleted file mode 100644 index 3b75bc56f..000000000 --- a/pkg/enqueue/Tests/Client/RpcClientTest.php +++ /dev/null @@ -1,363 +0,0 @@ -createProducerMock(), - $this->createPsrContextMock() - ); - } - - public function testShouldSetReplyToIfNotSet() - { - $context = new NullContext(); - - $producerMock = $this->createProducerMock(); - $producerMock - ->expects($this->once()) - ->method('send') - ->willReturnCallback(function ($topic, Message $message) { - $this->assertNotEmpty($message->getReplyTo()); - }) - ; - - $rpc = new RpcClient( - $producerMock, - $context - ); - - $rpc->callAsync('aTopic', new Message(), 2); - } - - public function testShouldNotSetReplyToIfSet() - { - $context = new NullContext(); - - $message = new Message(); - $message->setReplyTo('theReplyTo'); - - $producerMock = $this->createProducerMock(); - $producerMock - ->expects($this->once()) - ->method('send') - ->willReturnCallback(function ($topic, Message $message) { - $this->assertEquals('theReplyTo', $message->getReplyTo()); - }) - ; - - $rpc = new RpcClient( - $producerMock, - $context - ); - - $rpc->callAsync('aTopic', $message, 2); - } - - public function testShouldUseSameTopicOnProducerSendCall() - { - $context = new NullContext(); - - $producerMock = $this->createProducerMock(); - $producerMock - ->expects($this->once()) - ->method('send') - ->willReturnCallback(function ($topic) { - $this->assertEquals('theTopic', $topic); - }) - ; - - $rpc = new RpcClient( - $producerMock, - $context - ); - - $rpc->callAsync('theTopic', new Message(), 2); - } - - public function testShouldSetCorrelationIdIfNotSet() - { - $context = new NullContext(); - - $producerMock = $this->createProducerMock(); - $producerMock - ->expects($this->once()) - ->method('send') - ->willReturnCallback(function ($topic, Message $message) { - $this->assertNotEmpty($message->getCorrelationId()); - }) - ; - - $rpc = new RpcClient( - $producerMock, - $context - ); - - $rpc->callAsync('aTopic', new Message(), 2); - } - - public function testShouldNotSetCorrelationIdIfSet() - { - $context = new NullContext(); - - $message = new Message(); - $message->setCorrelationId('theCorrelationId'); - - $producerMock = $this->createProducerMock(); - $producerMock - ->expects($this->once()) - ->method('send') - ->willReturnCallback(function ($topic, Message $message) { - $this->assertEquals('theCorrelationId', $message->getCorrelationId()); - }) - ; - - $rpc = new RpcClient( - $producerMock, - $context - ); - - $rpc->callAsync('aTopic', $message, 2); - } - - public function testShouldDoSyncCall() - { - $timeout = 123; - $replyMessage = new NullMessage(); - - $promiseMock = $this->createMock(Promise::class); - $promiseMock - ->expects($this->once()) - ->method('receive') - ->willReturn($replyMessage) - ; - - /** @var \PHPUnit_Framework_MockObject_MockObject|RpcClient $rpc */ - $rpc = $this->getMockBuilder(RpcClient::class)->disableOriginalConstructor()->setMethods(['callAsync'])->getMock(); - $rpc - ->expects($this->once()) - ->method('callAsync') - ->with('theTopic', 'theMessage', $timeout) - ->willReturn($promiseMock) - ; - - $actualReplyMessage = $rpc->call('theTopic', 'theMessage', $timeout); - - $this->assertSame($replyMessage, $actualReplyMessage); - } - - public function testShouldReceiveMessageAndAckMessageIfCorrelationEquals() - { - $replyQueue = new NullQueue('theReplyTo'); - $message = new Message(); - $message->setCorrelationId('theCorrelationId'); - $message->setReplyTo('theReplyTo'); - - $receivedMessage = new NullMessage(); - $receivedMessage->setCorrelationId('theCorrelationId'); - - $consumer = $this->createPsrConsumerMock(); - $consumer - ->expects($this->once()) - ->method('receive') - ->with(12345) - ->willReturn($receivedMessage) - ; - $consumer - ->expects($this->once()) - ->method('acknowledge') - ->with($this->identicalTo($receivedMessage)) - ; - $consumer - ->expects($this->never()) - ->method('reject') - ; - - $context = $this->createPsrContextMock(); - $context - ->expects($this->once()) - ->method('createQueue') - ->with('theReplyTo') - ->willReturn($replyQueue) - ; - $context - ->expects($this->once()) - ->method('createConsumer') - ->with($this->identicalTo($replyQueue)) - ->willReturn($consumer) - ; - - $rpc = new RpcClient($this->createProducerMock(), $context); - - $rpc->callAsync('topic', $message, 2)->receive(12345); - } - - public function testShouldReceiveNoWaitMessageAndAckMessageIfCorrelationEquals() - { - $replyQueue = new NullQueue('theReplyTo'); - $message = new Message(); - $message->setCorrelationId('theCorrelationId'); - $message->setReplyTo('theReplyTo'); - - $receivedMessage = new NullMessage(); - $receivedMessage->setCorrelationId('theCorrelationId'); - - $consumer = $this->createPsrConsumerMock(); - $consumer - ->expects($this->once()) - ->method('receiveNoWait') - ->willReturn($receivedMessage) - ; - $consumer - ->expects($this->once()) - ->method('acknowledge') - ->with($this->identicalTo($receivedMessage)) - ; - $consumer - ->expects($this->never()) - ->method('reject') - ; - - $context = $this->createPsrContextMock(); - $context - ->expects($this->once()) - ->method('createQueue') - ->with('theReplyTo') - ->willReturn($replyQueue) - ; - $context - ->expects($this->once()) - ->method('createConsumer') - ->with($this->identicalTo($replyQueue)) - ->willReturn($consumer) - ; - - $rpc = new RpcClient($this->createProducerMock(), $context); - - $rpc->callAsync('topic', $message, 2)->receiveNoWait(); - } - - public function testShouldDeleteQueueAfterReceiveIfDeleteReplyQueueIsTrue() - { - $replyQueue = new NullQueue('theReplyTo'); - $message = new Message(); - $message->setCorrelationId('theCorrelationId'); - $message->setReplyTo('theReplyTo'); - - $receivedMessage = new NullMessage(); - $receivedMessage->setCorrelationId('theCorrelationId'); - - $consumer = $this->createPsrConsumerMock(); - $consumer - ->expects($this->once()) - ->method('receive') - ->willReturn($receivedMessage) - ; - - $context = $this->getMockBuilder(PsrContext::class) - ->disableOriginalConstructor() - ->setMethods(['deleteQueue']) - ->getMockForAbstractClass() - ; - $context - ->expects($this->once()) - ->method('createQueue') - ->with('theReplyTo') - ->willReturn($replyQueue) - ; - $context - ->expects($this->once()) - ->method('createConsumer') - ->with($this->identicalTo($replyQueue)) - ->willReturn($consumer) - ; - $context - ->expects($this->once()) - ->method('deleteQueue') - ->with($this->identicalTo($replyQueue)) - ; - - $rpc = new RpcClient($this->createProducerMock(), $context); - - $promise = $rpc->callAsync('topic', $message, 2); - $promise->setDeleteReplyQueue(true); - $promise->receive(); - } - - public function testShouldNotCallDeleteQueueIfDeleteReplyQueueIsTrueButContextHasNoDeleteQueueMethod() - { - $replyQueue = new NullQueue('theReplyTo'); - $message = new Message(); - $message->setCorrelationId('theCorrelationId'); - $message->setReplyTo('theReplyTo'); - - $receivedMessage = new NullMessage(); - $receivedMessage->setCorrelationId('theCorrelationId'); - - $consumer = $this->createPsrConsumerMock(); - $consumer - ->expects($this->once()) - ->method('receive') - ->willReturn($receivedMessage) - ; - - $context = $this->createPsrContextMock(); - $context - ->expects($this->once()) - ->method('createQueue') - ->with('theReplyTo') - ->willReturn($replyQueue) - ; - $context - ->expects($this->once()) - ->method('createConsumer') - ->with($this->identicalTo($replyQueue)) - ->willReturn($consumer) - ; - - $rpc = new RpcClient($this->createProducerMock(), $context); - - $promise = $rpc->callAsync('topic', $message, 2); - $promise->setDeleteReplyQueue(true); - - $promise->receive(); - } - - /** - * @return \PHPUnit_Framework_MockObject_MockObject|PsrContext - */ - private function createPsrContextMock() - { - return $this->createMock(PsrContext::class); - } - - /** - * @return \PHPUnit_Framework_MockObject_MockObject|ProducerInterface - */ - private function createProducerMock() - { - return $this->createMock(ProducerInterface::class); - } - - /** - * @return \PHPUnit_Framework_MockObject_MockObject|PsrConsumer - */ - private function createPsrConsumerMock() - { - return $this->createMock(PsrConsumer::class); - } -} diff --git a/pkg/enqueue/Tests/Client/SpoolProducerTest.php b/pkg/enqueue/Tests/Client/SpoolProducerTest.php index c9d76efd6..e842c0125 100644 --- a/pkg/enqueue/Tests/Client/SpoolProducerTest.php +++ b/pkg/enqueue/Tests/Client/SpoolProducerTest.php @@ -29,12 +29,12 @@ public function testShouldQueueMessageOnSend() $realProducer = $this->createProducerMock(); $realProducer ->expects($this->never()) - ->method('send') + ->method('sendEvent') ; $producer = new SpoolProducer($realProducer); - $producer->send('foo_topic', $message); - $producer->send('bar_topic', $message); + $producer->sendEvent('foo_topic', $message); + $producer->sendEvent('bar_topic', $message); } public function testShouldSendQueuedMessagesOnFlush() @@ -45,25 +45,25 @@ public function testShouldSendQueuedMessagesOnFlush() $realProducer = $this->createProducerMock(); $realProducer ->expects($this->at(0)) - ->method('send') + ->method('sendEVent') ->with('foo_topic', 'first') ; $realProducer ->expects($this->at(1)) - ->method('send') + ->method('sendEvent') ->with('bar_topic', ['second']) ; $realProducer ->expects($this->at(2)) - ->method('send') + ->method('sendEvent') ->with('baz_topic', $this->identicalTo($message)) ; $producer = new SpoolProducer($realProducer); - $producer->send('foo_topic', 'first'); - $producer->send('bar_topic', ['second']); - $producer->send('baz_topic', $message); + $producer->sendEvent('foo_topic', 'first'); + $producer->sendEvent('bar_topic', ['second']); + $producer->sendEvent('baz_topic', $message); $producer->flush(); } diff --git a/pkg/enqueue/Tests/Client/TraceableProducerTest.php b/pkg/enqueue/Tests/Client/TraceableProducerTest.php index 0912a549b..12231809e 100644 --- a/pkg/enqueue/Tests/Client/TraceableProducerTest.php +++ b/pkg/enqueue/Tests/Client/TraceableProducerTest.php @@ -30,24 +30,25 @@ public function testShouldPassAllArgumentsToInternalMessageProducerSendMethod() $internalMessageProducer = $this->createProducerMock(); $internalMessageProducer ->expects($this->once()) - ->method('send') + ->method('sendEvent') ->with($topic, $body) ; $messageProducer = new TraceableProducer($internalMessageProducer); - $messageProducer->send($topic, $body); + $messageProducer->sendEvent($topic, $body); } public function testShouldCollectInfoIfStringGivenAsMessage() { $messageProducer = new TraceableProducer($this->createProducerMock()); - $messageProducer->send('aFooTopic', 'aFooBody'); + $messageProducer->sendEvent('aFooTopic', 'aFooBody'); $this->assertSame([ [ 'topic' => 'aFooTopic', + 'command' => null, 'body' => 'aFooBody', 'headers' => [], 'properties' => [], @@ -65,11 +66,12 @@ public function testShouldCollectInfoIfArrayGivenAsMessage() { $messageProducer = new TraceableProducer($this->createProducerMock()); - $messageProducer->send('aFooTopic', ['foo' => 'fooVal', 'bar' => 'barVal']); + $messageProducer->sendEvent('aFooTopic', ['foo' => 'fooVal', 'bar' => 'barVal']); $this->assertSame([ [ 'topic' => 'aFooTopic', + 'command' => null, 'body' => ['foo' => 'fooVal', 'bar' => 'barVal'], 'headers' => [], 'properties' => [], @@ -98,11 +100,12 @@ public function testShouldCollectInfoIfMessageObjectGivenAsMessage() $message->setPriority('theMessagePriority'); $message->setTimestamp('theTimestamp'); - $messageProducer->send('aFooTopic', $message); + $messageProducer->sendEvent('aFooTopic', $message); $this->assertSame([ [ 'topic' => 'aFooTopic', + 'command' => null, 'body' => ['foo' => 'fooVal', 'bar' => 'barVal'], 'headers' => ['fooHeader' => 'fooVal'], 'properties' => ['fooProp' => 'fooVal'], @@ -120,8 +123,8 @@ public function testShouldAllowGetInfoSentToSameTopic() { $messageProducer = new TraceableProducer($this->createProducerMock()); - $messageProducer->send('aFooTopic', 'aFooBody'); - $messageProducer->send('aFooTopic', 'aFooBody'); + $messageProducer->sendEvent('aFooTopic', 'aFooBody'); + $messageProducer->sendEvent('aFooTopic', 'aFooBody'); $this->assertArraySubset([ ['topic' => 'aFooTopic', 'body' => 'aFooBody'], @@ -133,8 +136,8 @@ public function testShouldAllowGetInfoSentToDifferentTopics() { $messageProducer = new TraceableProducer($this->createProducerMock()); - $messageProducer->send('aFooTopic', 'aFooBody'); - $messageProducer->send('aBarTopic', 'aBarBody'); + $messageProducer->sendEvent('aFooTopic', 'aFooBody'); + $messageProducer->sendEvent('aBarTopic', 'aBarBody'); $this->assertArraySubset([ ['topic' => 'aFooTopic', 'body' => 'aFooBody'], @@ -146,8 +149,8 @@ public function testShouldAllowGetInfoSentToSpecialTopicTopics() { $messageProducer = new TraceableProducer($this->createProducerMock()); - $messageProducer->send('aFooTopic', 'aFooBody'); - $messageProducer->send('aBarTopic', 'aBarBody'); + $messageProducer->sendEvent('aFooTopic', 'aFooBody'); + $messageProducer->sendEvent('aBarTopic', 'aBarBody'); $this->assertArraySubset([ ['topic' => 'aFooTopic', 'body' => 'aFooBody'], @@ -163,7 +166,7 @@ public function testShouldNotStoreAnythingIfInternalMessageProducerThrowsExcepti $internalMessageProducer = $this->createProducerMock(); $internalMessageProducer ->expects($this->once()) - ->method('send') + ->method('sendEvent') ->willThrowException(new \Exception()) ; @@ -172,7 +175,7 @@ public function testShouldNotStoreAnythingIfInternalMessageProducerThrowsExcepti $this->expectException(\Exception::class); try { - $messageProducer->send('aFooTopic', 'aFooBody'); + $messageProducer->sendEvent('aFooTopic', 'aFooBody'); } finally { $this->assertEmpty($messageProducer->getTraces()); } @@ -182,7 +185,7 @@ public function testShouldAllowClearStoredTraces() { $messageProducer = new TraceableProducer($this->createProducerMock()); - $messageProducer->send('aFooTopic', 'aFooBody'); + $messageProducer->sendEvent('aFooTopic', 'aFooBody'); //guard $this->assertNotEmpty($messageProducer->getTraces()); diff --git a/pkg/enqueue/Tests/Functional/Client/RpcClientTest.php b/pkg/enqueue/Tests/Functional/Client/RpcClientTest.php deleted file mode 100644 index 9f18af46a..000000000 --- a/pkg/enqueue/Tests/Functional/Client/RpcClientTest.php +++ /dev/null @@ -1,85 +0,0 @@ -context = $this->buildAmqpContext(); - $this->replyContext = $this->buildAmqpContext(); - - $this->removeQueue('enqueue.app.default'); - } - - public function testProduceAndConsumeOneMessage() - { - $config = [ - 'transport' => [ - 'rabbitmq_amqp' => [ - 'host' => getenv('SYMFONY__RABBITMQ__HOST'), - 'port' => getenv('SYMFONY__RABBITMQ__AMQP__PORT'), - 'user' => getenv('SYMFONY__RABBITMQ__USER'), - 'pass' => getenv('SYMFONY__RABBITMQ__PASSWORD'), - 'vhost' => getenv('SYMFONY__RABBITMQ__VHOST'), - ], - ], - ]; - - $requestMessage = null; - - $client = new SimpleClient($config); - $client->setupBroker(); - $client->bind('foo_topic', 'foo_processor', function (PsrMessage $message, PsrContext $context) use (&$requestMessage) { - $requestMessage = $message; - - return Result::reply($context->createMessage('Hi John!')); - }); - - $rpcClient = new RpcClient($client->getProducer(), $this->replyContext); - $promise = $rpcClient->callAsync('foo_topic', 'Hi Thomas!', 5); - - $client->consume(new ChainExtension([ - new ReplyExtension(), - new LimitConsumptionTimeExtension(new \DateTime('+5sec')), - new LimitConsumedMessagesExtension(2), - ])); - - //guard - $this->assertInstanceOf(PsrMessage::class, $requestMessage); - $this->assertEquals('Hi Thomas!', $requestMessage->getBody()); - - $replyMessage = $promise->receive(); - $this->assertEquals('Hi John!', $replyMessage->getBody()); - } -} diff --git a/pkg/enqueue/Tests/Rpc/RpcClientTest.php b/pkg/enqueue/Tests/Rpc/RpcClientTest.php index 13f6779ae..846e62569 100644 --- a/pkg/enqueue/Tests/Rpc/RpcClientTest.php +++ b/pkg/enqueue/Tests/Rpc/RpcClientTest.php @@ -134,7 +134,7 @@ public function testShouldReceiveMessageAndAckMessageIfCorrelationEquals() ->willReturn($this->createPsrProducerMock()) ; $context - ->expects($this->once()) + ->expects($this->atLeastOnce()) ->method('createQueue') ->with('theReplyTo') ->willReturn($replyQueue) @@ -185,7 +185,7 @@ public function testShouldReceiveNoWaitMessageAndAckMessageIfCorrelationEquals() ->willReturn($this->createPsrProducerMock()) ; $context - ->expects($this->once()) + ->expects($this->atLeastOnce()) ->method('createQueue') ->with('theReplyTo') ->willReturn($replyQueue) @@ -232,7 +232,7 @@ public function testShouldDeleteQueueAfterReceiveIfDeleteReplyQueueIsTrue() ->willReturn($this->createPsrProducerMock()) ; $context - ->expects($this->once()) + ->expects($this->atLeastOnce()) ->method('createQueue') ->with('theReplyTo') ->willReturn($replyQueue) diff --git a/pkg/enqueue/Tests/Symfony/Client/ProduceMessageCommandTest.php b/pkg/enqueue/Tests/Symfony/Client/ProduceMessageCommandTest.php index 35211389c..2cd80952e 100644 --- a/pkg/enqueue/Tests/Symfony/Client/ProduceMessageCommandTest.php +++ b/pkg/enqueue/Tests/Symfony/Client/ProduceMessageCommandTest.php @@ -52,7 +52,7 @@ public function testShouldExecuteConsumptionAndUseDefaultQueueName() $producerMock = $this->createProducerMock(); $producerMock ->expects($this->once()) - ->method('send') + ->method('sendEvent') ->with('theTopic', 'theMessage') ; diff --git a/pkg/job-queue/CalculateRootJobStatusProcessor.php b/pkg/job-queue/CalculateRootJobStatusProcessor.php index 593b97e7d..dbf0a1c26 100644 --- a/pkg/job-queue/CalculateRootJobStatusProcessor.php +++ b/pkg/job-queue/CalculateRootJobStatusProcessor.php @@ -75,7 +75,7 @@ public function process(PsrMessage $message, PsrContext $context) $isRootJobStopped = $this->calculateRootJobStatusService->calculate($job); if ($isRootJobStopped) { - $this->producer->send(Topics::ROOT_JOB_STOPPED, [ + $this->producer->sendEvent(Topics::ROOT_JOB_STOPPED, [ 'jobId' => $job->getRootJob()->getId(), ]); } diff --git a/pkg/job-queue/DependentJobProcessor.php b/pkg/job-queue/DependentJobProcessor.php index e8a69be33..595aa388b 100644 --- a/pkg/job-queue/DependentJobProcessor.php +++ b/pkg/job-queue/DependentJobProcessor.php @@ -105,7 +105,7 @@ public function process(PsrMessage $message, PsrContext $context) $message->setPriority($dependentJob['priority']); } - $this->producer->send($dependentJob['topic'], $message); + $this->producer->sendEvent($dependentJob['topic'], $message); } return Result::ACK; diff --git a/pkg/job-queue/JobProcessor.php b/pkg/job-queue/JobProcessor.php index 147105369..2ab77a72d 100644 --- a/pkg/job-queue/JobProcessor.php +++ b/pkg/job-queue/JobProcessor.php @@ -99,7 +99,7 @@ public function findOrCreateChildJob($jobName, Job $rootJob) $this->jobStorage->saveJob($job); - $this->producer->send(Topics::CALCULATE_ROOT_JOB_STATUS, [ + $this->producer->sendEvent(Topics::CALCULATE_ROOT_JOB_STATUS, [ 'jobId' => $job->getId(), ]); @@ -130,7 +130,7 @@ public function startChildJob(Job $job) $this->jobStorage->saveJob($job); - $this->producer->send(Topics::CALCULATE_ROOT_JOB_STATUS, [ + $this->producer->sendEvent(Topics::CALCULATE_ROOT_JOB_STATUS, [ 'jobId' => $job->getId(), ]); } @@ -159,7 +159,7 @@ public function successChildJob(Job $job) $this->jobStorage->saveJob($job); - $this->producer->send(Topics::CALCULATE_ROOT_JOB_STATUS, [ + $this->producer->sendEvent(Topics::CALCULATE_ROOT_JOB_STATUS, [ 'jobId' => $job->getId(), ]); } @@ -188,7 +188,7 @@ public function failChildJob(Job $job) $this->jobStorage->saveJob($job); - $this->producer->send(Topics::CALCULATE_ROOT_JOB_STATUS, [ + $this->producer->sendEvent(Topics::CALCULATE_ROOT_JOB_STATUS, [ 'jobId' => $job->getId(), ]); } @@ -221,7 +221,7 @@ public function cancelChildJob(Job $job) $this->jobStorage->saveJob($job); - $this->producer->send(Topics::CALCULATE_ROOT_JOB_STATUS, [ + $this->producer->sendEvent(Topics::CALCULATE_ROOT_JOB_STATUS, [ 'jobId' => $job->getId(), ]); } diff --git a/pkg/job-queue/Tests/CalculateRootJobStatusProcessorTest.php b/pkg/job-queue/Tests/CalculateRootJobStatusProcessorTest.php index 3959c1939..a2c64741e 100644 --- a/pkg/job-queue/Tests/CalculateRootJobStatusProcessorTest.php +++ b/pkg/job-queue/Tests/CalculateRootJobStatusProcessorTest.php @@ -81,7 +81,7 @@ public function testShouldRejectMessageAndLogErrorIfJobWasNotFound() $producer = $this->createProducerMock(); $producer ->expects($this->never()) - ->method('send') + ->method('sendEvent') ; $message = new NullMessage(); @@ -121,7 +121,7 @@ public function testShouldCallCalculateJobRootStatusAndACKMessage() $producer = $this->createProducerMock(); $producer ->expects($this->never()) - ->method('send') + ->method('sendEvent') ; $message = new NullMessage(); @@ -161,7 +161,7 @@ public function testShouldSendRootJobStoppedMessageIfJobHasStopped() $producer = $this->createProducerMock(); $producer ->expects($this->once()) - ->method('send') + ->method('sendEvent') ->with(Topics::ROOT_JOB_STOPPED, ['jobId' => 12345]) ; diff --git a/pkg/job-queue/Tests/DependentJobProcessorTest.php b/pkg/job-queue/Tests/DependentJobProcessorTest.php index 43818a6bd..bf2140f8a 100644 --- a/pkg/job-queue/Tests/DependentJobProcessorTest.php +++ b/pkg/job-queue/Tests/DependentJobProcessorTest.php @@ -121,7 +121,7 @@ public function testShouldDoNothingIfDependentJobsAreMissing() $producer = $this->createProducerMock(); $producer ->expects($this->never()) - ->method('send') + ->method('sendEvent') ; $logger = $this->createLoggerMock(); @@ -157,7 +157,7 @@ public function testShouldLogCriticalAndRejectMessageIfDependentJobTopicIsMissin $producer = $this->createProducerMock(); $producer ->expects($this->never()) - ->method('send') + ->method('sendEvent') ; $logger = $this->createLoggerMock(); @@ -200,7 +200,7 @@ public function testShouldLogCriticalAndRejectMessageIfDependentJobMessageIsMiss $producer = $this->createProducerMock(); $producer ->expects($this->never()) - ->method('send') + ->method('sendEvent') ; $logger = $this->createLoggerMock(); @@ -246,7 +246,7 @@ public function testShouldPublishDependentMessage() $producer = $this->createProducerMock(); $producer ->expects($this->once()) - ->method('send') + ->method('sendEvent') ->with('topic-name', $this->isInstanceOf(Message::class)) ->will($this->returnCallback(function ($topic, Message $message) use (&$expectedMessage) { $expectedMessage = $message; @@ -294,7 +294,7 @@ public function testShouldPublishDependentMessageWithPriority() $producer = $this->createProducerMock(); $producer ->expects($this->once()) - ->method('send') + ->method('sendEvent') ->with('topic-name', $this->isInstanceOf(Message::class)) ->will($this->returnCallback(function ($topic, Message $message) use (&$expectedMessage) { $expectedMessage = $message; diff --git a/pkg/job-queue/Tests/JobProcessorTest.php b/pkg/job-queue/Tests/JobProcessorTest.php index 3ba7ea589..bb2386895 100644 --- a/pkg/job-queue/Tests/JobProcessorTest.php +++ b/pkg/job-queue/Tests/JobProcessorTest.php @@ -8,8 +8,9 @@ use Enqueue\JobQueue\Job; use Enqueue\JobQueue\JobProcessor; use Enqueue\JobQueue\Topics; +use PHPUnit\Framework\TestCase; -class JobProcessorTest extends \PHPUnit\Framework\TestCase +class JobProcessorTest extends TestCase { public function testCouldBeCreatedWithRequiredArguments() { @@ -168,7 +169,7 @@ public function testCreateChildJobShouldCreateAndSaveJobAndPublishRecalculateRoo $producer = $this->createProducerMock(); $producer ->expects($this->once()) - ->method('send') + ->method('sendEvent') ->with(Topics::CALCULATE_ROOT_JOB_STATUS, ['jobId' => 12345]) ; @@ -245,7 +246,7 @@ public function testStartJobShouldUpdateJobWithRunningStatusAndStartAtTime() $producer = $this->createProducerMock(); $producer ->expects($this->once()) - ->method('send') + ->method('sendEvent') ; $processor = new JobProcessor($storage, $producer); @@ -315,7 +316,7 @@ public function testSuccessJobShouldUpdateJobWithSuccessStatusAndStopAtTime() $producer = $this->createProducerMock(); $producer ->expects($this->once()) - ->method('send') + ->method('sendEvent') ; $processor = new JobProcessor($storage, $producer); @@ -385,7 +386,7 @@ public function testFailJobShouldUpdateJobWithFailStatusAndStopAtTime() $producer = $this->createProducerMock(); $producer ->expects($this->once()) - ->method('send') + ->method('sendEvent') ; $processor = new JobProcessor($storage, $producer); @@ -455,7 +456,7 @@ public function testCancelJobShouldUpdateJobWithCancelStatusAndStoppedAtTimeAndS $producer = $this->createProducerMock(); $producer ->expects($this->once()) - ->method('send') + ->method('sendEvent') ; $processor = new JobProcessor($storage, $producer); diff --git a/pkg/simple-client/SimpleClient.php b/pkg/simple-client/SimpleClient.php index 82f093c53..5e9efdd72 100644 --- a/pkg/simple-client/SimpleClient.php +++ b/pkg/simple-client/SimpleClient.php @@ -11,7 +11,6 @@ use Enqueue\Client\Meta\QueueMetaRegistry; use Enqueue\Client\Meta\TopicMetaRegistry; use Enqueue\Client\ProducerInterface; -use Enqueue\Client\ProducerV2Interface; use Enqueue\Client\RouterProcessor; use Enqueue\Consumption\CallbackProcessor; use Enqueue\Consumption\ExtensionInterface; @@ -103,6 +102,24 @@ public function bind($topic, $processorName, $processor) $this->getRouterProcessor()->add($topic, $queueName, $processorName); } + /** + * @param string $command + * @param mixed $message + */ + public function sendCommand($command, $message) + { + $this->getProducer()->sendCommand($command, $message); + } + + /** + * @param string $topic + * @param string|array $message + */ + public function sendEvent($topic, $message) + { + $this->getProducer()->sendEvent($topic, $message); + } + /** * @param string $topic * @param string|array $message @@ -110,7 +127,11 @@ public function bind($topic, $processorName, $processor) */ public function send($topic, $message, $setupBroker = false) { - $this->getProducer($setupBroker)->send($topic, $message); + if ($setupBroker) { + $this->setupBroker(); + } + + $this->sendEvent($topic, $message); } /** @@ -195,18 +216,6 @@ public function getProducer($setupBroker = false) return $this->container->get('enqueue.client.producer'); } - /** - * @param bool $setupBroker - * - * @return ProducerV2Interface - */ - public function getProducerV2($setupBroker = false) - { - $setupBroker && $this->setupBroker(); - - return $this->container->get('enqueue.client.producer.v2'); - } - public function setupBroker() { $this->getDriver()->setupBroker(); diff --git a/pkg/simple-client/SimpleClientContainerExtension.php b/pkg/simple-client/SimpleClientContainerExtension.php index bfe31ddc4..7fea6f03b 100644 --- a/pkg/simple-client/SimpleClientContainerExtension.php +++ b/pkg/simple-client/SimpleClientContainerExtension.php @@ -10,11 +10,10 @@ use Enqueue\Client\Meta\QueueMetaRegistry; use Enqueue\Client\Meta\TopicMetaRegistry; use Enqueue\Client\Producer; -use Enqueue\Client\ProducerV2; use Enqueue\Client\RouterProcessor; -use Enqueue\Client\RpcClient; use Enqueue\Consumption\ChainExtension as ConsumptionChainExtension; use Enqueue\Consumption\QueueConsumer; +use Enqueue\Rpc\RpcFactory; use Enqueue\Symfony\TransportFactoryInterface; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\NodeInterface; @@ -90,23 +89,19 @@ public function load(array $configs, ContainerBuilder $container) $transportConfig, ]); - $container->register('enqueue.client.producer', Producer::class) - ->setArguments([ - new Reference('enqueue.client.driver'), - ]); - - $container->register('enqueue.client.rpc', RpcClient::class) + $container->register('enqueue.client.rpc_factory', RpcFactory::class) ->setArguments([ - new Reference('enqueue.client.producer'), new Reference('enqueue.transport.context'), - ]); + ]); - $container->register('enqueue.client.producer.v2', ProducerV2::class) + $container->register('enqueue.client.producer', Producer::class) ->setArguments([ - new Reference('enqueue.client.producer'), - new Reference('enqueue.client.rpc'), + new Reference('enqueue.client.driver'), + new Reference('enqueue.client.rpc_factory'), ]); + $container->setAlias('enqueue.client.producer_v2', 'enqueue.client.producer'); + $container->register('enqueue.client.meta.topic_meta_registry', TopicMetaRegistry::class) ->setArguments([[]]); From f2a67e854c6d8580e33eccc8fec1dc8db97ce506 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Mon, 26 Jun 2017 16:35:17 +0300 Subject: [PATCH 18/20] [client] fixes and cleanups. --- docs/bundle/job_queue.md | 2 +- docs/bundle/message_producer.md | 4 +- docs/client/message_examples.md | 24 ++ docs/quick_tour.md | 52 +++- .../Tests/Client/SpoolProducerTest.php | 91 ++++++- .../Tests/Client/TraceableProducerTest.php | 256 ++++++++++++++---- pkg/simple-client/SimpleClient.php | 8 +- 7 files changed, 376 insertions(+), 61 deletions(-) diff --git a/docs/bundle/job_queue.md b/docs/bundle/job_queue.md index 7ca15042d..09a9ea175 100644 --- a/docs/bundle/job_queue.md +++ b/docs/bundle/job_queue.md @@ -89,7 +89,7 @@ class Step1Processor implements PsrProcessor $runner->createDelayed( $jobName, function (JobRunner $runner, Job $childJob) use ($entity) { - $this->producer->sendCommand('search:index:index-single-entity', [ + $this->producer->sendEvent('search:index:index-single-entity', [ 'entityId' => $entity->getId(), 'jobId' => $childJob->getId(), ]); diff --git a/docs/bundle/message_producer.md b/docs/bundle/message_producer.md index 181363815..6175804c2 100644 --- a/docs/bundle/message_producer.md +++ b/docs/bundle/message_producer.md @@ -26,8 +26,8 @@ where another one (it is called spool producer) collects them in memory and send The producer has two types on send methods: -* `sendEvent` - Message is sent to topic and many consumers can subscriber to it. It is "fire and forget" strategy. -* `sendCommand` - Message is to ONE exact consumer. It could be used as "fire and forget" or as RPC. +* `sendEvent` - Message is sent to topic and many consumers can subscriber to it. It is "fire and forget" strategy. The even could be sent to "message bus" to other applications. +* `sendCommand` - Message is to ONE exact consumer. It could be used as "fire and forget" or as RPC. The command message is always sent in scope of current application. ### Send event diff --git a/docs/client/message_examples.md b/docs/client/message_examples.md index 5bd627cad..11e13a100 100644 --- a/docs/client/message_examples.md +++ b/docs/client/message_examples.md @@ -1,4 +1,28 @@ # Client. Message examples + +* [Scope](#scope) +* [Delay](#delay) +* [Expiration (TTL)](#expiration-ttl) +* [Priority](#priority) +* [Timestamp, Content type, Message id](#timestamp-content-type-message-id) + +## Scope + +There are two two types possible scopes: `Message:SCOPE_MESSAGE_BUS` and `Message::SCOPE_APP`. +The first one instructs the client send messages (if driver supports) to the message bus so other apps can consume those messages. +The second in turns limits the message to the application that sent it. No other apps could receive it. + +```php +setScope(Message::SCOPE_MESSAGE_BUS); + +/** @var \Enqueue\Client\ProducerInterface $producer */ +$producer->sendEvent('aTopic', $message); +``` ## Delay diff --git a/docs/quick_tour.md b/docs/quick_tour.md index 7159af26a..a49bdc5bc 100644 --- a/docs/quick_tour.md +++ b/docs/quick_tour.md @@ -163,7 +163,7 @@ It provides easy to use services for producing and processing messages. It supports unified format for setting message expiration, delay, timestamp, correlation id. It supports message bus so different applications can talk to each other. -Here's an example of how you can send and consume messages. +Here's an example of how you can send and consume event messages. ```php setupBroker(); $client->bind('a_foo_topic', 'fooProcessor', function(PsrMessage $message) { - // your processing logic here + // your event processor logic here }); -$client->send('a_bar_topic', 'aMessageData'); - -// in another process you can consume messages. +// this is a blocking call, it'll consume message until it is interrupted $client->consume(); ``` +and command messages: + +```php + + +$client->setupBroker(); + +$client->bind(Config::COMMAND_TOPIC, 'bar_command', function(PsrMessage $message) { + // your bar command processor logic here +}); + +$client->bind(Config::COMMAND_TOPIC, 'baz_reply_command', function(PsrMessage $message, PsrContext $context) { + // your baz reply command processor logic here + + return Result::reply($context->createMessage('theReplyBody')); +}); + +// It is sent to one consumer. +$client->sendCommand('bar_command', 'aMessageData'); + +// It is possible to get reply +$promise = $client->sendCommand('bar_command', 'aMessageData', true); + +// you can send several commands and only after start getting replies. + +$replyMessage = $promise->receive(2000); // 2 sec + +// this is a blocking call, it'll consume message until it is interrupted +$client->consume([new ReplyExtension()]); +``` + ## Cli commands The library provides handy commands out of the box. diff --git a/pkg/enqueue/Tests/Client/SpoolProducerTest.php b/pkg/enqueue/Tests/Client/SpoolProducerTest.php index e842c0125..8c00dedcd 100644 --- a/pkg/enqueue/Tests/Client/SpoolProducerTest.php +++ b/pkg/enqueue/Tests/Client/SpoolProducerTest.php @@ -5,6 +5,7 @@ use Enqueue\Client\Message; use Enqueue\Client\ProducerInterface; use Enqueue\Client\SpoolProducer; +use Enqueue\Rpc\Promise; use Enqueue\Test\ClassExtensionTrait; use PHPUnit\Framework\TestCase; @@ -22,7 +23,7 @@ public function testCouldBeConstructedWithRealProducer() new SpoolProducer($this->createProducerMock()); } - public function testShouldQueueMessageOnSend() + public function testShouldQueueEventMessageOnSend() { $message = new Message(); @@ -31,13 +32,36 @@ public function testShouldQueueMessageOnSend() ->expects($this->never()) ->method('sendEvent') ; + $realProducer + ->expects($this->never()) + ->method('sendCommand') + ; $producer = new SpoolProducer($realProducer); $producer->sendEvent('foo_topic', $message); $producer->sendEvent('bar_topic', $message); } - public function testShouldSendQueuedMessagesOnFlush() + public function testShouldQueueCommandMessageOnSend() + { + $message = new Message(); + + $realProducer = $this->createProducerMock(); + $realProducer + ->expects($this->never()) + ->method('sendEvent') + ; + $realProducer + ->expects($this->never()) + ->method('sendCommand') + ; + + $producer = new SpoolProducer($realProducer); + $producer->sendCommand('foo_command', $message); + $producer->sendCommand('bar_command', $message); + } + + public function testShouldSendQueuedEventMessagesOnFlush() { $message = new Message(); $message->setScope('third'); @@ -45,7 +69,7 @@ public function testShouldSendQueuedMessagesOnFlush() $realProducer = $this->createProducerMock(); $realProducer ->expects($this->at(0)) - ->method('sendEVent') + ->method('sendEvent') ->with('foo_topic', 'first') ; $realProducer @@ -58,6 +82,10 @@ public function testShouldSendQueuedMessagesOnFlush() ->method('sendEvent') ->with('baz_topic', $this->identicalTo($message)) ; + $realProducer + ->expects($this->never()) + ->method('sendCommand') + ; $producer = new SpoolProducer($realProducer); @@ -68,6 +96,63 @@ public function testShouldSendQueuedMessagesOnFlush() $producer->flush(); } + public function testShouldSendQueuedCommandMessagesOnFlush() + { + $message = new Message(); + $message->setScope('third'); + + $realProducer = $this->createProducerMock(); + $realProducer + ->expects($this->at(0)) + ->method('sendCommand') + ->with('foo_command', 'first') + ; + $realProducer + ->expects($this->at(1)) + ->method('sendCommand') + ->with('bar_command', ['second']) + ; + $realProducer + ->expects($this->at(2)) + ->method('sendCommand') + ->with('baz_command', $this->identicalTo($message)) + ; + + $producer = new SpoolProducer($realProducer); + + $producer->sendCommand('foo_command', 'first'); + $producer->sendCommand('bar_command', ['second']); + $producer->sendCommand('baz_command', $message); + + $producer->flush(); + } + + public function testShouldSendImmediatelyCommandMessageWithNeedReplyTrue() + { + $message = new Message(); + $message->setScope('third'); + + $promise = $this->createMock(Promise::class); + + $realProducer = $this->createProducerMock(); + $realProducer + ->expects($this->never()) + ->method('sendEvent') + ; + $realProducer + ->expects($this->once()) + ->method('sendCommand') + ->with('foo_command', 'first') + ->willReturn($promise) + ; + + $producer = new SpoolProducer($realProducer); + + $actualPromise = $producer->sendCommand('foo_command', 'first', true); + + $this->assertSame($promise, $actualPromise); + } + /** * @return \PHPUnit_Framework_MockObject_MockObject|ProducerInterface */ diff --git a/pkg/enqueue/Tests/Client/TraceableProducerTest.php b/pkg/enqueue/Tests/Client/TraceableProducerTest.php index 12231809e..42c9c8b8f 100644 --- a/pkg/enqueue/Tests/Client/TraceableProducerTest.php +++ b/pkg/enqueue/Tests/Client/TraceableProducerTest.php @@ -2,6 +2,7 @@ namespace Enqueue\Tests\Client; +use Enqueue\Client\Config; use Enqueue\Client\Message; use Enqueue\Client\ProducerInterface; use Enqueue\Client\TraceableProducer; @@ -22,7 +23,7 @@ public function testCouldBeConstructedWithInternalMessageProducer() new TraceableProducer($this->createProducerMock()); } - public function testShouldPassAllArgumentsToInternalMessageProducerSendMethod() + public function testShouldPassAllArgumentsToInternalEventMessageProducerSendMethod() { $topic = 'theTopic'; $body = 'theBody'; @@ -34,16 +35,16 @@ public function testShouldPassAllArgumentsToInternalMessageProducerSendMethod() ->with($topic, $body) ; - $messageProducer = new TraceableProducer($internalMessageProducer); + $producer = new TraceableProducer($internalMessageProducer); - $messageProducer->sendEvent($topic, $body); + $producer->sendEvent($topic, $body); } - public function testShouldCollectInfoIfStringGivenAsMessage() + public function testShouldCollectInfoIfStringGivenAsEventMessage() { - $messageProducer = new TraceableProducer($this->createProducerMock()); + $producer = new TraceableProducer($this->createProducerMock()); - $messageProducer->sendEvent('aFooTopic', 'aFooBody'); + $producer->sendEvent('aFooTopic', 'aFooBody'); $this->assertSame([ [ @@ -59,14 +60,14 @@ public function testShouldCollectInfoIfStringGivenAsMessage() 'contentType' => null, 'messageId' => null, ], - ], $messageProducer->getTraces()); + ], $producer->getTraces()); } - public function testShouldCollectInfoIfArrayGivenAsMessage() + public function testShouldCollectInfoIfArrayGivenAsEventMessage() { - $messageProducer = new TraceableProducer($this->createProducerMock()); + $producer = new TraceableProducer($this->createProducerMock()); - $messageProducer->sendEvent('aFooTopic', ['foo' => 'fooVal', 'bar' => 'barVal']); + $producer->sendEvent('aFooTopic', ['foo' => 'fooVal', 'bar' => 'barVal']); $this->assertSame([ [ @@ -82,12 +83,12 @@ public function testShouldCollectInfoIfArrayGivenAsMessage() 'contentType' => null, 'messageId' => null, ], - ], $messageProducer->getTraces()); + ], $producer->getTraces()); } - public function testShouldCollectInfoIfMessageObjectGivenAsMessage() + public function testShouldCollectInfoIfEventMessageObjectGivenAsMessage() { - $messageProducer = new TraceableProducer($this->createProducerMock()); + $producer = new TraceableProducer($this->createProducerMock()); $message = new Message(); $message->setBody(['foo' => 'fooVal', 'bar' => 'barVal']); @@ -100,7 +101,7 @@ public function testShouldCollectInfoIfMessageObjectGivenAsMessage() $message->setPriority('theMessagePriority'); $message->setTimestamp('theTimestamp'); - $messageProducer->sendEvent('aFooTopic', $message); + $producer->sendEvent('aFooTopic', $message); $this->assertSame([ [ @@ -116,82 +117,241 @@ public function testShouldCollectInfoIfMessageObjectGivenAsMessage() 'contentType' => 'theContentType', 'messageId' => 'theMessageId', ], - ], $messageProducer->getTraces()); + ], $producer->getTraces()); + } + + public function testShouldNotStoreAnythingIfInternalEventMessageProducerThrowsException() + { + $internalMessageProducer = $this->createProducerMock(); + $internalMessageProducer + ->expects($this->once()) + ->method('sendEvent') + ->willThrowException(new \Exception()) + ; + + $producer = new TraceableProducer($internalMessageProducer); + + $this->expectException(\Exception::class); + + try { + $producer->sendEvent('aFooTopic', 'aFooBody'); + } finally { + $this->assertEmpty($producer->getTraces()); + } + } + + public function testShouldPassAllArgumentsToInternalCommandMessageProducerSendMethod() + { + $command = 'theCommand'; + $body = 'theBody'; + + $internalMessageProducer = $this->createProducerMock(); + $internalMessageProducer + ->expects($this->once()) + ->method('sendCommand') + ->with($command, $body) + ; + + $producer = new TraceableProducer($internalMessageProducer); + + $producer->sendCommand($command, $body); + } + + public function testShouldCollectInfoIfStringGivenAsCommandMessage() + { + $producer = new TraceableProducer($this->createProducerMock()); + + $producer->sendCommand('aFooCommand', 'aFooBody'); + + $this->assertSame([ + [ + 'topic' => Config::COMMAND_TOPIC, + 'command' => 'aFooCommand', + 'body' => 'aFooBody', + 'headers' => [], + 'properties' => [], + 'priority' => null, + 'expire' => null, + 'delay' => null, + 'timestamp' => null, + 'contentType' => null, + 'messageId' => null, + ], + ], $producer->getTraces()); + } + + public function testShouldCollectInfoIfArrayGivenAsCommandMessage() + { + $producer = new TraceableProducer($this->createProducerMock()); + + $producer->sendCommand('aFooCommand', ['foo' => 'fooVal', 'bar' => 'barVal']); + + $this->assertSame([ + [ + 'topic' => Config::COMMAND_TOPIC, + 'command' => 'aFooCommand', + 'body' => ['foo' => 'fooVal', 'bar' => 'barVal'], + 'headers' => [], + 'properties' => [], + 'priority' => null, + 'expire' => null, + 'delay' => null, + 'timestamp' => null, + 'contentType' => null, + 'messageId' => null, + ], + ], $producer->getTraces()); + } + + public function testShouldCollectInfoIfCommandMessageObjectGivenAsMessage() + { + $producer = new TraceableProducer($this->createProducerMock()); + + $message = new Message(); + $message->setBody(['foo' => 'fooVal', 'bar' => 'barVal']); + $message->setProperty('fooProp', 'fooVal'); + $message->setHeader('fooHeader', 'fooVal'); + $message->setContentType('theContentType'); + $message->setDelay('theDelay'); + $message->setExpire('theExpire'); + $message->setMessageId('theMessageId'); + $message->setPriority('theMessagePriority'); + $message->setTimestamp('theTimestamp'); + + $producer->sendCommand('aFooCommand', $message); + + $this->assertSame([ + [ + 'topic' => Config::COMMAND_TOPIC, + 'command' => 'aFooCommand', + 'body' => ['foo' => 'fooVal', 'bar' => 'barVal'], + 'headers' => ['fooHeader' => 'fooVal'], + 'properties' => ['fooProp' => 'fooVal'], + 'priority' => 'theMessagePriority', + 'expire' => 'theExpire', + 'delay' => 'theDelay', + 'timestamp' => 'theTimestamp', + 'contentType' => 'theContentType', + 'messageId' => 'theMessageId', + ], + ], $producer->getTraces()); + } + + public function testShouldNotStoreAnythingIfInternalCommandMessageProducerThrowsException() + { + $internalMessageProducer = $this->createProducerMock(); + $internalMessageProducer + ->expects($this->once()) + ->method('sendCommand') + ->willThrowException(new \Exception()) + ; + + $producer = new TraceableProducer($internalMessageProducer); + + $this->expectException(\Exception::class); + + try { + $producer->sendCommand('aFooCommand', 'aFooBody'); + } finally { + $this->assertEmpty($producer->getTraces()); + } } public function testShouldAllowGetInfoSentToSameTopic() { - $messageProducer = new TraceableProducer($this->createProducerMock()); + $producer = new TraceableProducer($this->createProducerMock()); - $messageProducer->sendEvent('aFooTopic', 'aFooBody'); - $messageProducer->sendEvent('aFooTopic', 'aFooBody'); + $producer->sendEvent('aFooTopic', 'aFooBody'); + $producer->sendEvent('aFooTopic', 'aFooBody'); $this->assertArraySubset([ ['topic' => 'aFooTopic', 'body' => 'aFooBody'], ['topic' => 'aFooTopic', 'body' => 'aFooBody'], - ], $messageProducer->getTraces()); + ], $producer->getTraces()); } public function testShouldAllowGetInfoSentToDifferentTopics() { - $messageProducer = new TraceableProducer($this->createProducerMock()); + $producer = new TraceableProducer($this->createProducerMock()); - $messageProducer->sendEvent('aFooTopic', 'aFooBody'); - $messageProducer->sendEvent('aBarTopic', 'aBarBody'); + $producer->sendEvent('aFooTopic', 'aFooBody'); + $producer->sendEvent('aBarTopic', 'aBarBody'); $this->assertArraySubset([ ['topic' => 'aFooTopic', 'body' => 'aFooBody'], ['topic' => 'aBarTopic', 'body' => 'aBarBody'], - ], $messageProducer->getTraces()); + ], $producer->getTraces()); } - public function testShouldAllowGetInfoSentToSpecialTopicTopics() + public function testShouldAllowGetInfoSentToSpecialTopic() { - $messageProducer = new TraceableProducer($this->createProducerMock()); + $producer = new TraceableProducer($this->createProducerMock()); - $messageProducer->sendEvent('aFooTopic', 'aFooBody'); - $messageProducer->sendEvent('aBarTopic', 'aBarBody'); + $producer->sendEvent('aFooTopic', 'aFooBody'); + $producer->sendEvent('aBarTopic', 'aBarBody'); $this->assertArraySubset([ ['topic' => 'aFooTopic', 'body' => 'aFooBody'], - ], $messageProducer->getTopicTraces('aFooTopic')); + ], $producer->getTopicTraces('aFooTopic')); $this->assertArraySubset([ ['topic' => 'aBarTopic', 'body' => 'aBarBody'], - ], $messageProducer->getTopicTraces('aBarTopic')); + ], $producer->getTopicTraces('aBarTopic')); } - public function testShouldNotStoreAnythingIfInternalMessageProducerThrowsException() + public function testShouldAllowGetInfoSentToSameCommand() { - $internalMessageProducer = $this->createProducerMock(); - $internalMessageProducer - ->expects($this->once()) - ->method('sendEvent') - ->willThrowException(new \Exception()) - ; + $producer = new TraceableProducer($this->createProducerMock()); - $messageProducer = new TraceableProducer($internalMessageProducer); + $producer->sendCommand('aFooCommand', 'aFooBody'); + $producer->sendCommand('aFooCommand', 'aFooBody'); - $this->expectException(\Exception::class); + $this->assertArraySubset([ + ['command' => 'aFooCommand', 'body' => 'aFooBody'], + ['command' => 'aFooCommand', 'body' => 'aFooBody'], + ], $producer->getTraces()); + } - try { - $messageProducer->sendEvent('aFooTopic', 'aFooBody'); - } finally { - $this->assertEmpty($messageProducer->getTraces()); - } + public function testShouldAllowGetInfoSentToDifferentCommands() + { + $producer = new TraceableProducer($this->createProducerMock()); + + $producer->sendCommand('aFooCommand', 'aFooBody'); + $producer->sendCommand('aBarCommand', 'aBarBody'); + + $this->assertArraySubset([ + ['command' => 'aFooCommand', 'body' => 'aFooBody'], + ['command' => 'aBarCommand', 'body' => 'aBarBody'], + ], $producer->getTraces()); + } + + public function testShouldAllowGetInfoSentToSpecialCommand() + { + $producer = new TraceableProducer($this->createProducerMock()); + + $producer->sendCommand('aFooCommand', 'aFooBody'); + $producer->sendCommand('aBarCommand', 'aBarBody'); + + $this->assertArraySubset([ + ['command' => 'aFooCommand', 'body' => 'aFooBody'], + ], $producer->getCommandTraces('aFooCommand')); + + $this->assertArraySubset([ + ['command' => 'aBarCommand', 'body' => 'aBarBody'], + ], $producer->getCommandTraces('aBarCommand')); } public function testShouldAllowClearStoredTraces() { - $messageProducer = new TraceableProducer($this->createProducerMock()); + $producer = new TraceableProducer($this->createProducerMock()); - $messageProducer->sendEvent('aFooTopic', 'aFooBody'); + $producer->sendEvent('aFooTopic', 'aFooBody'); //guard - $this->assertNotEmpty($messageProducer->getTraces()); + $this->assertNotEmpty($producer->getTraces()); - $messageProducer->clearTraces(); - $this->assertSame([], $messageProducer->getTraces()); + $producer->clearTraces(); + $this->assertSame([], $producer->getTraces()); } /** diff --git a/pkg/simple-client/SimpleClient.php b/pkg/simple-client/SimpleClient.php index 5e9efdd72..d3cc23047 100644 --- a/pkg/simple-client/SimpleClient.php +++ b/pkg/simple-client/SimpleClient.php @@ -20,6 +20,7 @@ use Enqueue\Psr\PsrContext; use Enqueue\Psr\PsrProcessor; use Enqueue\Redis\Symfony\RedisTransportFactory; +use Enqueue\Rpc\Promise; use Enqueue\Sqs\Symfony\SqsTransportFactory; use Enqueue\Stomp\Symfony\RabbitMqStompTransportFactory; use Enqueue\Stomp\Symfony\StompTransportFactory; @@ -105,10 +106,13 @@ public function bind($topic, $processorName, $processor) /** * @param string $command * @param mixed $message + * @param bool $needReply + * + * @return Promise|null */ - public function sendCommand($command, $message) + public function sendCommand($command, $message, $needReply = false) { - $this->getProducer()->sendCommand($command, $message); + return $this->getProducer()->sendCommand($command, $message, $needReply); } /** From 95cdb2805b7e0a2798271e0d29958b643367b546 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Mon, 26 Jun 2017 16:37:07 +0300 Subject: [PATCH 19/20] [client] fix phpstan errors. --- pkg/enqueue/Client/Producer.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/enqueue/Client/Producer.php b/pkg/enqueue/Client/Producer.php index 7f066b794..a7460aee5 100644 --- a/pkg/enqueue/Client/Producer.php +++ b/pkg/enqueue/Client/Producer.php @@ -103,10 +103,10 @@ public function sendCommand($command, $message, $needReply = false) $message = new Message($message); } - if ($needReply) { - $deleteReplyQueue = false; - $replyTo = $message->getReplyTo(); + $deleteReplyQueue = false; + $replyTo = $message->getReplyTo(); + if ($needReply) { if (false == $replyTo) { $message->setReplyTo($replyTo = $this->rpcFactory->createReplyTo()); $deleteReplyQueue = true; From ac4b8aa1f634cb0be8eecb23db6e4f191b543bb2 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Mon, 26 Jun 2017 16:49:03 +0300 Subject: [PATCH 20/20] Release 0.5.0 --- CHANGELOG.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ab6a633c..4b2b2f154 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,18 @@ # Change Log +## [0.5.0](https://github.com/php-enqueue/enqueue-dev/tree/0.5.0) (2017-06-26) +[Full Changelog](https://github.com/php-enqueue/enqueue-dev/compare/0.4.20...0.5.0) + +- \[WIP\]\[beanstalk\] Add transport for beanstalkd [\#123](https://github.com/php-enqueue/enqueue-dev/pull/123) ([makasim](https://github.com/makasim)) + +- DBAL Transport: polling\_interval not taken into account [\#121](https://github.com/php-enqueue/enqueue-dev/issues/121) + +- \[client\] Merge experimental ProducerV2 methods to Producer interface. [\#124](https://github.com/php-enqueue/enqueue-dev/pull/124) ([makasim](https://github.com/makasim)) +- fix dbal polling interval configuration option [\#122](https://github.com/php-enqueue/enqueue-dev/pull/122) ([ASKozienko](https://github.com/ASKozienko)) + +## [0.4.20](https://github.com/php-enqueue/enqueue-dev/tree/0.4.20) (2017-06-20) +[Full Changelog](https://github.com/php-enqueue/enqueue-dev/compare/0.4.19...0.4.20) + ## [0.4.19](https://github.com/php-enqueue/enqueue-dev/tree/0.4.19) (2017-06-20) [Full Changelog](https://github.com/php-enqueue/enqueue-dev/compare/0.4.18...0.4.19)