|
| 1 | +/* |
| 2 | + * Copyright 2024 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 | +package com.google.showcase.v1beta1.it; |
| 17 | + |
| 18 | +import com.google.api.gax.retrying.RetrySettings; |
| 19 | +import com.google.api.gax.rpc.StatusCode; |
| 20 | +import com.google.common.collect.ImmutableSet; |
| 21 | +import com.google.common.truth.Truth; |
| 22 | +import com.google.showcase.v1beta1.BlockRequest; |
| 23 | +import com.google.showcase.v1beta1.BlockResponse; |
| 24 | +import com.google.showcase.v1beta1.EchoClient; |
| 25 | +import com.google.showcase.v1beta1.EchoRequest; |
| 26 | +import com.google.showcase.v1beta1.it.util.TestClientInitializer; |
| 27 | +import org.junit.Test; |
| 28 | +import org.threeten.bp.Duration; |
| 29 | + |
| 30 | +public class ITClientShutdown { |
| 31 | + |
| 32 | + private static final long DEFAULT_RPC_TIMEOUT_MS = 15000L; |
| 33 | + private static final long DEFAULT_CLIENT_TERMINATION_MS = 5000L; |
| 34 | + |
| 35 | + // Test to ensure the client can close + terminate properly |
| 36 | + @Test(timeout = 15000L) |
| 37 | + public void testGrpc_closeClient() throws Exception { |
| 38 | + EchoClient grpcClient = TestClientInitializer.createGrpcEchoClient(); |
| 39 | + assertClientTerminated(grpcClient); |
| 40 | + } |
| 41 | + |
| 42 | + // Test to ensure the client can close + terminate properly |
| 43 | + @Test(timeout = 15000L) |
| 44 | + public void testHttpJson_closeClient() throws Exception { |
| 45 | + EchoClient httpjsonClient = TestClientInitializer.createHttpJsonEchoClient(); |
| 46 | + assertClientTerminated(httpjsonClient); |
| 47 | + } |
| 48 | + |
| 49 | + // Test to ensure the client can close + terminate after a quick RPC invocation |
| 50 | + @Test(timeout = 15000L) |
| 51 | + public void testGrpc_rpcInvoked_closeClient() throws Exception { |
| 52 | + EchoClient grpcClient = TestClientInitializer.createGrpcEchoClient(); |
| 53 | + // Response is ignored for this test |
| 54 | + grpcClient.echo(EchoRequest.newBuilder().setContent("Test").build()); |
| 55 | + assertClientTerminated(grpcClient); |
| 56 | + } |
| 57 | + |
| 58 | + // Test to ensure the client can close + terminate after a quick RPC invocation |
| 59 | + @Test(timeout = 15000L) |
| 60 | + public void testHttpJson_rpcInvoked_closeClient() throws Exception { |
| 61 | + EchoClient httpjsonClient = TestClientInitializer.createHttpJsonEchoClient(); |
| 62 | + // Response is ignored for this test |
| 63 | + httpjsonClient.echo(EchoRequest.newBuilder().setContent("Test").build()); |
| 64 | + assertClientTerminated(httpjsonClient); |
| 65 | + } |
| 66 | + |
| 67 | + // This test is to ensure that the client is able to close + terminate any resources |
| 68 | + // once a response has been received. Set a max test duration of 15s to ensure that |
| 69 | + // the test does not continue on forever. |
| 70 | + @Test(timeout = 15000L) |
| 71 | + public void testGrpc_rpcInvokedWithLargeTimeout_closeClientOnceResponseReceived() |
| 72 | + throws Exception { |
| 73 | + // Set the maxAttempts to 1 to ensure there are no retries scheduled. The single RPC |
| 74 | + // invocation should time out in 15s, but the client will receive a response in 2s. |
| 75 | + // Any outstanding tasks (timeout tasks) should be cancelled once a response has been |
| 76 | + // received so the client can properly terminate. |
| 77 | + RetrySettings defaultRetrySettings = |
| 78 | + RetrySettings.newBuilder() |
| 79 | + .setInitialRpcTimeout(Duration.ofMillis(DEFAULT_RPC_TIMEOUT_MS)) |
| 80 | + .setMaxRpcTimeout(Duration.ofMillis(DEFAULT_RPC_TIMEOUT_MS)) |
| 81 | + .setTotalTimeout(Duration.ofMillis(DEFAULT_RPC_TIMEOUT_MS)) |
| 82 | + .setMaxAttempts(1) |
| 83 | + .build(); |
| 84 | + EchoClient grpcClient = |
| 85 | + TestClientInitializer.createGrpcEchoClientCustomBlockSettings( |
| 86 | + defaultRetrySettings, ImmutableSet.of(StatusCode.Code.DEADLINE_EXCEEDED)); |
| 87 | + |
| 88 | + BlockRequest blockRequest = |
| 89 | + BlockRequest.newBuilder() |
| 90 | + .setSuccess(BlockResponse.newBuilder().setContent("gRPCBlockContent_2sDelay")) |
| 91 | + .setResponseDelay(com.google.protobuf.Duration.newBuilder().setSeconds(2).build()) |
| 92 | + .build(); |
| 93 | + |
| 94 | + // Response is ignored for this test |
| 95 | + grpcClient.block(blockRequest); |
| 96 | + |
| 97 | + assertClientTerminated(grpcClient); |
| 98 | + } |
| 99 | + |
| 100 | + // This test is to ensure that the client is able to close + terminate any resources |
| 101 | + // once a response has been received. Set a max test duration of 15s to ensure that |
| 102 | + // the test does not continue on forever. |
| 103 | + @Test(timeout = 15000L) |
| 104 | + public void testHttpJson_rpcInvokedWithLargeTimeout_closeClientOnceResponseReceived() |
| 105 | + throws Exception { |
| 106 | + // Set the maxAttempts to 1 to ensure there are no retries scheduled. The single RPC |
| 107 | + // invocation should time out in 15s, but the client will receive a response in 2s. |
| 108 | + // Any outstanding tasks (timeout tasks) should be cancelled once a response has been |
| 109 | + // received so the client can properly terminate. |
| 110 | + RetrySettings defaultRetrySettings = |
| 111 | + RetrySettings.newBuilder() |
| 112 | + .setInitialRpcTimeout(Duration.ofMillis(DEFAULT_RPC_TIMEOUT_MS)) |
| 113 | + .setMaxRpcTimeout(Duration.ofMillis(DEFAULT_RPC_TIMEOUT_MS)) |
| 114 | + .setTotalTimeout(Duration.ofMillis(DEFAULT_RPC_TIMEOUT_MS)) |
| 115 | + .setMaxAttempts(1) |
| 116 | + .build(); |
| 117 | + EchoClient httpjsonClient = |
| 118 | + TestClientInitializer.createHttpJsonEchoClientCustomBlockSettings( |
| 119 | + defaultRetrySettings, ImmutableSet.of(StatusCode.Code.DEADLINE_EXCEEDED)); |
| 120 | + |
| 121 | + BlockRequest blockRequest = |
| 122 | + BlockRequest.newBuilder() |
| 123 | + .setSuccess(BlockResponse.newBuilder().setContent("httpjsonBlockContent_2sDelay")) |
| 124 | + .setResponseDelay(com.google.protobuf.Duration.newBuilder().setSeconds(2).build()) |
| 125 | + .build(); |
| 126 | + |
| 127 | + // Response is ignored for this test |
| 128 | + httpjsonClient.block(blockRequest); |
| 129 | + |
| 130 | + assertClientTerminated(httpjsonClient); |
| 131 | + } |
| 132 | + |
| 133 | + // This helper method asserts that the client is able to terminate within |
| 134 | + // `AWAIT_TERMINATION_SECONDS` |
| 135 | + private void assertClientTerminated(EchoClient echoClient) throws InterruptedException { |
| 136 | + long start = System.currentTimeMillis(); |
| 137 | + // Intentionally do not run echoClient.awaitTermination(...) as this test will |
| 138 | + // check that everything is properly terminated after close() is called. |
| 139 | + echoClient.close(); |
| 140 | + |
| 141 | + // Loop until the client has terminated successfully. For tests that use this, |
| 142 | + // try to ensure there is a timeout associated, otherwise this may run forever. |
| 143 | + // Future enhancement: Use awaitility instead of busy waiting |
| 144 | + while (!echoClient.isTerminated()) { |
| 145 | + Thread.sleep(500L); |
| 146 | + } |
| 147 | + // The busy-wait time won't be accurate, so account for a bit of buffer |
| 148 | + long end = System.currentTimeMillis(); |
| 149 | + |
| 150 | + Truth.assertThat(echoClient.isShutdown()).isTrue(); |
| 151 | + |
| 152 | + // Check the termination time. If all the tasks/ resources are closed successfully, |
| 153 | + // the termination time should only occur shortly after `close()` was invoked. The |
| 154 | + // `DEFAULT_TERMINATION_MS` value should include a bit of buffer. |
| 155 | + long terminationTime = end - start; |
| 156 | + Truth.assertThat(terminationTime).isLessThan(DEFAULT_CLIENT_TERMINATION_MS); |
| 157 | + } |
| 158 | +} |
0 commit comments