Search in sources :

Example 1 with ClientStream

use of com.google.api.gax.rpc.ClientStream in project toolkit by googleapis.

the class ShowcaseTest method canHaveARandomChat.

@Test
public void canHaveARandomChat() throws InterruptedException {
    List<String> responses = new ArrayList<>();
    CountDownLatch latch = new CountDownLatch(1);
    List<String> inputs = IntStream.range(0, 5).mapToObj(idx -> new Random().ints(20).mapToObj(Integer::toString).collect(Collectors.joining("->"))).collect(Collectors.toList());
    client.chatCallable().call(new BidiStreamObserver<EchoRequest, EchoResponse>() {

        @Override
        public void onReady(ClientStream<EchoRequest> stream) {
            inputs.forEach(message -> stream.send(EchoRequest.newBuilder().setContent(message).build()));
            stream.closeSend();
        }

        @Override
        public void onStart(StreamController controller) {
        // skip...
        }

        @Override
        public void onResponse(EchoResponse response) {
            responses.add(response.getContent());
        }

        @Override
        public void onError(Throwable t) {
            fail("error not expected");
        }

        @Override
        public void onComplete() {
            latch.countDown();
        }
    });
    latch.await(7, TimeUnit.SECONDS);
    assertThat(responses).containsExactlyElementsIn(inputs).inOrder();
}
Also used : SequenceServiceClient(com.google.showcase.v1beta1.SequenceServiceClient) IntStream(java.util.stream.IntStream) BidiStreamObserver(com.google.api.gax.rpc.BidiStreamObserver) EchoSettings(com.google.showcase.v1beta1.EchoSettings) SequenceReport(com.google.showcase.v1beta1.SequenceReport) SequenceServiceSettings(com.google.showcase.v1beta1.SequenceServiceSettings) RunWith(org.junit.runner.RunWith) Random(java.util.Random) AttemptSequenceRequest(com.google.showcase.v1beta1.AttemptSequenceRequest) EchoClient(com.google.showcase.v1beta1.EchoClient) ArrayList(java.util.ArrayList) After(org.junit.After) Assert.fail(org.junit.Assert.fail) ServerStream(com.google.api.gax.rpc.ServerStream) Code(com.google.rpc.Code) Before(org.junit.Before) ClientStream(com.google.api.gax.rpc.ClientStream) Sequence(com.google.showcase.v1beta1.Sequence) EchoRequest(com.google.showcase.v1beta1.EchoRequest) EchoResponse(com.google.showcase.v1beta1.EchoResponse) BlockResponse(com.google.showcase.v1beta1.BlockResponse) ExpandRequest(com.google.showcase.v1beta1.ExpandRequest) Status(com.google.rpc.Status) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) AbortedException(com.google.api.gax.rpc.AbortedException) Collectors(java.util.stream.Collectors) StatusRuntimeException(io.grpc.StatusRuntimeException) TimeUnit(java.util.concurrent.TimeUnit) ApiStreamObserver(com.google.api.gax.rpc.ApiStreamObserver) CountDownLatch(java.util.concurrent.CountDownLatch) Duration(com.google.protobuf.Duration) List(java.util.List) BlockRequest(com.google.showcase.v1beta1.BlockRequest) StreamController(com.google.api.gax.rpc.StreamController) EchoResponse(com.google.showcase.v1beta1.EchoResponse) ArrayList(java.util.ArrayList) EchoRequest(com.google.showcase.v1beta1.EchoRequest) CountDownLatch(java.util.concurrent.CountDownLatch) StreamController(com.google.api.gax.rpc.StreamController) Random(java.util.Random) Test(org.junit.Test)

Aggregations

AbortedException (com.google.api.gax.rpc.AbortedException)1 ApiStreamObserver (com.google.api.gax.rpc.ApiStreamObserver)1 BidiStreamObserver (com.google.api.gax.rpc.BidiStreamObserver)1 ClientStream (com.google.api.gax.rpc.ClientStream)1 ServerStream (com.google.api.gax.rpc.ServerStream)1 StreamController (com.google.api.gax.rpc.StreamController)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 Duration (com.google.protobuf.Duration)1 Code (com.google.rpc.Code)1 Status (com.google.rpc.Status)1 AttemptSequenceRequest (com.google.showcase.v1beta1.AttemptSequenceRequest)1 BlockRequest (com.google.showcase.v1beta1.BlockRequest)1 BlockResponse (com.google.showcase.v1beta1.BlockResponse)1 EchoClient (com.google.showcase.v1beta1.EchoClient)1 EchoRequest (com.google.showcase.v1beta1.EchoRequest)1 EchoResponse (com.google.showcase.v1beta1.EchoResponse)1 EchoSettings (com.google.showcase.v1beta1.EchoSettings)1 ExpandRequest (com.google.showcase.v1beta1.ExpandRequest)1 Sequence (com.google.showcase.v1beta1.Sequence)1 SequenceReport (com.google.showcase.v1beta1.SequenceReport)1