Skip to content

Commit 5e23eed

Browse files
committed
Fix and extend metrics test
1 parent caac151 commit 5e23eed

File tree

1 file changed

+118
-8
lines changed

1 file changed

+118
-8
lines changed

tests/src/test/java/net/devh/boot/grpc/test/metric/MetricCollectingInterceptorTest.java

Lines changed: 118 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,12 @@ void testMetricsSuccessfulCall() {
175175
}
176176

177177
/**
178-
* Test cancelled call.
178+
* Test early cancelled call.
179179
*/
180180
@Test
181181
@DirtiesContext
182-
void testMetricsCancelledCall() {
183-
log.info("--- Starting tests with cancelled call ---");
182+
void testMetricsEarlyCancelledCall() {
183+
log.info("--- Starting tests with early cancelled call ---");
184184
final AtomicReference<Throwable> exception = new AtomicReference<>();
185185
final CountDownLatch counter = new CountDownLatch(1);
186186

@@ -241,7 +241,7 @@ private synchronized void setError(final Throwable t) {
241241
.tag(MetricConstants.TAG_METHOD_NAME, "echo")
242242
.counter();
243243
assertNotNull(requestSentCounter);
244-
assertEquals(1, requestSentCounter.count());
244+
assertEquals(0, requestSentCounter.count());
245245

246246
final Counter responseReceivedCounter = this.meterRegistry
247247
.find(METRIC_NAME_CLIENT_RESPONSES_RECEIVED)
@@ -257,15 +257,15 @@ private synchronized void setError(final Throwable t) {
257257
.timer();
258258
assertNotNull(clientTimer);
259259
assertEquals(1, clientTimer.count());
260-
assertTrue(clientTimer.max(TimeUnit.SECONDS) < 1);
260+
assertTrue(clientTimer.max(TimeUnit.SECONDS) < 3);
261261

262262
// Test-Server
263263
final Counter requestsReceivedCounter = this.meterRegistry
264264
.find(METRIC_NAME_SERVER_REQUESTS_RECEIVED)
265265
.tag(MetricConstants.TAG_METHOD_NAME, "echo")
266266
.counter();
267267
assertNotNull(requestsReceivedCounter);
268-
assertEquals(1, requestsReceivedCounter.count());
268+
assertEquals(0, requestsReceivedCounter.count());
269269

270270
final Counter responsesSentCounter = this.meterRegistry
271271
.find(METRIC_NAME_SERVER_RESPONSES_SENT)
@@ -277,11 +277,121 @@ private synchronized void setError(final Throwable t) {
277277
final Timer serverTimer = this.meterRegistry
278278
.find(METRIC_NAME_SERVER_PROCESSING_DURATION)
279279
.tag(MetricConstants.TAG_METHOD_NAME, "echo")
280-
.tag(TAG_STATUS_CODE, UNIMPLEMENTED.name())
280+
.tag(TAG_STATUS_CODE, CANCELLED.name())
281281
.timer();
282282
assertNotNull(serverTimer);
283283
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);
285395

286396
// Client has network overhead so it has to be slower
287397
assertTrue(serverTimer.max(TimeUnit.SECONDS) <= clientTimer.max(TimeUnit.SECONDS));

0 commit comments

Comments
 (0)