@@ -27,6 +27,10 @@ public class MethodInvokeIT extends BaseIT {
2727
2828 //Number of messages to be sent: 10
2929 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" ;
3034
3135 /**
3236 * Run of a Dapr application.
@@ -47,28 +51,27 @@ public void init() throws Exception {
4751 @ Test
4852 public void testInvoke () throws Exception {
4953 try (DaprClient client = new DaprClientBuilder ().build ()) {
50- MethodInvokeServiceGrpc .MethodInvokeServiceBlockingStub stub = client .newGrpcStub (
51- daprRun .getAppName (),
52- channel -> MethodInvokeServiceGrpc .newBlockingStub (channel ));
5354 client .waitForSidecar (10000 ).block ();
5455 daprRun .waitForAppHealth (10000 );
56+
57+ MethodInvokeServiceGrpc .MethodInvokeServiceBlockingStub stub = createGrpcStub (client );
5558
5659 for (int i = 0 ; i < NUM_MESSAGES ; i ++) {
5760 String message = String .format ("This is message #%d" , i );
58-
5961 PostMessageRequest req = PostMessageRequest .newBuilder ().setId (i ).setMessage (message ).build ();
62+
6063 stub .postMessage (req );
6164
6265 System .out .println ("Invoke method messages : " + message );
6366 }
6467
6568 Map <Integer , String > messages = stub .getMessages (GetMessagesRequest .newBuilder ().build ()).getMessagesMap ();
66- assertEquals (10 , messages .size ());
69+ assertEquals (NUM_MESSAGES , messages .size ());
6770
6871 // Delete one message.
6972 stub .deleteMessage (DeleteMessageRequest .newBuilder ().setId (1 ).build ());
7073 messages = stub .getMessages (GetMessagesRequest .newBuilder ().build ()).getMessagesMap ();
71- assertEquals (9 , messages .size ());
74+ assertEquals (NUM_MESSAGES - 1 , messages .size ());
7275
7376 // Now update one message.
7477 stub .postMessage (PostMessageRequest .newBuilder ().setId (2 ).setMessage ("updated message" ).build ());
@@ -79,21 +82,17 @@ public void testInvoke() throws Exception {
7982
8083 @ Test
8184 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 ()) {
8886 client .waitForSidecar (10000 ).block ();
8987 daprRun .waitForAppHealth (10000 );
9088
89+ MethodInvokeServiceGrpc .MethodInvokeServiceBlockingStub stub = createGrpcStub (client );
9190 long started = System .currentTimeMillis ();
9291 SleepRequest req = SleepRequest .newBuilder ().setSeconds (1 ).build ();
9392 String message = assertThrows (StatusRuntimeException .class , () -> stub .sleep (req )).getMessage ();
9493 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 );
9796 }
9897 }
9998
@@ -103,9 +102,7 @@ public void testInvokeException() throws Exception {
103102 client .waitForSidecar (10000 ).block ();
104103 daprRun .waitForAppHealth (10000 );
105104
106- MethodInvokeServiceGrpc .MethodInvokeServiceBlockingStub stub = client .newGrpcStub (
107- daprRun .getAppName (),
108- channel -> MethodInvokeServiceGrpc .newBlockingStub (channel ));
105+ MethodInvokeServiceGrpc .MethodInvokeServiceBlockingStub stub = createGrpcStub (client );
109106
110107 SleepRequest req = SleepRequest .newBuilder ().setSeconds (-9 ).build ();
111108 StatusRuntimeException exception = assertThrows (StatusRuntimeException .class , () -> stub .sleep (req ));
@@ -118,4 +115,8 @@ public void testInvokeException() throws Exception {
118115 assertEquals ("" , exception .getStatus ().getDescription ());
119116 }
120117 }
118+
119+ private MethodInvokeServiceGrpc .MethodInvokeServiceBlockingStub createGrpcStub (DaprClient client ) {
120+ return client .newGrpcStub (daprRun .getAppName (), MethodInvokeServiceGrpc ::newBlockingStub );
121+ }
121122}
0 commit comments