Skip to content

Commit 5dd1ec0

Browse files
committed
format
1 parent 72c5bad commit 5dd1ec0

File tree

1 file changed

+105
-112
lines changed

1 file changed

+105
-112
lines changed

src/test/java/redis/clients/jedis/mcf/EchoStrategyIntegrationTest.java

Lines changed: 105 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -22,119 +22,112 @@
2222
@Tag("failover")
2323
public class EchoStrategyIntegrationTest {
2424

25-
private static final EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("redis-failover-1");
26-
private static final HostAndPort proxyHostAndPort = endpoint.getHostAndPort();
27-
private static final ToxiproxyClient tp = new ToxiproxyClient("localhost", 8474);
28-
private static Proxy redisProxy;
29-
30-
@BeforeAll
31-
public static void setupProxy() throws IOException {
32-
if (tp.getProxyOrNull("redis-health-test") != null) {
33-
tp.getProxy("redis-health-test").delete();
25+
private static final EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("redis-failover-1");
26+
private static final HostAndPort proxyHostAndPort = endpoint.getHostAndPort();
27+
private static final ToxiproxyClient tp = new ToxiproxyClient("localhost", 8474);
28+
private static Proxy redisProxy;
29+
30+
@BeforeAll
31+
public static void setupProxy() throws IOException {
32+
if (tp.getProxyOrNull("redis-health-test") != null) {
33+
tp.getProxy("redis-health-test").delete();
34+
}
35+
redisProxy = tp.createProxy("redis-health-test", "0.0.0.0:29379", "redis-failover-1:9379");
3436
}
35-
redisProxy = tp.createProxy("redis-health-test", "0.0.0.0:29379", "redis-failover-1:9379");
36-
}
3737

38-
@AfterAll
39-
public static void cleanupProxy() throws IOException {
40-
if (redisProxy != null) {
41-
redisProxy.delete();
38+
@AfterAll
39+
public static void cleanupProxy() throws IOException {
40+
if (redisProxy != null) {
41+
redisProxy.delete();
42+
}
43+
}
44+
45+
@BeforeEach
46+
public void resetProxy() throws IOException {
47+
redisProxy.enable();
48+
redisProxy.toxics().getAll().forEach(toxic -> {
49+
try {
50+
toxic.remove();
51+
} catch (IOException e) {
52+
throw new RuntimeException(e);
53+
}
54+
});
55+
}
56+
57+
@Test
58+
public void testEchoStrategyRecoversAfterDisconnect() throws Exception {
59+
JedisClientConfig config = DefaultJedisClientConfig.builder().socketTimeoutMillis(1000)
60+
.connectionTimeoutMillis(1000).build();
61+
62+
UnifiedJedis jedis = new UnifiedJedis(proxyHostAndPort, config);
63+
jedis.ping();
64+
EchoStrategy strategy = new EchoStrategy(proxyHostAndPort, config, 1000, 500);
65+
66+
// Initial health check should work
67+
HealthStatus initialStatus = strategy.doHealthCheck(proxyHostAndPort);
68+
assertEquals(HealthStatus.HEALTHY, initialStatus);
69+
70+
// Disable the proxy to simulate network failure
71+
redisProxy.disable();
72+
73+
// Health check should now fail - this will expose the bug
74+
HealthStatus statusAfterDisable = strategy.doHealthCheck(proxyHostAndPort);
75+
assertEquals(HealthStatus.UNHEALTHY, statusAfterDisable);
76+
77+
// Re-enable proxy
78+
redisProxy.enable();
79+
// Health check should recover
80+
HealthStatus statusAfterEnable = strategy.doHealthCheck(proxyHostAndPort);
81+
assertEquals(HealthStatus.HEALTHY, statusAfterEnable);
82+
}
83+
84+
@Test
85+
public void testEchoStrategyWithConnectionTimeout() throws Exception {
86+
JedisClientConfig config = DefaultJedisClientConfig.builder().socketTimeoutMillis(100)
87+
.connectionTimeoutMillis(100).build();
88+
89+
EchoStrategy strategy = new EchoStrategy(proxyHostAndPort, config, 1000, 300);
90+
91+
// Initial health check should work
92+
assertEquals(HealthStatus.HEALTHY, strategy.doHealthCheck(proxyHostAndPort));
93+
94+
// Add latency toxic to simulate slow network
95+
redisProxy.toxics().latency("slow-connection", ToxicDirection.DOWNSTREAM, 1000);
96+
97+
// Health check should timeout and return unhealthy
98+
HealthStatus slowStatus = strategy.doHealthCheck(proxyHostAndPort);
99+
assertEquals(HealthStatus.UNHEALTHY, slowStatus, "Health check should fail with high latency");
100+
101+
// Remove toxic
102+
redisProxy.toxics().get("slow-connection").remove();
103+
104+
// Health check should recover
105+
HealthStatus recoveredStatus = strategy.doHealthCheck(proxyHostAndPort);
106+
assertEquals(HealthStatus.HEALTHY, recoveredStatus, "Health check should recover from high latency");
107+
}
108+
109+
@Test
110+
public void testConnectionDropDuringHealthCheck() throws Exception {
111+
JedisClientConfig config = DefaultJedisClientConfig.builder().socketTimeoutMillis(2000).build();
112+
113+
EchoStrategy strategy = new EchoStrategy(proxyHostAndPort, config, 1000, 1500);
114+
115+
// Initial health check
116+
assertEquals(HealthStatus.HEALTHY, strategy.doHealthCheck(proxyHostAndPort));
117+
118+
// Simulate connection drop by limiting data transfer
119+
redisProxy.toxics().limitData("connection-drop", ToxicDirection.UPSTREAM, 10);
120+
121+
// This should fail due to connection issues
122+
HealthStatus droppedStatus = strategy.doHealthCheck(proxyHostAndPort);
123+
assertEquals(HealthStatus.UNHEALTHY, droppedStatus);
124+
125+
// Remove toxic
126+
redisProxy.toxics().get("connection-drop").remove();
127+
128+
// Health check should recover
129+
HealthStatus afterRecovery = strategy.doHealthCheck(proxyHostAndPort);
130+
assertEquals(HealthStatus.HEALTHY, afterRecovery);
131+
42132
}
43-
}
44-
45-
@BeforeEach
46-
public void resetProxy() throws IOException {
47-
redisProxy.enable();
48-
redisProxy.toxics().getAll().forEach(toxic -> {
49-
try {
50-
toxic.remove();
51-
} catch (IOException e) {
52-
throw new RuntimeException(e);
53-
}
54-
});
55-
}
56-
57-
@Test
58-
public void testEchoStrategyRecoversAfterDisconnect() throws Exception {
59-
JedisClientConfig config = DefaultJedisClientConfig.builder()
60-
.socketTimeoutMillis(1000)
61-
.connectionTimeoutMillis(1000)
62-
.build();
63-
64-
UnifiedJedis jedis = new UnifiedJedis(proxyHostAndPort, config);
65-
jedis.ping();
66-
EchoStrategy strategy = new EchoStrategy(proxyHostAndPort, config, 1000, 500);
67-
68-
// Initial health check should work
69-
HealthStatus initialStatus = strategy.doHealthCheck(proxyHostAndPort);
70-
assertEquals(HealthStatus.HEALTHY, initialStatus);
71-
72-
// Disable the proxy to simulate network failure
73-
redisProxy.disable();
74-
75-
// Health check should now fail - this will expose the bug
76-
HealthStatus statusAfterDisable = strategy.doHealthCheck(proxyHostAndPort);
77-
assertEquals(HealthStatus.UNHEALTHY, statusAfterDisable);
78-
79-
80-
// Re-enable proxy
81-
redisProxy.enable();
82-
// Health check should recover
83-
HealthStatus statusAfterEnable = strategy.doHealthCheck(proxyHostAndPort);
84-
assertEquals(HealthStatus.HEALTHY, statusAfterEnable);
85-
}
86-
87-
@Test
88-
public void testEchoStrategyWithConnectionTimeout() throws Exception {
89-
JedisClientConfig config = DefaultJedisClientConfig.builder()
90-
.socketTimeoutMillis(100)
91-
.connectionTimeoutMillis(100)
92-
.build();
93-
94-
EchoStrategy strategy = new EchoStrategy(proxyHostAndPort, config, 1000, 300);
95-
96-
// Initial health check should work
97-
assertEquals(HealthStatus.HEALTHY, strategy.doHealthCheck(proxyHostAndPort));
98-
99-
// Add latency toxic to simulate slow network
100-
redisProxy.toxics().latency("slow-connection", ToxicDirection.DOWNSTREAM, 1000);
101-
102-
// Health check should timeout and return unhealthy
103-
HealthStatus slowStatus = strategy.doHealthCheck(proxyHostAndPort);
104-
assertEquals(HealthStatus.UNHEALTHY, slowStatus, "Health check should fail with high latency");
105-
106-
// Remove toxic
107-
redisProxy.toxics().get("slow-connection").remove();
108-
109-
// Health check should recover
110-
HealthStatus recoveredStatus = strategy.doHealthCheck(proxyHostAndPort);
111-
assertEquals(HealthStatus.HEALTHY, recoveredStatus, "Health check should recover from high latency");
112-
}
113-
114-
@Test
115-
public void testConnectionDropDuringHealthCheck() throws Exception {
116-
JedisClientConfig config = DefaultJedisClientConfig.builder()
117-
.socketTimeoutMillis(2000)
118-
.build();
119-
120-
EchoStrategy strategy = new EchoStrategy(proxyHostAndPort, config, 1000, 1500);
121-
122-
// Initial health check
123-
assertEquals(HealthStatus.HEALTHY, strategy.doHealthCheck(proxyHostAndPort));
124-
125-
// Simulate connection drop by limiting data transfer
126-
redisProxy.toxics().limitData("connection-drop", ToxicDirection.UPSTREAM, 10);
127-
128-
// This should fail due to connection issues
129-
HealthStatus droppedStatus = strategy.doHealthCheck(proxyHostAndPort);
130-
assertEquals(HealthStatus.UNHEALTHY, droppedStatus);
131-
132-
// Remove toxic
133-
redisProxy.toxics().get("connection-drop").remove();
134-
135-
// Health check should recover
136-
HealthStatus afterRecovery = strategy.doHealthCheck(proxyHostAndPort);
137-
assertEquals(HealthStatus.HEALTHY, afterRecovery);
138-
139-
}
140133
}

0 commit comments

Comments
 (0)