|
5 | 5 | use Illuminate\Cache\RateLimiter; |
6 | 6 | use Illuminate\Cache\RedisStore; |
7 | 7 | use Illuminate\Cache\Repository; |
| 8 | +use Illuminate\Foundation\Application; |
8 | 9 | use Illuminate\Foundation\Testing\Concerns\InteractsWithRedis; |
| 10 | +use Illuminate\Redis\RedisManager; |
| 11 | +use Illuminate\Support\Env; |
| 12 | +use InvalidArgumentException; |
9 | 13 | use PHPUnit\Framework\Attributes\DataProvider; |
10 | 14 | use PHPUnit\Framework\TestCase; |
| 15 | +use Redis; |
11 | 16 |
|
12 | 17 | class RedisCacheIntegrationTest extends TestCase |
13 | 18 | { |
@@ -82,4 +87,92 @@ public function testRedisCacheAddNull($driver) |
82 | 87 | $repository->forever('k', null); |
83 | 88 | $this->assertFalse($repository->add('k', 'v', 60)); |
84 | 89 | } |
| 90 | + |
| 91 | + #[DataProvider('phpRedisBackoffAlgorithmsProvider')] |
| 92 | + public function testPhpRedisBackoffAlgorithmParsing($friendlyAlgorithmName, $expectedAlgorithm) |
| 93 | + { |
| 94 | + $host = Env::get('REDIS_HOST', '127.0.0.1'); |
| 95 | + $port = Env::get('REDIS_PORT', 6379); |
| 96 | + |
| 97 | + $manager = new RedisManager(new Application(), 'phpredis', [ |
| 98 | + 'default' => [ |
| 99 | + 'host' => $host, |
| 100 | + 'port' => $port, |
| 101 | + 'backoff_algorithm' => $friendlyAlgorithmName, |
| 102 | + ], |
| 103 | + ]); |
| 104 | + |
| 105 | + $this->assertEquals( |
| 106 | + $expectedAlgorithm, |
| 107 | + $manager->connection()->client()->getOption(Redis::OPT_BACKOFF_ALGORITHM) |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + #[DataProvider('phpRedisBackoffAlgorithmsProvider')] |
| 112 | + public function testPhpRedisBackoffAlgorithm($friendlyAlgorithm, $expectedAlgorithm) |
| 113 | + { |
| 114 | + $host = Env::get('REDIS_HOST', '127.0.0.1'); |
| 115 | + $port = Env::get('REDIS_PORT', 6379); |
| 116 | + |
| 117 | + $manager = new RedisManager(new Application(), 'phpredis', [ |
| 118 | + 'default' => [ |
| 119 | + 'host' => $host, |
| 120 | + 'port' => $port, |
| 121 | + 'backoff_algorithm' => $expectedAlgorithm, |
| 122 | + ], |
| 123 | + ]); |
| 124 | + |
| 125 | + $this->assertEquals( |
| 126 | + $expectedAlgorithm, |
| 127 | + $manager->connection()->client()->getOption(Redis::OPT_BACKOFF_ALGORITHM) |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + public function testAnInvalidPhpRedisBackoffAlgorithmIsConvertedToDefault() |
| 132 | + { |
| 133 | + $host = Env::get('REDIS_HOST', '127.0.0.1'); |
| 134 | + $port = Env::get('REDIS_PORT', 6379); |
| 135 | + |
| 136 | + $manager = new RedisManager(new Application(), 'phpredis', [ |
| 137 | + 'default' => [ |
| 138 | + 'host' => $host, |
| 139 | + 'port' => $port, |
| 140 | + 'backoff_algorithm' => 7, |
| 141 | + ], |
| 142 | + ]); |
| 143 | + |
| 144 | + $this->assertEquals( |
| 145 | + Redis::BACKOFF_ALGORITHM_DEFAULT, |
| 146 | + $manager->connection()->client()->getOption(Redis::OPT_BACKOFF_ALGORITHM) |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + public function testItFailsWithAnInvalidPhpRedisAlgorithm() |
| 151 | + { |
| 152 | + $this->expectException(InvalidArgumentException::class); |
| 153 | + $this->expectExceptionMessage('Algorithm [foo] is not a valid PhpRedis backoff algorithm'); |
| 154 | + |
| 155 | + $host = Env::get('REDIS_HOST', '127.0.0.1'); |
| 156 | + $port = Env::get('REDIS_PORT', 6379); |
| 157 | + |
| 158 | + (new RedisManager(new Application(), 'phpredis', [ |
| 159 | + 'default' => [ |
| 160 | + 'host' => $host, |
| 161 | + 'port' => $port, |
| 162 | + 'backoff_algorithm' => 'foo', |
| 163 | + ], |
| 164 | + ]))->connection(); |
| 165 | + } |
| 166 | + |
| 167 | + public static function phpRedisBackoffAlgorithmsProvider() |
| 168 | + { |
| 169 | + return [ |
| 170 | + ['default', Redis::BACKOFF_ALGORITHM_DEFAULT], |
| 171 | + ['decorrelated_jitter', Redis::BACKOFF_ALGORITHM_DECORRELATED_JITTER], |
| 172 | + ['equal_jitter', Redis::BACKOFF_ALGORITHM_EQUAL_JITTER], |
| 173 | + ['exponential', Redis::BACKOFF_ALGORITHM_EXPONENTIAL], |
| 174 | + ['uniform', Redis::BACKOFF_ALGORITHM_UNIFORM], |
| 175 | + ['constant', Redis::BACKOFF_ALGORITHM_CONSTANT], |
| 176 | + ]; |
| 177 | + } |
85 | 178 | } |
0 commit comments