10000 Add a test for session compression. · phpredis/phpredis@9f3ca98 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9f3ca98

Browse files
Add a test for session compression.
See #2473 #2480
1 parent 2b555c8 commit 9f3ca98

File tree

2 files changed

+56
-30
lines changed

2 files changed

+56
-30
lines changed

tests/RedisTest.php

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ protected function getSerializers() {
5151
}
5252

5353
protected function getCompressors() {
54-
$result[] = Redis::COMPRESSION_NONE;
54+
$result['none'] = Redis::COMPRESSION_NONE;
5555
if (defined('Redis::COMPRESSION_LZF'))
56-
$result[] = Redis::COMPRESSION_LZF;
56+
$result['lzf'] = Redis::COMPRESSION_LZF;
5757
if (defined('Redis::COMPRESSION_LZ4'))
58-
$result[] = Redis::COMPRESSION_LZ4;
58+
$result['lz4'] = Redis::COMPRESSION_LZ4;
5959
if (defined('Redis::COMPRESSION_ZSTD'))
60-
$result[] = Redis::COMPRESSION_ZSTD;
60+
$result['zstd'] = Redis::COMPRESSION_ZSTD;
6161

6262
return $result;
6363
}
@@ -7377,6 +7377,26 @@ public function testHighPorts() {
73777377
}
73787378
}
73797379

7380+
public function testSession_compression() {
7381+
$this->setSessionHandler();
7382+
7383+
foreach ($this->getCompressors() as $name => $val) {
7384+
7385+
$id = $this->generateSessionId();
7386+
$res = $this->startSessionProcess($id, 0, false, 300, true, null,
7387+
-1, 0, "testing_compression_$name", 1440,
7388+
$name);
7389+
7390+
$this->assertTrue($res);
7391+
7392+
$key = $this->sessionPrefix . $id;
7393+
7394+
$this->redis->setOption(Redis::OPT_COMPRESSION, $val);
7395+
$this->assertTrue($this->redis->get($key) !== false);
7396+
$this->redis->setOption(Redis::OPT_COMPRESSION, Redis::COMPRESSION_NONE);
7397+
}
7398+
}
7399+
73807400
public function testSession_savedToRedis()
73817401
{
73827402
$this->setSessionHandler();
@@ -7878,33 +7898,40 @@ private function generateSessionId()
78787898
* @param int $lock_retries
78797899
* @param int $lock_expires
78807900
* @param string $sessionData
7881-
*
78827901
* @param int $sessionLifetime
7902+
* @param string $sessionCompression
78837903
*
78847904
* @return bool
78857905
* @throws Exception
78867906
*/
7887-
private function startSessionProcess($sessionId, $sleepTime, $background, $maxExecutionTime = 300, $locking_enabled = true, $lock_wait_time = null, $lock_retries = -1, $lock_expires = 0, $sessionData = '', $sessionLifetime = 1440)
7907+
private function startSessionProcess($sessionId, $sleepTime, $background,
7908+
$maxExecutionTime = 300,
7909+
$locking_enabled = true,
7910+
$lock_wait_time = null,
7911+
$lock_retries = -1,
7912+
$lock_expires = 0,
7913+
$sessionData = '',
7914+
$sessionLifetime = 1440,
7915+
$sessionCompression = 'none')
78887916
{
7889-
if (substr(php_uname(), 0, 7) == "Windows"){
7917+
if (strpos(php_uname(), 'Windows') !== false)
78907918
$this->markTestSkipped();
7891-
return true;
7892-
} else {
7893-
$commandParameters = [$this->getFullHostPath(), $this->sessionSaveHandler, $sessionId, $sleepTime, $maxExecutionTime, $lock_retries, $lock_expires, $sessionData, $sessionLifetime];
7894-
if ($locking_enabled) {
7895-
$commandParameters[] = '1';
78967919

7897-
if ($lock_wait_time != null) {
7898-
$commandParameters[] = $lock_wait_time;
7899-
}
7900-
}
7901-
$commandParameters = array_map('escapeshellarg', $commandParameters);
7920+
$commandParameters = [
7921+
$this->getFullHostPath(), $this->sessionSaveHandler, $sessionId,
7922+
$sleepTime, $maxExecutionTime, $lock_retries, $lock_expires,
7923+
$sessionData, $sessionLifetime, $locking_enabled ? 1 : 0,
7924+
$lock_wait_time ?? 0, $sessionCompression
7925+
];
79027926

7903-
$command = self::getPhpCommand('startSession.php') . implode(' ', $commandParameters);
7904-
$command .= $background ? ' 2>/dev/null > /dev/null &' : ' 2>&1';
7905-
exec($command, $output);
7906-
return ($background || (count($output) == 1 && $output[0] == 'SUCCESS')) ? true : false;
7907-
}
7927+
$commandParameters = array_map('escapeshellarg', $commandParameters);
7928+
$commandParameters[] = $background ? '>/dev/null 2>&1 &' : '2>&1';
7929+
7930+
$command = self::getPhpCommand('startSession.php') . implode(' ', $commandParameters);
7931+
7932+
exec($command, $output);
7933+
7934+
return ($background || (count($output) == 1 && $output[0] == 'SUCCESS'));
79087935
}
79097936

79107937
/**

tests/startSession.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
$lock_expire = $argv[7];
1111
$sessionData = $argv[8];
1212
$sessionLifetime = $argv[9];
13+
$lockingEnabled = $argv[10];
14+
$lockWaitTime = $argv[11];
15+
$sessionCompression = $argv[12];
1316

1417
if (empty($redisHost)) {
1518
$redisHost = 'tcp://localhost:6379';
@@ -21,21 +24,17 @@
2124
ini_set("{$saveHandler}.session.lock_retries", $lock_retries);
2225
ini_set("{$saveHandler}.session.lock_expire", $lock_expire);
2326
ini_set('session.gc_maxlifetime', $sessionLifetime);
24-
25-
if (isset($argv[10])) {
26-
ini_set("{$saveHandler}.session.locking_enabled", $argv[10]);
27-
}
28-
29-
if (isset($argv[11])) {
30-
ini_set("{$saveHandler}.session.lock_wait_time", $argv[11]);
31-
}
27+
ini_set("{$saveHandler}.session.locking_enabled", $lockingEnabled);
28+
ini_set("{$saveHandler}.session.lock_wait_time", $lockWaitTime);
29+
ini_set('redis.session.compression', $sessionCompression);
3230

3331
session_id($sessionId);
3432
$sessionStartSuccessful = session_start();
3533
sleep($sleepTime);
3634
if (!empty($sessionData)) {
3735
$_SESSION['redis_test'] = $sessionData;
3836
}
37+
3938
session_write_close();
4039

4140
echo $sessionStartSuccessful ? 'SUCCESS' : 'FAILURE';

0 commit comments

Comments
 (0)
0