|
23 | 23 | import org.apache.shenyu.common.enums.RpcTypeEnum; |
24 | 24 | import org.apache.shenyu.common.enums.UniqueHeaderEnum; |
25 | 25 | import org.apache.shenyu.common.enums.RetryEnum; |
| 26 | +import org.apache.shenyu.loadbalancer.cache.UpstreamCacheManager; |
| 27 | +import org.apache.shenyu.loadbalancer.entity.Upstream; |
26 | 28 | import org.apache.shenyu.plugin.httpclient.exception.ShenyuUpstreamStatusException; |
27 | 29 | import org.apache.shenyu.plugin.api.ShenyuPluginChain; |
28 | 30 | import org.apache.shenyu.plugin.api.context.ShenyuContext; |
|
55 | 57 | import org.springframework.web.reactive.function.client.ClientResponse; |
56 | 58 | import org.springframework.web.reactive.function.client.ExchangeFunction; |
57 | 59 | import org.springframework.web.reactive.function.client.WebClient; |
| 60 | +import org.springframework.web.server.ResponseStatusException; |
58 | 61 | import org.springframework.web.server.ServerWebExchange; |
59 | 62 | import reactor.core.publisher.Mono; |
60 | 63 | import reactor.core.publisher.Flux; |
|
69 | 72 | import static org.junit.jupiter.api.Assertions.assertFalse; |
70 | 73 | import static org.junit.jupiter.api.Assertions.assertTrue; |
71 | 74 | import static org.mockito.BDDMockito.given; |
| 75 | +import static org.mockito.ArgumentMatchers.any; |
72 | 76 | import static org.mockito.Mockito.mock; |
| 77 | +import static org.mockito.Mockito.times; |
| 78 | +import static org.mockito.Mockito.verify; |
| 79 | +import static org.mockito.Mockito.verifyNoInteractions; |
73 | 80 | import static org.mockito.Mockito.when; |
74 | 81 |
|
75 | 82 | /** |
@@ -218,6 +225,40 @@ public void testServerErrorTriggersFailover() { |
218 | 225 | .verify(); |
219 | 226 | } |
220 | 227 |
|
| 228 | + @Test |
| 229 | + public void testServerErrorAfterUpstreamRemoval() { |
| 230 | + final String selectorId = "webclient-removed-upstream"; |
| 231 | + final UpstreamCacheManager cacheManager = UpstreamCacheManager.getInstance(); |
| 232 | + final Upstream upstream = Upstream.builder().url("localhost:8080").healthCheckEnabled(false).build(); |
| 233 | + cacheManager.submit(selectorId, Collections.singletonList(upstream)); |
| 234 | + final ServerWebExchange exchange = generateServerWebExchange(); |
| 235 | + exchange.getAttributes().put(Constants.HTTP_URI, URI.create("http://localhost:8080/test")); |
| 236 | + exchange.getAttributes().put(Constants.RETRY_STRATEGY, RetryEnum.FAILOVER.getName()); |
| 237 | + exchange.getAttributes().put(Constants.HTTP_RETRY, 1); |
| 238 | + exchange.getAttributes().put(Constants.DIVIDE_SELECTOR_ID, selectorId); |
| 239 | + final ShenyuPluginChain chain = mock(ShenyuPluginChain.class); |
| 240 | + when(exchangeFunction.exchange(any())).thenAnswer(invocation -> Mono.defer(() -> { |
| 241 | + cacheManager.removeByKey(selectorId); |
| 242 | + return Mono.just(ClientResponse.create(HttpStatus.INTERNAL_SERVER_ERROR).build()); |
| 243 | + })); |
| 244 | + final WebClientPlugin plugin = new WebClientPlugin(WebClient.builder().exchangeFunction(exchangeFunction).build(), Constants.BYTES_PER_MB); |
| 245 | + |
| 246 | + try { |
| 247 | + StepVerifier.create(plugin.execute(exchange, chain)) |
| 248 | + .expectErrorSatisfies(error -> { |
| 249 | + assertTrue(error instanceof ResponseStatusException); |
| 250 | + ResponseStatusException exception = (ResponseStatusException) error; |
| 251 | + assertEquals(HttpStatus.SERVICE_UNAVAILABLE, exception.getStatusCode()); |
| 252 | + assertEquals("CANNOT_FIND_HEALTHY_UPSTREAM_URL_AFTER_FAILOVER", exception.getReason()); |
| 253 | + }) |
| 254 | + .verify(); |
| 255 | + verify(exchangeFunction, times(1)).exchange(any()); |
| 256 | + verifyNoInteractions(chain); |
| 257 | + } finally { |
| 258 | + cacheManager.removeByKey(selectorId); |
| 259 | + } |
| 260 | + } |
| 261 | + |
221 | 262 | @ParameterizedTest |
222 | 263 | @EnumSource(HeaderUniqueStrategyEnum.class) |
223 | 264 | public void testOutboundHeaderDeduplication(final HeaderUniqueStrategyEnum strategy) { |
|
0 commit comments