Skip to content

Commit 159ce50

Browse files
authored
chore: add client-side streaming showcase integration tests for gRPC support (#1582)
* chore: add client-side streaming showcase integration tests for gRPC transport
1 parent d14875a commit 159ce50

File tree

3 files changed

+135
-6
lines changed

3 files changed

+135
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
}

showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITServerSideStreaming.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static org.junit.Assert.assertThrows;
2121

2222
import com.google.api.gax.core.NoCredentialsProvider;
23-
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
2423
import com.google.api.gax.rpc.CancelledException;
2524
import com.google.api.gax.rpc.ServerStream;
2625
import com.google.api.gax.rpc.StatusCode;
@@ -32,7 +31,6 @@
3231
import com.google.showcase.v1beta1.ExpandRequest;
3332
import io.grpc.ManagedChannelBuilder;
3433
import java.io.IOException;
35-
import java.security.GeneralSecurityException;
3634
import java.util.ArrayList;
3735
import java.util.Iterator;
3836
import org.junit.After;
@@ -44,13 +42,13 @@ public class ITServerSideStreaming {
4442
private EchoClient grpcClient;
4543

4644
@Before
47-
public void createClients() throws IOException, GeneralSecurityException {
45+
public void createClients() throws IOException {
4846
// Create gRPC Echo Client
4947
EchoSettings grpcEchoSettings =
5048
EchoSettings.newBuilder()
5149
.setCredentialsProvider(NoCredentialsProvider.create())
5250
.setTransportChannelProvider(
53-
InstantiatingGrpcChannelProvider.newBuilder()
51+
EchoSettings.defaultGrpcTransportProviderBuilder()
5452
.setChannelConfigurator(ManagedChannelBuilder::usePlaintext)
5553
.build())
5654
.build();

showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITUnaryCallable.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.api.client.http.javanet.NetHttpTransport;
2323
import com.google.api.gax.core.NoCredentialsProvider;
2424
import com.google.api.gax.grpc.GrpcStatusCode;
25-
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
2625
import com.google.api.gax.rpc.CancelledException;
2726
import com.google.api.gax.rpc.StatusCode;
2827
import com.google.rpc.Status;
@@ -50,7 +49,7 @@ public void createClients() throws IOException, GeneralSecurityException {
5049
EchoSettings.newBuilder()
5150
.setCredentialsProvider(NoCredentialsProvider.create())
5251
.setTransportChannelProvider(
53-
InstantiatingGrpcChannelProvider.newBuilder()
52+
EchoSettings.defaultGrpcTransportProviderBuilder()
5453
.setChannelConfigurator(ManagedChannelBuilder::usePlaintext)
5554
.build())
5655
.build();

0 commit comments

Comments
 (0)