Skip to content

Commit bad065d

Browse files
authored
Remove unused redisURI parameter from PingStrategy constructors (#3573)
The redisURI parameter in PingStrategy constructors was never used in the implementation. The actual endpoint URI is passed to doHealthCheck() method when performing health checks, making the constructor parameter redundant. Changes: - Removed RedisURI parameter from both PingStrategy constructors - Updated DEFAULT supplier to use lambda instead of method reference Remove unused redisURI parameter from PingStrategy constructors The redisURI parameter in PingStrategy constructors was never used in the implementation. The actual endpoint URI is passed to doHealthCheck() method when performing health checks, making the constructor parameter redundant. Changes: - Removed RedisURI parameter from both PingStrategy constructors - Updated DEFAULT supplier to use lambda instead of method reference # Conflicts: # src/test/java/io/lettuce/core/failover/health/PingStrategyIntegrationTests.java
1 parent b156944 commit bad065d

File tree

3 files changed

+24
-29
lines changed

3 files changed

+24
-29
lines changed

src/main/java/io/lettuce/core/failover/health/PingStrategy.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ public class PingStrategy implements HealthCheckStrategy {
1010

1111
private final HealthCheckStrategy.Config config;
1212

13-
public PingStrategy(RedisURI redisURI, DatabaseRawConnectionFactory connectionFactory) {
14-
this(redisURI, connectionFactory, HealthCheckStrategy.Config.create());
13+
public PingStrategy(DatabaseRawConnectionFactory connectionFactory) {
14+
this(connectionFactory, HealthCheckStrategy.Config.create());
1515
}
1616

17-
public PingStrategy(RedisURI redisURI, DatabaseRawConnectionFactory connectionFactory, HealthCheckStrategy.Config config) {
17+
public PingStrategy(DatabaseRawConnectionFactory connectionFactory, HealthCheckStrategy.Config config) {
1818
this.connectionFactory = connectionFactory;
1919
this.config = config;
2020
}
@@ -63,6 +63,6 @@ public void close() {
6363
// No resources to close
6464
}
6565

66-
public static final HealthCheckStrategySupplier DEFAULT = PingStrategy::new;
66+
public static final HealthCheckStrategySupplier DEFAULT = (uri, connectionFactory) -> new PingStrategy(connectionFactory);
6767

6868
}

src/test/java/io/lettuce/core/failover/health/PingStrategyIntegrationTests.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.IOException;
2323
import java.time.Duration;
2424
import java.util.Arrays;
25+
import java.util.Collections;
2526

2627
import static org.assertj.core.api.Assertions.assertThat;
2728
import static org.awaitility.Awaitility.await;
@@ -119,7 +120,7 @@ static void cleanupDatabaseConnectionProvider() {
119120
@DisplayName("Should create MultiDbClient with PingStrategy health checks")
120121
void shouldCreateClientWithPingStrategy() {
121122
// Given: DatabaseConfigs with PingStrategy using proxy URIs
122-
HealthCheckStrategySupplier pingSupplier = (uri, options) -> new PingStrategy(uri, options,
123+
HealthCheckStrategySupplier pingSupplier = (uri, options) -> new PingStrategy(options,
123124
HealthCheckStrategy.Config.builder().interval(100) // Fast interval for testing
124125
.timeout(1000).numProbes(1).build());
125126

@@ -152,7 +153,7 @@ void shouldRecoverAfterDisconnect() throws Exception {
152153
RedisURI uri = RedisURI.builder().withHost(TestSettings.host()).withPort(9479).withTimeout(Duration.ofMillis(1000))
153154
.build();
154155

155-
try (PingStrategy strategy = new PingStrategy(uri, rawConnectionFactory,
156+
try (PingStrategy strategy = new PingStrategy(rawConnectionFactory,
156157
HealthCheckStrategy.Config.builder().interval(1000).timeout(500).numProbes(1).build())) {
157158
// When: Initial health check should work
158159
HealthStatus initialStatus = strategy.doHealthCheck(uri);
@@ -182,7 +183,7 @@ void shouldHandleConnectionTimeout() throws Exception {
182183
RedisURI uri = RedisURI.builder().withHost(TestSettings.host()).withPort(9479).withTimeout(Duration.ofMillis(100))
183184
.build();
184185

185-
try (PingStrategy strategy = new PingStrategy(uri, rawConnectionFactory,
186+
try (PingStrategy strategy = new PingStrategy(rawConnectionFactory,
186187
HealthCheckStrategy.Config.builder().interval(1000).timeout(500).numProbes(1).build())) {
187188
// When: Initial health check should work
188189
assertThat(strategy.doHealthCheck(uri)).isEqualTo(HealthStatus.HEALTHY);
@@ -211,7 +212,7 @@ void shouldHandleConnectionDrop() throws Exception {
211212
RedisURI uri = RedisURI.builder().withHost(TestSettings.host()).withPort(9479).withTimeout(Duration.ofMillis(2000))
212213
.build();
213214

214-
try (PingStrategy strategy = new PingStrategy(uri, rawConnectionFactory, HealthCheckStrategy.Config.create())) {
215+
try (PingStrategy strategy = new PingStrategy(rawConnectionFactory, HealthCheckStrategy.Config.create())) {
215216
// When: Initial health check
216217
assertThat(strategy.doHealthCheck(uri)).isEqualTo(HealthStatus.HEALTHY);
217218

@@ -236,18 +237,17 @@ void shouldHandleConnectionDrop() throws Exception {
236237
@DisplayName("Should detect healthy endpoint with PingStrategy")
237238
void shouldDetectHealthyEndpoint() {
238239
// Given: DatabaseConfig with PingStrategy using proxy URI
239-
HealthCheckStrategySupplier pingSupplier = (uri, options) -> new PingStrategy(uri, options,
240+
HealthCheckStrategySupplier pingSupplier = (uri, options) -> new PingStrategy(options,
240241
HealthCheckStrategy.Config.builder().interval(50) // Very fast for testing
241242
.timeout(1000).numProbes(1).build());
242243

243244
DatabaseConfig config = DatabaseConfig.builder(proxyUri1).weight(1.0f).healthCheckStrategySupplier(pingSupplier)
244245
.build();
245246

246247
// When: Create MultiDbClient and connect
247-
MultiDbClient testClient = MultiDbClient.create(Arrays.asList(config));
248-
StatefulRedisMultiDbConnection<String, String> connection = testClient.connect();
248+
MultiDbClient testClient = MultiDbClient.create(Collections.singletonList(config));
249249

250-
try {
250+
try (StatefulRedisMultiDbConnection<String, String> connection = testClient.connect()) {
251251
// Then: Endpoint should be healthy
252252
assertThat(connection.getCurrentEndpoint()).isEqualTo(proxyUri1);
253253

@@ -257,7 +257,6 @@ void shouldDetectHealthyEndpoint() {
257257
});
258258

259259
} finally {
260-
connection.close();
261260
testClient.shutdown();
262261
}
263262
}
@@ -271,9 +270,8 @@ void shouldUseDefaultPingStrategySupplier() {
271270

272271
// When: Create MultiDbClient and connect
273272
MultiDbClient testClient = MultiDbClient.create(Arrays.asList(config1, config2));
274-
StatefulRedisMultiDbConnection<String, String> connection = testClient.connect();
275273

276-
try {
274+
try (StatefulRedisMultiDbConnection<String, String> connection = testClient.connect()) {
277275
// Then: Connection should work with default PingStrategy
278276
assertThat(connection.sync().ping()).isEqualTo("PONG");
279277
assertThat(connection.getCurrentEndpoint()).isNotNull();
@@ -283,7 +281,6 @@ void shouldUseDefaultPingStrategySupplier() {
283281
assertThat(connection.sync().get("default-ping-key")).isEqualTo("default-ping-value");
284282

285283
} finally {
286-
connection.close();
287284
testClient.shutdown();
288285
}
289286
}
@@ -292,7 +289,7 @@ void shouldUseDefaultPingStrategySupplier() {
292289
@DisplayName("Should handle multiple probes with PingStrategy")
293290
void shouldHandleMultipleProbes() {
294291
// Given: DatabaseConfig with PingStrategy configured for multiple probes using proxy URI
295-
HealthCheckStrategySupplier pingSupplier = (uri, options) -> new PingStrategy(uri, options,
292+
HealthCheckStrategySupplier pingSupplier = (uri, options) -> new PingStrategy(options,
296293
HealthCheckStrategy.Config.builder().interval(100).timeout(1000).numProbes(3) // Multiple probes
297294
.delayInBetweenProbes(50).policy(ProbingPolicy.BuiltIn.MAJORITY_SUCCESS).build());
298295

@@ -301,14 +298,12 @@ void shouldHandleMultipleProbes() {
301298

302299
// When: Create MultiDbClient and connect
303300
MultiDbClient testClient = MultiDbClient.create(Arrays.asList(config));
304-
StatefulRedisMultiDbConnection<String, String> connection = testClient.connect();
305301

306-
try {
302+
try (StatefulRedisMultiDbConnection<String, String> connection = testClient.connect()) {
307303
// Then: Connection should work with multiple probes
308304
assertThat(connection.sync().ping()).isEqualTo("PONG");
309305

310306
} finally {
311-
connection.close();
312307
testClient.shutdown();
313308
}
314309
}

src/test/java/io/lettuce/core/failover/health/PingStrategyTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class ConstructorTests {
4949
@DisplayName("Should create PingStrategy with default config")
5050
void shouldCreateWithDefaultConfig() {
5151
// When
52-
PingStrategy strategy = new PingStrategy(testUri, connectionFactory);
52+
PingStrategy strategy = new PingStrategy(connectionFactory);
5353

5454
// Then
5555
assertThat(strategy).isNotNull();
@@ -69,7 +69,7 @@ void shouldCreateWithCustomConfig() {
6969
.numProbes(3).delayInBetweenProbes(100).policy(ProbingPolicy.BuiltIn.MAJORITY_SUCCESS).build();
7070

7171
// When
72-
PingStrategy strategy = new PingStrategy(testUri, connectionFactory, customConfig);
72+
PingStrategy strategy = new PingStrategy(connectionFactory, customConfig);
7373

7474
// Then
7575
assertThat(strategy.getInterval()).isEqualTo(500);
@@ -94,7 +94,7 @@ void shouldReturnHealthyWhenPingSucceeds() {
9494
when(connection.sync()).thenReturn((RedisCommands) syncCommands);
9595
when(syncCommands.ping()).thenReturn("PONG");
9696

97-
PingStrategy strategy = new PingStrategy(testUri, connectionFactory);
97+
PingStrategy strategy = new PingStrategy(connectionFactory);
9898

9999
// When
100100
HealthStatus status = strategy.doHealthCheck(testUri);
@@ -116,7 +116,7 @@ void shouldReturnUnhealthyWhenPingReturnsNonPong() {
116116
when(connection.sync()).thenReturn((RedisCommands) syncCommands);
117117
when(syncCommands.ping()).thenReturn("UNEXPECTED");
118118

119-
PingStrategy strategy = new PingStrategy(testUri, connectionFactory);
119+
PingStrategy strategy = new PingStrategy(connectionFactory);
120120

121121
// When
122122
HealthStatus status = strategy.doHealthCheck(testUri);
@@ -132,7 +132,7 @@ void shouldReturnUnhealthyWhenConnectionIsNull() {
132132
// Given
133133
when(connectionFactory.connectToDatabase(testUri)).thenReturn(null);
134134

135-
PingStrategy strategy = new PingStrategy(testUri, connectionFactory);
135+
PingStrategy strategy = new PingStrategy(connectionFactory);
136136

137137
// When
138138
HealthStatus status = strategy.doHealthCheck(testUri);
@@ -149,7 +149,7 @@ void shouldReturnUnhealthyWhenConnectionThrowsException() {
149149
// Given
150150
when(connectionFactory.connectToDatabase(testUri)).thenThrow(new RuntimeException("Connection failed"));
151151

152-
PingStrategy strategy = new PingStrategy(testUri, connectionFactory);
152+
PingStrategy strategy = new PingStrategy(connectionFactory);
153153

154154
// When
155155
HealthStatus status = strategy.doHealthCheck(testUri);
@@ -167,7 +167,7 @@ void shouldReturnUnhealthyWhenPingThrowsException() {
167167
when(connection.sync()).thenReturn((RedisCommands) syncCommands);
168168
when(syncCommands.ping()).thenThrow(new RuntimeException("PING failed"));
169169

170-
PingStrategy strategy = new PingStrategy(testUri, connectionFactory);
170+
PingStrategy strategy = new PingStrategy(connectionFactory);
171171

172172
// When
173173
HealthStatus status = strategy.doHealthCheck(testUri);
@@ -186,7 +186,7 @@ void shouldCloseConnectionWhenPingThrowsException() {
186186
when(connection.sync()).thenReturn((RedisCommands) syncCommands);
187187
when(syncCommands.ping()).thenThrow(new RuntimeException("PING failed"));
188188

189-
PingStrategy strategy = new PingStrategy(testUri, connectionFactory);
189+
PingStrategy strategy = new PingStrategy(connectionFactory);
190190

191191
// When
192192
strategy.doHealthCheck(testUri);
@@ -204,7 +204,7 @@ void shouldHandleMultipleHealthChecks() {
204204
when(connection.sync()).thenReturn((RedisCommands) syncCommands);
205205
when(syncCommands.ping()).thenReturn("PONG");
206206

207-
PingStrategy strategy = new PingStrategy(testUri, connectionFactory);
207+
PingStrategy strategy = new PingStrategy(connectionFactory);
208208

209209
// When
210210
HealthStatus status1 = strategy.doHealthCheck(testUri);

0 commit comments

Comments
 (0)