-
Notifications
You must be signed in to change notification settings - Fork 228
Change Actor gRPC client to async. #564
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,114 +5,127 @@ | |
|
|
||
| package io.dapr.actors.client; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.google.common.util.concurrent.SettableFuture; | ||
| import com.google.protobuf.ByteString; | ||
| import io.dapr.v1.DaprGrpc; | ||
| import io.dapr.v1.DaprProtos; | ||
| import io.grpc.ManagedChannel; | ||
| import io.grpc.Status; | ||
| import io.grpc.StatusException; | ||
| import io.grpc.inprocess.InProcessChannelBuilder; | ||
| import io.grpc.inprocess.InProcessServerBuilder; | ||
| import io.grpc.stub.StreamObserver; | ||
| import io.grpc.testing.GrpcCleanupRule; | ||
| import org.junit.Before; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| import static io.dapr.actors.TestUtils.assertThrowsDaprException; | ||
| import static org.junit.Assert.*; | ||
| import static org.mockito.AdditionalAnswers.delegatesTo; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| public class DaprGrpcClientTest { | ||
|
|
||
| private static final String ACTOR_TYPE = "MyActorType"; | ||
|
|
||
| private static final String ACTOR_ID = "1234567890"; | ||
|
|
||
| private DaprGrpc.DaprFutureStub grpcStub; | ||
| private static final String ACTOR_ID_OK = "123-Ok"; | ||
|
|
||
| private static final String ACTOR_ID_NULL_INPUT = "123-Null"; | ||
|
|
||
| private static final String ACTOR_ID_EXCEPTION = "123-Exception"; | ||
|
|
||
| private static final String METHOD_NAME = "myMethod"; | ||
|
|
||
| private static final byte[] REQUEST_PAYLOAD = "{ \"id\": 123 }".getBytes(); | ||
|
|
||
| private static final byte[] RESPONSE_PAYLOAD = "\"OK\"".getBytes(); | ||
|
|
||
| @Rule | ||
| public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule(); | ||
|
|
||
| private final DaprGrpc.DaprImplBase serviceImpl = | ||
| mock(DaprGrpc.DaprImplBase.class, delegatesTo( | ||
| new DaprGrpc.DaprImplBase() { | ||
| @Override | ||
| public void invokeActor(DaprProtos.InvokeActorRequest request, | ||
| StreamObserver<DaprProtos.InvokeActorResponse> responseObserver) { | ||
| assertEquals(ACTOR_TYPE, request.getActorType()); | ||
| assertEquals(METHOD_NAME, request.getMethod()); | ||
| switch (request.getActorId()) { | ||
| case ACTOR_ID_OK: | ||
| assertArrayEquals(REQUEST_PAYLOAD, request.getData().toByteArray()); | ||
| responseObserver.onNext( | ||
| DaprProtos.InvokeActorResponse.newBuilder().setData(ByteString.copyFrom(RESPONSE_PAYLOAD)) | ||
| .build()); | ||
| responseObserver.onCompleted(); | ||
| return; | ||
| case ACTOR_ID_NULL_INPUT: | ||
| assertArrayEquals(new byte[0], request.getData().toByteArray()); | ||
| responseObserver.onNext( | ||
| DaprProtos.InvokeActorResponse.newBuilder().setData(ByteString.copyFrom(RESPONSE_PAYLOAD)) | ||
| .build()); | ||
| responseObserver.onCompleted(); | ||
| return; | ||
|
|
||
| case ACTOR_ID_EXCEPTION: | ||
| Throwable e = new ArithmeticException(); | ||
| StatusException se = new StatusException(Status.UNKNOWN.withCause(e)); | ||
| responseObserver.onError(se); | ||
| return; | ||
| } | ||
| super.invokeActor(request, responseObserver); | ||
| } | ||
| })); | ||
|
|
||
| private DaprGrpcClient client; | ||
|
|
||
| @Before | ||
| public void setup() { | ||
| grpcStub = mock(DaprGrpc.DaprFutureStub.class); | ||
| client = new DaprGrpcClient(grpcStub); | ||
| public void setup() throws IOException { | ||
| // Generate a unique in-process server name. | ||
| String serverName = InProcessServerBuilder.generateName(); | ||
|
|
||
| // Create a server, add service, start, and register for automatic graceful shutdown. | ||
| grpcCleanup.register(InProcessServerBuilder | ||
| .forName(serverName).directExecutor().addService(serviceImpl).build().start()); | ||
|
|
||
| // Create a client channel and register for automatic graceful shutdown. | ||
| ManagedChannel channel = grpcCleanup.register( | ||
| InProcessChannelBuilder.forName(serverName).directExecutor().build()); | ||
|
|
||
| // Create a HelloWorldClient using the in-process channel; | ||
| client = new DaprGrpcClient(DaprGrpc.newStub(channel)); | ||
| } | ||
|
|
||
| @Test | ||
| public void invoke() { | ||
| String methodName = "mymethod"; | ||
| byte[] payload = "{ \"id\": 123 }".getBytes(); | ||
| byte[] response = "\"OK\"".getBytes(); | ||
|
|
||
| SettableFuture<DaprProtos.InvokeActorResponse> settableFuture = SettableFuture.create(); | ||
| settableFuture.set(DaprProtos.InvokeActorResponse.newBuilder().setData(ByteString.copyFrom(response)).build()); | ||
|
|
||
| when(grpcStub.invokeActor(argThat(argument -> { | ||
| assertEquals(ACTOR_TYPE, argument.getActorType()); | ||
| assertEquals(ACTOR_ID, argument.getActorId()); | ||
| assertEquals(methodName, argument.getMethod()); | ||
| assertArrayEquals(payload, argument.getData().toByteArray()); | ||
| return true; | ||
| }))).thenReturn(settableFuture); | ||
| Mono<byte[]> result = client.invoke(ACTOR_TYPE, ACTOR_ID, methodName, payload); | ||
| assertArrayEquals(response, result.block()); | ||
| Mono<byte[]> result = client.invoke(ACTOR_TYPE, ACTOR_ID_OK, METHOD_NAME, REQUEST_PAYLOAD); | ||
| assertArrayEquals(RESPONSE_PAYLOAD, result.block()); | ||
| } | ||
|
|
||
| @Test | ||
| public void invokeNullPayload() { | ||
| String methodName = "mymethod"; | ||
| byte[] response = "\"OK\"".getBytes(); | ||
|
|
||
| SettableFuture<DaprProtos.InvokeActorResponse> settableFuture = SettableFuture.create(); | ||
| settableFuture.set(DaprProtos.InvokeActorResponse.newBuilder().setData(ByteString.copyFrom(response)).build()); | ||
|
|
||
| when(grpcStub.invokeActor(argThat(argument -> { | ||
| assertEquals(ACTOR_TYPE, argument.getActorType()); | ||
| assertEquals(ACTOR_ID, argument.getActorId()); | ||
| assertEquals(methodName, argument.getMethod()); | ||
| assertArrayEquals(new byte[0], argument.getData().toByteArray()); | ||
| return true; | ||
| }))).thenReturn(settableFuture); | ||
| Mono<byte[]> result = client.invoke(ACTOR_TYPE, ACTOR_ID, methodName, null); | ||
| assertArrayEquals(response, result.block()); | ||
| Mono<byte[]> result = client.invoke(ACTOR_TYPE, ACTOR_ID_NULL_INPUT, METHOD_NAME, null); | ||
| assertArrayEquals(RESPONSE_PAYLOAD, result.block()); | ||
| } | ||
|
|
||
| @Test | ||
| public void invokeException() { | ||
| String methodName = "mymethod"; | ||
|
|
||
| SettableFuture<DaprProtos.InvokeActorResponse> settableFuture = SettableFuture.create(); | ||
| settableFuture.setException(new ArithmeticException()); | ||
|
|
||
| when(grpcStub.invokeActor(argThat(argument -> { | ||
| assertEquals(ACTOR_TYPE, argument.getActorType()); | ||
| assertEquals(ACTOR_ID, argument.getActorId()); | ||
| assertEquals(methodName, argument.getMethod()); | ||
| assertArrayEquals(new byte[0], argument.getData().toByteArray()); | ||
| return true; | ||
| }))).thenReturn(settableFuture); | ||
| Mono<byte[]> result = client.invoke(ACTOR_TYPE, ACTOR_ID, methodName, null); | ||
| Mono<byte[]> result = client.invoke(ACTOR_TYPE, ACTOR_ID_EXCEPTION, METHOD_NAME, null); | ||
|
|
||
| assertThrowsDaprException( | ||
| ExecutionException.class, | ||
| "UNKNOWN", | ||
| "UNKNOWN: java.lang.ArithmeticException", | ||
| "UNKNOWN: ", | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has to do with how Exceptions are handled in the grpc library, it does not show the exception as it would in the blocking path. |
||
| () -> result.block()); | ||
| } | ||
|
|
||
| @Test | ||
| public void invokeNotHotMono() { | ||
| String methodName = "mymethod"; | ||
|
|
||
| SettableFuture<DaprProtos.InvokeActorResponse> settableFuture = SettableFuture.create(); | ||
| settableFuture.setException(new ArithmeticException()); | ||
|
|
||
| when(grpcStub.invokeActor(argThat(argument -> { | ||
| assertEquals(ACTOR_TYPE, argument.getActorType()); | ||
| assertEquals(ACTOR_ID, argument.getActorId()); | ||
| assertEquals(methodName, argument.getMethod()); | ||
| assertArrayEquals(new byte[0], argument.getData().toByteArray()); | ||
| return true; | ||
| }))).thenReturn(settableFuture); | ||
| client.invoke(ACTOR_TYPE, ACTOR_ID, methodName, null); | ||
| client.invoke(ACTOR_TYPE, ACTOR_ID_EXCEPTION, METHOD_NAME, null); | ||
| // No exception thrown because Mono is ignored here. | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a negative case if the token is empty? Or is this related to the feature that has to be enabled and if it's not we hit the null (expected) case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We hit null if not set. If it is set as empty, it is OK to pass it through.