From 41ffbbe0cfaad1a42fb882ac55ffcc8bea2dd19c Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Tue, 16 Jan 2024 17:48:46 -0500 Subject: [PATCH] chore: Add Client Shutdown test case --- .../showcase/v1beta1/it/ITClientShutdown.java | 149 ++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITClientShutdown.java diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITClientShutdown.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITClientShutdown.java new file mode 100644 index 00000000000..91d24bf42fa --- /dev/null +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITClientShutdown.java @@ -0,0 +1,149 @@ +/* + * Copyright 2024 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 com.google.api.gax.retrying.RetrySettings; +import com.google.api.gax.rpc.StatusCode; +import com.google.common.collect.ImmutableSet; +import com.google.common.truth.Truth; +import com.google.showcase.v1beta1.BlockRequest; +import com.google.showcase.v1beta1.BlockResponse; +import com.google.showcase.v1beta1.EchoClient; +import com.google.showcase.v1beta1.EchoRequest; +import com.google.showcase.v1beta1.it.util.TestClientInitializer; +import java.util.concurrent.TimeUnit; +import org.junit.Test; +import org.threeten.bp.Duration; + +public class ITClientShutdown { + + @Test + public void testGrpc_closeClient() throws Exception { + EchoClient grpcClient = TestClientInitializer.createGrpcEchoClient(); + grpcClient.close(); + grpcClient.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS); + Truth.assertThat(grpcClient.isShutdown()).isTrue(); + Truth.assertThat(grpcClient.isTerminated()).isTrue(); + } + + @Test + public void testHttpJson_closeClient() throws Exception { + EchoClient httpjsonClient = TestClientInitializer.createHttpJsonEchoClient(); + httpjsonClient.close(); + httpjsonClient.awaitTermination( + TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS); + Truth.assertThat(httpjsonClient.isShutdown()).isTrue(); + Truth.assertThat(httpjsonClient.isTerminated()).isTrue(); + } + + @Test + public void testGrpc_rpcInvoked_closeClient() throws Exception { + EchoClient grpcClient = TestClientInitializer.createGrpcEchoClient(); + + grpcClient.echo(EchoRequest.newBuilder().setContent("Test").build()); + + grpcClient.close(); + grpcClient.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS); + Truth.assertThat(grpcClient.isShutdown()).isTrue(); + Truth.assertThat(grpcClient.isTerminated()).isTrue(); + } + + @Test + public void testHttpJson_rpcInvoked_closeClient() throws Exception { + EchoClient httpjsonClient = TestClientInitializer.createHttpJsonEchoClient(); + + httpjsonClient.echo(EchoRequest.newBuilder().setContent("Test").build()); + + httpjsonClient.close(); + httpjsonClient.awaitTermination( + TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS); + Truth.assertThat(httpjsonClient.isShutdown()).isTrue(); + Truth.assertThat(httpjsonClient.isTerminated()).isTrue(); + } + + // This test is to ensure that the client is able to close + terminate any resources + // once a response has been received. Set a timeout for this test to be 5s as it should + // only take 2s to receive a response. The extra 3s is leeway for the client to be able to be + // close properly. + @Test(timeout = 5000L) + public void testGrpc_rpcInvokedWithLargeTimeout_closeClientOnceResponseReceived() + throws Exception { + // Set the maxAttempts to 1 to ensure there are no retries scheduled. The single RPC + // invocation should time out in 15s, but the client will receive a response in 2s. + // Any outstanding tasks (timeout tasks) will be cancelled so the client can terminate. + RetrySettings defaultRetrySettings = + RetrySettings.newBuilder() + .setInitialRpcTimeout(Duration.ofMillis(15000L)) + .setMaxRpcTimeout(Duration.ofMillis(15000L)) + .setTotalTimeout(Duration.ofMillis(15000L)) + .setMaxAttempts(1) + .build(); + EchoClient grpcClient = + TestClientInitializer.createHttpJsonEchoClientCustomBlockSettings( + defaultRetrySettings, ImmutableSet.of(StatusCode.Code.DEADLINE_EXCEEDED)); + + BlockRequest blockRequest = + BlockRequest.newBuilder() + .setSuccess(BlockResponse.newBuilder().setContent("gRPCBlockContent_2sDelay")) + .setResponseDelay(com.google.protobuf.Duration.newBuilder().setSeconds(2).build()) + .build(); + + BlockResponse response = grpcClient.block(blockRequest); + Truth.assertThat(response.getContent()).isEqualTo("gRPCBlockContent_2sDelay"); + + grpcClient.close(); + grpcClient.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS); + Truth.assertThat(grpcClient.isShutdown()).isTrue(); + Truth.assertThat(grpcClient.isTerminated()).isTrue(); + } + + // This test is to ensure that the client is able to close + terminate any resources + // once a response has been received. Set a timeout for this test to be 5s as it should + // only take 2s to receive a response. The extra 3s is leeway for the client to be able to be + // close properly. + @Test(timeout = 5000L) + public void testHttpJson_rpcInvokedWithLargeTimeout_closeClientOnceResponseReceived() + throws Exception { + // Set the maxAttempts to 1 to ensure there are no retries scheduled. The single RPC + // invocation should time out in 15s, but the client will receive a response in 2s. + // Any outstanding tasks (timeout tasks) will be cancelled so the client can terminate. + RetrySettings defaultRetrySettings = + RetrySettings.newBuilder() + .setInitialRpcTimeout(Duration.ofMillis(15000L)) + .setMaxRpcTimeout(Duration.ofMillis(15000L)) + .setTotalTimeout(Duration.ofMillis(15000L)) + .setMaxAttempts(1) + .build(); + EchoClient httpjsonClient = + TestClientInitializer.createHttpJsonEchoClientCustomBlockSettings( + defaultRetrySettings, ImmutableSet.of(StatusCode.Code.DEADLINE_EXCEEDED)); + + BlockRequest blockRequest = + BlockRequest.newBuilder() + .setSuccess(BlockResponse.newBuilder().setContent("httpjsonBlockContent_2sDelay")) + .setResponseDelay(com.google.protobuf.Duration.newBuilder().setSeconds(2).build()) + .build(); + + BlockResponse response = httpjsonClient.block(blockRequest); + Truth.assertThat(response.getContent()).isEqualTo("httpjsonBlockContent_2sDelay"); + + httpjsonClient.close(); + httpjsonClient.awaitTermination( + TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS); + Truth.assertThat(httpjsonClient.isShutdown()).isTrue(); + Truth.assertThat(httpjsonClient.isTerminated()).isTrue(); + } +}