Skip to content

Commit 22d9874

Browse files
artur-ciocanuArtur Ciocanu
and
Artur Ciocanu
authored
Add app health check support to Dapr Testcontainer (dapr#1213)
* Add app health check support to Dapr Testcontainer Signed-off-by: Artur Ciocanu <[email protected]> * Some minor cleanup Signed-off-by: Artur Ciocanu <[email protected]> * Move waiting to beforeEach, it looks more natural Signed-off-by: Artur Ciocanu <[email protected]> --------- Signed-off-by: Artur Ciocanu <[email protected]> Co-authored-by: Artur Ciocanu <[email protected]>
1 parent 0cec586 commit 22d9874

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,14 @@
2323
import org.junit.jupiter.api.BeforeEach;
2424
import org.junit.jupiter.api.Tag;
2525
import org.junit.jupiter.api.Test;
26-
import org.junit.jupiter.api.Disabled;
2726
import org.slf4j.Logger;
2827
import org.slf4j.LoggerFactory;
2928
import org.springframework.beans.factory.annotation.Autowired;
3029
import org.springframework.boot.test.context.SpringBootTest;
3130
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
3231
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
33-
import org.springframework.test.context.DynamicPropertyRegistry;
34-
import org.springframework.test.context.DynamicPropertySource;
3532
import org.testcontainers.containers.Network;
33+
import org.testcontainers.containers.wait.strategy.Wait;
3634
import org.testcontainers.junit.jupiter.Container;
3735
import org.testcontainers.junit.jupiter.Testcontainers;
3836

@@ -56,16 +54,18 @@ public class DaprSpringMessagingIT {
5654
private static final Logger logger = LoggerFactory.getLogger(DaprSpringMessagingIT.class);
5755

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

6261
@Container
6362
@ServiceConnection
6463
private static final DaprContainer DAPR_CONTAINER = new DaprContainer("daprio/daprd:1.13.2")
6564
.withAppName("messaging-dapr-app")
6665
.withNetwork(DAPR_NETWORK)
6766
.withComponent(new Component("pubsub", "pubsub.in-memory", "v1", Collections.emptyMap()))
68-
.withAppPort(8080)
67+
.withAppPort(APP_PORT)
68+
.withAppHealthCheckPath("/ready")
6969
.withDaprLogLevel(DaprLogLevel.DEBUG)
7070
.withLogConsumer(outputFrame -> System.out.println(outputFrame.getUtf8String()))
7171
.withAppChannelAddress("host.testcontainers.internal");
@@ -78,16 +78,16 @@ public class DaprSpringMessagingIT {
7878

7979
@BeforeAll
8080
public static void beforeAll(){
81-
org.testcontainers.Testcontainers.exposeHostPorts(8080);
81+
org.testcontainers.Testcontainers.exposeHostPorts(APP_PORT);
8282
}
8383

8484
@BeforeEach
85-
public void beforeEach() throws InterruptedException {
86-
Thread.sleep(1000);
85+
public void beforeEach() {
86+
// Ensure the subscriptions are registered
87+
Wait.forLogMessage(SUBSCRIPTION_MESSAGE_PATTERN, 1).waitUntilReady(DAPR_CONTAINER);
8788
}
8889

8990
@Test
90-
@Disabled("Test is flaky due to global state in the spring test application.")
9191
public void testDaprMessagingTemplate() throws InterruptedException {
9292
for (int i = 0; i < 10; i++) {
9393
var msg = "ProduceAndReadWithPrimitiveMessageType:" + i;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ 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("/")
36+
@GetMapping("/ready")
3737
public String ok() {
3838
return "OK";
3939
}

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

+15-3
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,12 +239,12 @@ 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");
243249

244250
if (appChannelAddress != null && !appChannelAddress.isEmpty()) {
@@ -251,6 +257,12 @@ protected void configure() {
251257
cmds.add(Integer.toString(appPort));
252258
}
253259

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

0 commit comments

Comments
 (0)