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