93a39ae884982f75f5befeec063d45091d92dc70
[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.apache.kafka.common.TopicPartition
25 import org.assertj.core.api.Assertions.assertThat
26 import org.assertj.core.data.Percentage
27 import org.jetbrains.spek.api.Spek
28 import org.jetbrains.spek.api.dsl.describe
29 import org.jetbrains.spek.api.dsl.it
30 import org.jetbrains.spek.api.dsl.on
31 import org.onap.dcae.collectors.veshv.tests.utils.verifyGauge
32 import org.onap.dcae.collectors.veshv.tests.utils.verifyTimer
33 import java.time.Instant
34 import java.util.concurrent.TimeUnit
35
36 object MicrometerMetricsTest : Spek({
37     val PREFIX = "hv-kafka-consumer"
38     val doublePrecision = Percentage.withPercentage(0.5)
39     lateinit var registry: PrometheusMeterRegistry
40     lateinit var cut: MicrometerMetrics
41
42     beforeEachTest {
43         registry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT)
44         cut = MicrometerMetrics(registry)
45     }
46
47     describe("Timers") {
48         val arbitraryMessageTravelTime = 100L
49         val messageSentTimeMicros = Instant.now().minusMillis(arbitraryMessageTravelTime).toEpochMilli() * 1000
50         val timerName = "$PREFIX.travel.time"
51
52         on("notifyMessageTravelTime") {
53             it("should update timer $timerName") {
54
55                 val timeBeforeNotifyMicros = Instant.now().toEpochMilli() * 1000
56                 cut.notifyMessageTravelTime(messageSentTimeMicros)
57                 val timeAfterNotifyMicros = Instant.now().toEpochMilli() * 1000
58
59                 registry.verifyTimer(timerName) { timer ->
60                     val travelTimeBeforeNotify = (timeBeforeNotifyMicros - messageSentTimeMicros).toDouble()
61                     val travelTimeAfterNotify = (timeAfterNotifyMicros - messageSentTimeMicros).toDouble()
62                     assertThat(timer.totalTime(TimeUnit.MICROSECONDS))
63                             .isLessThanOrEqualTo(travelTimeAfterNotify)
64                             .isGreaterThanOrEqualTo(travelTimeBeforeNotify)
65
66                 }
67             }
68         }
69     }
70
71     describe("Gauges") {
72         val gaugeName = "$PREFIX.consumer.offset.topic"
73
74         on("notifyOffsetChanged") {
75             val offset = 966L
76             val topicPartition = TopicPartition("sample_topic", 1)
77
78             it("should update $gaugeName") {
79                 cut.notifyOffsetChanged(offset, topicPartition)
80
81                 registry.verifyGauge(gaugeName) {
82                     assertThat(it.value()).isCloseTo(offset.toDouble(), doublePrecision)
83                 }
84             }
85         }
86     }
87 })