Skip to content

Commit 1666fc7

Browse files
committed
[12.x] extract Backoff test to separate test
1 parent c29c35a commit 1666fc7

File tree

2 files changed

+128
-97
lines changed

2 files changed

+128
-97
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
}

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)