|
20 | 20 | import java.io.IOException;
|
21 | 21 | import java.net.URI;
|
22 | 22 | import java.util.ArrayList;
|
| 23 | +import java.util.Collections; |
23 | 24 | import java.util.HashMap;
|
24 | 25 | import java.util.Iterator;
|
25 | 26 | import java.util.LinkedHashMap;
|
@@ -332,6 +333,61 @@ public void shouldUseNextUriFor_500_And_ALWAYS_Strategy() throws Exception {
|
332 | 333 | assertNextUriIsTried(ConfigClientProperties.MultipleUriStrategy.ALWAYS, HttpStatus.INTERNAL_SERVER_ERROR);
|
333 | 334 | }
|
334 | 335 |
|
| 336 | + @Test |
| 337 | + public void shouldUseNextUriFor_NoExceptionNotOK_And_CONNECTION_TIMEOUT_ONLY_Strategy_FailFastIsFalse() |
| 338 | + throws Exception { |
| 339 | + // At the time of this writing, TEMPORARY_REDIRECT will not cause an exception to |
| 340 | + // be thrown back to |
| 341 | + // getRemoteEnvironment, but since status is not OK, the method returns null and |
| 342 | + // locate method will |
| 343 | + // simply return null since fail-fast=false. (Second URL is never tried, due to |
| 344 | + // the strategy. |
| 345 | + |
| 346 | + // Set up with two URIs. |
| 347 | + ConfigClientProperties clientProperties = new ConfigClientProperties(this.environment); |
| 348 | + String badURI = "http://baduri"; |
| 349 | + String goodURI = "http://localhost:8888"; |
| 350 | + String[] uris = new String[] { badURI, goodURI }; |
| 351 | + clientProperties.setUri(uris); |
| 352 | + clientProperties.setFailFast(false); |
| 353 | + // Strategy is CONNECTION_TIMEOUT_ONLY, so it should not try the next URI for |
| 354 | + // TEMPORARY_REDIRECT |
| 355 | + clientProperties.setMultipleUriStrategy(ConfigClientProperties.MultipleUriStrategy.CONNECTION_TIMEOUT_ONLY); |
| 356 | + this.locator = new ConfigServicePropertySourceLocator(clientProperties); |
| 357 | + ClientHttpRequestFactory requestFactory = Mockito.mock(ClientHttpRequestFactory.class); |
| 358 | + RestTemplate restTemplate1 = new RestTemplate(requestFactory); |
| 359 | + mockRequestResponse(requestFactory, badURI, HttpStatus.TEMPORARY_REDIRECT); |
| 360 | + mockRequestResponse(requestFactory, goodURI, HttpStatus.OK); |
| 361 | + this.locator.setRestTemplate(restTemplate1); |
| 362 | + assertThat(this.locator.locateCollection(this.environment)).isEqualTo(Collections.emptyList()); |
| 363 | + } |
| 364 | + |
| 365 | + @Test |
| 366 | + public void shouldUseNextUriFor_NoExceptionNotOK_And_CONNECTION_TIMEOUT_ONLY_Strategy_WithFailFastIsTrue() |
| 367 | + throws Exception { |
| 368 | + // At the time of this writing, TEMPORARY_REDIRECT will not cause an exception to |
| 369 | + // be thrown back to |
| 370 | + // getRemoteEnvironment, but since status is not OK, the method returns null and |
| 371 | + // locate method will |
| 372 | + // throw an IllegalStateException with no cause, since fail-fast=true. Second URL |
| 373 | + // is never tried, due to the strategy. |
| 374 | + assertNextUriIsNotTried(true, ConfigClientProperties.MultipleUriStrategy.CONNECTION_TIMEOUT_ONLY, |
| 375 | + HttpStatus.TEMPORARY_REDIRECT, null // IllegalStateException has no cause, |
| 376 | + // because getRemoteEnvironment did |
| 377 | + // not throw an exception |
| 378 | + ); |
| 379 | + } |
| 380 | + |
| 381 | + @Test |
| 382 | + public void shouldUseNextUriFor_NoExceptionNotOK_And_ALWAYS_Strategy() throws Exception { |
| 383 | + // At the time of this writing, TEMPORARY_REDIRECT will not cause an exception to |
| 384 | + // be thrown back to |
| 385 | + // getRemoteEnvironment, but since status is not OK, the method will treat it the |
| 386 | + // same as an exception and |
| 387 | + // thus try the next URL. |
| 388 | + assertNextUriIsTried(ConfigClientProperties.MultipleUriStrategy.ALWAYS, HttpStatus.TEMPORARY_REDIRECT); |
| 389 | + } |
| 390 | + |
335 | 391 | @Test
|
336 | 392 | public void shouldUseNextUriFor_TimeOut_And_ALWAYS_Strategy() throws Exception {
|
337 | 393 | assertNextUriIsTriedOnTimeout(ConfigClientProperties.MultipleUriStrategy.ALWAYS);
|
@@ -459,14 +515,20 @@ public void shouldPreserveOrder() {
|
459 | 515 |
|
460 | 516 | private void assertNextUriIsNotTried(ConfigClientProperties.MultipleUriStrategy multipleUriStrategy,
|
461 | 517 | HttpStatus firstUriResponse, Class<? extends Exception> expectedCause) {
|
| 518 | + assertNextUriIsNotTried(true, multipleUriStrategy, firstUriResponse, expectedCause); |
| 519 | + } |
| 520 | + |
| 521 | + private void assertNextUriIsNotTried(boolean failFast, |
| 522 | + ConfigClientProperties.MultipleUriStrategy multipleUriStrategy, HttpStatus firstUriResponse, |
| 523 | + Class<? extends Exception> expectedCause) { |
462 | 524 | AbstractThrowableAssert throwableAssert = Assertions.assertThatThrownBy(() -> {
|
463 | 525 | // Set up with two URIs.
|
464 | 526 | ConfigClientProperties clientProperties = new ConfigClientProperties(this.environment);
|
465 | 527 | String badURI = "http://baduri";
|
466 | 528 | String goodURI = "http://localhost:8888";
|
467 | 529 | String[] uris = new String[] { badURI, goodURI };
|
468 | 530 | clientProperties.setUri(uris);
|
469 |
| - clientProperties.setFailFast(true); |
| 531 | + clientProperties.setFailFast(failFast); |
470 | 532 | // Strategy is CONNECTION_TIMEOUT_ONLY, so it should not try the next URI for
|
471 | 533 | // INTERNAL_SERVER_ERROR
|
472 | 534 | clientProperties.setMultipleUriStrategy(multipleUriStrategy);
|
|
0 commit comments