Move docker compose readiness code and make it package-private

The `ReadinessCheck` interface has been removed making the dedicated
package less necessary. By relocating the code we can make more of it
pacakge-private.

See gh-35544
This commit is contained in:
Phillip Webb
2023-05-17 14:32:55 -07:00
parent 060581d078
commit 6b0b6ccf49
15 changed files with 102 additions and 234 deletions

View File

@@ -36,7 +36,6 @@ import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.docker.compose.core.DockerCompose;
import org.springframework.boot.docker.compose.core.DockerComposeFile;
import org.springframework.boot.docker.compose.core.RunningService;
import org.springframework.boot.docker.compose.readiness.ServiceReadinessChecks;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.support.GenericApplicationContext;

View File

@@ -48,6 +48,9 @@ class DockerComposePropertiesTests {
assertThat(properties.getStop().getCommand()).isEqualTo(StopCommand.STOP);
assertThat(properties.getStop().getTimeout()).isEqualTo(Duration.ofSeconds(10));
assertThat(properties.getProfiles().getActive()).isEmpty();
assertThat(properties.getReadiness().getTimeout()).isEqualTo(Duration.ofMinutes(2));
assertThat(properties.getReadiness().getTcp().getConnectTimeout()).isEqualTo(Duration.ofMillis(200));
assertThat(properties.getReadiness().getTcp().getReadTimeout()).isEqualTo(Duration.ofMillis(200));
}
@Test
@@ -60,6 +63,9 @@ class DockerComposePropertiesTests {
source.put("spring.docker.compose.stop.command", "down");
source.put("spring.docker.compose.stop.timeout", "5s");
source.put("spring.docker.compose.profiles.active", "myprofile");
source.put("spring.docker.compose.readiness.timeout", "10s");
source.put("spring.docker.compose.readiness.tcp.connect-timeout", "400ms");
source.put("spring.docker.compose.readiness.tcp.read-timeout", "500ms");
Binder binder = new Binder(new MapConfigurationPropertySource(source));
DockerComposeProperties properties = DockerComposeProperties.get(binder);
assertThat(properties.getFile()).isEqualTo(new File("my-compose.yml"));
@@ -69,6 +75,9 @@ class DockerComposePropertiesTests {
assertThat(properties.getStop().getCommand()).isEqualTo(StopCommand.DOWN);
assertThat(properties.getStop().getTimeout()).isEqualTo(Duration.ofSeconds(5));
assertThat(properties.getProfiles().getActive()).containsExactly("myprofile");
assertThat(properties.getReadiness().getTimeout()).isEqualTo(Duration.ofSeconds(10));
assertThat(properties.getReadiness().getTcp().getConnectTimeout()).isEqualTo(Duration.ofMillis(400));
assertThat(properties.getReadiness().getTcp().getReadTimeout()).isEqualTo(Duration.ofMillis(500));
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.docker.compose.readiness;
package org.springframework.boot.docker.compose.lifecycle;
import java.time.Duration;
import java.util.List;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.docker.compose.readiness;
package org.springframework.boot.docker.compose.lifecycle;
import org.junit.jupiter.api.Test;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.docker.compose.readiness;
package org.springframework.boot.docker.compose.lifecycle;
import java.time.Clock;
import java.time.Duration;
@@ -26,10 +26,7 @@ import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.docker.compose.core.RunningService;
import org.springframework.core.test.io.support.MockSpringFactoriesLoader;
import org.springframework.mock.env.MockEnvironment;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -49,14 +46,6 @@ class ServiceReadinessChecksTests {
Instant now = Instant.now();
private MockSpringFactoriesLoader loader;
private ClassLoader classLoader;
private MockEnvironment environment;
private Binder binder;
private RunningService runningService;
private List<RunningService> runningServices;
@@ -65,10 +54,6 @@ class ServiceReadinessChecksTests {
void setup() {
this.clock = mock(Clock.class);
given(this.clock.instant()).willAnswer((args) -> this.now);
this.loader = new MockSpringFactoriesLoader();
this.classLoader = getClass().getClassLoader();
this.environment = new MockEnvironment();
this.binder = Binder.get(this.environment);
this.runningService = mock(RunningService.class);
this.runningServices = List.of(this.runningService);
}
@@ -109,8 +94,8 @@ class ServiceReadinessChecksTests {
}
private ServiceReadinessChecks createChecks(TcpConnectServiceReadinessCheck check) {
return new ServiceReadinessChecks(this.clock, this::sleep, this.loader, this.classLoader, this.environment,
this.binder, (properties) -> check);
DockerComposeProperties properties = new DockerComposeProperties();
return new ServiceReadinessChecks(properties.getReadiness(), this.clock, this::sleep, check);
}
/**

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.docker.compose.readiness;
package org.springframework.boot.docker.compose.lifecycle;
import java.io.IOException;
import java.io.UncheckedIOException;
@@ -50,7 +50,7 @@ class TcpConnectServiceReadinessCheckTests {
@BeforeEach
void setup() {
ReadinessProperties.Tcp tcpProperties = new ReadinessProperties.Tcp();
DockerComposeProperties.Readiness.Tcp tcpProperties = new DockerComposeProperties.Readiness.Tcp();
tcpProperties.setConnectTimeout(Duration.ofMillis(100));
tcpProperties.setReadTimeout(Duration.ofMillis(100));
this.readinessCheck = new TcpConnectServiceReadinessCheck(tcpProperties);

View File

@@ -1,62 +0,0 @@
/*
* Copyright 2012-2023 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 org.springframework.boot.docker.compose.readiness;
import java.time.Duration;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link ReadinessProperties}.
*
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
*/
class ReadinessPropertiesTests {
@Test
void getWhenNoPropertiesReturnsNewInstance() {
Binder binder = new Binder(new MapConfigurationPropertySource());
ReadinessProperties properties = ReadinessProperties.get(binder);
assertThat(properties.getTimeout()).isEqualTo(Duration.ofMinutes(2));
assertThat(properties.getTcp().getConnectTimeout()).isEqualTo(Duration.ofMillis(200));
assertThat(properties.getTcp().getReadTimeout()).isEqualTo(Duration.ofMillis(200));
}
@Test
void getWhenPropertiesReturnsBoundInstance() {
Map<String, String> source = new LinkedHashMap<>();
source.put("spring.docker.compose.readiness.timeout", "10s");
source.put("spring.docker.compose.readiness.tcp.connect-timeout", "400ms");
source.put("spring.docker.compose.readiness.tcp.read-timeout", "500ms");
Binder binder = new Binder(new MapConfigurationPropertySource(source));
ReadinessProperties properties = ReadinessProperties.get(binder);
assertThat(properties.getTimeout()).isEqualTo(Duration.ofSeconds(10));
assertThat(properties.getTcp().getConnectTimeout()).isEqualTo(Duration.ofMillis(400));
assertThat(properties.getTcp().getReadTimeout()).isEqualTo(Duration.ofMillis(500));
}
}