Skip to content

Commit d0cada2

Browse files
PHPREDIS-1354: Refactored tests to work also with Redis cluster
1 parent 21436f6 commit d0cada2

File tree

6 files changed

+69
-32
lines changed

6 files changed

+69
-32
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: 10 additions & 5 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();
@@ -5503,8 +5508,8 @@ private function setSessionHandler()
55035508
{
55045509
$host = $this->getHost() ?: 'localhost';
55055510

5506-
ini_set('session.save_handler', 'redis');
5507-
ini_set('session.save_path', 'tcp://' . $host . ':6379');
5511+
@ini_set('session.save_handler', 'redis');
5512+
@ini_set('session.save_path', 'tcp://' . $host . ':6379');
55085513
}
55095514

55105515
/**
@@ -5545,7 +5550,7 @@ private function startSessionProcess($sessionId, $sleepTime, $background, $maxEx
55455550
$this->markTestSkipped();
55465551
return true;
55475552
} else {
5548-
$commandParameters = array($this->getHost(), $sessionId, $sleepTime, $maxExecutionTime, $lock_retries, $lock_expires, $sessionData, $sessionLifetime);
5553+
$commandParameters = array($this->getFullHostPath(), $this->sessionSaveHandler, $sessionId, $sleepTime, $maxExecutionTime, $lock_retries, $lock_expires, $sessionData, $sessionLifetime);
55495554
if ($locking_enabled) {
55505555
$commandParameters[] = '1';
55515556

@@ -5571,7 +5576,7 @@ private function startSessionProcess($sessionId, $sleepTime, $background, $maxEx
55715576
*/
55725577
private function getSessionData($sessionId, $sessionLifetime = 1440)
55735578
{
5574-
$command = 'php ' . __DIR__ . '/getSessionData.php ' . escapeshellarg($this->getHost()) . ' ' . escapeshellarg($sessionId) . ' ' . escapeshellarg($sessionLifetime);
5579+
$command = 'php ' . __DIR__ . '/getSessionData.php ' . escapeshellarg($this->getFullHostPath()) . ' ' . $this->sessionSaveHandler . ' ' . escapeshellarg($sessionId) . ' ' . escapeshellarg($sessionLifetime);
55755580
exec($command, $output);
55765581

55775582
return $output[0];
@@ -5589,7 +5594,7 @@ private function regenerateSessionId($sessionId, $locking = false, $destroyPrevi
55895594
{
55905595
$args = array_map('escapeshellarg', array($sessionId, $locking, $destroyPrevious, $sessionProxy));
55915596

5592-
$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);
55935598

55945599
exec($command, $output);
55955600

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: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
error_reporting(E_ERROR | E_WARNING);
33

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

89
if (empty($redisHost)) {
9-
$redisHost = 'localhost';
10+
$redisHost = 'tcp://localhost:6379';
1011
}
1112

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

1617
session_id($sessionId);

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: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +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];
11-
$sessionLifetime = $argv[8];
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];
1213

1314
if (empty($redisHost)) {
14-
$redisHost = 'localhost';
15+
$redisHost = 'tcp://localhost:6379';
1516
}
1617

17-
ini_set('session.save_handler', 'redis');
18-
ini_set('session.save_path', 'tcp://' . $redisHost . ':6379');
18+
ini_set('session.save_handler', $saveHandler);
19+
ini_set('session.save_path', $redisHost);
1920
ini_set('max_execution_time', $maxExecutionTime);
2021
ini_set('redis.session.lock_retries', $lock_retries);
2122
ini_set('redis.session.lock_expire', $lock_expire);
2223
ini_set('session.gc_maxlifetime', $sessionLifetime);
2324

2425
if (isset($argv[8])) {
25-
ini_set('redis.session.locking_enabled', $argv[9]);
26+
ini_set('redis.session.locking_enabled', $argv[10]);
2627
}
2728

2829
if (isset($argv[9])) {
29-
ini_set('redis.session.lock_wait_time', $argv[10]);
30+
ini_set('redis.session.lock_wait_time', $argv[11]);
3031
}
3132

3233
session_id($sessionId);

0 commit comments

Comments
 (0)