Add metrics for active connections count
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-ct / src / test / kotlin / org / onap / dcae / collectors / veshv / tests / component / MetricsSpecification.kt
index dd8acf7..f457aea 100644 (file)
@@ -23,17 +23,27 @@ import com.google.protobuf.ByteString
 import org.assertj.core.api.Assertions.assertThat
 import org.jetbrains.spek.api.Spek
 import org.jetbrains.spek.api.dsl.describe
+import org.jetbrains.spek.api.dsl.given
 import org.jetbrains.spek.api.dsl.it
+import org.jetbrains.spek.api.dsl.on
 import org.onap.dcae.collectors.veshv.domain.VesEventDomain
 import org.onap.dcae.collectors.veshv.domain.VesEventDomain.HEARTBEAT
 import org.onap.dcae.collectors.veshv.domain.VesEventDomain.PERF3GPP
+import org.onap.dcae.collectors.veshv.model.ClientRejectionCause
 import org.onap.dcae.collectors.veshv.model.MessageDropCause.INVALID_MESSAGE
 import org.onap.dcae.collectors.veshv.model.MessageDropCause.ROUTE_NOT_FOUND
 import org.onap.dcae.collectors.veshv.tests.fakes.MEASUREMENTS_FOR_VF_SCALING_TOPIC
 import org.onap.dcae.collectors.veshv.tests.fakes.PERF3GPP_TOPIC
 import org.onap.dcae.collectors.veshv.tests.fakes.basicConfiguration
 import org.onap.dcae.collectors.veshv.tests.fakes.twoDomainsToOneTopicConfiguration
-import org.onap.dcae.collectors.veshv.tests.utils.*
+import org.onap.dcae.collectors.veshv.tests.utils.garbageFrame
+import org.onap.dcae.collectors.veshv.tests.utils.messageWithInvalidListenerVersion
+import org.onap.dcae.collectors.veshv.tests.utils.messageWithInvalidWireFrameHeader
+import org.onap.dcae.collectors.veshv.tests.utils.messageWithPayloadOfSize
+import org.onap.dcae.collectors.veshv.tests.utils.vesEvent
+import org.onap.dcae.collectors.veshv.tests.utils.vesWireFrameMessage
+import org.onap.dcae.collectors.veshv.tests.utils.wireFrameMessageWithInvalidPayload
+import java.time.Duration
 
 object MetricsSpecification : Spek({
     debugRx(false)
@@ -102,6 +112,21 @@ object MetricsSpecification : Spek({
         }
     }
 
+    describe("Processing time") {
+        it("should gather processing time metric") {
+            val delay = Duration.ofMillis(10)
+            val sut = vesHvWithDelayingSink(delay)
+
+            sut.handleConnection(vesWireFrameMessage(PERF3GPP))
+
+
+            val metrics = sut.metrics
+            assertThat(metrics.lastProcessingTimeMicros)
+                    .describedAs("processingTime metric")
+                    .isGreaterThanOrEqualTo(delay.toNanos().toDouble() / 1000.0)
+        }
+    }
+
     describe("Messages dropped metrics") {
         it("should gather metrics for invalid messages") {
             val sut = vesHvWithNoOpSink(basicConfiguration)
@@ -133,7 +158,7 @@ object MetricsSpecification : Spek({
                     .isEqualTo(1)
         }
 
-        it("should gather summed metrics for dropped messages"){
+        it("should gather summed metrics for dropped messages") {
             val sut = vesHvWithNoOpSink(basicConfiguration)
 
             sut.handleConnection(
@@ -148,4 +173,30 @@ object MetricsSpecification : Spek({
                     .isEqualTo(2)
         }
     }
+
+    describe("clients rejected metrics") {
+        given("rejection causes") {
+            mapOf(
+                    ClientRejectionCause.PAYLOAD_SIZE_EXCEEDED_IN_MESSAGE to
+                            messageWithPayloadOfSize(Sut.MAX_PAYLOAD_SIZE_BYTES + 1),
+                    ClientRejectionCause.INVALID_WIRE_FRAME_MARKER to garbageFrame()
+            ).forEach { cause, vesMessage ->
+                on("cause $cause") {
+                    it("should notify correct metrics") {
+                        val sut = vesHvWithNoOpSink()
+
+                        sut.handleConnection(vesMessage)
+
+                        val metrics = sut.metrics
+                        assertThat(metrics.clientRejectionCause.size)
+                                .describedAs("metrics were notified with only one rejection cause")
+                                .isOne()
+                        assertThat(metrics.clientRejectionCause[cause])
+                                .describedAs("metrics were notified only once with correct client rejection cause")
+                                .isOne()
+                    }
+                }
+            }
+        }
+    }
 })