Skip to content

Commit

Permalink
chore: Add showcase tests for closing a client
Browse files Browse the repository at this point in the history
  • Loading branch information
lqiu96 committed Jan 17, 2024
1 parent 41ffbbe commit 7c99d52
Showing 1 changed file with 36 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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);
}
}

0 comments on commit 7c99d52

Please sign in to comment.