18
18
19
19
import static com .google .common .truth .Truth .assertThat ;
20
20
import static java .util .concurrent .TimeUnit .SECONDS ;
21
+ import static org .junit .Assert .assertNotNull ;
22
+ import static org .mockito .AdditionalAnswers .delegatesTo ;
21
23
import static org .mockito .ArgumentMatchers .any ;
24
+ import static org .mockito .Mockito .mock ;
22
25
import static org .mockito .Mockito .never ;
23
26
import static org .mockito .Mockito .timeout ;
24
27
import static org .mockito .Mockito .verify ;
78
81
import org .junit .Test ;
79
82
import org .junit .runner .RunWith ;
80
83
import org .junit .runners .JUnit4 ;
81
- import org .mockito .ArgumentCaptor ;
82
- import org .mockito .Mock ;
83
84
import org .mockito .junit .MockitoJUnit ;
84
85
import org .mockito .junit .MockitoRule ;
85
86
@@ -103,8 +104,11 @@ public class RetryTest {
103
104
@ Rule
104
105
public final GrpcCleanupRule cleanupRule = new GrpcCleanupRule ();
105
106
private final FakeClock fakeClock = new FakeClock ();
106
- @ Mock
107
- private ClientCall .Listener <Integer > mockCallListener ;
107
+ private TestListener testCallListener = new TestListener ();
108
+ @ SuppressWarnings ("unchecked" )
109
+ private ClientCall .Listener <Integer > mockCallListener =
110
+ mock (ClientCall .Listener .class , delegatesTo (testCallListener ));
111
+
108
112
private CountDownLatch backoffLatch = new CountDownLatch (1 );
109
113
private final EventLoopGroup group = new DefaultEventLoopGroup () {
110
114
@ SuppressWarnings ("FutureReturnValueIgnored" )
@@ -244,8 +248,10 @@ private void assertInboundWireSizeRecorded(long length) throws Exception {
244
248
245
249
private void assertRpcStatusRecorded (
246
250
Status .Code code , long roundtripLatencyMs , long outboundMessages ) throws Exception {
247
- MetricsRecord record = clientStatsRecorder .pollRecord (5 , SECONDS );
251
+ MetricsRecord record = clientStatsRecorder .pollRecord (7 , SECONDS );
252
+ assertNotNull (record );
248
253
TagValue statusTag = record .tags .get (RpcMeasureConstants .GRPC_CLIENT_STATUS );
254
+ assertNotNull (statusTag );
249
255
assertThat (statusTag .asString ()).isEqualTo (code .toString ());
250
256
assertThat (record .getMetricAsLongOrFail (DeprecatedCensusConstants .RPC_CLIENT_FINISHED_COUNT ))
251
257
.isEqualTo (1 );
@@ -295,14 +301,14 @@ public void retryUntilBufferLimitExceeded() throws Exception {
295
301
verify (mockCallListener , never ()).onClose (any (Status .class ), any (Metadata .class ));
296
302
// send one more message, should exceed buffer limit
297
303
call .sendMessage (message );
304
+
298
305
// let attempt fail
306
+ testCallListener .clear ();
299
307
serverCall .close (
300
308
Status .UNAVAILABLE .withDescription ("2nd attempt failed" ),
301
309
new Metadata ());
302
310
// no more retry
303
- ArgumentCaptor <Status > statusCaptor = ArgumentCaptor .forClass (Status .class );
304
- verify (mockCallListener , timeout (5000 )).onClose (statusCaptor .capture (), any (Metadata .class ));
305
- assertThat (statusCaptor .getValue ().getDescription ()).contains ("2nd attempt failed" );
311
+ testCallListener .verifyDescription ("2nd attempt failed" , 5000 );
306
312
}
307
313
308
314
@ Test
@@ -534,4 +540,26 @@ public ClientStreamTracer newClientStreamTracer(StreamInfo info, Metadata header
534
540
assertRpcStatusRecorded (Code .INVALID_ARGUMENT , 0 , 0 );
535
541
assertRetryStatsRecorded (0 , 1 , 0 );
536
542
}
543
+
544
+ private static class TestListener extends ClientCall .Listener <Integer > {
545
+ Status status = null ;
546
+ private CountDownLatch closeLatch = new CountDownLatch (1 );
547
+
548
+ @ Override
549
+ public void onClose (Status status , Metadata trailers ) {
550
+ this .status = status ;
551
+ closeLatch .countDown ();
552
+ }
553
+
554
+ void clear () {
555
+ status = null ;
556
+ closeLatch = new CountDownLatch (1 );
557
+ }
558
+
559
+ void verifyDescription (String description , long timeoutMs ) throws InterruptedException {
560
+ closeLatch .await (timeoutMs , TimeUnit .MILLISECONDS );
561
+ assertNotNull (status );
562
+ assertThat (status .getDescription ()).contains (description );
563
+ }
564
+ }
537
565
}
0 commit comments