Skip to content

Commit

Permalink
Replaced hard-coded values passed to sleep() with calculated values
Browse files Browse the repository at this point in the history
  • Loading branch information
avpaderno committed Feb 17, 2025
1 parent af31adb commit 9667aed
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
11 changes: 6 additions & 5 deletions tests/apc_storage_apcu.test
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,27 @@ class ApcStorageApcuStoreAndRetrieveTestCase extends ApcStorageBaseTestCase {
}

if ($this->assertApcuEmpty()) {
$max_ttl = 0;

foreach ($this->storageData() as $id => $data) {
$key = "store_delay_retrieve_$id";

if ($this->assertApcuKeySaved($key, $data['value'], $data['ttl'])) {
$max_ttl = max($max_ttl, $data['ttl']);
$stored[$key] = $data;
}
}

$message = 'New items were stored in APCu.';

if ($this->assertTrue(!empty($stored), $message)) {
// Halt the tests for 10 seconds, which is 2 seconds more than the
// maximum TTL set by
// ApcStorageApcuStoreAndRetrieveTestCase::storageData().
$message = format_string('The test will be halted for @delay seconds.', array('@delay' => 10));
$max_ttl++;
$message = format_string('The test will be halted for @delay seconds.', array('@delay' => $max_ttl));
$persistent_key_message = '@key persists and it was found.';
$purgeable_key_message = '@key was purgeable and it was not found.';

$this->pass($message);
sleep(10);
sleep($max_ttl);

foreach ($stored as $key => $data) {
$options = array('value' => $data['value']);
Expand Down
27 changes: 13 additions & 14 deletions tests/apc_storage_cache.test
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class ApcStorageCacheBaseTestCase extends ApcStorageBaseTestCase {
* The cache bins to add.
*/
protected function setCacheBins(string ...$bins): void {
$this->cacheBins = array_unique(array_merge($this->cacheBins, $bins));
$this->cacheBins = array_merge($this->cacheBins, $bins);
}

/**
Expand All @@ -65,6 +65,8 @@ class ApcStorageCacheBaseTestCase extends ApcStorageBaseTestCase {
* The cache bins stored on APCu.
*/
protected function getCacheBins(): array {
$this->cacheBins = array_unique($this->cacheBins);

return $this->cacheBins;
}

Expand Down Expand Up @@ -308,25 +310,25 @@ class ApcStorageCacheStoreAndRetrieveTestCase extends ApcStorageCacheBaseTestCas

if ($this->assertApcuEmpty() && $this->assertCacheBinsOnApcu()) {
$bin = $this->getCacheBins()[0];
$max_ttl = 0;

foreach ($this->storageData() as $id => $data) {
$cid = "store_delay_retrieve_cache_item_$id";

if ($this->assertCacheItemSaved($bin, $cid, $data['value'], $data['ttl'])) {
$max_ttl = max($max_ttl, $data['ttl']);
$stored[$cid] = $data;
}
}

$message = 'New items were stored in the cache.';

if ($this->assertTrue(!empty($stored), $message)) {
// Add 2 seconds to the maximum TTL
// ApcStorageCacheBaseTestCase::storageData() sets to be sure that
// non-permanent values are purged when the cache is read.
$message = format_string('The test will be halted for @delay seconds.', array('@delay' => 10));
$max_ttl++;
$message = format_string('The test will be halted for @delay seconds.', array('@delay' => $max_ttl));

$this->pass($message);
sleep(10);
sleep($max_ttl);

foreach ($stored as $cid => $data) {
if (!empty($data['ttl'])) {
Expand Down Expand Up @@ -385,29 +387,26 @@ class ApcStorageCacheStoreAndRetrieveTestCase extends ApcStorageCacheBaseTestCas
}

if ($this->assertApcuEmpty() && $this->assertCacheBinsOnApcu()) {
$max_ttl = 0;
$bin = $this->getCacheBins()[0];

foreach ($this->storageData() as $id => $data) {
$cid = "store_retrieve_cache_item_$id";

if ($this->assertCacheItemSaved($bin, $cid, $data['value'], $data['ttl'] + 2)) {
$max_ttl = max($max_ttl, $data['ttl'] + 2);
$stored[] = $cid;
}
}

$message = 'New items were stored in the cache.';

if ($this->assertTrue(!empty($stored), $message)) {
// 12 is the maximum TTL set by
// ApcStorageCacheStoreAndRetrieveTestCase::storageData(),
// plus 2 to make the permanent cache items
// ApcStorageCacheStoreAndRetrieveTestCase::storageData() returns, plus
// 2 to be sure that all the cached items are purged when
// cache_get_multiple() is called.
$message = format_string('The test will be halted for @delay seconds.', array('@delay' => 12));
$max_ttl++;
$message = format_string('The test will be halted for @delay seconds.', array('@delay' => $max_ttl));

$this->pass($message);
sleep(12);
sleep($max_ttl);

foreach (array_chunk($stored, 3) as $chunk) {
$this->assertNoCacheItems($bin, $stored, ...$chunk);
Expand Down

0 comments on commit 9667aed

Please sign in to comment.