diff --git a/src/Symfony/Component/Messenger/Middleware/TraceableMiddleware.php b/src/Symfony/Component/Messenger/Middleware/TraceableMiddleware.php index bedade318f..f391cfe6d0 100644 --- a/src/Symfony/Component/Messenger/Middleware/TraceableMiddleware.php +++ b/src/Symfony/Component/Messenger/Middleware/TraceableMiddleware.php @@ -71,7 +71,7 @@ class TraceableStack implements StackInterface */ public function next(): MiddlewareInterface { - if (null !== $this->currentEvent) { + if (null !== $this->currentEvent && $this->stopwatch->isStarted($this->currentEvent)) { $this->stopwatch->stop($this->currentEvent); } diff --git a/src/Symfony/Component/Messenger/Tests/Middleware/TraceableMiddlewareTest.php b/src/Symfony/Component/Messenger/Tests/Middleware/TraceableMiddlewareTest.php index 3bc64675d9..0370d86726 100644 --- a/src/Symfony/Component/Messenger/Tests/Middleware/TraceableMiddlewareTest.php +++ b/src/Symfony/Component/Messenger/Tests/Middleware/TraceableMiddlewareTest.php @@ -42,7 +42,7 @@ class TraceableMiddlewareTest extends MiddlewareTestCase }; $stopwatch = $this->createMock(Stopwatch::class); - $stopwatch->expects($this->once())->method('isStarted')->willReturn(true); + $stopwatch->expects($this->exactly(2))->method('isStarted')->willReturn(true); $stopwatch->expects($this->exactly(2)) ->method('start') ->withConsecutive( @@ -91,4 +91,36 @@ class TraceableMiddlewareTest extends MiddlewareTestCase $traced = new TraceableMiddleware($stopwatch, $busId); $traced->handle(new Envelope(new DummyMessage('Hello')), new StackMiddleware(new \ArrayIterator([null, $middleware]))); } + + public function testHandleWhenStopwatchHasBeenReset() + { + $busId = 'command_bus'; + $envelope = new Envelope(new DummyMessage('Hello')); + + $stopwatch = new Stopwatch(); + + $middleware = new class($stopwatch) implements MiddlewareInterface { + public $calls = 0; + private $stopwatch; + + public function __construct(Stopwatch $stopwatch) + { + $this->stopwatch = $stopwatch; + } + + public function handle(Envelope $envelope, StackInterface $stack): Envelope + { + $this->stopwatch->reset(); + + ++$this->calls; + + return $stack->next()->handle($envelope, $stack); + } + }; + + $traced = new TraceableMiddleware($stopwatch, $busId); + + $traced->handle($envelope, new StackMiddleware(new \ArrayIterator([null, $middleware]))); + $this->assertSame(1, $middleware->calls); + } }