Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add unary-unary showcase test for gRPC support #1501

Merged
merged 16 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ jobs:
- name: Install maven modules
run: |
mvn install -B -ntp -DskipTests -Dclirr.skip -Dcheckstyle.skip

- name: Java Linter
working-directory: showcase
run: |
mvn -B -ntp fmt:check
- name: Showcase golden tests
working-directory: showcase
run: |
Expand Down
19 changes: 15 additions & 4 deletions showcase/gapic-showcase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
<version>0.0.1-SNAPSHOT</version>
</parent>

<properties>
<fmt.skip>true</fmt.skip>
</properties>

<profiles>
<profile>
<id>enable-golden-tests</id>
Expand Down Expand Up @@ -102,6 +98,14 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.coveo</groupId>
<artifactId>fmt-maven-plugin</artifactId>
<version>2.9</version>
<configuration>
<filesNamePattern>(IT.*\.java)|(.*Test.java)</filesNamePattern>
</configuration>
</plugin>
</plugins>
</build>

Expand Down Expand Up @@ -162,6 +166,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.1.3</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.api.grpc</groupId>
<artifactId>grpc-gapic-showcase-v1beta1</artifactId>
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@
import com.google.showcase.v1beta1.ComplianceClient;
import com.google.showcase.v1beta1.ComplianceSettings;
import com.google.showcase.v1beta1.EnumRequest;
import com.google.showcase.v1beta1.EnumResponse;
import java.io.IOException;
import java.security.GeneralSecurityException;

import com.google.showcase.v1beta1.EnumResponse;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -61,4 +60,4 @@ public void verifyEnums() {
EnumResponse initialResponse = client.getEnum(request);
assertEquals(initialResponse, client.verifyEnum(initialResponse));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* 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 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.grpc.InstantiatingGrpcChannelProvider;
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 org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ITUnaryCallable {

private EchoClient grpcClient;

private EchoClient httpJsonClient;

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

// 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);
}

@After
public void destroyClient() {
grpcClient.close();
mpeddada1 marked this conversation as resolved.
Show resolved Hide resolved
httpJsonClient.close();
}

@Test
public void testGrpc_receiveContent() {
assertThat(echoGrpc("grpc-echo?")).isEqualTo("grpc-echo?");
assertThat(echoGrpc("grpc-echo!")).isEqualTo("grpc-echo!");
}

@Test
public void testGrpc_serverResponseError_throwsException() {
Status cancelledStatus =
Status.newBuilder().setCode(StatusCode.Code.CANCELLED.ordinal()).build();
EchoRequest requestWithServerError = EchoRequest.newBuilder().setError(cancelledStatus).build();
CancelledException exception =
assertThrows(CancelledException.class, () -> grpcClient.echo(requestWithServerError));
assertThat(exception.getStatusCode().getCode()).isEqualTo(GrpcStatusCode.Code.CANCELLED);
}

@Test
public void testGrpc_shutdown() {
assertThat(grpcClient.isShutdown()).isFalse();
grpcClient.shutdown();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QQ, would this make the test dependent on the ordering? i.e. if testGrpc_shutdown gets called first and then the client is shutdown, would the other tests still run properly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch! I think you're right, this would make the tests interdependent. Let me look into modifying this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, looks like avoiding the static states as @burkedavison suggested in the previous comment is the way to go if we are to have self-contained tests.

assertThat(grpcClient.isShutdown()).isTrue();
}

@Test
public void testHttpJson() {
assertThat(echoHttpJson("http-echo?")).isEqualTo("http-echo?");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the plan with the Http tests? Duplicates of the above coming soon?

Copy link
Contributor

@lqiu96 lqiu96 Mar 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add in httpjson versions of the gRPC calls in a separate PR

assertThat(echoHttpJson("http-echo!")).isEqualTo("http-echo!");
}

private String echoGrpc(String value) {
EchoResponse response = grpcClient.echo(EchoRequest.newBuilder().setContent(value).build());
return response.getContent();
}

private String echoHttpJson(String value) {
EchoResponse response = httpJsonClient.echo(EchoRequest.newBuilder().setContent(value).build());
return response.getContent();
}
}