|
| 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