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 index 91d24bf42fa..e2e12f643b6 100644 --- 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 @@ -75,15 +75,15 @@ public void testHttpJson_rpcInvoked_closeClient() throws Exception { } // 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) + // once a response has been received. Set a max test duration of 15s to ensure that + // the test does not continue on forever. + @Test(timeout = 15000L) 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. + // Any outstanding tasks (timeout tasks) should be cancelled once a response has been + // received so the client can properly terminate. RetrySettings defaultRetrySettings = RetrySettings.newBuilder() .setInitialRpcTimeout(Duration.ofMillis(15000L)) @@ -101,25 +101,35 @@ public void testGrpc_rpcInvokedWithLargeTimeout_closeClientOnceResponseReceived( .setResponseDelay(com.google.protobuf.Duration.newBuilder().setSeconds(2).build()) .build(); + long start = System.currentTimeMillis(); BlockResponse response = grpcClient.block(blockRequest); Truth.assertThat(response.getContent()).isEqualTo("gRPCBlockContent_2sDelay"); - grpcClient.close(); - grpcClient.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS); + // Loop until the client has terminated successfully + // Future enhancement: Use awaitility instead of busy waiting + while (!grpcClient.isTerminated()) ; + long end = System.currentTimeMillis(); Truth.assertThat(grpcClient.isShutdown()).isTrue(); Truth.assertThat(grpcClient.isTerminated()).isTrue(); + + // Check the termination time. If all the tasks/ resources are closed successfully, + // the termination time should only take about 2s (time to receive a response) + time + // to close the client. Check that this takes less than 5s (2s request time + 3s + // buffer time). + long terminationTime = end - start; + Truth.assertThat(terminationTime).isLessThan(5000L); } // 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) + // once a response has been received. Set a max test duration of 15s to ensure that + // the test does not continue on forever. + @Test(timeout = 15000L) 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. + // Any outstanding tasks (timeout tasks) should be cancelled once a response has been + // received so the client can properly terminate. RetrySettings defaultRetrySettings = RetrySettings.newBuilder() .setInitialRpcTimeout(Duration.ofMillis(15000L)) @@ -137,13 +147,24 @@ public void testHttpJson_rpcInvokedWithLargeTimeout_closeClientOnceResponseRecei .setResponseDelay(com.google.protobuf.Duration.newBuilder().setSeconds(2).build()) .build(); + long start = System.currentTimeMillis(); BlockResponse response = httpjsonClient.block(blockRequest); Truth.assertThat(response.getContent()).isEqualTo("httpjsonBlockContent_2sDelay"); - httpjsonClient.close(); - httpjsonClient.awaitTermination( - TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS); + // Loop until the client has terminated successfully + // Future enhancement: Use awaitility instead of busy waiting + while (!httpjsonClient.isTerminated()) { + Thread.sleep(1000L); + } + long end = System.currentTimeMillis(); Truth.assertThat(httpjsonClient.isShutdown()).isTrue(); Truth.assertThat(httpjsonClient.isTerminated()).isTrue(); + + // Check the termination time. If all the tasks/ resources are closed successfully, + // the termination time should only take about 2s (time to receive a response) + time + // to close the client. Check that this takes less than 5s (2s request time + 3s + // buffer time). + long terminationTime = end - start; + Truth.assertThat(terminationTime).isLessThan(5000L); } }