|
18 | 18 | import io.grpc.examples.streaming.Empty; |
19 | 19 | import io.grpc.examples.streaming.Item; |
20 | 20 | import io.grpc.examples.streaming.StreamingGrpc; |
| 21 | +import io.grpc.examples.streaming.StreamingGrpc.StreamingImplBase; |
21 | 22 | import io.grpc.protobuf.StatusProto; |
22 | 23 | import io.grpc.stub.ServerCallStreamObserver; |
23 | 24 | import io.grpc.stub.StreamObserver; |
| 25 | +import io.vertx.core.Promise; |
24 | 26 | import io.vertx.ext.unit.Async; |
25 | 27 | import io.vertx.ext.unit.TestContext; |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.concurrent.TimeUnit; |
26 | 31 | import org.junit.Test; |
27 | 32 |
|
28 | 33 | import java.util.concurrent.atomic.AtomicInteger; |
@@ -463,4 +468,78 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, Re |
463 | 468 | HelloReply res = stub.sayHello(request); |
464 | 469 | should.assertEquals(1, testAttributesStep.get()); |
465 | 470 | } |
| 471 | + |
| 472 | + @Test |
| 473 | + public void testCallNetworkInterrupted(TestContext should) throws InterruptedException, IOException { |
| 474 | + AtomicInteger requestCount = new AtomicInteger(); |
| 475 | + Promise<Void> completed = Promise.promise(); |
| 476 | + Async async = should.async(); |
| 477 | + |
| 478 | + StreamingGrpc.StreamingImplBase impl = new StreamingImplBase() { |
| 479 | + @Override |
| 480 | + public StreamObserver<Item> pipe(StreamObserver<Item> responseObserver) { |
| 481 | + return new StreamObserver<Item>() { |
| 482 | + @Override |
| 483 | + public void onNext(Item item) { |
| 484 | + requestCount.incrementAndGet(); |
| 485 | + } |
| 486 | + |
| 487 | + @Override |
| 488 | + public void onError(Throwable throwable) { |
| 489 | + completed.fail(throwable); |
| 490 | + async.complete(); |
| 491 | + } |
| 492 | + |
| 493 | + @Override |
| 494 | + public void onCompleted() { |
| 495 | + completed.complete(); |
| 496 | + async.complete(); |
| 497 | + } |
| 498 | + }; |
| 499 | + } |
| 500 | + }; |
| 501 | + |
| 502 | + GrpcServer server = GrpcServer.server(vertx); |
| 503 | + GrpcServiceBridge serverStub = GrpcServiceBridge.bridge(impl); |
| 504 | + serverStub.bind(server); |
| 505 | + startServer(server); |
| 506 | + |
| 507 | + Process client = ProcessHelper.exec(ServerBridgeTest.class, Collections.singletonList(String.valueOf(port))); |
| 508 | + // waiting for doing request |
| 509 | + Thread.sleep(1_000); |
| 510 | + client.destroy(); |
| 511 | + client.waitFor(); |
| 512 | + |
| 513 | + async.await(20_000); |
| 514 | + |
| 515 | + should.assertEquals(requestCount.get(), 3); |
| 516 | + should.assertTrue(completed.future().failed()); |
| 517 | + } |
| 518 | + |
| 519 | + public static void main(String... args) throws InterruptedException { |
| 520 | + StreamObserver<Item> noop = new StreamObserver<Item>() { |
| 521 | + @Override public void onNext(Item item) { |
| 522 | + |
| 523 | + } |
| 524 | + |
| 525 | + @Override public void onError(Throwable throwable) { |
| 526 | + |
| 527 | + } |
| 528 | + |
| 529 | + @Override public void onCompleted() { |
| 530 | + |
| 531 | + } |
| 532 | + }; |
| 533 | + |
| 534 | + Channel channel = ManagedChannelBuilder.forAddress("localhost", Integer.parseInt(args[0])).usePlaintext().build(); |
| 535 | + StreamingGrpc.StreamingStub stub = StreamingGrpc.newStub(channel); |
| 536 | + StreamObserver<Item> requestObserver = stub.pipe(noop); |
| 537 | + Item request = Item.newBuilder().setValue("item").build(); |
| 538 | + requestObserver.onNext(request); |
| 539 | + requestObserver.onNext(request); |
| 540 | + requestObserver.onNext(request); |
| 541 | + |
| 542 | + // waiting to be killed |
| 543 | + Thread.currentThread().join(); |
| 544 | + } |
466 | 545 | } |
0 commit comments