Skip to content

Commit cbd7149

Browse files
committed
test(Session): isolate Memcached and Redis instances to avoid race conditions with Cache component
- Add memcached2 service on port 11212 and MEMCACHED_SESSION_SAVE_PATH env var in test-random-execution workflow - Use database 1 in RedisHandlerTest to avoid flushDB collisions with Cache - Support configurable MEMCACHED_SESSION_SAVE_PATH in MemcachedHandlerTest
1 parent 147621f commit cbd7149

3 files changed

Lines changed: 24 additions & 9 deletions

File tree

‎.github/workflows/test-random-execution.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ jobs:
148148
ports:
149149
- 11211:11211
150150

151+
memcached2:
152+
image: memcached:1.6-alpine
153+
ports:
154+
- 11212:11211
155+
151156
steps:
152157
- name: Install mssql-tools on runner
153158
if: ${{ matrix.db-platform == 'SQLSRV' }}
@@ -227,6 +232,7 @@ jobs:
227232
env:
228233
DB: ${{ matrix.db-platform }}
229234
TERM: xterm-256color
235+
MEMCACHED_SESSION_SAVE_PATH: '127.0.0.1:11212'
230236

231237
- name: Upload random-test artifacts on failure
232238
if: ${{ always() && failure() }}

‎tests/system/Session/Handlers/Database/RedisHandlerTest.php‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final class RedisHandlerTest extends CIUnitTestCase
3333
{
3434
private string $sessionDriver = RedisHandler::class;
3535
private string $sessionName = 'ci_session';
36-
private string $sessionSavePath = 'tcp://127.0.0.1:6379';
36+
private string $sessionSavePath = 'tcp://127.0.0.1:6379?database=1';
3737
private string $userIpAddress = '127.0.0.1';
3838

3939
/**
@@ -73,9 +73,12 @@ protected function tearDown(): void
7373
try {
7474
$redis = new Redis();
7575
if ($redis->connect('127.0.0.1', 6379, 1.0)) {
76-
$keys = $redis->keys('ci_session:*');
77-
if ($keys !== false && $keys !== []) {
78-
$redis->del($keys);
76+
foreach ([0, 1] as $db) {
77+
$redis->select($db);
78+
$keys = $redis->keys('ci_session:*');
79+
if ($keys !== false && $keys !== []) {
80+
$redis->del($keys);
81+
}
7982
}
8083
$redis->close();
8184
}

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,14 @@ protected function setUp(): void
6565
{
6666
parent::setUp();
6767

68+
if ($envSavePath = getenv('MEMCACHED_SESSION_SAVE_PATH')) {
69+
$this->sessionSavePath = $envSavePath;
70+
}
71+
6872
if (extension_loaded('memcached')) {
69-
$memcached = new Memcached();
70-
$memcached->addServer('127.0.0.1', 11211);
73+
$memcached = new Memcached();
74+
[$host, $port] = explode(':', $this->sessionSavePath);
75+
$memcached->addServer($host, (int) $port);
7176
$memcached->flush();
7277
}
7378
}
@@ -79,8 +84,9 @@ protected function tearDown(): void
7984
MemcachedHandler::resetPersistentConnections();
8085

8186
if (extension_loaded('memcached')) {
82-
$memcached = new Memcached();
83-
$memcached->addServer('127.0.0.1', 11211);
87+
$memcached = new Memcached();
88+
[$host, $port] = explode(':', $this->sessionSavePath);
89+
$memcached->addServer($host, (int) $port);
8490
$memcached->flush();
8591
}
8692
}
@@ -94,7 +100,7 @@ public function testConstructorThrowsWithEmptySavePath(): void
94100

95101
public function testConstructorDoesNotThrowWithValidSavePath(): void
96102
{
97-
$handler = $this->getInstance(['savePath' => '127.0.0.1:11211']);
103+
$handler = $this->getInstance(['savePath' => $this->sessionSavePath]);
98104

99105
$this->assertInstanceOf(MemcachedHandler::class, $handler);
100106
}

0 commit comments

Comments
 (0)