Skip to content

Commit

Permalink
test: Add BIDI streaming showcase test. (#1604)
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeli0 authored Apr 5, 2023
1 parent a280a88 commit 7444fd2
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.showcase.v1beta1.it;

import static com.google.common.truth.Truth.assertThat;

import com.google.api.core.SettableApiFuture;
import com.google.api.gax.rpc.ClientStream;
import com.google.api.gax.rpc.ResponseObserver;
import com.google.api.gax.rpc.StreamController;
import com.google.showcase.v1beta1.EchoClient;
import com.google.showcase.v1beta1.EchoRequest;
import com.google.showcase.v1beta1.EchoResponse;
import com.google.showcase.v1beta1.it.util.TestClientInitializer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ITBidiStreaming {

private EchoClient grpcClient;

@Before
public void setUp() throws Exception {
// Create gRPC Echo Client
grpcClient = TestClientInitializer.createGrpcEchoClient();
}

// The current implementation of BIDI streaming on Echo showcase server is that it would echo the
// request content back on every request, so this test verifies that the response content is
// exactly the same as request content.
// Ideally we should make the BIDI streaming server more generic, e.g. only respond when there are
// three requests, respond twice for every request etc. If that happens, the response content may
// not be exactly the same as request content.
@Test
public void testGrpc_splitCall_shouldListensToResponse() throws Exception {
// given
List<String> expected = Arrays.asList("The rain in Spain stays mainly on the plain".split(" "));
TestResponseObserver responseObserver = new TestResponseObserver();

// when
ClientStream<EchoRequest> clientStream = grpcClient.chatCallable().splitCall(responseObserver);
expected.forEach(
requestContent -> {
EchoRequest request = EchoRequest.newBuilder().setContent(requestContent).build();
clientStream.send(request);
});
clientStream.closeSend();

// then
List<String> actual = responseObserver.getFuture().get();
assertThat(actual).containsExactlyElementsIn(expected).inOrder();
}

private static class TestResponseObserver implements ResponseObserver<EchoResponse> {
private final List<String> responses = new ArrayList<>();
private final SettableApiFuture<List<String>> future = SettableApiFuture.create();

@Override
public void onStart(StreamController controller) {
// no-op
}

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

@Override
public void onError(Throwable t) {
// no-op
}

@Override
public void onComplete() {
future.set(responses);
}

public SettableApiFuture<List<String>> getFuture() {
return future;
}
}

@After
public void tearDown() throws Exception {
grpcClient.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@
import static org.junit.Assert.assertThrows;

import com.google.api.core.SettableApiFuture;
import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.rpc.ApiStreamObserver;
import com.google.api.gax.rpc.CancelledException;
import com.google.api.gax.rpc.StatusCode;
import com.google.rpc.Status;
import com.google.showcase.v1beta1.EchoClient;
import com.google.showcase.v1beta1.EchoRequest;
import com.google.showcase.v1beta1.EchoResponse;
import com.google.showcase.v1beta1.EchoSettings;
import io.grpc.ManagedChannelBuilder;
import java.io.IOException;
import com.google.showcase.v1beta1.it.util.TestClientInitializer;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
Expand All @@ -43,17 +40,9 @@ public class ITClientSideStreaming {
private EchoClient grpcClient;

@Before
public void createClients() throws IOException {
public void createClients() throws Exception {
// Create gRPC Echo Client
EchoSettings grpcEchoSettings =
EchoSettings.newBuilder()
.setCredentialsProvider(NoCredentialsProvider.create())
.setTransportChannelProvider(
EchoSettings.defaultGrpcTransportProviderBuilder()
.setChannelConfigurator(ManagedChannelBuilder::usePlaintext)
.build())
.build();
grpcClient = EchoClient.create(grpcEchoSettings);
grpcClient = TestClientInitializer.createGrpcEchoClient();
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,15 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.rpc.CancelledException;
import com.google.api.gax.rpc.ServerStream;
import com.google.api.gax.rpc.StatusCode;
import com.google.common.collect.ImmutableList;
import com.google.rpc.Status;
import com.google.showcase.v1beta1.EchoClient;
import com.google.showcase.v1beta1.EchoResponse;
import com.google.showcase.v1beta1.EchoSettings;
import com.google.showcase.v1beta1.ExpandRequest;
import io.grpc.ManagedChannelBuilder;
import java.io.IOException;
import java.security.GeneralSecurityException;
import com.google.showcase.v1beta1.it.util.TestClientInitializer;
import java.util.ArrayList;
import java.util.Iterator;
import org.junit.After;
Expand All @@ -46,29 +41,11 @@ public class ITServerSideStreaming {
private EchoClient httpjsonClient;

@Before
public void createClients() throws IOException, GeneralSecurityException {
public void createClients() throws Exception {
// Create gRPC Echo Client
EchoSettings grpcEchoSettings =
EchoSettings.newBuilder()
.setCredentialsProvider(NoCredentialsProvider.create())
.setTransportChannelProvider(
EchoSettings.defaultGrpcTransportProviderBuilder()
.setChannelConfigurator(ManagedChannelBuilder::usePlaintext)
.build())
.build();
grpcClient = EchoClient.create(grpcEchoSettings);
grpcClient = TestClientInitializer.createGrpcEchoClient();
// Create Http JSON Echo Client
EchoSettings httpJsonEchoSettings =
EchoSettings.newHttpJsonBuilder()
.setCredentialsProvider(NoCredentialsProvider.create())
.setTransportChannelProvider(
EchoSettings.defaultHttpJsonTransportProviderBuilder()
.setHttpTransport(
new NetHttpTransport.Builder().doNotValidateCertificate().build())
.setEndpoint("http://localhost:7469")
.build())
.build();
httpjsonClient = EchoClient.create(httpJsonEchoSettings);
httpjsonClient = TestClientInitializer.createHttpJsonEchoClient();
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,14 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.grpc.GrpcStatusCode;
import com.google.api.gax.rpc.CancelledException;
import com.google.api.gax.rpc.StatusCode;
import com.google.rpc.Status;
import com.google.showcase.v1beta1.EchoClient;
import com.google.showcase.v1beta1.EchoRequest;
import com.google.showcase.v1beta1.EchoResponse;
import com.google.showcase.v1beta1.EchoSettings;
import io.grpc.ManagedChannelBuilder;
import java.io.IOException;
import java.security.GeneralSecurityException;
import com.google.showcase.v1beta1.it.util.TestClientInitializer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -43,30 +38,11 @@ public class ITUnaryCallable {
private EchoClient httpJsonClient;

@Before
public void createClients() throws IOException, GeneralSecurityException {
public void createClients() throws Exception {
// Create gRPC Echo Client
EchoSettings grpcEchoSettings =
EchoSettings.newBuilder()
.setCredentialsProvider(NoCredentialsProvider.create())
.setTransportChannelProvider(
EchoSettings.defaultGrpcTransportProviderBuilder()
.setChannelConfigurator(ManagedChannelBuilder::usePlaintext)
.build())
.build();
grpcClient = EchoClient.create(grpcEchoSettings);

grpcClient = TestClientInitializer.createGrpcEchoClient();
// Create Http JSON Echo Client
EchoSettings httpJsonEchoSettings =
EchoSettings.newHttpJsonBuilder()
.setCredentialsProvider(NoCredentialsProvider.create())
.setTransportChannelProvider(
EchoSettings.defaultHttpJsonTransportProviderBuilder()
.setHttpTransport(
new NetHttpTransport.Builder().doNotValidateCertificate().build())
.setEndpoint("http://localhost:7469")
.build())
.build();
httpJsonClient = EchoClient.create(httpJsonEchoSettings);
httpJsonClient = TestClientInitializer.createHttpJsonEchoClient();
}

@After
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.showcase.v1beta1.it.util;

import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.gax.core.NoCredentialsProvider;
import com.google.showcase.v1beta1.EchoClient;
import com.google.showcase.v1beta1.EchoSettings;
import io.grpc.ManagedChannelBuilder;

public class TestClientInitializer {

public static EchoClient createGrpcEchoClient() throws Exception {
EchoSettings grpcEchoSettings =
EchoSettings.newBuilder()
.setCredentialsProvider(NoCredentialsProvider.create())
.setTransportChannelProvider(
EchoSettings.defaultGrpcTransportProviderBuilder()
.setChannelConfigurator(ManagedChannelBuilder::usePlaintext)
.build())

.build();
return EchoClient.create(grpcEchoSettings);
}

public static EchoClient createHttpJsonEchoClient() throws Exception{
EchoSettings httpJsonEchoSettings =
EchoSettings.newHttpJsonBuilder()
.setCredentialsProvider(NoCredentialsProvider.create())
.setTransportChannelProvider(
EchoSettings.defaultHttpJsonTransportProviderBuilder()
.setHttpTransport(
new NetHttpTransport.Builder().doNotValidateCertificate().build())
.setEndpoint("http://localhost:7469")
.build())
.build();
return EchoClient.create(httpJsonEchoSettings);
}
}

0 comments on commit 7444fd2

Please sign in to comment.