mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-02 13:02:24 +08:00
Preventing breaking profiling messages when flushing (#18800)
This commit is contained in:
@ -341,15 +341,16 @@ class LoggerTest extends TestCase
|
||||
*/
|
||||
public function testGetProfilingWithEmptyCategoriesAndExcludeCategories()
|
||||
{
|
||||
$messages = ['anyData'];
|
||||
$returnValue = 'return value';
|
||||
/* @var $logger Logger|\PHPUnit_Framework_MockObject_MockObject */
|
||||
$logger = $this->getMockBuilder('yii\log\Logger')
|
||||
->setMethods(['calculateTimings'])
|
||||
->getMock();
|
||||
|
||||
$logger->messages = ['anyData'];
|
||||
$logger->messages = $messages;
|
||||
$logger->method('calculateTimings')->willReturn($returnValue);
|
||||
$logger->expects($this->once())->method('calculateTimings')->with([]);
|
||||
$logger->expects($this->once())->method('calculateTimings')->with($messages);
|
||||
$this->assertEquals($returnValue, $logger->getProfiling());
|
||||
}
|
||||
|
||||
@ -376,7 +377,7 @@ class LoggerTest extends TestCase
|
||||
|
||||
$logger->messages = $messages;
|
||||
$logger->method('calculateTimings')->willReturn($returnValue);
|
||||
$logger->expects($this->once())->method('calculateTimings')->with([]);
|
||||
$logger->expects($this->once())->method('calculateTimings')->with($messages);
|
||||
$this->assertEquals([], $logger->getProfiling(['not-matched-category']));
|
||||
}
|
||||
|
||||
@ -416,7 +417,7 @@ class LoggerTest extends TestCase
|
||||
|
||||
$logger->messages = $messages;
|
||||
$logger->method('calculateTimings')->willReturn($returnValue);
|
||||
$logger->expects($this->once())->method('calculateTimings')->with([]);
|
||||
$logger->expects($this->once())->method('calculateTimings')->with($messages);
|
||||
$this->assertEquals([$matchedByCategoryName], $logger->getProfiling(['category']));
|
||||
|
||||
/*
|
||||
@ -429,7 +430,7 @@ class LoggerTest extends TestCase
|
||||
|
||||
$logger->messages = $messages;
|
||||
$logger->method('calculateTimings')->willReturn($returnValue);
|
||||
$logger->expects($this->once())->method('calculateTimings')->with([]);
|
||||
$logger->expects($this->once())->method('calculateTimings')->with($messages);
|
||||
$this->assertEquals([$matchedByCategoryName, $secondCategory], $logger->getProfiling(['category*']));
|
||||
}
|
||||
|
||||
@ -478,7 +479,7 @@ class LoggerTest extends TestCase
|
||||
|
||||
$logger->messages = $messages;
|
||||
$logger->method('calculateTimings')->willReturn($returnValue);
|
||||
$logger->expects($this->once())->method('calculateTimings')->with([]);
|
||||
$logger->expects($this->once())->method('calculateTimings')->with($messages);
|
||||
$this->assertEquals([$fistCategory, $secondCategory], $logger->getProfiling(['cat*'], ['category3']));
|
||||
|
||||
/*
|
||||
@ -491,7 +492,7 @@ class LoggerTest extends TestCase
|
||||
|
||||
$logger->messages = $messages;
|
||||
$logger->method('calculateTimings')->willReturn($returnValue);
|
||||
$logger->expects($this->once())->method('calculateTimings')->with([]);
|
||||
$logger->expects($this->once())->method('calculateTimings')->with($messages);
|
||||
$this->assertEquals([$fistCategory], $logger->getProfiling(['cat*'], ['category*']));
|
||||
}
|
||||
|
||||
|
||||
@ -237,24 +237,108 @@ class TargetTest extends TestCase
|
||||
$this->assertCount(6, static::$messages[1]);
|
||||
}
|
||||
|
||||
public function testBreakProfilingWithFlush()
|
||||
public function testBreakProfilingWithFlushWithProfilingDisabled()
|
||||
{
|
||||
$logger = new Logger();
|
||||
new Dispatcher([
|
||||
'logger' => $logger,
|
||||
'targets' => [new TestTarget()],
|
||||
$dispatcher = $this->getMockBuilder('yii\log\Dispatcher')
|
||||
->setMethods(['dispatch'])
|
||||
->getMock();
|
||||
$dispatcher->expects($this->once())->method('dispatch')->with($this->callback(function ($messages) {
|
||||
return count($messages) === 2
|
||||
&& $messages[0][0] === 'token.a'
|
||||
&& $messages[0][1] == Logger::LEVEL_PROFILE_BEGIN
|
||||
&& $messages[1][0] === 'info';
|
||||
}), false);
|
||||
|
||||
$logger = new Logger([
|
||||
'dispatcher' => $dispatcher,
|
||||
'flushInterval' => 2,
|
||||
]);
|
||||
|
||||
$logger->log('token.a', Logger::LEVEL_PROFILE_BEGIN, 'category');
|
||||
$logger->log('info', Logger::LEVEL_INFO, 'category');
|
||||
$logger->log('token.a', Logger::LEVEL_PROFILE_END, 'category');
|
||||
}
|
||||
|
||||
$timings = $logger->getProfiling(['category']);
|
||||
public function testNotBreakProfilingWithFlushWithProfilingEnabled()
|
||||
{
|
||||
$dispatcher = $this->getMockBuilder('yii\log\Dispatcher')
|
||||
->setMethods(['dispatch'])
|
||||
->getMock();
|
||||
$dispatcher->expects($this->exactly(2))->method('dispatch')->withConsecutive(
|
||||
[
|
||||
$this->callback(function ($messages) {
|
||||
return count($messages) === 1 && $messages[0][0] === 'info';
|
||||
}),
|
||||
false
|
||||
],
|
||||
[
|
||||
$this->callback(function ($messages) {
|
||||
return count($messages) === 2
|
||||
&& $messages[0][0] === 'token.a'
|
||||
&& $messages[0][1] == Logger::LEVEL_PROFILE_BEGIN
|
||||
&& $messages[1][0] === 'token.a'
|
||||
&& $messages[1][1] == Logger::LEVEL_PROFILE_END;
|
||||
}),
|
||||
false
|
||||
]
|
||||
);
|
||||
|
||||
self::assertCount(1, $timings);
|
||||
self::assertArrayHasKey('info', $timings[0]);
|
||||
self::assertArrayHasKey('category', $timings[0]);
|
||||
$logger = new Logger([
|
||||
'profilingAware' => true,
|
||||
'dispatcher' => $dispatcher,
|
||||
'flushInterval' => 2,
|
||||
]);
|
||||
|
||||
$logger->log('token.a', Logger::LEVEL_PROFILE_BEGIN, 'category');
|
||||
$logger->log('info', Logger::LEVEL_INFO, 'category');
|
||||
$logger->log('token.a', Logger::LEVEL_PROFILE_END, 'category');
|
||||
}
|
||||
|
||||
public function testFlushingWithProfilingEnabledAndOverflow()
|
||||
{
|
||||
$dispatcher = $this->getMockBuilder('yii\log\Dispatcher')
|
||||
->setMethods(['dispatch'])
|
||||
->getMock();
|
||||
$dispatcher->expects($this->exactly(3))->method('dispatch')->withConsecutive(
|
||||
[
|
||||
$this->callback(function ($messages) {
|
||||
return count($messages) === 2
|
||||
&& $messages[0][0] === 'token.a'
|
||||
&& $messages[0][1] == Logger::LEVEL_PROFILE_BEGIN
|
||||
&& $messages[1][0] === 'token.b'
|
||||
&& $messages[1][1] == Logger::LEVEL_PROFILE_BEGIN;
|
||||
}),
|
||||
false
|
||||
],
|
||||
[
|
||||
$this->callback(function ($messages) {
|
||||
return count($messages) === 1
|
||||
&& $messages[0][0] === 'Number of dangling profiling block messages reached flushInterval value and therefore these were flushed. Please consider setting higher flushInterval value or making profiling blocks shorter.';
|
||||
}),
|
||||
false
|
||||
],
|
||||
[
|
||||
$this->callback(function ($messages) {
|
||||
return count($messages) === 2
|
||||
&& $messages[0][0] === 'token.b'
|
||||
&& $messages[0][1] == Logger::LEVEL_PROFILE_END
|
||||
&& $messages[1][0] === 'token.a'
|
||||
&& $messages[1][1] == Logger::LEVEL_PROFILE_END;
|
||||
}),
|
||||
false
|
||||
]
|
||||
);
|
||||
|
||||
$logger = new Logger([
|
||||
'profilingAware' => true,
|
||||
'dispatcher' => $dispatcher,
|
||||
'flushInterval' => 2,
|
||||
]);
|
||||
|
||||
$logger->log('token.a', Logger::LEVEL_PROFILE_BEGIN, 'category');
|
||||
$logger->log('token.b', Logger::LEVEL_PROFILE_BEGIN, 'category');
|
||||
$logger->log('token.b', Logger::LEVEL_PROFILE_END, 'category');
|
||||
$logger->log('token.a', Logger::LEVEL_PROFILE_END, 'category');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user