Skip to content

Commit

Permalink
Add test for forking failure and lockup of child process
Browse files Browse the repository at this point in the history
  • Loading branch information
srjlewis committed Jan 6, 2024
1 parent f175802 commit 93af157
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
Empty file modified quality.sh
100644 → 100755
Empty file.
41 changes: 36 additions & 5 deletions tests/Couchbasev4.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
*
* @author Georges.L (Geolim4) <[email protected]>
* @author Steven Lewis (srjlewis) https://github.com/srjlewis
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
*/

Expand All @@ -17,7 +18,6 @@
use Phpfastcache\Exceptions\PhpfastcacheDriverConnectException;
use Phpfastcache\Helper\Psr16Adapter;
use Phpfastcache\Tests\Helper\TestHelper;
use Webmozart\Assert\Assert;

chdir(__DIR__);
require_once __DIR__ . '/../vendor/autoload.php';
Expand All @@ -28,6 +28,37 @@
$configFileName = __DIR__ . '/Configs/github-actions.php';
}

$testHelper->printInfoText('Running forking failure process test');

$config = (new CouchbaseConfig(include $configFileName))
->setDoPosixCheck(false)
->setUseStaticItemCaching(false);

$cacheInstance = CacheManager::getInstance('Couchbasev4', $config);

$cache = new Psr16Adapter($cacheInstance);
$value = random_int(0, 254);

$cache->set('forkFailTestKey', $value);

$pid = pcntl_fork();
if ($pid == -1) {
$testHelper->assertFail('Unable to fork');
} else if ($pid) {
$testHelper->runAsyncProcess('php "'.__DIR__ . '/Scripts/monitor_fork.php" ' . $pid);
pcntl_wait($status);
} else {
exit($cache->get('forkFailTestKey'));
}

if ($value === pcntl_wexitstatus($status)) {
$testHelper->assertFail('The fork was a success was meant to lockup');
} else {
$testHelper->assertPass('The forked process locked up has expected');
}

$testHelper->printInfoText('Running forking success process test');

$config = (new CouchbaseConfig(include $configFileName))
->setDoPosixCheck(true)
->setUseStaticItemCaching(false);
Expand All @@ -37,15 +68,16 @@
$cache = new Psr16Adapter($cacheInstance);
$value = random_int(0, 254);

$cache->set('key1', $value);
$cache->set('forkSuccessTestKey', $value);

$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
$testHelper->assertFail('Unable to fork');
} else if ($pid) {
$testHelper->runAsyncProcess('php "'.__DIR__ . '/Scripts/monitor_fork.php" ' . $pid);
pcntl_wait($status);
} else {
exit($cache->get('key1'));
exit($cache->get('forkSuccessTestKey'));
}

if ($value === pcntl_wexitstatus($status)) {
Expand All @@ -58,7 +90,6 @@
$testHelper->runCRUDTests($cacheInstance);
} catch (PhpfastcacheDriverConnectException $e) {
$testHelper->assertSkip('Couchbase server unavailable: ' . $e->getMessage());
$testHelper->terminateTest();
}
$testHelper->terminateTest();

Expand Down
6 changes: 6 additions & 0 deletions tests/Scripts/monitor_fork.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
$pid = (int)$argv[1] ?? 0;
sleep(2);
if (posix_kill($pid, 0)) {
posix_kill($pid, SIGKILL);
}

0 comments on commit 93af157

Please sign in to comment.