From 93af157d27c8295eb3f1a370cb7b5b8cc9a1a348 Mon Sep 17 00:00:00 2001 From: "steven.lewis" Date: Sat, 6 Jan 2024 16:57:55 +0000 Subject: [PATCH] Add test for forking failure and lockup of child process --- quality.sh | 0 tests/Couchbasev4.test.php | 41 +++++++++++++++++++++++++++++----- tests/Scripts/monitor_fork.php | 6 +++++ 3 files changed, 42 insertions(+), 5 deletions(-) mode change 100644 => 100755 quality.sh create mode 100644 tests/Scripts/monitor_fork.php diff --git a/quality.sh b/quality.sh old mode 100644 new mode 100755 diff --git a/tests/Couchbasev4.test.php b/tests/Couchbasev4.test.php index cbdcebc..6893df0 100644 --- a/tests/Couchbasev4.test.php +++ b/tests/Couchbasev4.test.php @@ -9,6 +9,7 @@ * For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files. * * @author Georges.L (Geolim4) + * @author Steven Lewis (srjlewis) https://github.com/srjlewis * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors */ @@ -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'; @@ -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); @@ -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)) { @@ -58,7 +90,6 @@ $testHelper->runCRUDTests($cacheInstance); } catch (PhpfastcacheDriverConnectException $e) { $testHelper->assertSkip('Couchbase server unavailable: ' . $e->getMessage()); - $testHelper->terminateTest(); } $testHelper->terminateTest(); diff --git a/tests/Scripts/monitor_fork.php b/tests/Scripts/monitor_fork.php new file mode 100644 index 0000000..9495bd8 --- /dev/null +++ b/tests/Scripts/monitor_fork.php @@ -0,0 +1,6 @@ +