Skip to content

Commit 7827c6e

Browse files
committed
Fix cache tests race conditions and teardown memory leaks
1 parent 5e7c6ef commit 7827c6e

7 files changed

Lines changed: 26 additions & 75 deletions

File tree

‎system/Cache/Handlers/PredisHandler.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function save(string $key, mixed $value, int $ttl = 60): bool
125125
}
126126

127127
if ($ttl !== 0) {
128-
$this->redis->expireat($key, Time::now()->getTimestamp() + $ttl);
128+
$this->redis->expire($key, $ttl);
129129
}
130130

131131
return true;

‎system/Cache/Handlers/RedisHandler.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function save(string $key, mixed $value, int $ttl = 60): bool
141141
}
142142

143143
if ($ttl !== 0) {
144-
$this->redis->expireAt($key, Time::now()->getTimestamp() + $ttl);
144+
$this->redis->expire($key, $ttl);
145145
}
146146

147147
return true;

‎tests/system/Cache/Handlers/ApcuHandlerTest.php‎

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,25 @@
2727
#[RequiresPhpExtension('apcu')]
2828
final class ApcuHandlerTest extends AbstractHandlerTestCase
2929
{
30-
/**
31-
* @return list<string>
32-
*/
33-
private static function getKeyArray(): array
34-
{
35-
return [
36-
self::$key1,
37-
self::$key2,
38-
self::$key3,
39-
];
40-
}
4130

4231
protected function setUp(): void
4332
{
4433
parent::setUp();
4534

35+
if (! function_exists('apcu_enabled') || ! apcu_enabled()) {
36+
$this->markTestSkipped('APCu extension is not loaded or not enabled in CLI.');
37+
}
38+
4639
$this->handler = CacheFactory::getHandler(new Cache(), 'apcu');
4740
}
4841

4942
protected function tearDown(): void
5043
{
51-
foreach (self::getKeyArray() as $key) {
52-
$this->handler->delete($key);
44+
if (! isset($this->handler)) {
45+
return;
5346
}
47+
48+
$this->handler->clean();
5449
}
5550

5651
public function testNew(): void

‎tests/system/Cache/Handlers/FileHandlerTest.php‎

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,6 @@ final class FileHandlerTest extends AbstractHandlerTestCase
3131
private static string $directory = 'FileHandler';
3232
private Cache $config;
3333

34-
/**
35-
* @return list<string>
36-
*/
37-
private static function getKeyArray(): array
38-
{
39-
return [
40-
self::$key1,
41-
self::$key2,
42-
self::$key3,
43-
];
44-
}
4534

4635
protected function setUp(): void
4736
{
@@ -65,19 +54,13 @@ protected function setUp(): void
6554
protected function tearDown(): void
6655
{
6756
parent::tearDown();
57+
clearstatcache();
6858

6959
if (is_dir($this->config->file['storePath'])) {
7060
chmod($this->config->file['storePath'], 0777);
7161

72-
foreach (self::getKeyArray() as $key) {
73-
if (is_file($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $key)) {
74-
chmod($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $key, 0777);
75-
unlink($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $key);
76-
}
77-
if (is_file($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $this->config->prefix . $key)) {
78-
chmod($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $this->config->prefix . $key, 0777);
79-
unlink($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $this->config->prefix . $key);
80-
}
62+
if (isset($this->handler)) {
63+
$this->handler->clean();
8164
}
8265

8366
rmdir($this->config->file['storePath']);
@@ -118,7 +101,7 @@ public function testSetDefaultPath(): void
118101
*/
119102
public function testGet(): void
120103
{
121-
$this->handler->save(self::$key1, 'value', 2);
104+
$this->assertTrue($this->handler->save(self::$key1, 'value', 2));
122105

123106
$this->assertSame('value', $this->handler->get(self::$key1));
124107
$this->assertNull($this->handler->get(self::$dummy));

‎tests/system/Cache/Handlers/MemcachedHandlerTest.php‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,6 @@
2626
#[Group('CacheLive')]
2727
final class MemcachedHandlerTest extends AbstractHandlerTestCase
2828
{
29-
/**
30-
* @return list<string>
31-
*/
32-
private static function getKeyArray(): array
33-
{
34-
return [
35-
self::$key1,
36-
self::$key2,
37-
self::$key3,
38-
];
39-
}
4029

4130
protected function setUp(): void
4231
{
@@ -51,9 +40,11 @@ protected function setUp(): void
5140

5241
protected function tearDown(): void
5342
{
54-
foreach (self::getKeyArray() as $key) {
55-
$this->handler->delete($key);
43+
if (! isset($this->handler)) {
44+
return;
5645
}
46+
47+
$this->handler->clean();
5748
}
5849

5950
public function testNew(): void

‎tests/system/Cache/Handlers/PredisHandlerTest.php‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,6 @@ final class PredisHandlerTest extends AbstractHandlerTestCase
2727
{
2828
private Cache $config;
2929

30-
/**
31-
* @return list<string>
32-
*/
33-
private static function getKeyArray(): array
34-
{
35-
return [
36-
self::$key1,
37-
self::$key2,
38-
self::$key3,
39-
];
40-
}
4130

4231
protected function setUp(): void
4332
{
@@ -49,9 +38,11 @@ protected function setUp(): void
4938

5039
protected function tearDown(): void
5140
{
52-
foreach (self::getKeyArray() as $key) {
53-
$this->handler->delete($key);
41+
if (! isset($this->handler)) {
42+
return;
5443
}
44+
45+
$this->handler->clean();
5546
}
5647

5748
public function testNew(): void

‎tests/system/Cache/Handlers/RedisHandlerTest.php‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,6 @@ final class RedisHandlerTest extends AbstractHandlerTestCase
2828
{
2929
private Cache $config;
3030

31-
/**
32-
* @return list<string>
33-
*/
34-
private static function getKeyArray(): array
35-
{
36-
return [
37-
self::$key1,
38-
self::$key2,
39-
self::$key3,
40-
];
41-
}
4231

4332
protected function setUp(): void
4433
{
@@ -54,9 +43,11 @@ protected function setUp(): void
5443

5544
protected function tearDown(): void
5645
{
57-
foreach (self::getKeyArray() as $key) {
58-
$this->handler->delete($key);
46+
if (! isset($this->handler)) {
47+
return;
5948
}
49+
50+
$this->handler->clean();
6051
}
6152

6253
public function testNew(): void

0 commit comments

Comments
 (0)