diff options
author | Pavlo Yatsukhnenko <yatsukhnenko@users.noreply.github.com> | 2018-06-06 15:03:16 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-06 15:03:16 +0300 |
commit | 65aa0430826ff739dcffb289835a781ba1bc83ac (patch) | |
tree | 05164de65731fd7b74e76237cdfd03e3c22835a3 | |
parent | 1bb7fe48fa7a102776eef1d4b557cae63d0990b3 (diff) | |
parent | d0cada2268a807af255255a53e5f3d79bda644b9 (diff) |
Merge pull request #1361 from SkydiveMarius/PHPREDIS-1354
Refactored session tests to work also with Redis cluster
-rw-r--r-- | tests/RedisClusterTest.php | 22 | ||||
-rw-r--r-- | tests/RedisTest.php | 58 | ||||
-rw-r--r-- | tests/TestSuite.php | 13 | ||||
-rw-r--r-- | tests/getSessionData.php | 11 | ||||
-rw-r--r-- | tests/regenerateSessionId.php | 15 | ||||
-rw-r--r-- | tests/startSession.php | 25 |
6 files changed, 110 insertions, 34 deletions
diff --git a/tests/RedisClusterTest.php b/tests/RedisClusterTest.php index f841fa26..74515d4f 100644 --- a/tests/RedisClusterTest.php +++ b/tests/RedisClusterTest.php @@ -28,6 +28,11 @@ class Redis_Cluster_Test extends Redis_Test { */ protected $sessionPrefix = 'PHPREDIS_CLUSTER_SESSION:'; + /** + * @var string + */ + protected $sessionSaveHandler = 'rediscluster'; + /* Tests we'll skip all together in the context of RedisCluster. The * RedisCluster class doesn't implement specialized (non-redis) commands * such as sortAsc, or sortDesc and other commands such as SELECT are @@ -44,7 +49,6 @@ class Redis_Cluster_Test extends Redis_Test { /* Session locking feature is currently not supported in in context of Redis Cluster. The biggest issue for this is the distribution nature of Redis cluster */ - public function testSession_savedToRedis() { return $this->markTestSkipped(); } public function testSession_lockKeyCorrect() { return $this->markTestSkipped(); } public function testSession_lockingDisabledByDefault() { return $this->markTestSkipped(); } public function testSession_lockReleasedOnClose() { return $this->markTestSkipped(); } @@ -596,8 +600,8 @@ class Redis_Cluster_Test extends Redis_Test { public function testSession() { - ini_set('session.save_handler', 'rediscluster'); - ini_set('session.save_path', implode('&', array_map(function ($seed) { + @ini_set('session.save_handler', 'rediscluster'); + @ini_set('session.save_path', implode('&', array_map(function ($seed) { return 'seed[]=' . $seed; }, self::$_arr_node_map)) . '&failover=error'); if (!@session_start()) { @@ -606,5 +610,17 @@ class Redis_Cluster_Test extends Redis_Test { session_write_close(); $this->assertTrue($this->redis->exists('PHPREDIS_CLUSTER_SESSION:' . session_id())); } + + /** + * @inheritdoc + */ + protected function getFullHostPath() + { + $hosts = array_map(function ($host) { + return 'seed[]=' . $host . ''; + }, self::$_arr_node_map); + + return implode('&', $hosts); + } } ?> diff --git a/tests/RedisTest.php b/tests/RedisTest.php index 15c216a0..ff0b7537 100644 --- a/tests/RedisTest.php +++ b/tests/RedisTest.php @@ -26,6 +26,11 @@ class Redis_Test extends TestSuite */ protected $sessionPrefix = 'PHPREDIS_SESSION:'; + /** + * @var string + */ + protected $sessionSaveHandler = 'redis'; + public function setUp() { $this->redis = $this->newInstance(); $info = $this->redis->info(); @@ -5217,7 +5222,7 @@ class Redis_Test extends TestSuite $this->assertFalse($this->redis->exists($this->sessionPrefix . $sessionId . '_LOCK')); } - public function testSession_ttlMaxExecutionTime() + public function testSession_lock_ttlMaxExecutionTime() { $this->setSessionHandler(); $sessionId = $this->generateSessionId(); @@ -5233,7 +5238,7 @@ class Redis_Test extends TestSuite $this->assertTrue($sessionSuccessful); } - public function testSession_ttlLockExpire() + public function testSession_lock_ttlLockExpire() { $this->setSessionHandler(); $sessionId = $this->generateSessionId(); @@ -5468,12 +5473,43 @@ class Redis_Test extends TestSuite $this->assertEquals('bar', $this->getSessionData($newSessionId)); } + public function testSession_ttl_equalsToSessionLifetime() + { + $sessionId = $this->generateSessionId(); + $this->startSessionProcess($sessionId, 0, false, 300, true, null, -1, 0, 'test', 600); + $ttl = $this->redis->ttl($this->sessionPrefix . $sessionId); + + $this->assertEquals(600, $ttl); + } + + public function testSession_ttl_resetOnWrite() + { + $sessionId = $this->generateSessionId(); + $this->startSessionProcess($sessionId, 0, false, 300, true, null, -1, 0, 'test', 600); + $this->redis->expire($this->sessionPrefix . $sessionId, 9999); + $this->startSessionProcess($sessionId, 0, false, 300, true, null, -1, 0, 'test', 600); + $ttl = $this->redis->ttl($this->sessionPrefix . $sessionId); + + $this->assertEquals(600, $ttl); + } + + public function testSession_ttl_resetOnRead() + { + $sessionId = $this->generateSessionId(); + $this->startSessionProcess($sessionId, 0, false, 300, true, null, -1, 0, 'test', 600); + $this->redis->expire($this->sessionPrefix . $sessionId, 9999); + $this->getSessionData($sessionId, 600); + $ttl = $this->redis->ttl($this->sessionPrefix . $sessionId); + + $this->assertEquals(600, $ttl); + } + private function setSessionHandler() { $host = $this->getHost() ?: 'localhost'; - ini_set('session.save_handler', 'redis'); - ini_set('session.save_path', 'tcp://' . $host . ':6379'); + @ini_set('session.save_handler', 'redis'); + @ini_set('session.save_path', 'tcp://' . $host . ':6379'); } /** @@ -5503,15 +5539,18 @@ class Redis_Test extends TestSuite * @param int $lock_expires * @param string $sessionData * + * @param int $sessionLifetime + * * @return bool + * @throws Exception */ - private function startSessionProcess($sessionId, $sleepTime, $background, $maxExecutionTime = 300, $locking_enabled = true, $lock_wait_time = null, $lock_retries = -1, $lock_expires = 0, $sessionData = '') + private function startSessionProcess($sessionId, $sleepTime, $background, $maxExecutionTime = 300, $locking_enabled = true, $lock_wait_time = null, $lock_retries = -1, $lock_expires = 0, $sessionData = '', $sessionLifetime = 1440) { if (substr(php_uname(), 0, 7) == "Windows"){ $this->markTestSkipped(); return true; } else { - $commandParameters = array($this->getHost(), $sessionId, $sleepTime, $maxExecutionTime, $lock_retries, $lock_expires, $sessionData); + $commandParameters = array($this->getFullHostPath(), $this->sessionSaveHandler, $sessionId, $sleepTime, $maxExecutionTime, $lock_retries, $lock_expires, $sessionData, $sessionLifetime); if ($locking_enabled) { $commandParameters[] = '1'; @@ -5531,12 +5570,13 @@ class Redis_Test extends TestSuite /** * @param string $sessionId + * @param int $sessionLifetime * * @return string */ - private function getSessionData($sessionId) + private function getSessionData($sessionId, $sessionLifetime = 1440) { - $command = 'php ' . __DIR__ . '/getSessionData.php ' . escapeshellarg($this->getHost()) . ' ' . escapeshellarg($sessionId); + $command = 'php ' . __DIR__ . '/getSessionData.php ' . escapeshellarg($this->getFullHostPath()) . ' ' . $this->sessionSaveHandler . ' ' . escapeshellarg($sessionId) . ' ' . escapeshellarg($sessionLifetime); exec($command, $output); return $output[0]; @@ -5554,7 +5594,7 @@ class Redis_Test extends TestSuite { $args = array_map('escapeshellarg', array($sessionId, $locking, $destroyPrevious, $sessionProxy)); - $command = 'php --no-php-ini --define extension=igbinary.so --define extension=' . __DIR__ . '/../modules/redis.so ' . __DIR__ . '/regenerateSessionId.php ' . escapeshellarg($this->getHost()) . ' ' . implode(' ', $args); + $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); exec($command, $output); diff --git a/tests/TestSuite.php b/tests/TestSuite.php index 03186bf9..461ac7dc 100644 --- a/tests/TestSuite.php +++ b/tests/TestSuite.php @@ -27,6 +27,19 @@ class TestSuite { public function getHost() { return $this->str_host; } + /** + * Returns the fully qualified host path, + * which may be used directly for php.ini parameters like session.save_path + * + * @return null|string + */ + protected function getFullHostPath() + { + return $this->str_host + ? 'tcp://' . $this->str_host . ':6379' + : null; + } + public static function make_bold($str_msg) { return self::$_boo_colorize ? self::$BOLD_ON . $str_msg . self::$BOLD_OFF diff --git a/tests/getSessionData.php b/tests/getSessionData.php index b5bea74a..d49256c2 100644 --- a/tests/getSessionData.php +++ b/tests/getSessionData.php @@ -2,14 +2,17 @@ error_reporting(E_ERROR | E_WARNING); $redisHost = $argv[1]; -$sessionId = $argv[2]; +$saveHandler = $argv[2]; +$sessionId = $argv[3]; +$sessionLifetime = $argv[4]; if (empty($redisHost)) { - $redisHost = 'localhost'; + $redisHost = 'tcp://localhost:6379'; } -ini_set('session.save_handler', 'redis'); -ini_set('session.save_path', 'tcp://' . $redisHost . ':6379'); +ini_set('session.save_handler', $saveHandler); +ini_set('session.save_path', $redisHost); +ini_set('session.gc_maxlifetime', $sessionLifetime); session_id($sessionId); if (!session_start()) { diff --git a/tests/regenerateSessionId.php b/tests/regenerateSessionId.php index 6d49c0ab..f0e4c4e0 100644 --- a/tests/regenerateSessionId.php +++ b/tests/regenerateSessionId.php @@ -2,17 +2,18 @@ error_reporting(E_ERROR | E_WARNING); $redisHost = $argv[1]; -$sessionId = $argv[2]; -$locking = !!$argv[3]; -$destroyPrevious = !!$argv[4]; -$sessionProxy = !!$argv[5]; +$saveHandler = $argv[2]; +$sessionId = $argv[3]; +$locking = !!$argv[4]; +$destroyPrevious = !!$argv[5]; +$sessionProxy = !!$argv[6]; if (empty($redisHost)) { - $redisHost = 'localhost'; + $redisHost = 'tcp://localhost:6379'; } -ini_set('session.save_handler', 'redis'); -ini_set('session.save_path', 'tcp://' . $redisHost . ':6379'); +ini_set('session.save_handler', $saveHandler); +ini_set('session.save_path', $redisHost); if ($locking) { ini_set('redis.session.locking_enabled', true); diff --git a/tests/startSession.php b/tests/startSession.php index 081a6951..2149da68 100644 --- a/tests/startSession.php +++ b/tests/startSession.php @@ -2,29 +2,32 @@ error_reporting(E_ERROR | E_WARNING); $redisHost = $argv[1]; -$sessionId = $argv[2]; -$sleepTime = $argv[3]; -$maxExecutionTime = $argv[4]; -$lock_retries = $argv[5]; -$lock_expire = $argv[6]; -$sessionData = $argv[7]; +$saveHandler = $argv[2]; +$sessionId = $argv[3]; +$sleepTime = $argv[4]; +$maxExecutionTime = $argv[5]; +$lock_retries = $argv[6]; +$lock_expire = $argv[7]; +$sessionData = $argv[8]; +$sessionLifetime = $argv[9]; if (empty($redisHost)) { - $redisHost = 'localhost'; + $redisHost = 'tcp://localhost:6379'; } -ini_set('session.save_handler', 'redis'); -ini_set('session.save_path', 'tcp://' . $redisHost . ':6379'); +ini_set('session.save_handler', $saveHandler); +ini_set('session.save_path', $redisHost); ini_set('max_execution_time', $maxExecutionTime); ini_set('redis.session.lock_retries', $lock_retries); ini_set('redis.session.lock_expire', $lock_expire); +ini_set('session.gc_maxlifetime', $sessionLifetime); if (isset($argv[8])) { - ini_set('redis.session.locking_enabled', $argv[8]); + ini_set('redis.session.locking_enabled', $argv[10]); } if (isset($argv[9])) { - ini_set('redis.session.lock_wait_time', $argv[9]); + ini_set('redis.session.lock_wait_time', $argv[11]); } session_id($sessionId); |