Skip to content

Commit ec23917

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

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
@@ -68,6 +68,7 @@ public class DaprContainer extends GenericContainer<DaprContainer> {
6868
private DaprPlacementContainer placementContainer;
6969
private String appName;
7070
private Integer appPort;
71+
private String appHealthCheckPath;
7172
private boolean shouldReusePlacement;
7273

7374
/**
@@ -116,6 +117,11 @@ public DaprContainer withAppChannelAddress(String appChannelAddress) {
116117
return this;
117118
}
118119

120+
public DaprContainer withAppHealthCheckPath(String appHealthCheckPath) {
121+
this.appHealthCheckPath = appHealthCheckPath;
122+
return this;
123+
}
124+
119125
public DaprContainer withConfiguration(Configuration configuration) {
120126
this.configuration = configuration;
121127
return this;
@@ -173,7 +179,7 @@ public DaprContainer withComponent(Component component) {
173179
*/
174180
public DaprContainer withComponent(Path path) {
175181
try {
176-
Map<String, Object> component = this.YAML_MAPPER.loadAs(Files.newInputStream(path), Map.class);
182+
Map<String, Object> component = YAML_MAPPER.loadAs(Files.newInputStream(path), Map.class);
177183

178184
String type = (String) component.get("type");
179185
Map<String, Object> metadata = (Map<String, Object>) component.get("metadata");
@@ -233,13 +239,14 @@ protected void configure() {
233239

234240
List<String> cmds = new ArrayList<>();
235241
cmds.add("./daprd");
236-
cmds.add("-app-id");
242+
cmds.add("--app-id");
237243
cmds.add(appName);
238244
cmds.add("--dapr-listen-addresses=0.0.0.0");
239245
cmds.add("--app-protocol");
240246
cmds.add(DAPR_PROTOCOL.getName());
241-
cmds.add("-placement-host-address");
247+
cmds.add("--placement-host-address");
242248
cmds.add(placementService + ":50005");
249+
cmds.add("--enable-app-health-check");
243250

244251
if (appChannelAddress != null && !appChannelAddress.isEmpty()) {
245252
cmds.add("--app-channel-address");
@@ -251,6 +258,12 @@ protected void configure() {
251258
cmds.add(Integer.toString(appPort));
252259
}
253260

261+
if (appHealthCheckPath != null && !appHealthCheckPath.isEmpty()) {
262+
cmds.add("--enable-app-health-check");
263+
cmds.add("--app-health-check-path");
264+
cmds.add(appHealthCheckPath);
265+
}
266+
254267
if (configuration != null) {
255268
cmds.add("--config");
256269
cmds.add("/dapr-resources/" + configuration.getName() + ".yaml");

0 commit comments

Comments
 (0)