Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Messenger] Fix cloned TraceableStack not unstacking the stack independently #51675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,9 @@ public function stop(): void
}
$this->currentEvent = null;
}

public function __clone()
{
$this->stack = clone $this->stack;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Messenger\Middleware\StackInterface;
use Symfony\Component\Messenger\Middleware\StackMiddleware;
use Symfony\Component\Messenger\Middleware\TraceableMiddleware;
use Symfony\Component\Messenger\Middleware\TraceableStack;
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
use Symfony\Component\Stopwatch\Stopwatch;
Expand Down Expand Up @@ -140,4 +141,92 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
$traced->handle($envelope, new StackMiddleware(new \ArrayIterator([null, $middleware])));
$this->assertSame(1, $middleware->calls);
}

public function testClonedTraceableStackUnstacksIndependently()
{
// import TraceableStack
class_exists(TraceableMiddleware::class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas, I added this test method to test if a clone can be unstacked independently. I noticed that the existing method testHandle() in the same file does test TraceableStack unstacking via the use of TraceableMiddleware, but it doesn't test if a cloned version will unstack the stack independently, so I was thinking their could be a new test for that as well?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant the class_exists check, why is it needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, without it I get the following error:
Error: Class 'Symfony\Component\Messenger\Middleware\TraceableStack' not found

To fix the error, I tried to import TraceableStack at the top of the test file, but I can't get it to work because it's located within TraceableMiddleware.php instead of within its own file.


$stackMiddleware = new StackMiddleware([
null,
$this->createMock(MiddlewareInterface::class),
$this->createMock(MiddlewareInterface::class),
]);

$stopwatch = $this->createMock(Stopwatch::class);

$traceableStack = new TraceableStack($stackMiddleware, $stopwatch, 'command_bus', 'messenger.middleware');
$clonedStack = clone $traceableStack;

$traceableStackMiddleware1 = $traceableStack->next();
$traceableStackMiddleware2 = $traceableStack->next();
$traceableStackTail = $traceableStack->next();
self::assertSame($stackMiddleware, $traceableStackTail);

// unstack clonedStack independently
$clonedStackMiddleware1 = $clonedStack->next();
self::assertSame($traceableStackMiddleware1, $clonedStackMiddleware1);
self::assertNotSame($traceableStackMiddleware2, $clonedStackMiddleware1);

$clonedStackMiddleware2 = $clonedStack->next();
self::assertSame($traceableStackMiddleware2, $clonedStackMiddleware2);

$clonedStackTail = $clonedStack->next();
self::assertNotSame($stackMiddleware, $clonedStackTail, 'stackMiddleware was also cloned');
}

public function testClonedTraceableStackUsesSameStopwatch()
{
// import TraceableStack
class_exists(TraceableMiddleware::class);

$middlewareIterable = [null, $this->createMock(MiddlewareInterface::class)];

$stackMiddleware = new StackMiddleware($middlewareIterable);

$stopwatch = $this->createMock(Stopwatch::class);
$stopwatch->expects($this->exactly(2))->method('isStarted')->willReturn(true);

$startSeries = [
[$this->matches('"%sMiddlewareInterface%s" on "command_bus"'), 'messenger.middleware'],
[$this->identicalTo('Tail on "command_bus"'), 'messenger.middleware'],
[$this->matches('"%sMiddlewareInterface%s" on "command_bus"'), 'messenger.middleware'],
[$this->identicalTo('Tail on "command_bus"'), 'messenger.middleware'],
];
$stopwatch->expects($this->exactly(4))
->method('start')
->willReturnCallback(function (string $name, string $category = null) use (&$startSeries) {
[$constraint, $expectedCategory] = array_shift($startSeries);

$constraint->evaluate($name);
$this->assertSame($expectedCategory, $category);

return $this->createMock(StopwatchEvent::class);
})
;

$stopSeries = [
$this->matches('"%sMiddlewareInterface%s" on "command_bus"'),
$this->matches('"%sMiddlewareInterface%s" on "command_bus"'),
];
$stopwatch->expects($this->exactly(2))
->method('stop')
->willReturnCallback(function (string $name) use (&$stopSeries) {
$constraint = array_shift($stopSeries);
$constraint->evaluate($name);

return $this->createMock(StopwatchEvent::class);
})
;

$traceableStack = new TraceableStack($stackMiddleware, $stopwatch, 'command_bus', 'messenger.middleware');
$clonedStack = clone $traceableStack;

// unstack the stacks independently
$traceableStack->next();
$traceableStack->next();

$clonedStack->next();
$clonedStack->next();
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.