[Messenger] Fix stopwach usage if it has been reset

This commit is contained in:
Grégoire Pineau 2020-12-21 19:37:54 +01:00
parent fc794e5964
commit bf4b0cc022
2 changed files with 34 additions and 2 deletions

View File

@ -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);
}

View File

@ -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);
}
}