-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14697 from gaetancollaud/feature/grpc-context-blo…
…cking-simple Forward gRPC context when using @Blocking
- Loading branch information
Showing
4 changed files
with
109 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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
92 changes: 92 additions & 0 deletions
92
...runtime/src/test/java/io/quarkus/grpc/runtime/supports/BlockingServerInterceptorTest.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package io.quarkus.grpc.runtime.supports; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.Arrays; | ||
import java.util.concurrent.CountDownLatch; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.Timeout; | ||
|
||
import io.grpc.Context; | ||
import io.grpc.Metadata; | ||
import io.grpc.MethodDescriptor; | ||
import io.grpc.ServerCall; | ||
import io.grpc.ServerCallHandler; | ||
import io.vertx.core.Vertx; | ||
|
||
class BlockingServerInterceptorTest { | ||
public static final Context.Key<String> USERNAME = Context.key("username"); | ||
|
||
BlockingServerInterceptor blockingServerInterceptor; | ||
Vertx vertx; | ||
|
||
@BeforeEach | ||
void setup() { | ||
vertx = Vertx.vertx(); | ||
blockingServerInterceptor = new BlockingServerInterceptor(vertx, Arrays.asList("blocking")); | ||
} | ||
|
||
@Test | ||
@Timeout(10) | ||
void testContextPropagation() throws Exception { | ||
final ServerCall serverCall = mock(ServerCall.class); | ||
final BlockingServerCallHandler serverCallHandler = new BlockingServerCallHandler(); | ||
final MethodDescriptor methodDescriptor = mock(MethodDescriptor.class); | ||
when(methodDescriptor.getFullMethodName()).thenReturn("my-service/blocking"); | ||
when(serverCall.getMethodDescriptor()).thenReturn(methodDescriptor); | ||
|
||
// setting grpc context | ||
final Context context = Context.current().withValue(USERNAME, "my-user"); | ||
|
||
final ServerCall.Listener listener = blockingServerInterceptor.interceptCall(serverCall, null, serverCallHandler); | ||
serverCallHandler.awaitSetup(); | ||
|
||
// simulate GRPC call | ||
context.wrap(() -> listener.onMessage("hello")).run(); | ||
|
||
// await for the message to be received | ||
serverCallHandler.await(); | ||
|
||
// check that the thread is a worker thread | ||
assertThat(serverCallHandler.threadName).contains("vert.x").contains("worker"); | ||
|
||
// check that the context was propagated correctly | ||
assertThat(serverCallHandler.contextUserName).isEqualTo("my-user"); | ||
} | ||
|
||
static class BlockingServerCallHandler implements ServerCallHandler { | ||
String threadName; | ||
String contextUserName; | ||
private CountDownLatch latch = new CountDownLatch(1); | ||
private CountDownLatch setupLatch = new CountDownLatch(1); | ||
|
||
@Override | ||
public ServerCall.Listener startCall(ServerCall serverCall, Metadata metadata) { | ||
final ServerCall.Listener listener = new ServerCall.Listener() { | ||
@Override | ||
public void onMessage(Object message) { | ||
threadName = Thread.currentThread().getName(); | ||
contextUserName = USERNAME.get(); | ||
super.onMessage(message); | ||
latch.countDown(); | ||
} | ||
}; | ||
setupLatch.countDown(); | ||
return listener; | ||
} | ||
|
||
public void awaitSetup() throws InterruptedException { | ||
setupLatch.await(); | ||
Thread.sleep(100); | ||
} | ||
|
||
public void await() throws InterruptedException { | ||
latch.await(); | ||
} | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
extensions/grpc/runtime/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker
This file contains 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
mock-maker-inline |