Skip to content

Commit e4e1b5a

Browse files
committed
Fix a few issues related to Dapr container usage in ITs.
Signed-off-by: Artur Ciocanu <artur.ciocanu@gmail.com>
1 parent 3fc60a7 commit e4e1b5a

File tree

3 files changed

+66
-39
lines changed

3 files changed

+66
-39
lines changed

sdk-tests/src/test/java/io/dapr/it/testcontainers/actors/DaprActorsIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.dapr.testcontainers.Component;
2121
import io.dapr.testcontainers.DaprContainer;
2222
import io.dapr.testcontainers.DaprLogLevel;
23+
import io.dapr.testcontainers.spring.DaprContainerFactory;
2324
import io.dapr.testcontainers.spring.DaprSidecarContainer;
2425
import io.dapr.testcontainers.spring.DaprSpringBootTest;
2526
import org.junit.jupiter.api.BeforeEach;
@@ -40,7 +41,7 @@ public class DaprActorsIT {
4041
private static final String ACTORS_MESSAGE_PATTERN = ".*Actor runtime started.*";
4142

4243
@DaprSidecarContainer
43-
private static final DaprContainer DAPR_CONTAINER = DaprContainer.createForSpringBootTest("actor-dapr-app")
44+
private static final DaprContainer DAPR_CONTAINER = DaprContainerFactory.createForSpringBootTest("actor-dapr-app")
4445
.withComponent(new Component("kvstore", "state.in-memory", "v1",
4546
Map.of("actorStateStore", "true")))
4647
.withDaprLogLevel(DaprLogLevel.DEBUG)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2025 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.testcontainers.spring;
15+
16+
import io.dapr.testcontainers.DaprContainer;
17+
18+
import java.io.IOException;
19+
import java.net.ServerSocket;
20+
21+
import static io.dapr.testcontainers.DaprContainerConstants.DAPR_RUNTIME_IMAGE_TAG;
22+
23+
/**
24+
* Factory for creating DaprContainer instances configured for Spring Boot integration tests.
25+
*
26+
* <p>This class handles the common setup required for bidirectional communication
27+
* between Spring Boot applications and the Dapr sidecar in test scenarios.</p>
28+
*/
29+
public final class DaprContainerFactory {
30+
31+
private DaprContainerFactory() {
32+
// Utility class
33+
}
34+
35+
/**
36+
* Creates a DaprContainer pre-configured for Spring Boot integration tests.
37+
* This factory method handles the common setup required for bidirectional
38+
* communication between Spring Boot and the Dapr sidecar:
39+
* <ul>
40+
* <li>Allocates a free port for the Spring Boot application</li>
41+
* <li>Configures the app channel address for container-to-host communication</li>
42+
* </ul>
43+
*
44+
* @param appName the Dapr application name
45+
* @return a pre-configured DaprContainer for Spring Boot tests
46+
*/
47+
public static DaprContainer createForSpringBootTest(String appName) {
48+
int port = allocateFreePort();
49+
50+
return new DaprContainer(DAPR_RUNTIME_IMAGE_TAG)
51+
.withAppName(appName)
52+
.withAppPort(port)
53+
.withAppChannelAddress("host.testcontainers.internal");
54+
}
55+
56+
private static int allocateFreePort() {
57+
try (ServerSocket socket = new ServerSocket(0)) {
58+
socket.setReuseAddress(true);
59+
return socket.getLocalPort();
60+
} catch (IOException e) {
61+
throw new IllegalStateException("Failed to allocate free port", e);
62+
}
63+
}
64+
}

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

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.yaml.snakeyaml.Yaml;
3131

3232
import java.io.IOException;
33-
import java.net.ServerSocket;
3433
import java.nio.file.Files;
3534
import java.nio.file.Path;
3635
import java.util.ArrayList;
@@ -106,43 +105,6 @@ public DaprContainer(String image) {
106105
this(DockerImageName.parse(image));
107106
}
108107

109-
/**
110-
* Creates a DaprContainer pre-configured for Spring Boot integration tests.
111-
* This factory method handles the common setup required for bidirectional
112-
* communication between Spring Boot and the Dapr sidecar:
113-
* <ul>
114-
* <li>Allocates a free port for the Spring Boot application</li>
115-
* <li>Configures the app channel address for container-to-host communication</li>
116-
* </ul>
117-
*
118-
* <p>Example usage:</p>
119-
* <pre>{@code
120-
* @Container
121-
* private static final DaprContainer DAPR = DaprContainer.createForSpringBootTest("my-app")
122-
* .withComponent(new Component("statestore", "state.in-memory", "v1", Map.of()));
123-
* }</pre>
124-
*
125-
* @param appName the Dapr application name
126-
* @return a pre-configured DaprContainer for Spring Boot tests
127-
*/
128-
public static DaprContainer createForSpringBootTest(String appName) {
129-
int port = allocateFreePort();
130-
131-
return new DaprContainer(DAPR_RUNTIME_IMAGE_TAG)
132-
.withAppName(appName)
133-
.withAppPort(port)
134-
.withAppChannelAddress("host.testcontainers.internal");
135-
}
136-
137-
private static int allocateFreePort() {
138-
try (ServerSocket socket = new ServerSocket(0)) {
139-
socket.setReuseAddress(true);
140-
return socket.getLocalPort();
141-
} catch (IOException e) {
142-
throw new IllegalStateException("Failed to allocate free port", e);
143-
}
144-
}
145-
146108
public Configuration getConfiguration() {
147109
return configuration;
148110
}

0 commit comments

Comments
 (0)