[Messenger] Fix stopwach usage if it has been reset
This commit is contained in:
parent
fc794e5964
commit
bf4b0cc022
@ -71,7 +71,7 @@ class TraceableStack implements StackInterface
|
|||||||
*/
|
*/
|
||||||
public function next(): MiddlewareInterface
|
public function next(): MiddlewareInterface
|
||||||
{
|
{
|
||||||
if (null !== $this->currentEvent) {
|
if (null !== $this->currentEvent && $this->stopwatch->isStarted($this->currentEvent)) {
|
||||||
$this->stopwatch->stop($this->currentEvent);
|
$this->stopwatch->stop($this->currentEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ class TraceableMiddlewareTest extends MiddlewareTestCase
|
|||||||
};
|
};
|
||||||
|
|
||||||
$stopwatch = $this->createMock(Stopwatch::class);
|
$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))
|
$stopwatch->expects($this->exactly(2))
|
||||||
->method('start')
|
->method('start')
|
||||||
->withConsecutive(
|
->withConsecutive(
|
||||||
@ -91,4 +91,36 @@ class TraceableMiddlewareTest extends MiddlewareTestCase
|
|||||||
$traced = new TraceableMiddleware($stopwatch, $busId);
|
$traced = new TraceableMiddleware($stopwatch, $busId);
|
||||||
$traced->handle(new Envelope(new DummyMessage('Hello')), new StackMiddleware(new \ArrayIterator([null, $middleware])));
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user