-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration test for SIGTERM handling
- Loading branch information
Showing
3 changed files
with
198 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
tests/PHPUnit/Integration/CronArchiveProcessSignalTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
<?php | ||
|
||
/** | ||
* Matomo - free/libre analytics platform | ||
* | ||
* @link https://matomo.org | ||
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later | ||
*/ | ||
|
||
namespace Piwik\Tests\Integration; | ||
|
||
use Piwik\CliMulti\CliPhp; | ||
use Piwik\CliMulti\ProcessSymfony; | ||
use Piwik\Common; | ||
use Piwik\Container\StaticContainer; | ||
use Piwik\DataAccess\Model; | ||
use Piwik\Db; | ||
use Piwik\Option; | ||
use Piwik\Plugins\CoreConsole\FeatureFlags\CliMultiProcessSymfony; | ||
use Piwik\Plugins\FeatureFlags\FeatureFlagManager; | ||
use Piwik\Tests\Fixtures\OneVisit; | ||
use Piwik\Tests\Framework\TestCase\IntegrationTestCase; | ||
|
||
/** | ||
* @group Archiver | ||
* @group CronArchive | ||
* @group CronArchiveProcessSignal | ||
*/ | ||
class CronArchiveProcessSignalTest extends IntegrationTestCase | ||
{ | ||
public const ENV_TRIGGER = 'MATOMO_TEST_CRON_ARCHIVE_PROCESS_SIGNAL'; | ||
|
||
public const OPTION_ARCHIVE = 'CronArchiveProcessSignalTest.archive'; | ||
public const OPTION_START = 'CronArchiveProcessSignalTest.start'; | ||
|
||
/** | ||
* @var OneVisit | ||
*/ | ||
public static $fixture; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
} | ||
|
||
public function testSigterm(): void | ||
{ | ||
if (!extension_loaded('pcntl') || !function_exists('pcntl_signal')) { | ||
$this->markTestSkipped('signal test cannot run without ext-pcntl'); | ||
} | ||
|
||
$cliPhp = new CliPhp(); | ||
$phpBinary = $cliPhp->findPhpBinary(); | ||
|
||
$featureFlag = new CliMultiProcessSymfony(); | ||
$featureFlagManager = StaticContainer::get(FeatureFlagManager::class); | ||
$isFeatureAlreadyActive = $featureFlagManager->isFeatureActive(get_class($featureFlag)); | ||
|
||
try { | ||
if (!$isFeatureAlreadyActive) { | ||
// activate FeatureFlag for instance outside of the testing environment | ||
// only required if it was not already activated | ||
$exitCode = ProcessSymfony::fromShellCommandline(sprintf( | ||
'exec %s %s/console featureflag:enable %s', | ||
$phpBinary, | ||
PIWIK_INCLUDE_PATH, | ||
$featureFlag->getName() | ||
))->run(); | ||
|
||
self::assertSame(0, $exitCode); | ||
} | ||
|
||
// exec is mandatory to send signals to the process | ||
// not using array notation because "$phpBinary" can contain parameters | ||
$process = ProcessSymfony::fromShellCommandline(sprintf( | ||
'exec %s %s/tests/PHPUnit/proxy/console core:archive', | ||
$phpBinary, | ||
PIWIK_INCLUDE_PATH | ||
)); | ||
|
||
$process->setEnv([self::ENV_TRIGGER => '1']); | ||
$process->setTimeout(null); | ||
$process->start(); | ||
|
||
self::assertTrue($process->isRunning()); | ||
self::assertNotNull($process->getPid()); | ||
|
||
Option::set(self::OPTION_START, true); | ||
|
||
$dataAccessModel = new Model(); | ||
|
||
for ($i = 0; $i < 10; $i++) { | ||
$invalidationsInProgress = $dataAccessModel->getInvalidationsInProgress(self::$fixture->idSite); | ||
|
||
if ([] !== $invalidationsInProgress) { | ||
break; | ||
} | ||
|
||
sleep(1); | ||
} | ||
|
||
self::assertNotEmpty($invalidationsInProgress); | ||
self::assertSame(4, $this->getArchiveInvalidationCount(self::$fixture->idSite)); | ||
|
||
//Option::set(self::OPTION_ARCHIVE, true); | ||
|
||
self::assertTrue($process->isRunning()); | ||
|
||
$process->stop(10, SIGTERM); | ||
|
||
self::assertFalse($process->isRunning()); | ||
self::assertSame(0, $process->getExitCode()); | ||
|
||
$invalidationsInProgress = $dataAccessModel->getInvalidationsInProgress(self::$fixture->idSite); | ||
|
||
self::assertEmpty($invalidationsInProgress); | ||
self::assertSame(4, $this->getArchiveInvalidationCount(self::$fixture->idSite)); | ||
} finally { | ||
if (!$isFeatureAlreadyActive) { | ||
// deactivate FeatureFlag for instance outside of the testing environment | ||
// only required if it was not already activated | ||
ProcessSymfony::fromShellCommandline(sprintf( | ||
'exec %s %s/console featureflag:disable %s', | ||
$phpBinary, | ||
PIWIK_INCLUDE_PATH, | ||
$featureFlag->getName() | ||
))->run(); | ||
} | ||
} | ||
} | ||
|
||
private function getArchiveInvalidationCount(int $idSite): int | ||
{ | ||
return Db::fetchOne( | ||
'SELECT COUNT(*) FROM ' . Common::prefixTable('archive_invalidations') . ' WHERE idsite = ?', | ||
[ | ||
$idSite | ||
] | ||
); | ||
} | ||
} | ||
|
||
CronArchiveProcessSignalTest::$fixture = new OneVisit(); |