Skip to content

Commit e2d5b5e

Browse files
PHPREDIS-1354: Added session TTL tests
1 parent 97bb6bd commit e2d5b5e

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

tests/RedisTest.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5217,7 +5217,7 @@ public function testSession_lockReleasedOnClose()
52175217
$this->assertFalse($this->redis->exists($this->sessionPrefix . $sessionId . '_LOCK'));
52185218
}
52195219

5220-
public function testSession_ttlMaxExecutionTime()
5220+
public function testSession_lock_ttlMaxExecutionTime()
52215221
{
52225222
$this->setSessionHandler();
52235223
$sessionId = $this->generateSessionId();
@@ -5233,7 +5233,7 @@ public function testSession_ttlMaxExecutionTime()
52335233
$this->assertTrue($sessionSuccessful);
52345234
}
52355235

5236-
public function testSession_ttlLockExpire()
5236+
public function testSession_lock_ttlLockExpire()
52375237
{
52385238
$this->setSessionHandler();
52395239
$sessionId = $this->generateSessionId();
@@ -5468,6 +5468,37 @@ public function testSession_regenerateSessionId_withLock_withDestroy_withProxy(
54685468
$this->assertEquals('bar', $this->getSessionData($newSessionId));
54695469
}
54705470

5471+
public function testSession_ttl_equalsToSessionLifetime()
5472+
{
5473+
$sessionId = $this->generateSessionId();
5474+
$this->startSessionProcess($sessionId, 0, false, 300, true, null, -1, 0, 'test', 600);
5475+
$ttl = $this->redis->ttl($this->sessionPrefix . $sessionId);
5476+
5477+
$this->assertEquals(600, $ttl);
5478+
}
5479+
5480+
public function testSession_ttl_resetOnWrite()
5481+
{
5482+
$sessionId = $this->generateSessionId();
5483+
$this->startSessionProcess($sessionId, 0, false, 300, true, null, -1, 0, 'test', 600);
5484+
$this->redis->expire($this->sessionPrefix . $sessionId, 9999);
5485+
$this->startSessionProcess($sessionId, 0, false, 300, true, null, -1, 0, 'test', 600);
5486+
$ttl = $this->redis->ttl($this->sessionPrefix . $sessionId);
5487+
5488+
$this->assertEquals(600, $ttl);
5489+
}
5490+
5491+
public function testSession_ttl_resetOnRead()
5492+
{
5493+
$sessionId = $this->generateSessionId();
5494+
$this->startSessionProcess($sessionId, 0, false, 300, true, null, -1, 0, 'test', 600);
5495+
$this->redis->expire($this->sessionPrefix . $sessionId, 9999);
5496+
$this->getSessionData($sessionId);
5497+
$ttl = $this->redis->ttl($this->sessionPrefix . $sessionId);
5498+
5499+
$this->assertEquals(600, $ttl);
5500+
}
5501+
54715502
private function setSessionHandler()
54725503
{
54735504
$host = $this->getHost() ?: 'localhost';
@@ -5503,15 +5534,18 @@ private function generateSessionId()
55035534
* @param int $lock_expires
55045535
* @param string $sessionData
55055536
*
5537+
* @param int $sessionLifetime
5538+
*
55065539
* @return bool
5540+
* @throws Exception
55075541
*/
5508-
private function startSessionProcess($sessionId, $sleepTime, $background, $maxExecutionTime = 300, $locking_enabled = true, $lock_wait_time = null, $lock_retries = -1, $lock_expires = 0, $sessionData = '')
5542+
private function startSessionProcess($sessionId, $sleepTime, $background, $maxExecutionTime = 300, $locking_enabled = true, $lock_wait_time = null, $lock_retries = -1, $lock_expires = 0, $sessionData = '', $sessionLifetime = 1440)
55095543
{
55105544
if (substr(php_uname(), 0, 7) == "Windows"){
55115545
$this->markTestSkipped();
55125546
return true;
55135547
} else {
5514-
$commandParameters = array($this->getHost(), $sessionId, $sleepTime, $maxExecutionTime, $lock_retries, $lock_expires, $sessionData);
5548+
$commandParameters = array($this->getHost(), $sessionId, $sleepTime, $maxExecutionTime, $lock_retries, $lock_expires, $sessionData, $sessionLifetime);
55155549
if ($locking_enabled) {
55165550
$commandParameters[] = '1';
55175551

tests/startSession.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
$lock_retries = $argv[5];
99
$lock_expire = $argv[6];
1010
$sessionData = $argv[7];
11+
$sessionLifetime = $argv[8];
1112

1213
if (empty($redisHost)) {
1314
$redisHost = 'localhost';
@@ -18,13 +19,14 @@
1819
ini_set('max_execution_time', $maxExecutionTime);
1920
ini_set('redis.session.lock_retries', $lock_retries);
2021
ini_set('redis.session.lock_expire', $lock_expire);
22+
ini_set('session.gc_maxlifetime', $sessionLifetime);
2123

2224
if (isset($argv[8])) {
23-
ini_set('redis.session.locking_enabled', $argv[8]);
25+
ini_set('redis.session.locking_enabled', $argv[9]);
2426
}
2527

2628
if (isset($argv[9])) {
27-
ini_set('redis.session.lock_wait_time', $argv[9]);
29+
ini_set('redis.session.lock_wait_time', $argv[10]);
2830
}
2931

3032
session_id($sessionId);

0 commit comments

Comments
 (0)