|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.showcase.v1beta1.it; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static org.junit.Assert.assertThrows; |
| 21 | + |
| 22 | +import com.google.api.core.SettableApiFuture; |
| 23 | +import com.google.api.gax.core.NoCredentialsProvider; |
| 24 | +import com.google.api.gax.rpc.ApiStreamObserver; |
| 25 | +import com.google.api.gax.rpc.CancelledException; |
| 26 | +import com.google.api.gax.rpc.StatusCode; |
| 27 | +import com.google.rpc.Status; |
| 28 | +import com.google.showcase.v1beta1.EchoClient; |
| 29 | +import com.google.showcase.v1beta1.EchoRequest; |
| 30 | +import com.google.showcase.v1beta1.EchoResponse; |
| 31 | +import com.google.showcase.v1beta1.EchoSettings; |
| 32 | +import io.grpc.ManagedChannelBuilder; |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.List; |
| 36 | +import java.util.concurrent.ExecutionException; |
| 37 | +import org.junit.After; |
| 38 | +import org.junit.Before; |
| 39 | +import org.junit.Test; |
| 40 | + |
| 41 | +public class ITClientSideStreaming { |
| 42 | + |
| 43 | + private EchoClient grpcClient; |
| 44 | + |
| 45 | + @Before |
| 46 | + public void createClients() throws IOException { |
| 47 | + // Create gRPC Echo Client |
| 48 | + EchoSettings grpcEchoSettings = |
| 49 | + EchoSettings.newBuilder() |
| 50 | + .setCredentialsProvider(NoCredentialsProvider.create()) |
| 51 | + .setTransportChannelProvider( |
| 52 | + EchoSettings.defaultGrpcTransportProviderBuilder() |
| 53 | + .setChannelConfigurator(ManagedChannelBuilder::usePlaintext) |
| 54 | + .build()) |
| 55 | + .build(); |
| 56 | + grpcClient = EchoClient.create(grpcEchoSettings); |
| 57 | + } |
| 58 | + |
| 59 | + @After |
| 60 | + public void destroyClient() { |
| 61 | + grpcClient.close(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testGrpc_sendStreamedContent_receiveConcatenatedResponse() |
| 66 | + throws ExecutionException, InterruptedException { |
| 67 | + CollectStreamObserver<EchoResponse> responseObserver = new CollectStreamObserver<>(); |
| 68 | + ApiStreamObserver<EchoRequest> requestObserver = |
| 69 | + grpcClient.collectCallable().clientStreamingCall(responseObserver); |
| 70 | + String content = "The rain in Spain stays mainly on the plain"; |
| 71 | + for (String message : content.split("\\s")) { |
| 72 | + requestObserver.onNext(EchoRequest.newBuilder().setContent(message).build()); |
| 73 | + } |
| 74 | + requestObserver.onCompleted(); |
| 75 | + |
| 76 | + List<EchoResponse> serverResponse = responseObserver.future().get(); |
| 77 | + assertThat(serverResponse).hasSize(1); |
| 78 | + assertThat(serverResponse.get(0).getContent()) |
| 79 | + .isEqualTo("The rain in Spain stays mainly on the plain"); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testGrpc_sendStreamedContent_handleServerError() { |
| 84 | + CollectStreamObserver<EchoResponse> responseObserver = new CollectStreamObserver<>(); |
| 85 | + ApiStreamObserver<EchoRequest> requestObserver = |
| 86 | + grpcClient.collectCallable().clientStreamingCall(responseObserver); |
| 87 | + String content = "The rain in Spain stays mainly on the plain"; |
| 88 | + for (String message : content.split("\\s")) { |
| 89 | + requestObserver.onNext(EchoRequest.newBuilder().setContent(message).build()); |
| 90 | + } |
| 91 | + Status cancelledStatus = |
| 92 | + Status.newBuilder().setCode(StatusCode.Code.CANCELLED.ordinal()).build(); |
| 93 | + requestObserver.onNext(EchoRequest.newBuilder().setError(cancelledStatus).build()); |
| 94 | + requestObserver.onCompleted(); |
| 95 | + |
| 96 | + ExecutionException exception = |
| 97 | + assertThrows(ExecutionException.class, () -> responseObserver.future().get()); |
| 98 | + assertThat(exception.getCause()).isInstanceOf(CancelledException.class); |
| 99 | + CancelledException cancelledException = (CancelledException) exception.getCause(); |
| 100 | + assertThat(cancelledException.getStatusCode().getCode()).isEqualTo(StatusCode.Code.CANCELLED); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Implementation of {@link ApiStreamObserver} to accumulate streamed content. |
| 105 | + * |
| 106 | + * @param <T> |
| 107 | + */ |
| 108 | + private class CollectStreamObserver<T> implements ApiStreamObserver<T> { |
| 109 | + |
| 110 | + private final SettableApiFuture<List<T>> future = SettableApiFuture.create(); |
| 111 | + private final List<T> messages = new ArrayList<>(); |
| 112 | + |
| 113 | + @Override |
| 114 | + public void onNext(T message) { |
| 115 | + this.messages.add(message); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void onError(Throwable throwable) { |
| 120 | + this.future.setException(throwable); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void onCompleted() { |
| 125 | + future.set(this.messages); |
| 126 | + } |
| 127 | + |
| 128 | + public SettableApiFuture<List<T>> future() { |
| 129 | + return this.future; |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments