41587867193523a0fd6f4248cf2439cc99033269
[dcaegen2/collectors/hv-ves.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2019 NOKIA
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.dcae.collectors.veshv.kafkaconsumer.metrics
21
22 import io.micrometer.prometheus.PrometheusConfig
23 import io.micrometer.prometheus.PrometheusMeterRegistry
24 import org.assertj.core.api.Assertions.assertThat
25 import org.assertj.core.data.Percentage
26 import org.jetbrains.spek.api.Spek
27 import org.jetbrains.spek.api.dsl.describe
28 import org.jetbrains.spek.api.dsl.it
29 import org.jetbrains.spek.api.dsl.on
30 import org.onap.dcae.collectors.veshv.tests.utils.verifyGauge
31 import org.onap.dcae.collectors.veshv.tests.utils.verifyTimer
32 import java.time.Instant
33 import java.util.concurrent.TimeUnit
34
35 object MicrometerMetricsTest : Spek({
36     val PREFIX = "hv-kafka-consumer"
37     val doublePrecision = Percentage.withPercentage(0.5)
38     lateinit var registry: PrometheusMeterRegistry
39     lateinit var cut: MicrometerMetrics
40
41     beforeEachTest {
42         registry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT)
43         cut = MicrometerMetrics(registry)
44     }
45
46     describe("Timers") {
47         val arbitraryMessageTravelTime = 100L
48         val messageSentTimeMicros = Instant.now().minusMillis(arbitraryMessageTravelTime).toEpochMilli() * 1000
49         val timerName = "$PREFIX.travel.time"
50
51         on("notifyMessageTravelTime") {
52             it("should update timer $timerName") {
53
54                 val timeBeforeNotifyMicros = Instant.now().toEpochMilli() * 1000
55                 cut.notifyMessageTravelTime(messageSentTimeMicros)
56                 val timeAfterNotifyMicros = Instant.now().toEpochMilli() * 1000
57
58                 registry.verifyTimer(timerName) { timer ->
59                     val travelTimeBeforeNotify = (timeBeforeNotifyMicros - messageSentTimeMicros).toDouble()
60                     val travelTimeAfterNotify = (timeAfterNotifyMicros - messageSentTimeMicros).toDouble()
61                     assertThat(timer.totalTime(TimeUnit.MICROSECONDS))
62                             .isLessThanOrEqualTo(travelTimeAfterNotify)
63                             .isGreaterThanOrEqualTo(travelTimeBeforeNotify)
64
65                 }
66             }
67         }
68     }
69
70     describe("Gauges") {
71         val gaugeName = "$PREFIX.consumer.offset"
72
73         on("notifyOffsetChanged") {
74             val offset = 966L
75
76             it("should update $gaugeName") {
77                 cut.notifyOffsetChanged(offset)
78
79                 registry.verifyGauge(gaugeName) {
80                     assertThat(it.value()).isCloseTo(offset.toDouble(), doublePrecision)
81                 }
82             }
83         }
84     }
85 })