Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bf4b0cc

Browse files
committed
[Messenger] Fix stopwach usage if it has been reset
1 parent fc794e5 commit bf4b0cc

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/Symfony/Component/Messenger/Middleware/TraceableMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function __construct(StackInterface $stack, Stopwatch $stopwatch, string
7171
*/
7272
public function next(): MiddlewareInterface
7373
{
74-
if (null !== $this->currentEvent) {
74+
if (null !== $this->currentEvent && $this->stopwatch->isStarted($this->currentEvent)) {
7575
$this->stopwatch->stop($this->currentEvent);
7676
}
7777

src/Symfony/Component/Messenger/Tests/Middleware/TraceableMiddlewareTest.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
4242
};
4343

4444
$stopwatch = $this->createMock(Stopwatch::class);
45-
$stopwatch->expects($this->once())->method('isStarted')->willReturn(true);
45+
$stopwatch->expects($this->exactly(2))->method('isStarted')->willReturn(true);
4646
$stopwatch->expects($this->exactly(2))
4747
->method('start')
4848
->withConsecutive(
@@ -91,4 +91,36 @@ public function testHandleWithException()
9191
$traced = new TraceableMiddleware($stopwatch, $busId);
9292
$traced->handle(new Envelope(new DummyMessage('Hello')), new StackMiddleware(new \ArrayIterator([null, $middleware])));
9393
}
94+
95+
public function testHandleWhenStopwatchHasBeenReset()
96+
{
97+
$busId = 'command_bus';
98+
$envelope = new Envelope(new DummyMessage('Hello'));
99+
100+
$stopwatch = new Stopwatch();
101+
102+
$middleware = new class($stopwatch) implements MiddlewareInterface {
103+
public $calls = 0;
104+
private $stopwatch;
105+
106+
public function __construct(Stopwatch $stopwatch)
107+
{
108+
$this->stopwatch = $stopwatch;
109+
}
110+
111+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
112+
{
113+
$this->stopwatch->reset();
114+
115+
++$this->calls;
116+
117+
return $stack->next()->handle($envelope, $stack);
118+
}
119+
};
120+
121+
$traced = new TraceableMiddleware($stopwatch, $busId);
122+
123+
$traced->handle($envelope, new StackMiddleware(new \ArrayIterator([null, $middleware])));
124+
$this->assertSame(1, $middleware->calls);
125+
}
94126
}

0 commit comments

Comments
 (0)