Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add failure messages to GRPC method invoke IT test #1077

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public class MethodInvokeIT extends BaseIT {

//Number of messages to be sent: 10
private static final int NUM_MESSAGES = 10;
private static final int TIMEOUT_MS = 100;
private static final ResiliencyOptions RESILIENCY_OPTIONS = new ResiliencyOptions()
.setTimeout(Duration.ofMillis(TIMEOUT_MS));
private static final String EXCEPTION_MARKER = "DEADLINE_EXCEEDED: deadline exceeded after";

/**
* Run of a Dapr application.
Expand All @@ -47,28 +51,27 @@ public void init() throws Exception {
@Test
public void testInvoke() throws Exception {
try (DaprClient client = new DaprClientBuilder().build()) {
MethodInvokeServiceGrpc.MethodInvokeServiceBlockingStub stub = client.newGrpcStub(
daprRun.getAppName(),
channel -> MethodInvokeServiceGrpc.newBlockingStub(channel));
client.waitForSidecar(10000).block();
daprRun.waitForAppHealth(10000);

MethodInvokeServiceGrpc.MethodInvokeServiceBlockingStub stub = createGrpcStub(client);

for (int i = 0; i < NUM_MESSAGES; i++) {
String message = String.format("This is message #%d", i);

PostMessageRequest req = PostMessageRequest.newBuilder().setId(i).setMessage(message).build();

stub.postMessage(req);

System.out.println("Invoke method messages : " + message);
}

Map<Integer, String> messages = stub.getMessages(GetMessagesRequest.newBuilder().build()).getMessagesMap();
assertEquals(10, messages.size());
assertEquals(NUM_MESSAGES, messages.size());

// Delete one message.
stub.deleteMessage(DeleteMessageRequest.newBuilder().setId(1).build());
messages = stub.getMessages(GetMessagesRequest.newBuilder().build()).getMessagesMap();
assertEquals(9, messages.size());
assertEquals(NUM_MESSAGES - 1, messages.size());

// Now update one message.
stub.postMessage(PostMessageRequest.newBuilder().setId(2).setMessage("updated message").build());
Expand All @@ -79,21 +82,17 @@ public void testInvoke() throws Exception {

@Test
public void testInvokeTimeout() throws Exception {
long timeoutMs = 100;
ResiliencyOptions resiliencyOptions = new ResiliencyOptions().setTimeout(Duration.ofMillis(timeoutMs));
try (DaprClient client = new DaprClientBuilder().withResiliencyOptions(resiliencyOptions).build()) {
MethodInvokeServiceGrpc.MethodInvokeServiceBlockingStub stub = client.newGrpcStub(
daprRun.getAppName(),
channel -> MethodInvokeServiceGrpc.newBlockingStub(channel));
try (DaprClient client = new DaprClientBuilder().withResiliencyOptions(RESILIENCY_OPTIONS).build()) {
client.waitForSidecar(10000).block();
daprRun.waitForAppHealth(10000);

MethodInvokeServiceGrpc.MethodInvokeServiceBlockingStub stub = createGrpcStub(client);
long started = System.currentTimeMillis();
SleepRequest req = SleepRequest.newBuilder().setSeconds(1).build();
String message = assertThrows(StatusRuntimeException.class, () -> stub.sleep(req)).getMessage();
long delay = System.currentTimeMillis() - started;
assertTrue(delay >= timeoutMs);
assertTrue(message.startsWith("DEADLINE_EXCEEDED: deadline exceeded after"));
assertTrue(delay >= TIMEOUT_MS, "Delay: " + delay + " is not greater than timeout: " + TIMEOUT_MS);
assertTrue(message.startsWith(EXCEPTION_MARKER), "Message: " + message + " does not start with: " + EXCEPTION_MARKER);
}
}

Expand All @@ -103,9 +102,7 @@ public void testInvokeException() throws Exception {
client.waitForSidecar(10000).block();
daprRun.waitForAppHealth(10000);

MethodInvokeServiceGrpc.MethodInvokeServiceBlockingStub stub = client.newGrpcStub(
daprRun.getAppName(),
channel -> MethodInvokeServiceGrpc.newBlockingStub(channel));
MethodInvokeServiceGrpc.MethodInvokeServiceBlockingStub stub = createGrpcStub(client);

SleepRequest req = SleepRequest.newBuilder().setSeconds(-9).build();
StatusRuntimeException exception = assertThrows(StatusRuntimeException.class, () -> stub.sleep(req));
Expand All @@ -118,4 +115,8 @@ public void testInvokeException() throws Exception {
assertEquals("", exception.getStatus().getDescription());
}
}

private MethodInvokeServiceGrpc.MethodInvokeServiceBlockingStub createGrpcStub(DaprClient client) {
return client.newGrpcStub(daprRun.getAppName(), MethodInvokeServiceGrpc::newBlockingStub);
}
}
Loading