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

[HttpKernel] always close open stopwatch section after handling kernel.request events #42331

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
Aug 6, 2021
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 @@ -30,6 +30,7 @@ protected function beforeDispatch(string $eventName, $event)
{
switch ($eventName) {
case KernelEvents::REQUEST:
$event->getRequest()->attributes->set('_stopwatch_token', substr(hash('sha256', uniqid(mt_rand(), true)), 0, 6));
$this->stopwatch->openSection();
break;
case KernelEvents::VIEW:
Expand All @@ -40,8 +41,8 @@ protected function beforeDispatch(string $eventName, $event)
}
break;
case KernelEvents::TERMINATE:
$token = $event->getResponse()->headers->get('X-Debug-Token');
if (null === $token) {
$sectionId = $event->getRequest()->attributes->get('_stopwatch_token');
if (null === $sectionId) {
break;
}
// There is a very special case when using built-in AppCache class as kernel wrapper, in the case
Expand All @@ -50,7 +51,7 @@ protected function beforeDispatch(string $eventName, $event)
// is equal to the [A] debug token. Trying to reopen section with the [B] token throws an exception
// which must be caught.
try {
$this->stopwatch->openSection($token);
$this->stopwatch->openSection($sectionId);
} catch (\LogicException $e) {
}
break;
Expand All @@ -67,21 +68,21 @@ protected function afterDispatch(string $eventName, $event)
$this->stopwatch->start('controller', 'section');
break;
case KernelEvents::RESPONSE:
$token = $event->getResponse()->headers->get('X-Debug-Token');
if (null === $token) {
$sectionId = $event->getRequest()->attributes->get('_stopwatch_token');
if (null === $sectionId) {
break;
}
$this->stopwatch->stopSection($token);
$this->stopwatch->stopSection($sectionId);
break;
case KernelEvents::TERMINATE:
// In the special case described in the `preDispatch` method above, the `$token` section
// does not exist, then closing it throws an exception which must be caught.
$token = $event->getResponse()->headers->get('X-Debug-Token');
if (null === $token) {
$sectionId = $event->getRequest()->attributes->get('_stopwatch_token');
if (null === $sectionId) {
break;
}
try {
$this->stopwatch->stopSection($token);
$this->stopwatch->stopSection($sectionId);
} catch (\LogicException $e) {
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ class TraceableEventDispatcherTest extends TestCase
public function testStopwatchSections()
{
$dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch = new Stopwatch());
$kernel = $this->getHttpKernel($dispatcher, function () { return new Response('', 200, ['X-Debug-Token' => '292e1e']); });
$kernel = $this->getHttpKernel($dispatcher);
$request = Request::create('/');
$response = $kernel->handle($request);
$kernel->terminate($request, $response);

$events = $stopwatch->getSectionEvents($response->headers->get('X-Debug-Token'));
$events = $stopwatch->getSectionEvents($request->attributes->get('_stopwatch_token'));
$this->assertEquals([
'__section__',
'kernel.request',
Expand All @@ -56,7 +56,7 @@ public function testStopwatchCheckControllerOnRequestEvent()

$dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch);

$kernel = $this->getHttpKernel($dispatcher, function () { return new Response(); });
$kernel = $this->getHttpKernel($dispatcher);
$request = Request::create('/');
$kernel->handle($request);
}
Expand All @@ -69,12 +69,12 @@ public function testStopwatchStopControllerOnRequestEvent()
$stopwatch->expects($this->once())
->method('isStarted')
->willReturn(true);
$stopwatch->expects($this->once())
$stopwatch->expects($this->exactly(3))
->method('stop');

$dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch);

$kernel = $this->getHttpKernel($dispatcher, function () { return new Response(); });
$kernel = $this->getHttpKernel($dispatcher);
$request = Request::create('/');
$kernel->handle($request);
}
Expand Down Expand Up @@ -110,10 +110,12 @@ public function testListenerCanRemoveItselfWhenExecuted()
$this->assertCount(1, $eventDispatcher->getListeners('foo'), 'expected listener1 to be removed');
}

protected function getHttpKernel($dispatcher, $controller)
protected function getHttpKernel($dispatcher)
{
$controllerResolver = $this->createMock(ControllerResolverInterface::class);
$controllerResolver->expects($this->once())->method('getController')->willReturn($controller);
$controllerResolver->expects($this->once())->method('getController')->willReturn(function () {
return new Response();
});
$argumentResolver = $this->createMock(ArgumentResolverInterface::class);
$argumentResolver->expects($this->once())->method('getArguments')->willReturn([]);

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.