Skip to content

Commit 8ddf4ed

Browse files
committed
Test multiple reminder state types + improve timer tests.
Signed-off-by: Artur Souza <asouza.pro@gmail.com>
1 parent 7649ae4 commit 8ddf4ed

15 files changed

+395
-54
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
GOPROXY: https://proxy.golang.org
4545
JDK_VER: ${{ matrix.java }}
4646
DAPR_CLI_VER: 1.10.0
47-
DAPR_RUNTIME_VER: 1.10.0
47+
DAPR_RUNTIME_VER: 1.10.6-rc.2
4848
DAPR_INSTALL_URL: https://raw.githubusercontent.com/dapr/cli/v1.10.0/install/install.sh
4949
DAPR_CLI_REF:
5050
DAPR_REF:

.github/workflows/validate.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
GOPROXY: https://proxy.golang.org
3939
JDK_VER: ${{ matrix.java }}
4040
DAPR_CLI_VER: 1.10.0
41-
DAPR_RUNTIME_VER: 1.10.0
41+
DAPR_RUNTIME_VER: 1.10.6-rc.2
4242
DAPR_INSTALL_URL: https://raw.githubusercontent.com/dapr/cli/v1.10.0/install/install.sh
4343
DAPR_CLI_REF:
4444
DAPR_REF:

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

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,85 @@
1919
import io.dapr.it.AppRun;
2020
import io.dapr.it.BaseIT;
2121
import io.dapr.it.DaprRun;
22+
import io.dapr.it.actors.app.ActorReminderDataParam;
2223
import io.dapr.it.actors.app.MyActorService;
2324
import org.apache.commons.lang3.tuple.ImmutablePair;
2425
import org.junit.After;
2526
import org.junit.Before;
2627
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
import org.junit.runners.Parameterized;
2730
import org.slf4j.Logger;
2831
import org.slf4j.LoggerFactory;
2932

3033
import java.time.Duration;
31-
import java.util.ArrayList;
32-
import java.util.List;
33-
import java.util.UUID;
34+
import java.util.*;
3435

3536
import static io.dapr.it.Retry.callWithRetry;
36-
import static io.dapr.it.actors.MyActorTestUtils.countMethodCalls;
37-
import static io.dapr.it.actors.MyActorTestUtils.fetchMethodCallLogs;
38-
import static io.dapr.it.actors.MyActorTestUtils.validateMethodCalls;
37+
import static io.dapr.it.actors.MyActorTestUtils.*;
3938

39+
@RunWith(Parameterized.class)
4040
public class ActorReminderRecoveryIT extends BaseIT {
4141

4242
private static final Logger logger = LoggerFactory.getLogger(ActorReminderRecoveryIT.class);
4343

4444
private static final String METHOD_NAME = "receiveReminder";
4545

46+
/**
47+
* Parameters for this test.
48+
* Param #1: useGrpc.
49+
*
50+
* @return Collection of parameter tuples.
51+
*/
52+
@Parameterized.Parameters
53+
public static Collection<Object[]> data() {
54+
return Arrays.asList(new Object[][] {
55+
{
56+
"MyActorTest",
57+
new ActorReminderDataParam("36", "String"),
58+
"36"
59+
},
60+
{
61+
"MyActorTest",
62+
new ActorReminderDataParam("\"my_text\"", "String"),
63+
"\"my_text\""
64+
},
65+
{
66+
"MyActorBinaryTest",
67+
new ActorReminderDataParam(new byte[]{0, 1}, "Binary"),
68+
"AAE="
69+
},
70+
{
71+
"MyActorObjectTest",
72+
new ActorReminderDataParam("{\"name\":\"abc\",\"age\":30}", "Object"),
73+
"abc,30"
74+
},
75+
});
76+
}
77+
78+
public String actorType;
79+
80+
public ActorReminderDataParam reminderDataParam;
81+
82+
public String expectedReminderStateText;
83+
84+
public String reminderName = UUID.randomUUID().toString();
85+
4686
private ActorProxy proxy;
4787

4888
private ImmutablePair<AppRun, DaprRun> runs;
4989

5090
private DaprRun clientRun;
5191

92+
public ActorReminderRecoveryIT(
93+
String actorType,
94+
ActorReminderDataParam reminderDataParam,
95+
String expectedReminderStateText) {
96+
this.actorType = actorType;
97+
this.reminderDataParam = reminderDataParam;
98+
this.expectedReminderStateText = expectedReminderStateText;
99+
}
100+
52101
@Before
53102
public void init() throws Exception {
54103
runs = startSplitDaprAndApp(
@@ -66,11 +115,10 @@ public void init() throws Exception {
66115
Thread.sleep(3000);
67116

68117
ActorId actorId = new ActorId(UUID.randomUUID().toString());
69-
String actorType="MyActorTest";
70118
logger.debug("Creating proxy builder");
71119

72120
ActorProxyBuilder<ActorProxy> proxyBuilder =
73-
new ActorProxyBuilder(actorType, ActorProxy.class, newActorClient());
121+
new ActorProxyBuilder(this.actorType, ActorProxy.class, newActorClient());
74122
logger.debug("Creating actorId");
75123
logger.debug("Building proxy");
76124
proxy = proxyBuilder.build(actorId);
@@ -80,7 +128,7 @@ public void init() throws Exception {
80128
public void tearDown() {
81129
// call unregister
82130
logger.debug("Calling actor method 'stopReminder' to unregister reminder");
83-
proxy.invokeMethod("stopReminder", "myReminder").block();
131+
proxy.invokeMethod("stopReminder", this.reminderName).block();
84132
}
85133

86134
/**
@@ -90,7 +138,9 @@ public void tearDown() {
90138
@Test
91139
public void reminderRecoveryTest() throws Exception {
92140
logger.debug("Invoking actor method 'startReminder' which will register a reminder");
93-
proxy.invokeMethod("startReminder", "myReminder").block();
141+
proxy.invokeMethod("setReminderData", this.reminderDataParam).block();
142+
143+
proxy.invokeMethod("startReminder", this.reminderName).block();
94144

95145
logger.debug("Pausing 7 seconds to allow reminder to fire");
96146
Thread.sleep(7000);
@@ -100,6 +150,7 @@ public void reminderRecoveryTest() throws Exception {
100150
logs.clear();
101151
logs.addAll(fetchMethodCallLogs(proxy));
102152
validateMethodCalls(logs, METHOD_NAME, 3);
153+
validateMessageContent(logs, METHOD_NAME, this.expectedReminderStateText);
103154
}, 5000);
104155

105156
// Restarts runtime only.
@@ -121,6 +172,7 @@ public void reminderRecoveryTest() throws Exception {
121172
logger.info("Fetching logs for " + METHOD_NAME);
122173
List<MethodEntryTracker> newLogs = fetchMethodCallLogs(proxy);
123174
validateMethodCalls(newLogs, METHOD_NAME, 1);
175+
validateMessageContent(newLogs, METHOD_NAME, this.expectedReminderStateText);
124176

125177
logger.info("Pausing 10 seconds to allow reminder to fire a few times");
126178
try {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import static io.dapr.it.Retry.callWithRetry;
3434
import static io.dapr.it.actors.MyActorTestUtils.fetchMethodCallLogs;
3535
import static io.dapr.it.actors.MyActorTestUtils.validateMethodCalls;
36+
import static io.dapr.it.actors.MyActorTestUtils.validateMessageContent;
3637
import static org.junit.Assert.assertNotEquals;
3738

3839
public class ActorTimerRecoveryIT extends BaseIT {
@@ -76,6 +77,7 @@ public void timerRecoveryTest() throws Exception {
7677
logs.clear();
7778
logs.addAll(fetchMethodCallLogs(proxy));
7879
validateMethodCalls(logs, METHOD_NAME, 3);
80+
validateMessageContent(logs, METHOD_NAME, "ping!");
7981
}, 5000);
8082

8183
// Restarts app only.

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@
1818
public class MethodEntryTracker {
1919
private boolean isEnter;
2020
private String methodName;
21+
private String message;
2122
private Date date;
2223

2324
public MethodEntryTracker(boolean isEnter, String methodName, Date date) {
25+
this(isEnter, methodName, null, date);
26+
}
27+
28+
public MethodEntryTracker(boolean isEnter, String methodName, String message, Date date) {
2429
this.isEnter = isEnter;
2530
this.methodName = methodName;
31+
this.message = message;
2632
this.date = date;
2733
}
2834

@@ -34,12 +40,16 @@ public String getMethodName() {
3440
return this.methodName;
3541
}
3642

43+
public String getMessage() {
44+
return this.message;
45+
}
46+
3747
public Date getDate() {
3848
return this.date;
3949
}
4050

4151
@Override
4252
public String toString() {
43-
return this.date + " " + this.isEnter + " " + this.methodName;
53+
return this.date + " " + this.isEnter + " " + (this.message != null? this.message + " ":"") + this.methodName;
4454
}
4555
}

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

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import java.util.List;
2121
import java.util.stream.Collectors;
2222

23-
import static org.junit.Assert.assertTrue;
23+
import static org.junit.Assert.*;
2424

2525
/**
2626
* Utility class for tests that use MyActor class.
@@ -39,12 +39,26 @@ static int countMethodCalls(List<MethodEntryTracker> logs, String methodName) {
3939
// Counts number of times reminder is invoked.
4040
// Events for each actor method call include "enter" and "exit" calls, so they are divided by 2.
4141
List<MethodEntryTracker> calls =
42-
logs.stream().filter(x -> x.getMethodName().equals(methodName)).collect(Collectors.toList());
42+
logs.stream().filter(x -> x.getMethodName().equals(methodName)).collect(Collectors.toList());
4343
System.out.printf(
44-
"Size of %s count list is %d, which means it's been invoked half that many times.\n", methodName, calls.size());
44+
"Size of %s count list is %d, which means it's been invoked half that many times.\n", methodName, calls.size());
4545
return calls.size() / 2;
4646
}
4747

48+
/**
49+
* Checks if all entries for a method call contain the same message.
50+
* @param logs logs with info about method entries and exits returned from the app
51+
* @param methodName name of the method to be validated
52+
* @param message expected message
53+
*/
54+
static void validateMessageContent(List<MethodEntryTracker> logs, String methodName, String message) {
55+
List<MethodEntryTracker> calls =
56+
logs.stream().filter(x -> x.getMethodName().equals(methodName)).collect(Collectors.toList());
57+
for (MethodEntryTracker m : calls) {
58+
assertEquals(message, m.getMessage());
59+
}
60+
}
61+
4862
/**
4963
* Validate the number of call of a given method.
5064
* @param logs logs with info about method entries and exits returned from the app
@@ -66,11 +80,22 @@ static List<MethodEntryTracker> fetchMethodCallLogs(ActorProxy proxy) {
6680
ArrayList<MethodEntryTracker> trackers = new ArrayList<MethodEntryTracker>();
6781
for(String t : logs) {
6882
String[] toks = t.split("\\|");
69-
MethodEntryTracker m = new MethodEntryTracker(
70-
toks[0].equals("Enter") ? true : false,
71-
toks[1],
72-
new Date(toks[2]));
73-
trackers.add(m);
83+
if (toks.length == 3) {
84+
MethodEntryTracker m = new MethodEntryTracker(
85+
toks[0].equals("Enter") ? true : false,
86+
toks[1],
87+
new Date(toks[2]));
88+
trackers.add(m);
89+
} else if (toks.length == 4) {
90+
MethodEntryTracker m = new MethodEntryTracker(
91+
toks[0].equals("Enter") ? true : false,
92+
toks[1],
93+
toks[2],
94+
new Date(toks[3]));
95+
trackers.add(m);
96+
} else {
97+
fail("Invalid log entry");
98+
}
7499
}
75100

76101
return trackers;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2021 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.actors.app;
15+
16+
import com.fasterxml.jackson.databind.ObjectMapper;
17+
import io.dapr.utils.TypeRef;
18+
19+
/**
20+
* This class is for passing string or binary data to the Actor for registering reminder later on during test.
21+
*/
22+
public class ActorReminderDataParam {
23+
24+
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
25+
26+
private String data;
27+
28+
private byte[] binaryData;
29+
30+
private String typeHint;
31+
32+
public ActorReminderDataParam() {
33+
}
34+
35+
public ActorReminderDataParam(String data, String typeHint) {
36+
this.data = data;
37+
this.typeHint = typeHint;
38+
}
39+
40+
public ActorReminderDataParam(byte[] data, String typeHint) {
41+
this.binaryData = data;
42+
this.typeHint = typeHint;
43+
}
44+
45+
public String getData() {
46+
return data;
47+
}
48+
49+
public void setData(String data) {
50+
this.data = data;
51+
}
52+
53+
public byte[] getBinaryData() {
54+
return binaryData;
55+
}
56+
57+
public void setBinaryData(byte[] binaryData) {
58+
this.binaryData = binaryData;
59+
}
60+
61+
public String getTypeHint() {
62+
return typeHint;
63+
}
64+
65+
public void setTypeHint(String typeHint) {
66+
this.typeHint = typeHint;
67+
}
68+
69+
public <T> T asObject(TypeRef<T> type) throws Exception {
70+
if (this.data != null) {
71+
return OBJECT_MAPPER.readValue(this.data, OBJECT_MAPPER.constructType(type.getType()));
72+
} else if (this.binaryData != null) {
73+
return OBJECT_MAPPER.readValue(this.binaryData, OBJECT_MAPPER.constructType(type.getType()));
74+
}
75+
return null;
76+
}
77+
}

sdk-tests/src/test/java/io/dapr/it/actors/app/MyActor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ public interface MyActor {
2323

2424
List<String> retrieveActiveActors();
2525

26-
void startReminder(String name);
26+
void setReminderData(ActorReminderDataParam param);
27+
28+
void startReminder(String name) throws Exception;
2729

2830
void stopReminder(String name);
2931

0 commit comments

Comments
 (0)