@@ -75,15 +75,15 @@ public void testHttpJson_rpcInvoked_closeClient() throws Exception {
75
75
}
76
76
77
77
// 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 )
82
81
public void testGrpc_rpcInvokedWithLargeTimeout_closeClientOnceResponseReceived ()
83
82
throws Exception {
84
83
// Set the maxAttempts to 1 to ensure there are no retries scheduled. The single RPC
85
84
// 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.
87
87
RetrySettings defaultRetrySettings =
88
88
RetrySettings .newBuilder ()
89
89
.setInitialRpcTimeout (Duration .ofMillis (15000L ))
@@ -101,25 +101,35 @@ public void testGrpc_rpcInvokedWithLargeTimeout_closeClientOnceResponseReceived(
101
101
.setResponseDelay (com .google .protobuf .Duration .newBuilder ().setSeconds (2 ).build ())
102
102
.build ();
103
103
104
+ long start = System .currentTimeMillis ();
104
105
BlockResponse response = grpcClient .block (blockRequest );
105
106
Truth .assertThat (response .getContent ()).isEqualTo ("gRPCBlockContent_2sDelay" );
106
-
107
107
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 ();
109
112
Truth .assertThat (grpcClient .isShutdown ()).isTrue ();
110
113
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 );
111
121
}
112
122
113
123
// 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 )
118
127
public void testHttpJson_rpcInvokedWithLargeTimeout_closeClientOnceResponseReceived ()
119
128
throws Exception {
120
129
// Set the maxAttempts to 1 to ensure there are no retries scheduled. The single RPC
121
130
// 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.
123
133
RetrySettings defaultRetrySettings =
124
134
RetrySettings .newBuilder ()
125
135
.setInitialRpcTimeout (Duration .ofMillis (15000L ))
@@ -137,13 +147,24 @@ public void testHttpJson_rpcInvokedWithLargeTimeout_closeClientOnceResponseRecei
137
147
.setResponseDelay (com .google .protobuf .Duration .newBuilder ().setSeconds (2 ).build ())
138
148
.build ();
139
149
150
+ long start = System .currentTimeMillis ();
140
151
BlockResponse response = httpjsonClient .block (blockRequest );
141
152
Truth .assertThat (response .getContent ()).isEqualTo ("httpjsonBlockContent_2sDelay" );
142
-
143
153
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 ();
146
160
Truth .assertThat (httpjsonClient .isShutdown ()).isTrue ();
147
161
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 );
148
169
}
149
170
}
0 commit comments