-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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 #19015 from michalszynkiewicz/grpc-client-lock-det…
…ection gRPC blocking client calls - event loop detection
- Loading branch information
Showing
8 changed files
with
141 additions
and
6 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
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
43 changes: 43 additions & 0 deletions
43
...yment/src/test/java/io/quarkus/grpc/lock/detection/BlockingClientCallOnEventLoopTest.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,43 @@ | ||
package io.quarkus.grpc.lock.detection; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.time.Duration; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.grpc.examples.helloworld.GreeterGrpc; | ||
import io.quarkus.grpc.GrpcClient; | ||
import io.quarkus.grpc.blocking.call.test.CallBlocking; | ||
import io.quarkus.grpc.blocking.call.test.CallBlockingGrpc; | ||
import io.quarkus.grpc.blocking.call.test.CallHello; | ||
import io.quarkus.grpc.server.services.HelloService; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
public class BlockingClientCallOnEventLoopTest { | ||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.setFlatClassPath(true) | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(HelloService.class, CallBlockingService.class, CallBlocking.class) | ||
.addPackage(GreeterGrpc.class.getPackage()) | ||
.addPackage(CallBlockingGrpc.class.getPackage())); | ||
|
||
@GrpcClient | ||
CallBlocking callBlocking; | ||
|
||
@Test | ||
void shouldThrowExceptionOnBlockingClientCall() { | ||
Uni<CallHello.SuccessOrFailureDescription> result = callBlocking.doBlockingCall(CallHello.Empty.getDefaultInstance()); | ||
CallHello.SuccessOrFailureDescription response = result.await().atMost(Duration.ofSeconds(10)); | ||
|
||
assertThat(response.getSuccess()).isFalse(); | ||
|
||
assertThat(response.getErrorDescription()).contains(CallBlockingService.EXPECTED_ERROR_PREFIX); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...ons/grpc/deployment/src/test/java/io/quarkus/grpc/lock/detection/CallBlockingService.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,39 @@ | ||
package io.quarkus.grpc.lock.detection; | ||
|
||
import org.jboss.logging.Logger; | ||
|
||
import io.grpc.examples.helloworld.GreeterGrpc; | ||
import io.grpc.examples.helloworld.HelloReply; | ||
import io.grpc.examples.helloworld.HelloRequest; | ||
import io.quarkus.grpc.GrpcClient; | ||
import io.quarkus.grpc.GrpcService; | ||
import io.quarkus.grpc.blocking.call.test.CallBlocking; | ||
import io.quarkus.grpc.blocking.call.test.CallHello; | ||
import io.quarkus.grpc.blocking.call.test.CallHello.SuccessOrFailureDescription; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
@GrpcService | ||
public class CallBlockingService implements CallBlocking { | ||
|
||
public static String EXPECTED_ERROR_PREFIX = "Blocking gRPC client call made from the event loop"; | ||
|
||
private static final Logger log = Logger.getLogger(CallBlockingService.class); | ||
|
||
@GrpcClient | ||
GreeterGrpc.GreeterBlockingStub blockingClient; | ||
|
||
@Override | ||
public Uni<SuccessOrFailureDescription> doBlockingCall(CallHello.Empty request) { | ||
try { | ||
HelloReply reply = blockingClient.sayHello(HelloRequest.newBuilder().setName("Bob the Blocker").build()); | ||
return Uni.createFrom().item(reply) | ||
.map(helloReply -> SuccessOrFailureDescription.newBuilder().setSuccess(true).build()); | ||
} catch (Exception e) { | ||
if (!e.getMessage().contains(EXPECTED_ERROR_PREFIX)) { | ||
log.error("Error ", e); | ||
} | ||
return Uni.createFrom().item(SuccessOrFailureDescription.newBuilder().setSuccess(false) | ||
.setErrorDescription(e.getMessage()).build()); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
extensions/grpc/deployment/src/test/proto/call-hello.proto
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,16 @@ | ||
syntax = "proto3"; | ||
|
||
package blocking.call.test; | ||
|
||
option java_package = "io.quarkus.grpc.blocking.call.test"; | ||
|
||
service CallBlocking { | ||
rpc doBlockingCall(Empty) returns (SuccessOrFailureDescription); | ||
} | ||
|
||
message Empty {} | ||
|
||
message SuccessOrFailureDescription { | ||
bool success = 1; | ||
string error_description = 2; | ||
} |
21 changes: 21 additions & 0 deletions
21
...ime/src/main/java/io/quarkus/grpc/runtime/supports/EventLoopBlockingCheckInterceptor.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,21 @@ | ||
package io.quarkus.grpc.runtime.supports; | ||
|
||
import io.grpc.CallOptions; | ||
import io.grpc.Channel; | ||
import io.grpc.ClientCall; | ||
import io.grpc.ClientInterceptor; | ||
import io.grpc.MethodDescriptor; | ||
import io.vertx.core.Context; | ||
|
||
public class EventLoopBlockingCheckInterceptor implements ClientInterceptor { | ||
@Override | ||
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, | ||
Channel next) { | ||
if (Context.isOnEventLoopThread()) { | ||
throw new IllegalStateException("Blocking gRPC client call made from the event loop. " + | ||
"If the code is executed from a gRPC service or a RESTEasy Reactive resource, either annotate the method " + | ||
" that makes the call with `@Blocking` or use the non-blocking client."); | ||
} | ||
return next.newCall(method, callOptions); | ||
} | ||
} |
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