From 279a100e7c23b0e957a85207cfb555878ac691c3 Mon Sep 17 00:00:00 2001 From: Ian Jenkins Date: Wed, 9 Aug 2017 09:19:57 +0100 Subject: [PATCH 1/2] Fix compilation of command routes. All subscriptions were being added as event routes meaning that RouterProcessor::routeCommand() wouldn't do anything as commandRoutes were appearing empty. This fixes that by checking to see if command subscriptions exist and extracting them into the commandRoutes argument on RouterProcessor. --- .../Compiler/BuildClientRoutingPass.php | 11 +++++++++++ pkg/enqueue-bundle/Resources/config/client.yml | 1 + 2 files changed, 12 insertions(+) diff --git a/pkg/enqueue-bundle/DependencyInjection/Compiler/BuildClientRoutingPass.php b/pkg/enqueue-bundle/DependencyInjection/Compiler/BuildClientRoutingPass.php index cb924de42..27a904a4c 100644 --- a/pkg/enqueue-bundle/DependencyInjection/Compiler/BuildClientRoutingPass.php +++ b/pkg/enqueue-bundle/DependencyInjection/Compiler/BuildClientRoutingPass.php @@ -2,6 +2,7 @@ namespace Enqueue\Bundle\DependencyInjection\Compiler; +use Enqueue\Client\Config; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -35,5 +36,15 @@ public function process(ContainerBuilder $container) $router = $container->getDefinition($routerId); $router->replaceArgument(1, $configs); + + if (isset($configs[Config::COMMAND_TOPIC])) { + $commandRoutes = []; + + foreach ($configs[Config::COMMAND_TOPIC] as $command) { + $commandRoutes[$command[0]] = $command[1]; + } + + $router->replaceArgument(2, $commandRoutes); + } } } diff --git a/pkg/enqueue-bundle/Resources/config/client.yml b/pkg/enqueue-bundle/Resources/config/client.yml index cc2b30f73..f7bf69ae8 100644 --- a/pkg/enqueue-bundle/Resources/config/client.yml +++ b/pkg/enqueue-bundle/Resources/config/client.yml @@ -42,6 +42,7 @@ services: arguments: - '@enqueue.client.driver' - [] + - [] tags: - name: 'enqueue.client.processor' From c65f70ff184004b9157a7deae2e70f18eab20c77 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Wed, 9 Aug 2017 12:19:35 +0300 Subject: [PATCH 2/2] [client] fix commands routing. --- .../Compiler/BuildClientRoutingPass.php | 28 +++++------ .../Functional/App/config/custom-config.yml | 5 ++ .../Tests/Functional/TestCommandProcessor.php | 30 ++++++++++++ .../Tests/Functional/UseCasesTest.php | 47 +++++++++++++++++++ .../Compiler/BuildClientRoutingPassTest.php | 15 +++--- 5 files changed, 100 insertions(+), 25 deletions(-) create mode 100644 pkg/enqueue-bundle/Tests/Functional/TestCommandProcessor.php diff --git a/pkg/enqueue-bundle/DependencyInjection/Compiler/BuildClientRoutingPass.php b/pkg/enqueue-bundle/DependencyInjection/Compiler/BuildClientRoutingPass.php index 27a904a4c..541f1f977 100644 --- a/pkg/enqueue-bundle/DependencyInjection/Compiler/BuildClientRoutingPass.php +++ b/pkg/enqueue-bundle/DependencyInjection/Compiler/BuildClientRoutingPass.php @@ -22,29 +22,25 @@ public function process(ContainerBuilder $container) return; } - $configs = []; + $events = []; + $commands = []; foreach ($container->findTaggedServiceIds($processorTagName) as $serviceId => $tagAttributes) { $subscriptions = $this->extractSubscriptions($container, $serviceId, $tagAttributes); foreach ($subscriptions as $subscription) { - $configs[$subscription['topicName']][] = [ - $subscription['processorName'], - $subscription['queueName'], - ]; + if (Config::COMMAND_TOPIC === $subscription['topicName']) { + $commands[$subscription['processorName']] = $subscription['queueName']; + } else { + $events[$subscription['topicName']][] = [ + $subscription['processorName'], + $subscription['queueName'], + ]; + } } } $router = $container->getDefinition($routerId); - $router->replaceArgument(1, $configs); - - if (isset($configs[Config::COMMAND_TOPIC])) { - $commandRoutes = []; - - foreach ($configs[Config::COMMAND_TOPIC] as $command) { - $commandRoutes[$command[0]] = $command[1]; - } - - $router->replaceArgument(2, $commandRoutes); - } + $router->replaceArgument(1, $events); + $router->replaceArgument(2, $commands); } } diff --git a/pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml b/pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml index b0bdee12e..d99f650d2 100644 --- a/pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml +++ b/pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml @@ -26,3 +26,8 @@ services: class: 'Enqueue\Bundle\Tests\Functional\TestProcessor' tags: - { name: 'enqueue.client.processor' } + + test.message.command_processor: + class: 'Enqueue\Bundle\Tests\Functional\TestCommandProcessor' + tags: + - { name: 'enqueue.client.processor' } diff --git a/pkg/enqueue-bundle/Tests/Functional/TestCommandProcessor.php b/pkg/enqueue-bundle/Tests/Functional/TestCommandProcessor.php new file mode 100644 index 000000000..be6438ae2 --- /dev/null +++ b/pkg/enqueue-bundle/Tests/Functional/TestCommandProcessor.php @@ -0,0 +1,30 @@ +message = $message; + + return self::ACK; + } + + public static function getSubscribedCommand() + { + return self::COMMAND; + } +} diff --git a/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php b/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php index 9e544c200..da103744c 100644 --- a/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php +++ b/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php @@ -179,6 +179,53 @@ public function testProducerSendsMessage(array $enqueueConfig) $this->assertSame('test message body', $message->getBody()); } + /** + * @dataProvider provideEnqueueConfigs + */ + public function testProducerSendsCommandMessage(array $enqueueConfig) + { + $this->customSetUp($enqueueConfig); + + $expectedBody = __METHOD__.time(); + + $this->getMessageProducer()->sendCommand(TestCommandProcessor::COMMAND, $expectedBody); + + $queue = $this->getPsrContext()->createQueue('enqueue.test'); + + $consumer = $this->getPsrContext()->createConsumer($queue); + + $message = $consumer->receive(100); + $consumer->acknowledge($message); + + $this->assertInstanceOf(PsrMessage::class, $message); + $this->assertSame($expectedBody, $message->getBody()); + } + + /** + * @dataProvider provideEnqueueConfigs + */ + public function testClientConsumeCommandMessagesFromExplicitlySetQueue(array $enqueueConfig) + { + $this->customSetUp($enqueueConfig); + + $command = $this->container->get('enqueue.client.consume_messages_command'); + $processor = $this->container->get('test.message.command_processor'); + + $expectedBody = __METHOD__.time(); + + $this->getMessageProducer()->sendCommand(TestCommandProcessor::COMMAND, $expectedBody); + + $tester = new CommandTester($command); + $tester->execute([ + '--message-limit' => 2, + '--time-limit' => 'now +10 seconds', + 'client-queue-names' => ['test'], + ]); + + $this->assertInstanceOf(PsrMessage::class, $processor->message); + $this->assertEquals($expectedBody, $processor->message->getBody()); + } + /** * @dataProvider provideEnqueueConfigs */ diff --git a/pkg/enqueue-bundle/Tests/Unit/DependencyInjection/Compiler/BuildClientRoutingPassTest.php b/pkg/enqueue-bundle/Tests/Unit/DependencyInjection/Compiler/BuildClientRoutingPassTest.php index 4ed56d36d..a4b769c02 100644 --- a/pkg/enqueue-bundle/Tests/Unit/DependencyInjection/Compiler/BuildClientRoutingPassTest.php +++ b/pkg/enqueue-bundle/Tests/Unit/DependencyInjection/Compiler/BuildClientRoutingPassTest.php @@ -9,7 +9,6 @@ use Enqueue\Bundle\Tests\Unit\DependencyInjection\Compiler\Mock\ProcessorNameCommandSubscriber; use Enqueue\Bundle\Tests\Unit\DependencyInjection\Compiler\Mock\ProcessorNameTopicSubscriber; use Enqueue\Bundle\Tests\Unit\DependencyInjection\Compiler\Mock\QueueNameTopicSubscriber; -use Enqueue\Client\Config; use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; @@ -250,12 +249,11 @@ public function testShouldBuildRouteFromCommandSubscriberIfOnlyCommandNameSpecif $pass->process($container); $expectedRoutes = [ - Config::COMMAND_TOPIC => [ - ['the-command-name', 'aDefaultQueueName'], - ], + 'the-command-name' => 'aDefaultQueueName', ]; - $this->assertEquals($expectedRoutes, $router->getArgument(1)); + $this->assertEquals([], $router->getArgument(1)); + $this->assertEquals($expectedRoutes, $router->getArgument(2)); } public function testShouldBuildRouteFromCommandSubscriberIfProcessorNameSpecified() @@ -274,12 +272,11 @@ public function testShouldBuildRouteFromCommandSubscriberIfProcessorNameSpecifie $pass->process($container); $expectedRoutes = [ - Config::COMMAND_TOPIC => [ - ['the-command-name', 'the-command-queue-name'], - ], + 'the-command-name' => 'the-command-queue-name', ]; - $this->assertEquals($expectedRoutes, $router->getArgument(1)); + $this->assertEquals([], $router->getArgument(1)); + $this->assertEquals($expectedRoutes, $router->getArgument(2)); } /**