Skip to content

Commit 118d977

Browse files
author
Artur Ciocanu
committed
Add app health check support to Dapr Testcontainer
Signed-off-by: Artur Ciocanu <[email protected]>
1 parent 58d6218 commit 118d977

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

sdk-tests/src/test/java/io/dapr/it/spring/messaging/DaprSpringMessagingIT.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,16 @@
2020
import io.dapr.testcontainers.DaprContainer;
2121
import io.dapr.testcontainers.DaprLogLevel;
2222
import org.junit.jupiter.api.BeforeAll;
23-
import org.junit.jupiter.api.BeforeEach;
2423
import org.junit.jupiter.api.Tag;
2524
import org.junit.jupiter.api.Test;
26-
import org.junit.jupiter.api.Disabled;
2725
import org.slf4j.Logger;
2826
import org.slf4j.LoggerFactory;
2927
import org.springframework.beans.factory.annotation.Autowired;
3028
import org.springframework.boot.test.context.SpringBootTest;
3129
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
3230
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
33-
import org.springframework.test.context.DynamicPropertyRegistry;
34-
import org.springframework.test.context.DynamicPropertySource;
3531
import org.testcontainers.containers.Network;
32+
import org.testcontainers.containers.wait.strategy.Wait;
3633
import org.testcontainers.junit.jupiter.Container;
3734
import org.testcontainers.junit.jupiter.Testcontainers;
3835

@@ -56,16 +53,18 @@ public class DaprSpringMessagingIT {
5653
private static final Logger logger = LoggerFactory.getLogger(DaprSpringMessagingIT.class);
5754

5855
private static final String TOPIC = "mockTopic";
59-
6056
private static final Network DAPR_NETWORK = Network.newNetwork();
57+
private static final int APP_PORT = 8080;
58+
private static final String SUBSCRIPTION_MESSAGE_PATTERN = ".*app is subscribed to the following topics.*";
6159

6260
@Container
6361
@ServiceConnection
6462
private static final DaprContainer DAPR_CONTAINER = new DaprContainer("daprio/daprd:1.13.2")
6563
.withAppName("messaging-dapr-app")
6664
.withNetwork(DAPR_NETWORK)
6765
.withComponent(new Component("pubsub", "pubsub.in-memory", "v1", Collections.emptyMap()))
68-
.withAppPort(8080)
66+
.withAppPort(APP_PORT)
67+
.withAppHealthCheckPath("/ready")
6968
.withDaprLogLevel(DaprLogLevel.DEBUG)
7069
.withLogConsumer(outputFrame -> System.out.println(outputFrame.getUtf8String()))
7170
.withAppChannelAddress("host.testcontainers.internal");
@@ -78,17 +77,13 @@ public class DaprSpringMessagingIT {
7877

7978
@BeforeAll
8079
public static void beforeAll(){
81-
org.testcontainers.Testcontainers.exposeHostPorts(8080);
82-
}
83-
84-
@BeforeEach
85-
public void beforeEach() throws InterruptedException {
86-
Thread.sleep(1000);
80+
org.testcontainers.Testcontainers.exposeHostPorts(APP_PORT);
8781
}
8882

8983
@Test
90-
@Disabled("Test is flaky due to global state in the spring test application.")
9184
public void testDaprMessagingTemplate() throws InterruptedException {
85+
Wait.forLogMessage(SUBSCRIPTION_MESSAGE_PATTERN, 1).waitUntilReady(DAPR_CONTAINER);
86+
9287
for (int i = 0; i < 10; i++) {
9388
var msg = "ProduceAndReadWithPrimitiveMessageType:" + i;
9489

sdk-tests/src/test/java/io/dapr/it/spring/messaging/TestRestController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public class TestRestController {
3333
private static final Logger LOG = LoggerFactory.getLogger(TestRestController.class);
3434
private final List<CloudEvent<String>> events = new ArrayList<>();
3535

36-
@GetMapping("/")
37-
public String ok() {
36+
@GetMapping("/ready")
37+
public String ok() throws InterruptedException {
3838
return "OK";
3939
}
4040

testcontainers-dapr/src/main/java/io/dapr/testcontainers/DaprContainer.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public class DaprContainer extends GenericContainer<DaprContainer> {
6565
private DaprPlacementContainer placementContainer;
6666
private String appName;
6767
private Integer appPort;
68+
private String appHealthCheckPath;
6869
private boolean shouldReusePlacement;
6970

7071
/**
@@ -109,6 +110,11 @@ public DaprContainer withAppChannelAddress(String appChannelAddress) {
109110
return this;
110111
}
111112

113+
public DaprContainer withAppHealthCheckPath(String appHealthCheckPath) {
114+
this.appHealthCheckPath = appHealthCheckPath;
115+
return this;
116+
}
117+
112118
public DaprContainer withConfiguration(Configuration configuration) {
113119
this.configuration = configuration;
114120
return this;
@@ -161,7 +167,7 @@ public DaprContainer withComponent(Component component) {
161167
*/
162168
public DaprContainer withComponent(Path path) {
163169
try {
164-
Map<String, Object> component = this.YAML_MAPPER.loadAs(Files.newInputStream(path), Map.class);
170+
Map<String, Object> component = YAML_MAPPER.loadAs(Files.newInputStream(path), Map.class);
165171

166172
String type = (String) component.get("type");
167173
Map<String, Object> metadata = (Map<String, Object>) component.get("metadata");
@@ -221,13 +227,14 @@ protected void configure() {
221227

222228
List<String> cmds = new ArrayList<>();
223229
cmds.add("./daprd");
224-
cmds.add("-app-id");
230+
cmds.add("--app-id");
225231
cmds.add(appName);
226232
cmds.add("--dapr-listen-addresses=0.0.0.0");
227233
cmds.add("--app-protocol");
228234
cmds.add(DAPR_PROTOCOL.getName());
229-
cmds.add("-placement-host-address");
235+
cmds.add("--placement-host-address");
230236
cmds.add(placementService + ":50005");
237+
cmds.add("--enable-app-health-check");
231238

232239
if (appChannelAddress != null && !appChannelAddress.isEmpty()) {
233240
cmds.add("--app-channel-address");
@@ -239,6 +246,12 @@ protected void configure() {
239246
cmds.add(Integer.toString(appPort));
240247
}
241248

249+
if (appHealthCheckPath != null && !appHealthCheckPath.isEmpty()) {
250+
cmds.add("--enable-app-health-check");
251+
cmds.add("--app-health-check-path");
252+
cmds.add(appHealthCheckPath);
253+
}
254+
242255
if (configuration != null) {
243256
cmds.add("--config");
244257
cmds.add("/dapr-resources/" + configuration.getName() + ".yaml");

0 commit comments

Comments
 (0)