|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Integration\Cache; |
| 4 | + |
| 5 | +use Illuminate\Foundation\Application; |
| 6 | +use Illuminate\Foundation\Testing\Concerns\InteractsWithRedis; |
| 7 | +use Illuminate\Redis\RedisManager; |
| 8 | +use Illuminate\Support\Env; |
| 9 | +use InvalidArgumentException; |
| 10 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 11 | +use PHPUnit\Framework\Attributes\RequiresPhpExtension; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | +use Redis; |
| 14 | + |
| 15 | +#[RequiresPhpExtension('redis')] |
| 16 | +class PhpRedisBackoffTest extends TestCase |
| 17 | +{ |
| 18 | + use InteractsWithRedis; |
| 19 | + |
| 20 | + protected function setUp(): void |
| 21 | + { |
| 22 | + parent::setUp(); |
| 23 | + $this->setUpRedis(); |
| 24 | + |
| 25 | + $client = $this->redis['phpredis']->connection()->client(); |
| 26 | + if (!$client instanceof Redis) { |
| 27 | + $this->markTestSkipped('Backoff option is only supported with phpredis in non-cluster mode'); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + protected function tearDown(): void |
| 32 | + { |
| 33 | + parent::tearDown(); |
| 34 | + $this->tearDownRedis(); |
| 35 | + } |
| 36 | + |
| 37 | + #[DataProvider('phpRedisBackoffAlgorithmsProvider')] |
| 38 | + public function testPhpRedisBackoffAlgorithmParsing($friendlyAlgorithmName, $expectedAlgorithm) |
| 39 | + { |
| 40 | + $host = Env::get('REDIS_HOST', '127.0.0.1'); |
| 41 | + $port = Env::get('REDIS_PORT', 6379); |
| 42 | + |
| 43 | + $manager = new RedisManager(new Application(), 'phpredis', [ |
| 44 | + 'default' => [ |
| 45 | + 'host' => $host, |
| 46 | + 'port' => $port, |
| 47 | + 'backoff_algorithm' => $friendlyAlgorithmName, |
| 48 | + ], |
| 49 | + ]); |
| 50 | + |
| 51 | + $this->assertEquals( |
| 52 | + $expectedAlgorithm, |
| 53 | + $manager->connection()->client()->getOption(Redis::OPT_BACKOFF_ALGORITHM) |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + #[DataProvider('phpRedisBackoffAlgorithmsProvider')] |
| 58 | + public function testPhpRedisBackoffAlgorithm($friendlyAlgorithm, $expectedAlgorithm) |
| 59 | + { |
| 60 | + $host = Env::get('REDIS_HOST', '127.0.0.1'); |
| 61 | + $port = Env::get('REDIS_PORT', 6379); |
| 62 | + |
| 63 | + $manager = new RedisManager(new Application(), 'phpredis', [ |
| 64 | + 'default' => [ |
| 65 | + 'host' => $host, |
| 66 | + 'port' => $port, |
| 67 | + 'backoff_algorithm' => $expectedAlgorithm, |
| 68 | + ], |
| 69 | + ]); |
| 70 | + |
| 71 | + $this->assertEquals( |
| 72 | + $expectedAlgorithm, |
| 73 | + $manager->connection()->client()->getOption(Redis::OPT_BACKOFF_ALGORITHM) |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + public function testAnInvalidPhpRedisBackoffAlgorithmIsConvertedToDefault() |
| 78 | + { |
| 79 | + $host = Env::get('REDIS_HOST', '127.0.0.1'); |
| 80 | + $port = Env::get('REDIS_PORT', 6379); |
| 81 | + |
| 82 | + $manager = new RedisManager(new Application(), 'phpredis', [ |
| 83 | + 'default' => [ |
| 84 | + 'host' => $host, |
| 85 | + 'port' => $port, |
| 86 | + 'backoff_algorithm' => 7, |
| 87 | + ], |
| 88 | + ]); |
| 89 | + |
| 90 | + $this->assertEquals( |
| 91 | + Redis::BACKOFF_ALGORITHM_DEFAULT, |
| 92 | + $manager->connection()->client()->getOption(Redis::OPT_BACKOFF_ALGORITHM) |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + public function testItFailsWithAnInvalidPhpRedisAlgorithm() |
| 97 | + { |
| 98 | + $this->expectException(InvalidArgumentException::class); |
| 99 | + $this->expectExceptionMessage('Algorithm [foo] is not a valid PhpRedis backoff algorithm'); |
| 100 | + |
| 101 | + $host = Env::get('REDIS_HOST', '127.0.0.1'); |
| 102 | + $port = Env::get('REDIS_PORT', 6379); |
| 103 | + |
| 104 | + (new RedisManager(new Application(), 'phpredis', [ |
| 105 | + 'default' => [ |
| 106 | + 'host' => $host, |
| 107 | + 'port' => $port, |
| 108 | + 'backoff_algorithm' => 'foo', |
| 109 | + ], |
| 110 | + ]))->connection(); |
| 111 | + } |
| 112 | + |
| 113 | + public static function phpRedisBackoffAlgorithmsProvider() |
| 114 | + { |
| 115 | + if (! class_exists(Redis::class)) { |
| 116 | + return []; |
| 117 | + } |
| 118 | + |
| 119 | + return [ |
| 120 | + ['default', Redis::BACKOFF_ALGORITHM_DEFAULT], |
| 121 | + ['decorrelated_jitter', Redis::BACKOFF_ALGORITHM_DECORRELATED_JITTER], |
| 122 | + ['equal_jitter', Redis::BACKOFF_ALGORITHM_EQUAL_JITTER], |
| 123 | + ['exponential', Redis::BACKOFF_ALGORITHM_EXPONENTIAL], |
| 124 | + ['uniform', Redis::BACKOFF_ALGORITHM_UNIFORM], |
| 125 | + ['constant', Redis::BACKOFF_ALGORITHM_CONSTANT], |
| 126 | + ]; |
| 127 | + } |
| 128 | +} |
0 commit comments