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

Skip to content

Commit 6b8456d

Browse files
committed
[Messenger] Fix stopwach usage if it has been reset
1 parent 18927fe commit 6b8456d

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ public function __construct(StackInterface $stack, Stopwatch $stopwatch, string
7272
public function next(): MiddlewareInterface
7373
{
7474
if (null !== $this->currentEvent) {
75-
$this->stopwatch->stop($this->currentEvent);
75+
try {
76+
$this->stopwatch->stop($this->currentEvent);
77+
} catch (\LogicException $th) {
78+
// Maybe the stopwatch has been reset, noop
79+
}
7680
}
7781

7882
if ($this->stack === $nextMiddleware = $this->stack->next()) {

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 $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)