Skip to content

Commit 8577748

Browse files
committed
fixing tests
1 parent e2a9ed9 commit 8577748

File tree

7 files changed

+36
-7
lines changed

7 files changed

+36
-7
lines changed

.github/workflows/tests.yml

+21-1
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,16 @@ jobs:
3030
- 5432:5432
3131

3232
env:
33-
# The hostname used to communicate with the Redis service container
33+
# The hostname used to communicate with the Redis/Sentinel service containers
3434
REDIS_HOST: redis
35+
# The hostname for redis sentinel
36+
REDIS_SENTINEL_HOST: redis-sentinel
3537
# The default Redis port
3638
REDIS_PORT: 6379
39+
# The default Redis Sentinel port
40+
REDIS_SENTINEL_PORT: 26379
41+
# The default Redis Sentinel primary
42+
REDIS_SENTINEL_SERVICE: myprimary
3743
# MySQL
3844
DB_DATABASE: test
3945
DB_USER: root
@@ -69,6 +75,20 @@ jobs:
6975
with:
7076
redis-version: ${{ matrix.redis-version }}
7177

78+
- name: Generate Redis Sentinel conf compatible with redis 5 assuming 127.0.0.1 (no resolve hostname)
79+
run: |
80+
REDIS_SENTINEL_IP=127.0.0.1
81+
cat <<EOF > sentinel5.conf
82+
port ${{ env.REDIS_SENTINEL_PORT }}
83+
sentinel monitor ${{ env.REDIS_SENTINEL_SERVICE }} $REDIS_SENTINEL_IP ${{ env.REDIS_PORT }} 2
84+
sentinel down-after-milliseconds ${{ env.REDIS_SENTINEL_SERVICE }} 10000
85+
sentinel failover-timeout ${{ env.REDIS_SENTINEL_SERVICE }} 180000
86+
sentinel parallel-syncs ${{ env.REDIS_SENTINEL_SERVICE }} 2
87+
EOF
88+
89+
- name: Start Redis Sentinel
90+
run: docker run -d --name ${{env.REDIS_SENTINEL_HOST}} -p 26379:26379 --link redis:redis -v $PWD/sentinel5.conf:/data/sentinel.conf redis:${{ matrix.redis-version }} redis-server sentinel.conf --sentinel
91+
7292
- name: Execute tests (PDO with Sqlite)
7393
run: vendor/bin/phpunit
7494

.vscode/settings.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"github-actions.workflows.pinned.workflows": []
2+
"github-actions.workflows.pinned.workflows": [],
3+
"githubPullRequests.ignoredPullRequestBranches": [
4+
"main"
5+
]
36
}

examples/flush_adapter.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
if ($adapterName === 'redis') {
1010
define('REDIS_HOST', $_SERVER['REDIS_HOST'] ?? '127.0.0.1');
11+
define('REDIS_SENTINEL_HOST', $_SERVER['REDIS_SENTINEL_HOST'] ?? '127.0.0.1');
1112

12-
$adapter = new Prometheus\Storage\Redis(['host' => REDIS_HOST]);
13+
$adapter = new Prometheus\Storage\Redis(['host' => REDIS_HOST, 'sentinel' => ['host' => REDIS_SENTINEL_HOST]]);
1314
} elseif ($adapterName === 'apc') {
1415
$adapter = new Prometheus\Storage\APC();
1516
} elseif ($adapterName === 'apcng') {

sentinel.conf

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
bind 0.0.0.0
2+
port 26379
23
sentinel monitor myprimary redis 6379 2
34
sentinel resolve-hostnames yes
45
sentinel down-after-milliseconds myprimary 10000

src/Prometheus/Storage/Redis.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function getRedisSentinel(): \RedisSentinel
130130
* @return self
131131
* @throws StorageException
132132
*/
133-
public static function fromExistingConnection(\Redis $redis, \RedisSentinel $redisSentinel = null): self
133+
public static function fromExistingConnection(\Redis $redis, ?\RedisSentinel $redisSentinel = null): self
134134
{
135135
if (isset($redisSentinel)) {
136136
RedisSentinel::fromExistingConnection($redisSentinel);

src/Prometheus/Storage/RedisSentinel.php

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Prometheus\Exception\StorageException;
66

7+
// Redis Sentinel connector based on https://github.com/Namoshek/laravel-redis-sentinel
78
class RedisSentinel
89
{
910
/**

tests/Test/Prometheus/Storage/RedisSentinelTest.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ public function itShouldThrowExceptionWhenInjectedRedisIsNotConnected(): void
6060
{
6161
$connection = new \Redis();
6262
// @phpstan-ignore arguments.count
63-
$sentinel = new \RedisSentinel();
63+
64+
$sentinel = version_compare((string)phpversion('redis'), '6.0', '>=') ?
65+
new \RedisSentinel(['host' => '/dev/null']) :
66+
new \RedisSentinel('/dev/null');
6467

6568
self::expectException(StorageException::class);
6669
self::expectExceptionMessageMatches("/Can't connect to RedisSentinel server\\..*/");
@@ -86,8 +89,8 @@ public function itShouldThrowAnExceptionOnPrimaryFailure(): void
8689
* @test
8790
*/
8891
public function itShouldGetMaster(): void
89-
{
90-
$redis = new Redis(['host' => '/dev/null',
92+
{
93+
$redis = new Redis(['host' => REDIS_HOST,
9194
'sentinel' => ['host' => REDIS_SENTINEL_HOST, 'enable' => true, 'service' => 'myprimary']
9295
]);
9396

0 commit comments

Comments
 (0)