Skip to content

Commit 65aa043

Browse files
authored
Merge pull request phpredis#1361 from SkydiveMarius/PHPREDIS-1354
Refactored session tests to work also with Redis cluster
2 parents 1bb7fe4 + d0cada2 commit 65aa043

File tree

6 files changed

+110
-34
lines changed

6 files changed

+110
-34
lines changed

tests/RedisClusterTest.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ class Redis_Cluster_Test extends Redis_Test {
2828
*/
2929
protected $sessionPrefix = 'PHPREDIS_CLUSTER_SESSION:';
3030

31+
/**
32+
* @var string
33+
*/
34+
protected $sessionSaveHandler = 'rediscluster';
35+
3136
/* Tests we'll skip all together in the context of RedisCluster. The
3237
* RedisCluster class doesn't implement specialized (non-redis) commands
3338
* such as sortAsc, or sortDesc and other commands such as SELECT are
@@ -44,7 +49,6 @@ public function testConnectException() { return $this->markTestSkipped(); }
4449

4550
/* Session locking feature is currently not supported in in context of Redis Cluster.
4651
The biggest issue for this is the distribution nature of Redis cluster */
47-
public function testSession_savedToRedis() { return $this->markTestSkipped(); }
4852
public function testSession_lockKeyCorrect() { return $this->markTestSkipped(); }
4953
public function testSession_lockingDisabledByDefault() { return $this->markTestSkipped(); }
5054
public function testSession_lockReleasedOnClose() { return $this->markTestSkipped(); }
@@ -596,8 +600,8 @@ protected function rawCommandArray($key, $args) {
596600

597601
public function testSession()
598602
{
599-
ini_set('session.save_handler', 'rediscluster');
600-
ini_set('session.save_path', implode('&', array_map(function ($seed) {
603+
@ini_set('session.save_handler', 'rediscluster');
604+
@ini_set('session.save_path', implode('&', array_map(function ($seed) {
601605
return 'seed[]=' . $seed;
602606
}, self::$_arr_node_map)) . '&failover=error');
603607
if (!@session_start()) {
@@ -606,5 +610,17 @@ public function testSession()
606610
session_write_close();
607611
$this->assertTrue($this->redis->exists('PHPREDIS_CLUSTER_SESSION:' . session_id()));
608612
}
613+
614+
/**
615+
* @inheritdoc
616+
*/
617+
protected function getFullHostPath()
618+
{
619+
$hosts = array_map(function ($host) {
620+
return 'seed[]=' . $host . '';
621+
}, self::$_arr_node_map);
622+
623+
return implode('&', $hosts);
624+
}
609625
}
610626
?>

tests/RedisTest.php

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class Redis_Test extends TestSuite
2626
*/
2727
protected $sessionPrefix = 'PHPREDIS_SESSION:';
2828

29+
/**
30+
* @var string
31+
*/
32+
protected $sessionSaveHandler = 'redis';
33+
2934
public function setUp() {
3035
$this->redis = $this->newInstance();
3136
$info = $this->redis->info();
@@ -5217,7 +5222,7 @@ public function testSession_lockReleasedOnClose()
52175222
$this->assertFalse($this->redis->exists($this->sessionPrefix . $sessionId . '_LOCK'));
52185223
}
52195224

5220-
public function testSession_ttlMaxExecutionTime()
5225+
public function testSession_lock_ttlMaxExecutionTime()
52215226
{
52225227
$this->setSessionHandler();
52235228
$sessionId = $this->generateSessionId();
@@ -5233,7 +5238,7 @@ public function testSession_ttlMaxExecutionTime()
52335238
$this->assertTrue($sessionSuccessful);
52345239
}
52355240

5236-
public function testSession_ttlLockExpire()
5241+
public function testSession_lock_ttlLockExpire()
52375242
{
52385243
$this->setSessionHandler();
52395244
$sessionId = $this->generateSessionId();
@@ -5468,12 +5473,43 @@ public function testSession_regenerateSessionId_withLock_withDestroy_withProxy(
54685473
$this->assertEquals('bar', $this->getSessionData($newSessionId));
54695474
}
54705475

5476+
public function testSession_ttl_equalsToSessionLifetime()
5477+
{
5478+
$sessionId = $this->generateSessionId();
5479+
$this->startSessionProcess($sessionId, 0, false, 300, true, null, -1, 0, 'test', 600);
5480+
$ttl = $this->redis->ttl($this->sessionPrefix . $sessionId);
5481+
5482+
$this->assertEquals(600, $ttl);
5483+
}
5484+
5485+
public function testSession_ttl_resetOnWrite()
5486+
{
5487+
$sessionId = $this->generateSessionId();
5488+
$this->startSessionProcess($sessionId, 0, false, 300, true, null, -1, 0, 'test', 600);
5489+
$this->redis->expire($this->sessionPrefix . $sessionId, 9999);
5490+
$this->startSessionProcess($sessionId, 0, false, 300, true, null, -1, 0, 'test', 600);
5491+
$ttl = $this->redis->ttl($this->sessionPrefix . $sessionId);
5492+
5493+
$this->assertEquals(600, $ttl);
5494+
}
5495+
5496+
public function testSession_ttl_resetOnRead()
5497+
{
5498+
$sessionId = $this->generateSessionId();
5499+
$this->startSessionProcess($sessionId, 0, false, 300, true, null, -1, 0, 'test', 600);
5500+
$this->redis->expire($this->sessionPrefix . $sessionId, 9999);
5501+
$this->getSessionData($sessionId, 600);
5502+
$ttl = $this->redis->ttl($this->sessionPrefix . $sessionId);
5503+
5504+
$this->assertEquals(600, $ttl);
5505+
}
5506+
54715507
private function setSessionHandler()
54725508
{
54735509
$host = $this->getHost() ?: 'localhost';
54745510

5475-
ini_set('session.save_handler', 'redis');
5476-
ini_set('session.save_path', 'tcp://' . $host . ':6379');
5511+
@ini_set('session.save_handler', 'redis');
5512+
@ini_set('session.save_path', 'tcp://' . $host . ':6379');
54775513
}
54785514

54795515
/**
@@ -5503,15 +5539,18 @@ private function generateSessionId()
55035539
* @param int $lock_expires
55045540
* @param string $sessionData
55055541
*
5542+
* @param int $sessionLifetime
5543+
*
55065544
* @return bool
5545+
* @throws Exception
55075546
*/
5508-
private function startSessionProcess($sessionId, $sleepTime, $background, $maxExecutionTime = 300, $locking_enabled = true, $lock_wait_time = null, $lock_retries = -1, $lock_expires = 0, $sessionData = '')
5547+
private function startSessionProcess($sessionId, $sleepTime, $background, $maxExecutionTime = 300, $locking_enabled = true, $lock_wait_time = null, $lock_retries = -1, $lock_expires = 0, $sessionData = '', $sessionLifetime = 1440)
55095548
{
55105549
if (substr(php_uname(), 0, 7) == "Windows"){
55115550
$this->markTestSkipped();
55125551
return true;
55135552
} else {
5514-
$commandParameters = array($this->getHost(), $sessionId, $sleepTime, $maxExecutionTime, $lock_retries, $lock_expires, $sessionData);
5553+
$commandParameters = array($this->getFullHostPath(), $this->sessionSaveHandler, $sessionId, $sleepTime, $maxExecutionTime, $lock_retries, $lock_expires, $sessionData, $sessionLifetime);
55155554
if ($locking_enabled) {
55165555
$commandParameters[] = '1';
55175556

@@ -5531,12 +5570,13 @@ private function startSessionProcess($sessionId, $sleepTime, $background, $maxEx
55315570

55325571
/**
55335572
* @param string $sessionId
5573+
* @param int $sessionLifetime
55345574
*
55355575
* @return string
55365576
*/
5537-
private function getSessionData($sessionId)
5577+
private function getSessionData($sessionId, $sessionLifetime = 1440)
55385578
{
5539-
$command = 'php ' . __DIR__ . '/getSessionData.php ' . escapeshellarg($this->getHost()) . ' ' . escapeshellarg($sessionId);
5579+
$command = 'php ' . __DIR__ . '/getSessionData.php ' . escapeshellarg($this->getFullHostPath()) . ' ' . $this->sessionSaveHandler . ' ' . escapeshellarg($sessionId) . ' ' . escapeshellarg($sessionLifetime);
55405580
exec($command, $output);
55415581

55425582
return $output[0];
@@ -5554,7 +5594,7 @@ private function regenerateSessionId($sessionId, $locking = false, $destroyPrevi
55545594
{
55555595
$args = array_map('escapeshellarg', array($sessionId, $locking, $destroyPrevious, $sessionProxy));
55565596

5557-
$command = 'php --no-php-ini --define extension=igbinary.so --define extension=' . __DIR__ . '/../modules/redis.so ' . __DIR__ . '/regenerateSessionId.php ' . escapeshellarg($this->getHost()) . ' ' . implode(' ', $args);
5597+
$command = 'php --no-php-ini --define extension=igbinary.so --define extension=' . __DIR__ . '/../modules/redis.so ' . __DIR__ . '/regenerateSessionId.php ' . escapeshellarg($this->getFullHostPath()) . ' ' . $this->sessionSaveHandler . ' ' . implode(' ', $args);
55585598

55595599
exec($command, $output);
55605600

tests/TestSuite.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ public function __construct($str_host) {
2727

2828
public function getHost() { return $this->str_host; }
2929

30+
/**
31+
* Returns the fully qualified host path,
32+
* which may be used directly for php.ini parameters like session.save_path
33+
*
34+
* @return null|string
35+
*/
36+
protected function getFullHostPath()
37+
{
38+
return $this->str_host
39+
? 'tcp://' . $this->str_host . ':6379'
40+
: null;
41+
}
42+
3043
public static function make_bold($str_msg) {
3144
return self::$_boo_colorize
3245
? self::$BOLD_ON . $str_msg . self::$BOLD_OFF

tests/getSessionData.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
error_reporting(E_ERROR | E_WARNING);
33

44
$redisHost = $argv[1];
5-
$sessionId = $argv[2];
5+
$saveHandler = $argv[2];
6+
$sessionId = $argv[3];
7+
$sessionLifetime = $argv[4];
68

79
if (empty($redisHost)) {
8-
$redisHost = 'localhost';
10+
$redisHost = 'tcp://localhost:6379';
911
}
1012

11-
ini_set('session.save_handler', 'redis');
12-
ini_set('session.save_path', 'tcp://' . $redisHost . ':6379');
13+
ini_set('session.save_handler', $saveHandler);
14+
ini_set('session.save_path', $redisHost);
15+
ini_set('session.gc_maxlifetime', $sessionLifetime);
1316

1417
session_id($sessionId);
1518
if (!session_start()) {

tests/regenerateSessionId.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22
error_reporting(E_ERROR | E_WARNING);
33

44
$redisHost = $argv[1];
5-
$sessionId = $argv[2];
6-
$locking = !!$argv[3];
7-
$destroyPrevious = !!$argv[4];
8-
$sessionProxy = !!$argv[5];
5+
$saveHandler = $argv[2];
6+
$sessionId = $argv[3];
7+
$locking = !!$argv[4];
8+
$destroyPrevious = !!$argv[5];
9+
$sessionProxy = !!$argv[6];
910

1011
if (empty($redisHost)) {
11-
$redisHost = 'localhost';
12+
$redisHost = 'tcp://localhost:6379';
1213
}
1314

14-
ini_set('session.save_handler', 'redis');
15-
ini_set('session.save_path', 'tcp://' . $redisHost . ':6379');
15+
ini_set('session.save_handler', $saveHandler);
16+
ini_set('session.save_path', $redisHost);
1617

1718
if ($locking) {
1819
ini_set('redis.session.locking_enabled', true);

tests/startSession.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,32 @@
22
error_reporting(E_ERROR | E_WARNING);
33

44
$redisHost = $argv[1];
5-
$sessionId = $argv[2];
6-
$sleepTime = $argv[3];
7-
$maxExecutionTime = $argv[4];
8-
$lock_retries = $argv[5];
9-
$lock_expire = $argv[6];
10-
$sessionData = $argv[7];
5+
$saveHandler = $argv[2];
6+
$sessionId = $argv[3];
7+
$sleepTime = $argv[4];
8+
$maxExecutionTime = $argv[5];
9+
$lock_retries = $argv[6];
10+
$lock_expire = $argv[7];
11+
$sessionData = $argv[8];
12+
$sessionLifetime = $argv[9];
1113

1214
if (empty($redisHost)) {
13-
$redisHost = 'localhost';
15+
$redisHost = 'tcp://localhost:6379';
1416
}
1517

16-
ini_set('session.save_handler', 'redis');
17-
ini_set('session.save_path', 'tcp://' . $redisHost . ':6379');
18+
ini_set('session.save_handler', $saveHandler);
19+
ini_set('session.save_path', $redisHost);
1820
ini_set('max_execution_time', $maxExecutionTime);
1921
ini_set('redis.session.lock_retries', $lock_retries);
2022
ini_set('redis.session.lock_expire', $lock_expire);
23+
ini_set('session.gc_maxlifetime', $sessionLifetime);
2124

2225
if (isset($argv[8])) {
23-
ini_set('redis.session.locking_enabled', $argv[8]);
26+
ini_set('redis.session.locking_enabled', $argv[10]);
2427
}
2528

2629
if (isset($argv[9])) {
27-
ini_set('redis.session.lock_wait_time', $argv[9]);
30+
ini_set('redis.session.lock_wait_time', $argv[11]);
2831
}
2932

3033
session_id($sessionId);

0 commit comments

Comments
 (0)