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();
}
Aggregations