@@ -27,6 +27,10 @@ public class MethodInvokeIT extends BaseIT {
27
27
28
28
//Number of messages to be sent: 10
29
29
private static final int NUM_MESSAGES = 10 ;
30
+ private static final int TIMEOUT_MS = 100 ;
31
+ private static final ResiliencyOptions RESILIENCY_OPTIONS = new ResiliencyOptions ()
32
+ .setTimeout (Duration .ofMillis (TIMEOUT_MS ));
33
+ private static final String EXCEPTION_MARKER = "DEADLINE_EXCEEDED: deadline exceeded after" ;
30
34
31
35
/**
32
36
* Run of a Dapr application.
@@ -47,28 +51,27 @@ public void init() throws Exception {
47
51
@ Test
48
52
public void testInvoke () throws Exception {
49
53
try (DaprClient client = new DaprClientBuilder ().build ()) {
50
- MethodInvokeServiceGrpc .MethodInvokeServiceBlockingStub stub = client .newGrpcStub (
51
- daprRun .getAppName (),
52
- channel -> MethodInvokeServiceGrpc .newBlockingStub (channel ));
53
54
client .waitForSidecar (10000 ).block ();
54
55
daprRun .waitForAppHealth (10000 );
56
+
57
+ MethodInvokeServiceGrpc .MethodInvokeServiceBlockingStub stub = createGrpcStub (client );
55
58
56
59
for (int i = 0 ; i < NUM_MESSAGES ; i ++) {
57
60
String message = String .format ("This is message #%d" , i );
58
-
59
61
PostMessageRequest req = PostMessageRequest .newBuilder ().setId (i ).setMessage (message ).build ();
62
+
60
63
stub .postMessage (req );
61
64
62
65
System .out .println ("Invoke method messages : " + message );
63
66
}
64
67
65
68
Map <Integer , String > messages = stub .getMessages (GetMessagesRequest .newBuilder ().build ()).getMessagesMap ();
66
- assertEquals (10 , messages .size ());
69
+ assertEquals (NUM_MESSAGES , messages .size ());
67
70
68
71
// Delete one message.
69
72
stub .deleteMessage (DeleteMessageRequest .newBuilder ().setId (1 ).build ());
70
73
messages = stub .getMessages (GetMessagesRequest .newBuilder ().build ()).getMessagesMap ();
71
- assertEquals (9 , messages .size ());
74
+ assertEquals (NUM_MESSAGES - 1 , messages .size ());
72
75
73
76
// Now update one message.
74
77
stub .postMessage (PostMessageRequest .newBuilder ().setId (2 ).setMessage ("updated message" ).build ());
@@ -79,21 +82,17 @@ public void testInvoke() throws Exception {
79
82
80
83
@ Test
81
84
public void testInvokeTimeout () throws Exception {
82
- long timeoutMs = 100 ;
83
- ResiliencyOptions resiliencyOptions = new ResiliencyOptions ().setTimeout (Duration .ofMillis (timeoutMs ));
84
- try (DaprClient client = new DaprClientBuilder ().withResiliencyOptions (resiliencyOptions ).build ()) {
85
- MethodInvokeServiceGrpc .MethodInvokeServiceBlockingStub stub = client .newGrpcStub (
86
- daprRun .getAppName (),
87
- channel -> MethodInvokeServiceGrpc .newBlockingStub (channel ));
85
+ try (DaprClient client = new DaprClientBuilder ().withResiliencyOptions (RESILIENCY_OPTIONS ).build ()) {
88
86
client .waitForSidecar (10000 ).block ();
89
87
daprRun .waitForAppHealth (10000 );
90
88
89
+ MethodInvokeServiceGrpc .MethodInvokeServiceBlockingStub stub = createGrpcStub (client );
91
90
long started = System .currentTimeMillis ();
92
91
SleepRequest req = SleepRequest .newBuilder ().setSeconds (1 ).build ();
93
92
String message = assertThrows (StatusRuntimeException .class , () -> stub .sleep (req )).getMessage ();
94
93
long delay = System .currentTimeMillis () - started ;
95
- assertTrue (delay >= timeoutMs );
96
- assertTrue (message .startsWith ("DEADLINE_EXCEEDED: deadline exceeded after" ) );
94
+ assertTrue (delay >= TIMEOUT_MS , "Delay: " + delay + " is not greater than timeout: " + TIMEOUT_MS );
95
+ assertTrue (message .startsWith (EXCEPTION_MARKER ), "Message: " + message + " does not start with: " + EXCEPTION_MARKER );
97
96
}
98
97
}
99
98
@@ -103,9 +102,7 @@ public void testInvokeException() throws Exception {
103
102
client .waitForSidecar (10000 ).block ();
104
103
daprRun .waitForAppHealth (10000 );
105
104
106
- MethodInvokeServiceGrpc .MethodInvokeServiceBlockingStub stub = client .newGrpcStub (
107
- daprRun .getAppName (),
108
- channel -> MethodInvokeServiceGrpc .newBlockingStub (channel ));
105
+ MethodInvokeServiceGrpc .MethodInvokeServiceBlockingStub stub = createGrpcStub (client );
109
106
110
107
SleepRequest req = SleepRequest .newBuilder ().setSeconds (-9 ).build ();
111
108
StatusRuntimeException exception = assertThrows (StatusRuntimeException .class , () -> stub .sleep (req ));
@@ -118,4 +115,8 @@ public void testInvokeException() throws Exception {
118
115
assertEquals ("" , exception .getStatus ().getDescription ());
119
116
}
120
117
}
118
+
119
+ private MethodInvokeServiceGrpc .MethodInvokeServiceBlockingStub createGrpcStub (DaprClient client ) {
120
+ return client .newGrpcStub (daprRun .getAppName (), MethodInvokeServiceGrpc ::newBlockingStub );
121
+ }
121
122
}
0 commit comments