Skip to content

Commit a9fdda8

Browse files
salaboyakkie
authored andcommitted
using random port thanks to @artur-ciocanu
Signed-off-by: Christian Kaps <[email protected]>
1 parent 2519af5 commit a9fdda8

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

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

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,40 +34,37 @@
3434
import org.testcontainers.junit.jupiter.Testcontainers;
3535

3636
import java.util.Map;
37+
import java.util.Random;
3738
import java.util.UUID;
3839

3940
import static org.junit.jupiter.api.Assertions.assertEquals;
4041

4142
@SpringBootTest(
42-
webEnvironment = WebEnvironment.DEFINED_PORT,
43-
classes = {
44-
TestActorsApplication.class,
45-
TestDaprActorsConfiguration.class
46-
},
47-
properties = {
48-
"server.port=64080", // must be constant, not a static attribute from class below.
49-
}
43+
webEnvironment = WebEnvironment.RANDOM_PORT,
44+
classes = {
45+
TestActorsApplication.class,
46+
TestDaprActorsConfiguration.class
47+
}
5048
)
5149
@Testcontainers
5250
@Tag("testcontainers")
5351
public class DaprActorsIT {
54-
55-
private static final int APP_PORT = 64080;
56-
5752
private static final Network DAPR_NETWORK = Network.newNetwork();
53+
private static final Random RANDOM = new Random();
54+
private static final int PORT = RANDOM.nextInt(1000) + 8000;
5855

5956
private static final String ACTORS_MESSAGE_PATTERN = ".*Actor API level in the cluster has been updated to 10.*";
6057

6158
@Container
6259
private static final DaprContainer DAPR_CONTAINER = new DaprContainer("daprio/daprd:1.14.4")
63-
.withAppName("actor-dapr-app")
64-
.withNetwork(DAPR_NETWORK)
65-
.withComponent(new Component("kvstore", "state.in-memory", "v1",
66-
Map.of("actorStateStore", "true")))
67-
.withDaprLogLevel(DaprLogLevel.DEBUG)
68-
.withLogConsumer(outputFrame -> System.out.println(outputFrame.getUtf8String()))
69-
.withAppChannelAddress("host.testcontainers.internal")
70-
.withAppPort(APP_PORT);
60+
.withAppName("actor-dapr-app")
61+
.withNetwork(DAPR_NETWORK)
62+
.withComponent(new Component("kvstore", "state.in-memory", "v1",
63+
Map.of("actorStateStore", "true")))
64+
.withDaprLogLevel(DaprLogLevel.DEBUG)
65+
.withLogConsumer(outputFrame -> System.out.println(outputFrame.getUtf8String()))
66+
.withAppChannelAddress("host.testcontainers.internal")
67+
.withAppPort(PORT);
7168

7269
/**
7370
* Expose the Dapr ports to the host.
@@ -78,6 +75,7 @@ public class DaprActorsIT {
7875
static void daprProperties(DynamicPropertyRegistry registry) {
7976
registry.add("dapr.http.endpoint", DAPR_CONTAINER::getHttpEndpoint);
8077
registry.add("dapr.grpc.endpoint", DAPR_CONTAINER::getGrpcEndpoint);
78+
registry.add("server.port", () -> PORT);
8179
}
8280

8381
@Autowired
@@ -88,15 +86,14 @@ static void daprProperties(DynamicPropertyRegistry registry) {
8886

8987
@BeforeEach
9088
public void setUp(){
91-
org.testcontainers.Testcontainers.exposeHostPorts(APP_PORT);
89+
org.testcontainers.Testcontainers.exposeHostPorts(PORT);
9290
daprActorRuntime.registerActor(TestActorImpl.class);
9391
// Ensure the subscriptions are registered
9492
Wait.forLogMessage(ACTORS_MESSAGE_PATTERN, 1).waitUntilReady(DAPR_CONTAINER);
9593
}
9694

9795
@Test
98-
public void testActors() throws Exception {
99-
96+
public void testActors() {
10097
ActorProxyBuilder<TestActor> builder = new ActorProxyBuilder<>(TestActor.class, daprActorClient);
10198
ActorId actorId = ActorId.createRandom();
10299
TestActor actor = builder.build(actorId);
@@ -107,4 +104,4 @@ public void testActors() throws Exception {
107104

108105
assertEquals(echoedMessage, message);
109106
}
110-
}
107+
}

0 commit comments

Comments
 (0)