Skip to content

Commit f9a56c2

Browse files
committed
[12.x] split PhpRedisBackoffTest
1 parent c29c35a commit f9a56c2

File tree

2 files changed

+130
-97
lines changed

2 files changed

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

tests/Integration/Cache/RedisCacheIntegrationTest.php

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,10 @@
55
use Illuminate\Cache\RateLimiter;
66
use Illuminate\Cache\RedisStore;
77
use Illuminate\Cache\Repository;
8-
use Illuminate\Foundation\Application;
98
use Illuminate\Foundation\Testing\Concerns\InteractsWithRedis;
10-
use Illuminate\Redis\RedisManager;
11-
use Illuminate\Support\Env;
12-
use InvalidArgumentException;
139
use PHPUnit\Framework\Attributes\DataProvider;
1410
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
1511
use PHPUnit\Framework\TestCase;
16-
use Redis;
1712

1813
#[RequiresPhpExtension('redis')]
1914
class RedisCacheIntegrationTest extends TestCase
@@ -89,96 +84,4 @@ public function testRedisCacheAddNull($driver)
8984
$repository->forever('k', null);
9085
$this->assertFalse($repository->add('k', 'v', 60));
9186
}
92-
93-
#[DataProvider('phpRedisBackoffAlgorithmsProvider')]
94-
public function testPhpRedisBackoffAlgorithmParsing($friendlyAlgorithmName, $expectedAlgorithm)
95-
{
96-
$host = Env::get('REDIS_HOST', '127.0.0.1');
97-
$port = Env::get('REDIS_PORT', 6379);
98-
99-
$manager = new RedisManager(new Application(), 'phpredis', [
100-
'default' => [
101-
'host' => $host,
102-
'port' => $port,
103-
'backoff_algorithm' => $friendlyAlgorithmName,
104-
],
105-
]);
106-
107-
$this->assertEquals(
108-
$expectedAlgorithm,
109-
$manager->connection()->client()->getOption(Redis::OPT_BACKOFF_ALGORITHM)
110-
);
111-
}
112-
113-
#[DataProvider('phpRedisBackoffAlgorithmsProvider')]
114-
public function testPhpRedisBackoffAlgorithm($friendlyAlgorithm, $expectedAlgorithm)
115-
{
116-
$host = Env::get('REDIS_HOST', '127.0.0.1');
117-
$port = Env::get('REDIS_PORT', 6379);
118-
119-
$manager = new RedisManager(new Application(), 'phpredis', [
120-
'default' => [
121-
'host' => $host,
122-
'port' => $port,
123-
'backoff_algorithm' => $expectedAlgorithm,
124-
],
125-
]);
126-
127-
$this->assertEquals(
128-
$expectedAlgorithm,
129-
$manager->connection()->client()->getOption(Redis::OPT_BACKOFF_ALGORITHM)
130-
);
131-
}
132-
133-
public function testAnInvalidPhpRedisBackoffAlgorithmIsConvertedToDefault()
134-
{
135-
$host = Env::get('REDIS_HOST', '127.0.0.1');
136-
$port = Env::get('REDIS_PORT', 6379);
137-
138-
$manager = new RedisManager(new Application(), 'phpredis', [
139-
'default' => [
140-
'host' => $host,
141-
'port' => $port,
142-
'backoff_algorithm' => 7,
143-
],
144-
]);
145-
146-
$this->assertEquals(
147-
Redis::BACKOFF_ALGORITHM_DEFAULT,
148-
$manager->connection()->client()->getOption(Redis::OPT_BACKOFF_ALGORITHM)
149-
);
150-
}
151-
152-
public function testItFailsWithAnInvalidPhpRedisAlgorithm()
153-
{
154-
$this->expectException(InvalidArgumentException::class);
155-
$this->expectExceptionMessage('Algorithm [foo] is not a valid PhpRedis backoff algorithm');
156-
157-
$host = Env::get('REDIS_HOST', '127.0.0.1');
158-
$port = Env::get('REDIS_PORT', 6379);
159-
160-
(new RedisManager(new Application(), 'phpredis', [
161-
'default' => [
162-
'host' => $host,
163-
'port' => $port,
164-
'backoff_algorithm' => 'foo',
165-
],
166-
]))->connection();
167-
}
168-
169-
public static function phpRedisBackoffAlgorithmsProvider()
170-
{
171-
if (! class_exists(Redis::class)) {
172-
return [];
173-
}
174-
175-
return [
176-
['default', Redis::BACKOFF_ALGORITHM_DEFAULT],
177-
['decorrelated_jitter', Redis::BACKOFF_ALGORITHM_DECORRELATED_JITTER],
178-
['equal_jitter', Redis::BACKOFF_ALGORITHM_EQUAL_JITTER],
179-
['exponential', Redis::BACKOFF_ALGORITHM_EXPONENTIAL],
180-
['uniform', Redis::BACKOFF_ALGORITHM_UNIFORM],
181-
['constant', Redis::BACKOFF_ALGORITHM_CONSTANT],
182-
];
183-
}
18487
}

0 commit comments

Comments
 (0)