Skip to content

Commit ab19896

Browse files
committed
clean up
- add unit test - remove unused HealthCheckStrategySupplier DEFAULT_WITH_PROVIDER
1 parent e0d5b3f commit ab19896

File tree

3 files changed

+243
-36
lines changed

3 files changed

+243
-36
lines changed

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,4 @@ public void close() {
6565

6666
public static final HealthCheckStrategySupplier DEFAULT = PingStrategy::new;
6767

68-
/**
69-
* Default supplier that uses connection provider when available for resource efficiency.
70-
*/
71-
public static final HealthCheckStrategySupplier DEFAULT_WITH_PROVIDER = PingStrategy::new;
72-
7368
}

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

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -323,37 +323,6 @@ void shouldHandleMultipleProbes() {
323323
}
324324
}
325325

326-
@Test
327-
@DisplayName("Should reuse MultiDbClient connections with DEFAULT_WITH_PROVIDER supplier")
328-
void shouldReuseConnectionsWithProvider() {
329-
// Given: DatabaseConfig with DEFAULT_WITH_PROVIDER supplier that reuses connections
330-
DatabaseConfig config1 = new DatabaseConfig(proxyUri1, 1.0f, null, null, PingStrategy.DEFAULT_WITH_PROVIDER);
331-
DatabaseConfig config2 = new DatabaseConfig(proxyUri2, 0.5f, null, null, PingStrategy.DEFAULT_WITH_PROVIDER);
332-
333-
// When: Create MultiDbClient and connect
334-
MultiDbClient testClient = MultiDbClient.create(Arrays.asList(config1, config2));
335-
StatefulRedisMultiDbConnection<String, String> connection = testClient.connect();
336-
337-
try {
338-
// Then: Connection should work normally
339-
assertThat(connection.sync().ping()).isEqualTo("PONG");
340-
assertThat(connection.getCurrentEndpoint()).isNotNull();
341-
342-
// And: Should be able to execute commands
343-
connection.sync().set("provider-test-key", "provider-test-value");
344-
assertThat(connection.sync().get("provider-test-key")).isEqualTo("provider-test-value");
345-
346-
// And: Health checks should be running using the same connection
347-
await().pollDelay(Duration.ofMillis(100)).atMost(Duration.ofMillis(500)).untilAsserted(() -> {
348-
assertThat(connection.sync().ping()).isEqualTo("PONG");
349-
});
350-
351-
} finally {
352-
connection.close();
353-
testClient.shutdown();
354-
}
355-
}
356-
357326
private static class TestDatabaseRawConnectionFactoryImpl implements DatabaseRawConnectionFactory {
358327

359328
private final RedisClient client;
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
package io.lettuce.core.failover.health;
2+
3+
import static org.assertj.core.api.Assertions.*;
4+
import static org.mockito.Mockito.*;
5+
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.DisplayName;
8+
import org.junit.jupiter.api.Nested;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.extension.ExtendWith;
11+
import org.mockito.Mock;
12+
import org.mockito.junit.jupiter.MockitoExtension;
13+
14+
import io.lettuce.core.RedisURI;
15+
import io.lettuce.core.api.StatefulRedisConnection;
16+
import io.lettuce.core.api.sync.RedisCommands;
17+
import io.lettuce.core.failover.DatabaseRawConnectionFactory;
18+
19+
/**
20+
* Unit tests for {@link PingStrategy}.
21+
*
22+
* @author Ivo Gaydazhiev
23+
*/
24+
@ExtendWith(MockitoExtension.class)
25+
@DisplayName("PingStrategy Unit Tests")
26+
class PingStrategyTest {
27+
28+
@Mock
29+
private DatabaseRawConnectionFactory connectionFactory;
30+
31+
@Mock
32+
private StatefulRedisConnection<?, ?> connection;
33+
34+
@Mock
35+
private RedisCommands<?, ?> syncCommands;
36+
37+
private RedisURI testUri;
38+
39+
@BeforeEach
40+
void setUp() {
41+
testUri = RedisURI.builder().withHost("localhost").withPort(6379).build();
42+
}
43+
44+
@Nested
45+
@DisplayName("Constructor Tests")
46+
class ConstructorTests {
47+
48+
@Test
49+
@DisplayName("Should create PingStrategy with default config")
50+
void shouldCreateWithDefaultConfig() {
51+
// When
52+
PingStrategy strategy = new PingStrategy(testUri, connectionFactory);
53+
54+
// Then
55+
assertThat(strategy).isNotNull();
56+
assertThat(strategy.getInterval()).isEqualTo(HealthCheckStrategy.Config.create().getInterval());
57+
assertThat(strategy.getTimeout()).isEqualTo(HealthCheckStrategy.Config.create().getTimeout());
58+
assertThat(strategy.getNumProbes()).isEqualTo(HealthCheckStrategy.Config.create().getNumProbes());
59+
assertThat(strategy.getDelayInBetweenProbes())
60+
.isEqualTo(HealthCheckStrategy.Config.create().getDelayInBetweenProbes());
61+
assertThat(strategy.getPolicy()).isEqualTo(HealthCheckStrategy.Config.create().getPolicy());
62+
}
63+
64+
@Test
65+
@DisplayName("Should create PingStrategy with custom config")
66+
void shouldCreateWithCustomConfig() {
67+
// Given
68+
HealthCheckStrategy.Config customConfig = HealthCheckStrategy.Config.builder().interval(500).timeout(2000)
69+
.numProbes(3).delayInBetweenProbes(100).policy(ProbingPolicy.BuiltIn.MAJORITY_SUCCESS).build();
70+
71+
// When
72+
PingStrategy strategy = new PingStrategy(testUri, connectionFactory, customConfig);
73+
74+
// Then
75+
assertThat(strategy.getInterval()).isEqualTo(500);
76+
assertThat(strategy.getTimeout()).isEqualTo(2000);
77+
assertThat(strategy.getNumProbes()).isEqualTo(3);
78+
assertThat(strategy.getDelayInBetweenProbes()).isEqualTo(100);
79+
assertThat(strategy.getPolicy()).isEqualTo(ProbingPolicy.BuiltIn.MAJORITY_SUCCESS);
80+
}
81+
82+
}
83+
84+
@Nested
85+
@DisplayName("Health Check Tests")
86+
class HealthCheckTests {
87+
88+
@Test
89+
@DisplayName("Should return HEALTHY when PING returns PONG")
90+
@SuppressWarnings("unchecked")
91+
void shouldReturnHealthyWhenPingSucceeds() {
92+
// Given
93+
when(connectionFactory.connectToDatabase(testUri)).thenReturn((StatefulRedisConnection) connection);
94+
when(connection.sync()).thenReturn((RedisCommands) syncCommands);
95+
when(syncCommands.ping()).thenReturn("PONG");
96+
97+
PingStrategy strategy = new PingStrategy(testUri, connectionFactory);
98+
99+
// When
100+
HealthStatus status = strategy.doHealthCheck(testUri);
101+
102+
// Then
103+
assertThat(status).isEqualTo(HealthStatus.HEALTHY);
104+
verify(connectionFactory).connectToDatabase(testUri);
105+
verify(connection).sync();
106+
verify(syncCommands).ping();
107+
verify(connection).close();
108+
}
109+
110+
@Test
111+
@DisplayName("Should return UNHEALTHY when PING returns non-PONG response")
112+
@SuppressWarnings("unchecked")
113+
void shouldReturnUnhealthyWhenPingReturnsNonPong() {
114+
// Given
115+
when(connectionFactory.connectToDatabase(testUri)).thenReturn((StatefulRedisConnection) connection);
116+
when(connection.sync()).thenReturn((RedisCommands) syncCommands);
117+
when(syncCommands.ping()).thenReturn("UNEXPECTED");
118+
119+
PingStrategy strategy = new PingStrategy(testUri, connectionFactory);
120+
121+
// When
122+
HealthStatus status = strategy.doHealthCheck(testUri);
123+
124+
// Then
125+
assertThat(status).isEqualTo(HealthStatus.UNHEALTHY);
126+
verify(connection).close();
127+
}
128+
129+
@Test
130+
@DisplayName("Should return UNHEALTHY when connection is null")
131+
void shouldReturnUnhealthyWhenConnectionIsNull() {
132+
// Given
133+
when(connectionFactory.connectToDatabase(testUri)).thenReturn(null);
134+
135+
PingStrategy strategy = new PingStrategy(testUri, connectionFactory);
136+
137+
// When
138+
HealthStatus status = strategy.doHealthCheck(testUri);
139+
140+
// Then
141+
assertThat(status).isEqualTo(HealthStatus.UNHEALTHY);
142+
verify(connectionFactory).connectToDatabase(testUri);
143+
verifyNoInteractions(connection);
144+
}
145+
146+
@Test
147+
@DisplayName("Should return UNHEALTHY when connection throws exception")
148+
void shouldReturnUnhealthyWhenConnectionThrowsException() {
149+
// Given
150+
when(connectionFactory.connectToDatabase(testUri)).thenThrow(new RuntimeException("Connection failed"));
151+
152+
PingStrategy strategy = new PingStrategy(testUri, connectionFactory);
153+
154+
// When
155+
HealthStatus status = strategy.doHealthCheck(testUri);
156+
157+
// Then
158+
assertThat(status).isEqualTo(HealthStatus.UNHEALTHY);
159+
}
160+
161+
@Test
162+
@DisplayName("Should return UNHEALTHY when PING throws exception")
163+
@SuppressWarnings("unchecked")
164+
void shouldReturnUnhealthyWhenPingThrowsException() {
165+
// Given
166+
when(connectionFactory.connectToDatabase(testUri)).thenReturn((StatefulRedisConnection) connection);
167+
when(connection.sync()).thenReturn((RedisCommands) syncCommands);
168+
when(syncCommands.ping()).thenThrow(new RuntimeException("PING failed"));
169+
170+
PingStrategy strategy = new PingStrategy(testUri, connectionFactory);
171+
172+
// When
173+
HealthStatus status = strategy.doHealthCheck(testUri);
174+
175+
// Then
176+
assertThat(status).isEqualTo(HealthStatus.UNHEALTHY);
177+
verify(connection).close();
178+
}
179+
180+
@Test
181+
@DisplayName("Should close connection even when PING throws exception")
182+
@SuppressWarnings("unchecked")
183+
void shouldCloseConnectionWhenPingThrowsException() {
184+
// Given
185+
when(connectionFactory.connectToDatabase(testUri)).thenReturn((StatefulRedisConnection) connection);
186+
when(connection.sync()).thenReturn((RedisCommands) syncCommands);
187+
when(syncCommands.ping()).thenThrow(new RuntimeException("PING failed"));
188+
189+
PingStrategy strategy = new PingStrategy(testUri, connectionFactory);
190+
191+
// When
192+
strategy.doHealthCheck(testUri);
193+
194+
// Then
195+
verify(connection).close();
196+
}
197+
198+
@Test
199+
@DisplayName("Should handle multiple health checks")
200+
@SuppressWarnings("unchecked")
201+
void shouldHandleMultipleHealthChecks() {
202+
// Given
203+
when(connectionFactory.connectToDatabase(testUri)).thenReturn((StatefulRedisConnection) connection);
204+
when(connection.sync()).thenReturn((RedisCommands) syncCommands);
205+
when(syncCommands.ping()).thenReturn("PONG");
206+
207+
PingStrategy strategy = new PingStrategy(testUri, connectionFactory);
208+
209+
// When
210+
HealthStatus status1 = strategy.doHealthCheck(testUri);
211+
HealthStatus status2 = strategy.doHealthCheck(testUri);
212+
HealthStatus status3 = strategy.doHealthCheck(testUri);
213+
214+
// Then
215+
assertThat(status1).isEqualTo(HealthStatus.HEALTHY);
216+
assertThat(status2).isEqualTo(HealthStatus.HEALTHY);
217+
assertThat(status3).isEqualTo(HealthStatus.HEALTHY);
218+
verify(connectionFactory, times(3)).connectToDatabase(testUri);
219+
verify(connection, times(3)).close();
220+
}
221+
222+
}
223+
224+
@Nested
225+
@DisplayName("Default Supplier Tests")
226+
class DefaultSupplierTests {
227+
228+
@Test
229+
@DisplayName("Should create PingStrategy using DEFAULT supplier")
230+
void shouldCreateUsingDefaultSupplier() {
231+
// Given
232+
HealthCheckStrategySupplier supplier = PingStrategy.DEFAULT;
233+
234+
// When
235+
HealthCheckStrategy strategy = supplier.get(testUri, connectionFactory);
236+
237+
// Then
238+
assertThat(strategy).isInstanceOf(PingStrategy.class);
239+
}
240+
241+
}
242+
243+
}

0 commit comments

Comments
 (0)