Skip to content

Commit c520108

Browse files
dirxLKaemmerling
andauthored
Fix #73 Redis::ensureOpenConnection should only connect once (#76)
Signed-off-by: Dirk Adler <dirx@klitsche.de> Co-authored-by: Lukas Kämmerling <lukas.kaemmerling@hetzner-cloud.de>
1 parent 60f395f commit c520108

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

src/Prometheus/Storage/Redis.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ private function ensureOpenConnection(): void
204204
}
205205

206206
$this->redis->setOption(\Redis::OPT_READ_TIMEOUT, $this->options['read_timeout']);
207+
208+
$this->connectionInitialized = true;
207209
}
208210

209211
/**

tests/Test/Prometheus/Storage/RedisTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,72 @@ public function itShouldNotClearWholeRedisOnFlush(): void
7878
])
7979
);
8080
}
81+
82+
/**
83+
* @test
84+
*/
85+
public function itShouldOnlyConnectOnceOnSubsequentCalls(): void
86+
{
87+
$clientId = $this->redisConnection->rawCommand('client', 'id');
88+
$expectedClientId = 'id=' . ($clientId + 1) . ' ';
89+
$notExpectedClientId = 'id=' . ($clientId + 2) . ' ';
90+
91+
$redis = new Redis(['host' => REDIS_HOST]);
92+
93+
$redis->collect();
94+
95+
$this->assertStringContainsString(
96+
$expectedClientId,
97+
$this->redisConnection->rawCommand('client', 'list')
98+
);
99+
$this->assertStringNotContainsString(
100+
$notExpectedClientId,
101+
$this->redisConnection->rawCommand('client', 'list')
102+
);
103+
104+
$redis->collect();
105+
106+
$this->assertStringContainsString(
107+
$expectedClientId,
108+
$this->redisConnection->rawCommand('client', 'list')
109+
);
110+
$this->assertStringNotContainsString(
111+
$notExpectedClientId,
112+
$this->redisConnection->rawCommand('client', 'list')
113+
);
114+
}
115+
116+
/**
117+
* @test
118+
*/
119+
public function itShouldOnlyConnectOnceForInjectedRedisConnectionOnSubsequentCalls(): void
120+
{
121+
$clientId = $this->redisConnection->rawCommand('client', 'id');
122+
$expectedClientId = 'id=' . $clientId . ' ';
123+
$notExpectedClientId = 'id=' . ($clientId + 1) . ' ';
124+
125+
$redis = Redis::fromExistingConnection($this->redisConnection);
126+
127+
$redis->collect();
128+
129+
$this->assertStringContainsString(
130+
$expectedClientId,
131+
$this->redisConnection->rawCommand('client', 'list')
132+
);
133+
$this->assertStringNotContainsString(
134+
$notExpectedClientId,
135+
$this->redisConnection->rawCommand('client', 'list')
136+
);
137+
138+
$redis->collect();
139+
140+
$this->assertStringContainsString(
141+
$expectedClientId,
142+
$this->redisConnection->rawCommand('client', 'list')
143+
);
144+
$this->assertStringNotContainsString(
145+
$notExpectedClientId,
146+
$this->redisConnection->rawCommand('client', 'list')
147+
);
148+
}
81149
}

0 commit comments

Comments
 (0)