@@ -175,12 +175,12 @@ void testMetricsSuccessfulCall() {
175
175
}
176
176
177
177
/**
178
- * Test cancelled call.
178
+ * Test early cancelled call.
179
179
*/
180
180
@ Test
181
181
@ DirtiesContext
182
- void testMetricsCancelledCall () {
183
- log .info ("--- Starting tests with cancelled call ---" );
182
+ void testMetricsEarlyCancelledCall () {
183
+ log .info ("--- Starting tests with early cancelled call ---" );
184
184
final AtomicReference <Throwable > exception = new AtomicReference <>();
185
185
final CountDownLatch counter = new CountDownLatch (1 );
186
186
@@ -241,7 +241,7 @@ private synchronized void setError(final Throwable t) {
241
241
.tag (MetricConstants .TAG_METHOD_NAME , "echo" )
242
242
.counter ();
243
243
assertNotNull (requestSentCounter );
244
- assertEquals (1 , requestSentCounter .count ());
244
+ assertEquals (0 , requestSentCounter .count ());
245
245
246
246
final Counter responseReceivedCounter = this .meterRegistry
247
247
.find (METRIC_NAME_CLIENT_RESPONSES_RECEIVED )
@@ -257,15 +257,15 @@ private synchronized void setError(final Throwable t) {
257
257
.timer ();
258
258
assertNotNull (clientTimer );
259
259
assertEquals (1 , clientTimer .count ());
260
- assertTrue (clientTimer .max (TimeUnit .SECONDS ) < 1 );
260
+ assertTrue (clientTimer .max (TimeUnit .SECONDS ) < 3 );
261
261
262
262
// Test-Server
263
263
final Counter requestsReceivedCounter = this .meterRegistry
264
264
.find (METRIC_NAME_SERVER_REQUESTS_RECEIVED )
265
265
.tag (MetricConstants .TAG_METHOD_NAME , "echo" )
266
266
.counter ();
267
267
assertNotNull (requestsReceivedCounter );
268
- assertEquals (1 , requestsReceivedCounter .count ());
268
+ assertEquals (0 , requestsReceivedCounter .count ());
269
269
270
270
final Counter responsesSentCounter = this .meterRegistry
271
271
.find (METRIC_NAME_SERVER_RESPONSES_SENT )
@@ -277,11 +277,121 @@ private synchronized void setError(final Throwable t) {
277
277
final Timer serverTimer = this .meterRegistry
278
278
.find (METRIC_NAME_SERVER_PROCESSING_DURATION )
279
279
.tag (MetricConstants .TAG_METHOD_NAME , "echo" )
280
- .tag (TAG_STATUS_CODE , UNIMPLEMENTED .name ())
280
+ .tag (TAG_STATUS_CODE , CANCELLED .name ())
281
281
.timer ();
282
282
assertNotNull (serverTimer );
283
283
assertEquals (1 , serverTimer .count ());
284
- assertTrue (serverTimer .max (TimeUnit .SECONDS ) < 1 );
284
+ assertTrue (serverTimer .max (TimeUnit .SECONDS ) < 3 );
285
+
286
+ // Client has network overhead so it has to be slower
287
+ assertTrue (serverTimer .max (TimeUnit .SECONDS ) <= clientTimer .max (TimeUnit .SECONDS ));
288
+ log .info ("--- Test completed ---" );
289
+ }
290
+
291
+ /**
292
+ * Test cancelled call.
293
+ */
294
+ @ Test
295
+ @ DirtiesContext
296
+ void testMetricsCancelledCall () {
297
+ log .info ("--- Starting tests with cancelled call ---" );
298
+ final AtomicReference <Throwable > exception = new AtomicReference <>();
299
+ final CountDownLatch counter = new CountDownLatch (2 );
300
+
301
+ // Invoke
302
+ final ClientCallStreamObserver <SomeType > observer =
303
+ (ClientCallStreamObserver <SomeType >) this .testStreamService .echo (new StreamObserver <SomeType >() {
304
+
305
+ @ Override
306
+ public void onNext (final SomeType value ) {
307
+ counter .countDown ();
308
+ }
309
+
310
+ @ Override
311
+ public void onError (final Throwable t ) {
312
+ setError (t );
313
+ counter .countDown ();
314
+ }
315
+
316
+ @ Override
317
+ public void onCompleted () {
318
+ try {
319
+ fail ("Should never be here" );
320
+ } catch (final RuntimeException t ) {
321
+ setError (t );
322
+ counter .countDown ();
323
+ throw t ;
324
+ }
325
+ }
326
+
327
+ private synchronized void setError (final Throwable t ) {
328
+ final Throwable previous = exception .get ();
329
+ if (previous == null ) {
330
+ exception .set (t );
331
+ } else {
332
+ previous .addSuppressed (t );
333
+ }
334
+ }
335
+
336
+ });
337
+
338
+ observer .onNext (SomeType .getDefaultInstance ());
339
+ assertDoesNotThrow (() -> counter .await (1 , TimeUnit .SECONDS ));
340
+
341
+ observer .cancel ("Cancelled" , null );
342
+ assertTimeoutPreemptively (Duration .ofSeconds (3 ), (Executable ) counter ::await );
343
+ assertThat (exception .get ())
344
+ .isNotNull ()
345
+ .isInstanceOfSatisfying (StatusRuntimeException .class ,
346
+ t -> assertEquals (CANCELLED , t .getStatus ().getCode ()));
347
+
348
+ // Test-Client
349
+ final Counter requestSentCounter = this .meterRegistry
350
+ .find (METRIC_NAME_CLIENT_REQUESTS_SENT )
351
+ .tag (MetricConstants .TAG_METHOD_NAME , "echo" )
352
+ .counter ();
353
+ assertNotNull (requestSentCounter );
354
+ assertEquals (1 , requestSentCounter .count ());
355
+
356
+ final Counter responseReceivedCounter = this .meterRegistry
357
+ .find (METRIC_NAME_CLIENT_RESPONSES_RECEIVED )
358
+ .tag (MetricConstants .TAG_METHOD_NAME , "echo" )
359
+ .counter ();
360
+ assertNotNull (responseReceivedCounter );
361
+ assertEquals (1 , responseReceivedCounter .count ());
362
+
363
+ final Timer clientTimer = this .meterRegistry
364
+ .find (METRIC_NAME_CLIENT_PROCESSING_DURATION )
365
+ .tag (MetricConstants .TAG_METHOD_NAME , "echo" )
366
+ .tag (TAG_STATUS_CODE , CANCELLED .name ())
367
+ .timer ();
368
+ assertNotNull (clientTimer );
369
+ assertEquals (1 , clientTimer .count ());
370
+ assertTrue (clientTimer .max (TimeUnit .SECONDS ) < 3 );
371
+
372
+ // Test-Server
373
+ final Counter requestsReceivedCounter = this .meterRegistry
374
+ .find (METRIC_NAME_SERVER_REQUESTS_RECEIVED )
375
+ .tag (MetricConstants .TAG_METHOD_NAME , "echo" )
376
+ .counter ();
377
+ assertNotNull (requestsReceivedCounter );
378
+ assertEquals (1 , requestsReceivedCounter .count ());
379
+
380
+ final Counter responsesSentCounter = this .meterRegistry
381
+ .find (METRIC_NAME_SERVER_RESPONSES_SENT )
382
+ .tag (MetricConstants .TAG_METHOD_NAME , "echo" )
383
+ .counter ();
384
+ assertNotNull (responsesSentCounter );
385
+ assertEquals (1 , responsesSentCounter .count ());
386
+
387
+ final Timer serverTimer = this .meterRegistry
388
+ .find (METRIC_NAME_SERVER_PROCESSING_DURATION )
389
+ .tag (MetricConstants .TAG_METHOD_NAME , "echo" )
390
+ .tag (TAG_STATUS_CODE , CANCELLED .name ())
391
+ .timer ();
392
+ assertNotNull (serverTimer );
393
+ assertEquals (1 , serverTimer .count ());
394
+ assertTrue (serverTimer .max (TimeUnit .SECONDS ) < 3 );
285
395
286
396
// Client has network overhead so it has to be slower
287
397
assertTrue (serverTimer .max (TimeUnit .SECONDS ) <= clientTimer .max (TimeUnit .SECONDS ));
0 commit comments