|
18 | 18 | package net.devh.boot.grpc.test.metric;
|
19 | 19 |
|
20 | 20 | import static io.grpc.Status.Code.CANCELLED;
|
| 21 | +import static io.grpc.Status.Code.INTERNAL; |
21 | 22 | import static io.grpc.Status.Code.UNIMPLEMENTED;
|
| 23 | +import static io.grpc.Status.Code.UNKNOWN; |
22 | 24 | import static net.devh.boot.grpc.common.metric.MetricConstants.METRIC_NAME_CLIENT_PROCESSING_DURATION;
|
23 | 25 | import static net.devh.boot.grpc.common.metric.MetricConstants.METRIC_NAME_CLIENT_REQUESTS_SENT;
|
24 | 26 | import static net.devh.boot.grpc.common.metric.MetricConstants.METRIC_NAME_CLIENT_RESPONSES_RECEIVED;
|
@@ -411,12 +413,12 @@ private synchronized void setError(final Throwable t) {
|
411 | 413 | }
|
412 | 414 |
|
413 | 415 | /**
|
414 |
| - * Test failing call. |
| 416 | + * Test unimplemented call. |
415 | 417 | */
|
416 | 418 | @Test
|
417 | 419 | @DirtiesContext
|
418 |
| - void testMetricsFailingCall() { |
419 |
| - log.info("--- Starting tests with failing call ---"); |
| 420 | + void testMetricsUniplementedCall() { |
| 421 | + log.info("--- Starting tests with unimplemented call ---"); |
420 | 422 |
|
421 | 423 | final CountDownLatch counter = awaitNextServerAndClientCallCloses(1);
|
422 | 424 |
|
@@ -479,4 +481,140 @@ void testMetricsFailingCall() {
|
479 | 481 | log.info("--- Test completed ---");
|
480 | 482 | }
|
481 | 483 |
|
| 484 | + /** |
| 485 | + * Test failed call. |
| 486 | + */ |
| 487 | + @Test |
| 488 | + @DirtiesContext |
| 489 | + void testMetricsFailedCall() { |
| 490 | + log.info("--- Starting tests with failing call ---"); |
| 491 | + |
| 492 | + final CountDownLatch counter = awaitNextServerAndClientCallCloses(1); |
| 493 | + |
| 494 | + // Invoke |
| 495 | + assertThrows(StatusRuntimeException.class, () -> this.testService.secure(EMPTY)); |
| 496 | + |
| 497 | + assertTimeoutPreemptively(Duration.ofSeconds(1), (Executable) counter::await); |
| 498 | + |
| 499 | + // Test-Client |
| 500 | + final Counter requestSentCounter = this.meterRegistry |
| 501 | + .find(METRIC_NAME_CLIENT_REQUESTS_SENT) |
| 502 | + .tag(MetricConstants.TAG_METHOD_NAME, "secure") |
| 503 | + .counter(); |
| 504 | + assertNotNull(requestSentCounter); |
| 505 | + assertEquals(1, requestSentCounter.count()); |
| 506 | + |
| 507 | + final Counter responseReceivedCounter = this.meterRegistry |
| 508 | + .find(METRIC_NAME_CLIENT_RESPONSES_RECEIVED) |
| 509 | + .tag(MetricConstants.TAG_METHOD_NAME, "secure") |
| 510 | + .counter(); |
| 511 | + assertNotNull(responseReceivedCounter); |
| 512 | + assertEquals(0, responseReceivedCounter.count()); |
| 513 | + |
| 514 | + final Timer clientTimer = this.meterRegistry |
| 515 | + .find(METRIC_NAME_CLIENT_PROCESSING_DURATION) |
| 516 | + .tag(MetricConstants.TAG_METHOD_NAME, "secure") |
| 517 | + .tag(TAG_STATUS_CODE, UNKNOWN.name()) |
| 518 | + .timer(); |
| 519 | + assertNotNull(clientTimer); |
| 520 | + assertEquals(1, clientTimer.count()); |
| 521 | + assertTrue(clientTimer.max(TimeUnit.SECONDS) < 1); |
| 522 | + |
| 523 | + // Test-Server |
| 524 | + final Counter requestsReceivedCounter = this.meterRegistry |
| 525 | + .find(METRIC_NAME_SERVER_REQUESTS_RECEIVED) |
| 526 | + .tag(MetricConstants.TAG_METHOD_NAME, "secure") |
| 527 | + .counter(); |
| 528 | + assertNotNull(requestsReceivedCounter); |
| 529 | + assertEquals(1, requestsReceivedCounter.count()); |
| 530 | + |
| 531 | + final Counter responsesSentCounter = this.meterRegistry |
| 532 | + .find(METRIC_NAME_SERVER_RESPONSES_SENT) |
| 533 | + .tag(MetricConstants.TAG_METHOD_NAME, "secure") |
| 534 | + .counter(); |
| 535 | + assertNotNull(responsesSentCounter); |
| 536 | + assertEquals(0, responsesSentCounter.count()); |
| 537 | + |
| 538 | + final Timer serverTimer = this.meterRegistry |
| 539 | + .find(METRIC_NAME_SERVER_PROCESSING_DURATION) |
| 540 | + .tag(MetricConstants.TAG_METHOD_NAME, "secure") |
| 541 | + .tag(TAG_STATUS_CODE, UNKNOWN.name()) |
| 542 | + .timer(); |
| 543 | + assertNotNull(serverTimer); |
| 544 | + assertEquals(1, serverTimer.count()); |
| 545 | + assertTrue(serverTimer.max(TimeUnit.SECONDS) < 1); |
| 546 | + |
| 547 | + // Client has network overhead so it has to be slower |
| 548 | + assertTrue(serverTimer.max(TimeUnit.SECONDS) <= clientTimer.max(TimeUnit.SECONDS)); |
| 549 | + log.info("--- Test completed ---"); |
| 550 | + } |
| 551 | + |
| 552 | + /** |
| 553 | + * Test error call. |
| 554 | + */ |
| 555 | + @Test |
| 556 | + @DirtiesContext |
| 557 | + void testMetricsErrorCall() { |
| 558 | + log.info("--- Starting tests with error status call ---"); |
| 559 | + |
| 560 | + final CountDownLatch counter = awaitNextServerAndClientCallCloses(1); |
| 561 | + |
| 562 | + // Invoke |
| 563 | + assertThrows(StatusRuntimeException.class, () -> this.testService.error(EMPTY)); |
| 564 | + |
| 565 | + assertTimeoutPreemptively(Duration.ofSeconds(1), (Executable) counter::await); |
| 566 | + |
| 567 | + // Test-Client |
| 568 | + final Counter requestSentCounter = this.meterRegistry |
| 569 | + .find(METRIC_NAME_CLIENT_REQUESTS_SENT) |
| 570 | + .tag(MetricConstants.TAG_METHOD_NAME, "error") |
| 571 | + .counter(); |
| 572 | + assertNotNull(requestSentCounter); |
| 573 | + assertEquals(1, requestSentCounter.count()); |
| 574 | + |
| 575 | + final Counter responseReceivedCounter = this.meterRegistry |
| 576 | + .find(METRIC_NAME_CLIENT_RESPONSES_RECEIVED) |
| 577 | + .tag(MetricConstants.TAG_METHOD_NAME, "error") |
| 578 | + .counter(); |
| 579 | + assertNotNull(responseReceivedCounter); |
| 580 | + assertEquals(0, responseReceivedCounter.count()); |
| 581 | + |
| 582 | + final Timer clientTimer = this.meterRegistry |
| 583 | + .find(METRIC_NAME_CLIENT_PROCESSING_DURATION) |
| 584 | + .tag(MetricConstants.TAG_METHOD_NAME, "error") |
| 585 | + .tag(TAG_STATUS_CODE, INTERNAL.name()) |
| 586 | + .timer(); |
| 587 | + assertNotNull(clientTimer); |
| 588 | + assertEquals(1, clientTimer.count()); |
| 589 | + assertTrue(clientTimer.max(TimeUnit.SECONDS) < 1); |
| 590 | + |
| 591 | + // Test-Server |
| 592 | + final Counter requestsReceivedCounter = this.meterRegistry |
| 593 | + .find(METRIC_NAME_SERVER_REQUESTS_RECEIVED) |
| 594 | + .tag(MetricConstants.TAG_METHOD_NAME, "error") |
| 595 | + .counter(); |
| 596 | + assertNotNull(requestsReceivedCounter); |
| 597 | + assertEquals(1, requestsReceivedCounter.count()); |
| 598 | + |
| 599 | + final Counter responsesSentCounter = this.meterRegistry |
| 600 | + .find(METRIC_NAME_SERVER_RESPONSES_SENT) |
| 601 | + .tag(MetricConstants.TAG_METHOD_NAME, "error") |
| 602 | + .counter(); |
| 603 | + assertNotNull(responsesSentCounter); |
| 604 | + assertEquals(0, responsesSentCounter.count()); |
| 605 | + |
| 606 | + final Timer serverTimer = this.meterRegistry |
| 607 | + .find(METRIC_NAME_SERVER_PROCESSING_DURATION) |
| 608 | + .tag(MetricConstants.TAG_METHOD_NAME, "error") |
| 609 | + .tag(TAG_STATUS_CODE, INTERNAL.name()) |
| 610 | + .timer(); |
| 611 | + assertNotNull(serverTimer); |
| 612 | + assertEquals(1, serverTimer.count()); |
| 613 | + assertTrue(serverTimer.max(TimeUnit.SECONDS) < 1); |
| 614 | + |
| 615 | + // Client has network overhead so it has to be slower |
| 616 | + assertTrue(serverTimer.max(TimeUnit.SECONDS) <= clientTimer.max(TimeUnit.SECONDS)); |
| 617 | + log.info("--- Test completed ---"); |
| 618 | + } |
| 619 | + |
482 | 620 | }
|
0 commit comments