Support Retry in spring.config.import=configserver:

Fixes gh-1775
This commit is contained in:
spencergibb
2020-12-22 17:40:32 -05:00
parent cb2981d7a0
commit f840725c12
11 changed files with 417 additions and 24 deletions

View File

@@ -20,22 +20,27 @@ import org.junit.Test;
import org.springframework.boot.builder.SpringApplicationBuilder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class ApplicationFailFastTests {
@Test
public void contextFails() {
try {
public void bootstrapContextFails() {
assertThatThrownBy(() -> {
new SpringApplicationBuilder().sources(Application.class).run("--spring.config.use-legacy-processing=true",
"--server.port=0", "--spring.cloud.config.enabled=true", "--spring.cloud.config.fail-fast=true",
"--spring.cloud.config.uri=http://serverhostdoesnotexist:1234");
fail("failFast option did not produce an exception");
}
catch (Exception e) {
assertThat(e.getMessage().contains("fail fast")).as("Exception not caused by fail fast").isTrue();
}
}).as("Exception not caused by fail fast").hasMessageContaining("fail fast");
}
@Test
public void configDataContextFails() {
assertThatThrownBy(() -> {
new SpringApplicationBuilder().sources(Application.class).run("--server.port=0",
"--spring.cloud.config.enabled=true", "--spring.cloud.config.fail-fast=true",
"--spring.config.import=optional:configserver:http://serverhostdoesnotexist:1234");
}).as("Exception not caused by fail fast").hasMessageContaining("fail fast");
}
}

View File

@@ -0,0 +1,121 @@
/*
* Copyright 2018-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.config.server.test.ConfigServerTestUtils;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.SocketUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class,
// Normally spring.cloud.config.enabled:true is the default but since we have the
// config server on the classpath we need to set it explicitly
properties = { "spring.application.name=retryapp", "spring.cloud.config.fail-fast=true",
"spring.cloud.config.enabled=true", "spring.config.import=configserver:",
"management.security.enabled=false", "management.endpoints.web.exposure.include=*",
"logging.level.org.springframework.retry=TRACE" },
webEnvironment = RANDOM_PORT)
public class ConfigDataRetryIntegrationTests {
private static final String BASE_PATH = new WebEndpointProperties().getBasePath();
private static int configPort = SocketUtils.findAvailableTcpPort();
private static ConfigurableApplicationContext server;
@LocalServerPort
private int port;
@BeforeClass
public static void startConfigServer() throws IOException {
String baseDir = ConfigServerTestUtils.getBaseDirectory("spring-cloud-config-sample");
String repo = ConfigServerTestUtils.prepareLocalRepo(baseDir, "target/repos", "config-repo", "target/config");
server = SpringApplication.run(TestConfig.class, "--server.port=" + configPort, "--spring.config.name=server",
"--spring.cloud.config.server.git.uri=" + repo);
System.setProperty("spring.cloud.config.uri", "http://localhost:" + configPort);
}
@AfterClass
public static void close() {
System.clearProperty("spring.cloud.config.uri");
if (server != null) {
server.close();
}
}
@Test
@SuppressWarnings("unchecked")
public void contextLoads() {
Map res = new TestRestTemplate().getForObject("http://localhost:" + this.port + BASE_PATH + "/env/info.foo",
Map.class);
assertThat(res).containsKey("propertySources");
Map<String, Object> property = (Map<String, Object>) res.get("property");
assertThat(property).containsEntry("value", "bar");
}
@Configuration
@EnableAutoConfiguration
@EnableConfigServer
static class TestConfig implements WebMvcConfigurer {
AtomicInteger count = new AtomicInteger(0);
// @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new HandlerInterceptor() {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
if (request.getServletPath().equals("/retryapp/default")) {
return count.incrementAndGet() > 1;
}
return true;
}
});
}
}
}