|
28 | 28 |
|
29 | 29 | import java.util.ArrayList; |
30 | 30 | import java.util.List; |
| 31 | +import java.util.Objects; |
31 | 32 |
|
32 | 33 | /** |
33 | 34 | * The type UpstreamCacheManager check task test. |
@@ -106,4 +107,188 @@ public void testSubmitSyncsHealthCheckEnabled() { |
106 | 107 | // Clean up |
107 | 108 | upstreamCacheManager.removeByKey(testSelectorId); |
108 | 109 | } |
| 110 | + |
| 111 | + @Test |
| 112 | + @Order(6) |
| 113 | + public void testSubmitWithStatusFalsePreservesUnhealthyState() { |
| 114 | + final UpstreamCacheManager upstreamCacheManager = UpstreamCacheManager.getInstance(); |
| 115 | + final String testSelectorId = "PRESERVE_UNHEALTHY_TEST"; |
| 116 | + |
| 117 | + // First, submit healthy upstreams to establish baseline |
| 118 | + List<Upstream> initialList = new ArrayList<>(2); |
| 119 | + initialList.add(Upstream.builder() |
| 120 | + .protocol("http://") |
| 121 | + .url("upstream1:8080") |
| 122 | + .status(true) |
| 123 | + .healthCheckEnabled(true) |
| 124 | + .build()); |
| 125 | + initialList.add(Upstream.builder() |
| 126 | + .protocol("http://") |
| 127 | + .url("upstream2:8080") |
| 128 | + .status(true) |
| 129 | + .healthCheckEnabled(true) |
| 130 | + .build()); |
| 131 | + upstreamCacheManager.submit(testSelectorId, initialList); |
| 132 | + |
| 133 | + // Simulate health check marking one as unhealthy |
| 134 | + UpstreamCheckTask task = getUpstreamCheckTask(upstreamCacheManager); |
| 135 | + if (Objects.nonNull(task)) { |
| 136 | + Upstream unhealthyUpstream = initialList.get(0); |
| 137 | + unhealthyUpstream.setHealthy(false); |
| 138 | + task.putToMap(task.getUnhealthyUpstream(), testSelectorId, unhealthyUpstream); |
| 139 | + task.removeFromMap(task.getHealthyUpstream(), testSelectorId, unhealthyUpstream); |
| 140 | + |
| 141 | + // Verify it's in unhealthy map |
| 142 | + Assertions.assertNotNull(task.getUnhealthyUpstream().get(testSelectorId)); |
| 143 | + Assertions.assertTrue(task.getUnhealthyUpstream().get(testSelectorId).stream() |
| 144 | + .anyMatch(u -> u.getUrl().equals("upstream1:8080"))); |
| 145 | + } |
| 146 | + |
| 147 | + // Now admin sends update with status=false for that upstream |
| 148 | + List<Upstream> updateList = new ArrayList<>(2); |
| 149 | + updateList.add(Upstream.builder() |
| 150 | + .protocol("http://") |
| 151 | + .url("upstream1:8080") |
| 152 | + .status(false) |
| 153 | + .healthCheckEnabled(true) |
| 154 | + .build()); |
| 155 | + updateList.add(Upstream.builder() |
| 156 | + .protocol("http://") |
| 157 | + .url("upstream2:8080") |
| 158 | + .status(true) |
| 159 | + .healthCheckEnabled(true) |
| 160 | + .build()); |
| 161 | + upstreamCacheManager.submit(testSelectorId, updateList); |
| 162 | + |
| 163 | + // Verify: upstream1 should still be in unhealthy map (preserved state) |
| 164 | + if (Objects.nonNull(task)) { |
| 165 | + List<Upstream> unhealthyList = task.getUnhealthyUpstream().get(testSelectorId); |
| 166 | + Assertions.assertNotNull(unhealthyList); |
| 167 | + Assertions.assertTrue(unhealthyList.stream() |
| 168 | + .anyMatch(u -> u.getUrl().equals("upstream1:8080")), |
| 169 | + "upstream1 should be preserved in unhealthy map"); |
| 170 | + } |
| 171 | + |
| 172 | + // Clean up |
| 173 | + upstreamCacheManager.removeByKey(testSelectorId); |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + @Order(7) |
| 178 | + public void testSubmitWithNewOfflineUpstreamAddedToUnhealthy() { |
| 179 | + final UpstreamCacheManager upstreamCacheManager = UpstreamCacheManager.getInstance(); |
| 180 | + final String testSelectorId = "NEW_OFFLINE_UNHEALTHY_TEST"; |
| 181 | + |
| 182 | + // Submit a list with a new upstream having status=false |
| 183 | + List<Upstream> upstreamList = new ArrayList<>(1); |
| 184 | + upstreamList.add(Upstream.builder() |
| 185 | + .protocol("http://") |
| 186 | + .url("new-upstream:8080") |
| 187 | + .status(false) |
| 188 | + .healthCheckEnabled(true) |
| 189 | + .build()); |
| 190 | + upstreamCacheManager.submit(testSelectorId, upstreamList); |
| 191 | + |
| 192 | + // Verify: new upstream with status=false should be in unhealthy map for monitoring |
| 193 | + UpstreamCheckTask task = getUpstreamCheckTask(upstreamCacheManager); |
| 194 | + if (Objects.nonNull(task)) { |
| 195 | + List<Upstream> unhealthyList = task.getUnhealthyUpstream().get(testSelectorId); |
| 196 | + Assertions.assertNotNull(unhealthyList); |
| 197 | + Assertions.assertTrue(unhealthyList.stream() |
| 198 | + .anyMatch(u -> u.getUrl().equals("new-upstream:8080")), |
| 199 | + "New upstream with status=false should be in unhealthy map"); |
| 200 | + } |
| 201 | + |
| 202 | + // Clean up |
| 203 | + upstreamCacheManager.removeByKey(testSelectorId); |
| 204 | + } |
| 205 | + |
| 206 | + @Test |
| 207 | + @Order(8) |
| 208 | + public void testSubmitPreservesUnhealthyForValidUpstream() { |
| 209 | + final UpstreamCacheManager upstreamCacheManager = UpstreamCacheManager.getInstance(); |
| 210 | + final String testSelectorId = "PRESERVE_UNHEALTHY_VALID_TEST"; |
| 211 | + |
| 212 | + // First submit and mark an upstream as unhealthy |
| 213 | + List<Upstream> initialList = new ArrayList<>(1); |
| 214 | + initialList.add(Upstream.builder() |
| 215 | + .protocol("http://") |
| 216 | + .url("recovering-upstream:8080") |
| 217 | + .status(true) |
| 218 | + .healthCheckEnabled(true) |
| 219 | + .build()); |
| 220 | + upstreamCacheManager.submit(testSelectorId, initialList); |
| 221 | + |
| 222 | + UpstreamCheckTask task = getUpstreamCheckTask(upstreamCacheManager); |
| 223 | + if (Objects.nonNull(task)) { |
| 224 | + // Manually mark as unhealthy |
| 225 | + Upstream unhealthyUpstream = initialList.get(0); |
| 226 | + unhealthyUpstream.setHealthy(false); |
| 227 | + task.putToMap(task.getUnhealthyUpstream(), testSelectorId, unhealthyUpstream); |
| 228 | + task.removeFromMap(task.getHealthyUpstream(), testSelectorId, unhealthyUpstream); |
| 229 | + |
| 230 | + // Now admin sends update with status=true (valid) for the same upstream |
| 231 | + List<Upstream> updateList = new ArrayList<>(1); |
| 232 | + updateList.add(Upstream.builder() |
| 233 | + .protocol("http://") |
| 234 | + .url("recovering-upstream:8080") |
| 235 | + .status(true) |
| 236 | + .healthCheckEnabled(true) |
| 237 | + .build()); |
| 238 | + upstreamCacheManager.submit(testSelectorId, updateList); |
| 239 | + |
| 240 | + // Verify: should preserve unhealthy state since it was previously unhealthy |
| 241 | + List<Upstream> unhealthyList = task.getUnhealthyUpstream().get(testSelectorId); |
| 242 | + Assertions.assertNotNull(unhealthyList); |
| 243 | + Assertions.assertTrue(unhealthyList.stream() |
| 244 | + .anyMatch(u -> u.getUrl().equals("recovering-upstream:8080")), |
| 245 | + "Previously unhealthy upstream should remain in unhealthy map"); |
| 246 | + } |
| 247 | + |
| 248 | + // Clean up |
| 249 | + upstreamCacheManager.removeByKey(testSelectorId); |
| 250 | + } |
| 251 | + |
| 252 | + @Test |
| 253 | + @Order(9) |
| 254 | + public void testSubmitWithHealthCheckDisabledAndStatusFalse() { |
| 255 | + final UpstreamCacheManager upstreamCacheManager = UpstreamCacheManager.getInstance(); |
| 256 | + final String testSelectorId = "HEALTH_CHECK_DISABLED_STATUS_FALSE_TEST"; |
| 257 | + |
| 258 | + // Submit upstream with healthCheckEnabled=false and status=false |
| 259 | + // This upstream should be removed, not added to unhealthy map |
| 260 | + List<Upstream> upstreamList = new ArrayList<>(1); |
| 261 | + upstreamList.add(Upstream.builder() |
| 262 | + .protocol("http://") |
| 263 | + .url("no-check-upstream:8080") |
| 264 | + .status(false) |
| 265 | + .healthCheckEnabled(false) |
| 266 | + .build()); |
| 267 | + upstreamCacheManager.submit(testSelectorId, upstreamList); |
| 268 | + |
| 269 | + UpstreamCheckTask task = getUpstreamCheckTask(upstreamCacheManager); |
| 270 | + if (Objects.nonNull(task)) { |
| 271 | + // Verify: should NOT be in unhealthy map since health check is disabled |
| 272 | + List<Upstream> unhealthyList = task.getUnhealthyUpstream().get(testSelectorId); |
| 273 | + Assertions.assertTrue(Objects.isNull(unhealthyList) || unhealthyList.isEmpty(), |
| 274 | + "Upstream with healthCheckEnabled=false should not be in unhealthy map"); |
| 275 | + } |
| 276 | + |
| 277 | + // Clean up |
| 278 | + upstreamCacheManager.removeByKey(testSelectorId); |
| 279 | + } |
| 280 | + |
| 281 | + /** |
| 282 | + * Helper method to get the UpstreamCheckTask using reflection. |
| 283 | + */ |
| 284 | + private UpstreamCheckTask getUpstreamCheckTask(final UpstreamCacheManager manager) { |
| 285 | + try { |
| 286 | + java.lang.reflect.Field field = UpstreamCacheManager.class.getDeclaredField("task"); |
| 287 | + field.setAccessible(true); |
| 288 | + return (UpstreamCheckTask) field.get(manager); |
| 289 | + } catch (Exception e) { |
| 290 | + // If reflection fails, return null |
| 291 | + return null; |
| 292 | + } |
| 293 | + } |
109 | 294 | } |
0 commit comments