From 5c1f3a2414a6b580b7989b90d6ad02278e32e696 Mon Sep 17 00:00:00 2001 From: "tien.xuan.vo" Date: Fri, 23 Aug 2019 10:48:20 +0700 Subject: [PATCH 1/2] [Messenger] Stop worker when it should stop --- .../Component/Messenger/Tests/WorkerTest.php | 25 +++++++++++++++++++ src/Symfony/Component/Messenger/Worker.php | 4 +++ 2 files changed, 29 insertions(+) diff --git a/src/Symfony/Component/Messenger/Tests/WorkerTest.php b/src/Symfony/Component/Messenger/Tests/WorkerTest.php index ac8c2a7ad8..8c8e54dda9 100644 --- a/src/Symfony/Component/Messenger/Tests/WorkerTest.php +++ b/src/Symfony/Component/Messenger/Tests/WorkerTest.php @@ -27,6 +27,7 @@ use Symfony\Component\Messenger\Stamp\SentStamp; use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface; use Symfony\Component\Messenger\Worker; +use Symfony\Component\Messenger\Worker\StopWhenMessageCountIsExceededWorker; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; /** @@ -361,6 +362,30 @@ class WorkerTest extends TestCase // make sure they were processed in the correct order $this->assertSame([$envelope1, $envelope2, $envelope3, $envelope4, $envelope5, $envelope6], $processedEnvelopes); } + + public function testWorkerWithDecorator() + { + $envelope1 = new Envelope(new DummyMessage('message1')); + $envelope2 = new Envelope(new DummyMessage('message2')); + $envelope3 = new Envelope(new DummyMessage('message3')); + + $receiver = new DummyReceiver([ + [$envelope1, $envelope2, $envelope3], + ]); + + $bus = $this->getMockBuilder(MessageBusInterface::class)->getMock(); + + $worker = new Worker([$receiver], $bus); + $workerWithDecorator = new StopWhenMessageCountIsExceededWorker($worker, 2); + $processedEnvelopes = []; + $workerWithDecorator->run([], function (?Envelope $envelope) use ($worker, &$processedEnvelopes) { + if (null !== $envelope) { + $processedEnvelopes[] = $envelope; + } + }); + + $this->assertSame([$envelope1, $envelope2], $processedEnvelopes); + } } class DummyReceiver implements ReceiverInterface diff --git a/src/Symfony/Component/Messenger/Worker.php b/src/Symfony/Component/Messenger/Worker.php index d0c98b76ff..6bfb4cb675 100644 --- a/src/Symfony/Component/Messenger/Worker.php +++ b/src/Symfony/Component/Messenger/Worker.php @@ -92,6 +92,10 @@ class Worker implements WorkerInterface $this->handleMessage($envelope, $receiver, $transportName, $this->retryStrategies[$transportName] ?? null); $onHandled($envelope); + + if ($this->shouldStop) { + break 2; + } } // after handling a single receiver, quit and start the loop again From 834d5cbce20bfb941d582bf3fcdd6b4ccc340de1 Mon Sep 17 00:00:00 2001 From: pdommelen Date: Mon, 26 Aug 2019 09:37:14 +0200 Subject: [PATCH 2/2] [DependencyInjection] Fixed the `getServiceIds` implementation to always return aliases --- .../DependencyInjection/Container.php | 2 +- .../Tests/ContainerTest.php | 4 ++-- .../Tests/Dumper/PhpDumperTest.php | 24 +++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Container.php b/src/Symfony/Component/DependencyInjection/Container.php index ab69579adb..28b54da0ec 100644 --- a/src/Symfony/Component/DependencyInjection/Container.php +++ b/src/Symfony/Component/DependencyInjection/Container.php @@ -405,7 +405,7 @@ class Container implements ResettableContainerInterface } $ids[] = 'service_container'; - return array_map('strval', array_unique(array_merge($ids, array_keys($this->methodMap), array_keys($this->fileMap), array_keys($this->services)))); + return array_map('strval', array_unique(array_merge($ids, array_keys($this->methodMap), array_keys($this->fileMap), array_keys($this->aliases), array_keys($this->services)))); } /** diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php index ebc422ca93..46527e09dd 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php @@ -156,7 +156,7 @@ class ContainerTest extends TestCase $sc = new ProjectServiceContainer(); $sc->set('foo', $obj = new \stdClass()); - $this->assertEquals(['service_container', 'internal', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'internal_dependency', 'foo'], $sc->getServiceIds(), '->getServiceIds() returns defined service ids by factory methods in the method map, followed by service ids defined by set()'); + $this->assertEquals(['service_container', 'internal', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'internal_dependency', 'alias', 'foo'], $sc->getServiceIds(), '->getServiceIds() returns defined service ids by factory methods in the method map, followed by service ids defined by set()'); } /** @@ -168,7 +168,7 @@ class ContainerTest extends TestCase $sc = new LegacyProjectServiceContainer(); $sc->set('foo', $obj = new \stdClass()); - $this->assertEquals(['internal', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'service_container', 'foo'], $sc->getServiceIds(), '->getServiceIds() returns defined service ids by getXXXService() methods, followed by service ids defined by set()'); + $this->assertEquals(['internal', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'service_container', 'alias', 'foo'], $sc->getServiceIds(), '->getServiceIds() returns defined service ids by getXXXService() methods, followed by service ids defined by set()'); } public function testSet() diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php index c19f717538..c63380d3d9 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php @@ -1139,6 +1139,30 @@ class PhpDumperTest extends TestCase $this->assertTrue($container->has('foo')); $this->assertSame('some value', $container->get('foo')); } + + public function testAliasCanBeFoundInTheDumpedContainerWhenBothTheAliasAndTheServiceArePublic() + { + $container = new ContainerBuilder(); + + $container->register('foo', 'stdClass')->setPublic(true); + $container->setAlias('bar', 'foo')->setPublic(true); + + $container->compile(); + + // Bar is found in the compiled container + $service_ids = $container->getServiceIds(); + $this->assertContains('bar', $service_ids); + + $dumper = new PhpDumper($container); + $dump = $dumper->dump(['class' => 'Symfony_DI_PhpDumper_AliasesCanBeFoundInTheDumpedContainer']); + eval('?>'.$dump); + + $container = new \Symfony_DI_PhpDumper_AliasesCanBeFoundInTheDumpedContainer(); + + // Bar should still be found in the compiled container + $service_ids = $container->getServiceIds(); + $this->assertContains('bar', $service_ids); + } } class Rot13EnvVarProcessor implements EnvVarProcessorInterface