Skip to content

Commit 0c7448e

Browse files
committed
Add actor testcontainer tests
1 parent 9cd6db2 commit 0c7448e

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package io.dapr.it.testcontainers;
2+
3+
import io.dapr.actors.ActorId;
4+
import io.dapr.actors.client.ActorClient;
5+
import io.dapr.actors.client.ActorProxyBuilder;
6+
import io.dapr.testcontainers.Component;
7+
import io.dapr.testcontainers.DaprContainer;
8+
import io.dapr.testcontainers.DaprLogLevel;
9+
import org.junit.jupiter.api.Tag;
10+
import org.junit.jupiter.api.Test;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.boot.test.context.SpringBootTest;
13+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
14+
import org.springframework.test.context.DynamicPropertyRegistry;
15+
import org.springframework.test.context.DynamicPropertySource;
16+
import org.testcontainers.containers.Network;
17+
import org.testcontainers.junit.jupiter.Container;
18+
import org.testcontainers.junit.jupiter.Testcontainers;
19+
20+
import java.util.Map;
21+
import java.util.UUID;
22+
23+
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
25+
@SpringBootTest(
26+
webEnvironment = WebEnvironment.RANDOM_PORT,
27+
classes = {
28+
TestDaprActorsConfiguration.class,
29+
TestActorsApplication.class
30+
}
31+
)
32+
@Testcontainers
33+
@Tag("testcontainers")
34+
public class DaprActorsIT {
35+
private static final Network DAPR_NETWORK = Network.newNetwork();
36+
37+
@Container
38+
private static final DaprContainer DAPR_CONTAINER = new DaprContainer("daprio/daprd:1.13.2")
39+
.withAppName("actor-dapr-app")
40+
.withNetwork(DAPR_NETWORK)
41+
.withComponent(new Component("kvstore", "state.in-memory", "v1",
42+
Map.of("actorStateStore", "true")))
43+
.withDaprLogLevel(DaprLogLevel.DEBUG)
44+
.withLogConsumer(outputFrame -> System.out.println(outputFrame.getUtf8String()))
45+
.withAppChannelAddress("host.testcontainers.internal");
46+
47+
/**
48+
* Expose the Dapr ports to the host.
49+
*
50+
* @param registry the dynamic property registry
51+
*/
52+
@DynamicPropertySource
53+
static void daprProperties(DynamicPropertyRegistry registry) {
54+
registry.add("dapr.http.endpoint", DAPR_CONTAINER::getHttpEndpoint);
55+
registry.add("dapr.grpc.endpoint", DAPR_CONTAINER::getGrpcEndpoint);
56+
}
57+
58+
@Autowired
59+
private ActorClient actorClient;
60+
61+
@Test
62+
public void testWorkflows() throws Exception {
63+
ActorProxyBuilder<TestActor> builder = new ActorProxyBuilder(TestActor.class, actorClient);
64+
ActorId actorId = ActorId.createRandom();
65+
TestActor actor = builder.build(actorId);
66+
67+
String message = UUID.randomUUID().toString();
68+
69+
String echoedMessage = actor.echo(message);
70+
71+
assertEquals(echoedMessage, message);
72+
}
73+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.dapr.it.testcontainers;
2+
import io.dapr.actors.ActorMethod;
3+
import io.dapr.actors.ActorType;
4+
5+
@ActorType(name = "TestActor")
6+
public interface TestActor {
7+
@ActorMethod(name = "echo_message")
8+
String echo(String message);
9+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.dapr.it.testcontainers;
2+
3+
import io.dapr.actors.ActorId;
4+
import io.dapr.actors.runtime.AbstractActor;
5+
import io.dapr.actors.runtime.ActorRuntimeContext;
6+
7+
public class TestActorImpl extends AbstractActor implements TestActor {
8+
public TestActorImpl(ActorRuntimeContext runtimeContext, ActorId id) {
9+
super(runtimeContext, id);
10+
}
11+
12+
@Override
13+
public String echo(String message) {
14+
return message;
15+
}
16+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2024 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.it.testcontainers;
15+
16+
import org.springframework.boot.SpringApplication;
17+
import org.springframework.boot.autoconfigure.SpringBootApplication;
18+
19+
@SpringBootApplication
20+
public class TestActorsApplication {
21+
22+
public static void main(String[] args) {
23+
SpringApplication.run(TestActorsApplication.class, args);
24+
}
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.dapr.it.testcontainers;
2+
3+
import java.util.Map;
4+
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
9+
import io.dapr.actors.client.ActorClient;
10+
import io.dapr.config.Properties;
11+
12+
@Configuration
13+
public class TestDaprActorsConfiguration {
14+
@Bean
15+
public ActorClient daprActorClient(
16+
@Value("${dapr.http.endpoint}") String daprHttpEndpoint,
17+
@Value("${dapr.grpc.endpoint}") String daprGrpcEndpoint
18+
){
19+
Map<String, String> overrides = Map.of(
20+
"dapr.http.endpoint", daprHttpEndpoint,
21+
"dapr.grpc.endpoint", daprGrpcEndpoint
22+
);
23+
24+
return new ActorClient(new Properties(overrides));
25+
}
26+
}

0 commit comments

Comments
 (0)