Skip to content

Commit 7c99d52

Browse files
committed
chore: Add showcase tests for closing a client
1 parent 41ffbbe commit 7c99d52

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITClientShutdown.java

+36-15
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ public void testHttpJson_rpcInvoked_closeClient() throws Exception {
7575
}
7676

7777
// This test is to ensure that the client is able to close + terminate any resources
78-
// once a response has been received. Set a timeout for this test to be 5s as it should
79-
// only take 2s to receive a response. The extra 3s is leeway for the client to be able to be
80-
// close properly.
81-
@Test(timeout = 5000L)
78+
// once a response has been received. Set a max test duration of 15s to ensure that
79+
// the test does not continue on forever.
80+
@Test(timeout = 15000L)
8281
public void testGrpc_rpcInvokedWithLargeTimeout_closeClientOnceResponseReceived()
8382
throws Exception {
8483
// Set the maxAttempts to 1 to ensure there are no retries scheduled. The single RPC
8584
// invocation should time out in 15s, but the client will receive a response in 2s.
86-
// Any outstanding tasks (timeout tasks) will be cancelled so the client can terminate.
85+
// Any outstanding tasks (timeout tasks) should be cancelled once a response has been
86+
// received so the client can properly terminate.
8787
RetrySettings defaultRetrySettings =
8888
RetrySettings.newBuilder()
8989
.setInitialRpcTimeout(Duration.ofMillis(15000L))
@@ -101,25 +101,35 @@ public void testGrpc_rpcInvokedWithLargeTimeout_closeClientOnceResponseReceived(
101101
.setResponseDelay(com.google.protobuf.Duration.newBuilder().setSeconds(2).build())
102102
.build();
103103

104+
long start = System.currentTimeMillis();
104105
BlockResponse response = grpcClient.block(blockRequest);
105106
Truth.assertThat(response.getContent()).isEqualTo("gRPCBlockContent_2sDelay");
106-
107107
grpcClient.close();
108-
grpcClient.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
108+
// Loop until the client has terminated successfully
109+
// Future enhancement: Use awaitility instead of busy waiting
110+
while (!grpcClient.isTerminated()) ;
111+
long end = System.currentTimeMillis();
109112
Truth.assertThat(grpcClient.isShutdown()).isTrue();
110113
Truth.assertThat(grpcClient.isTerminated()).isTrue();
114+
115+
// Check the termination time. If all the tasks/ resources are closed successfully,
116+
// the termination time should only take about 2s (time to receive a response) + time
117+
// to close the client. Check that this takes less than 5s (2s request time + 3s
118+
// buffer time).
119+
long terminationTime = end - start;
120+
Truth.assertThat(terminationTime).isLessThan(5000L);
111121
}
112122

113123
// This test is to ensure that the client is able to close + terminate any resources
114-
// once a response has been received. Set a timeout for this test to be 5s as it should
115-
// only take 2s to receive a response. The extra 3s is leeway for the client to be able to be
116-
// close properly.
117-
@Test(timeout = 5000L)
124+
// once a response has been received. Set a max test duration of 15s to ensure that
125+
// the test does not continue on forever.
126+
@Test(timeout = 15000L)
118127
public void testHttpJson_rpcInvokedWithLargeTimeout_closeClientOnceResponseReceived()
119128
throws Exception {
120129
// Set the maxAttempts to 1 to ensure there are no retries scheduled. The single RPC
121130
// invocation should time out in 15s, but the client will receive a response in 2s.
122-
// Any outstanding tasks (timeout tasks) will be cancelled so the client can terminate.
131+
// Any outstanding tasks (timeout tasks) should be cancelled once a response has been
132+
// received so the client can properly terminate.
123133
RetrySettings defaultRetrySettings =
124134
RetrySettings.newBuilder()
125135
.setInitialRpcTimeout(Duration.ofMillis(15000L))
@@ -137,13 +147,24 @@ public void testHttpJson_rpcInvokedWithLargeTimeout_closeClientOnceResponseRecei
137147
.setResponseDelay(com.google.protobuf.Duration.newBuilder().setSeconds(2).build())
138148
.build();
139149

150+
long start = System.currentTimeMillis();
140151
BlockResponse response = httpjsonClient.block(blockRequest);
141152
Truth.assertThat(response.getContent()).isEqualTo("httpjsonBlockContent_2sDelay");
142-
143153
httpjsonClient.close();
144-
httpjsonClient.awaitTermination(
145-
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
154+
// Loop until the client has terminated successfully
155+
// Future enhancement: Use awaitility instead of busy waiting
156+
while (!httpjsonClient.isTerminated()) {
157+
Thread.sleep(1000L);
158+
}
159+
long end = System.currentTimeMillis();
146160
Truth.assertThat(httpjsonClient.isShutdown()).isTrue();
147161
Truth.assertThat(httpjsonClient.isTerminated()).isTrue();
162+
163+
// Check the termination time. If all the tasks/ resources are closed successfully,
164+
// the termination time should only take about 2s (time to receive a response) + time
165+
// to close the client. Check that this takes less than 5s (2s request time + 3s
166+
// buffer time).
167+
long terminationTime = end - start;
168+
Truth.assertThat(terminationTime).isLessThan(5000L);
148169
}
149170
}

0 commit comments

Comments
 (0)