-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jasper Kamerling <[email protected]>
- Loading branch information
1 parent
5fe8938
commit bb3dff0
Showing
3 changed files
with
159 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
application/src/test/kotlin/org/gxf/soapbridge/monitoring/MonitoringServiceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package org.gxf.soapbridge.monitoring | ||
|
||
import io.micrometer.core.instrument.ImmutableTag | ||
import io.micrometer.core.instrument.MeterRegistry | ||
import io.micrometer.core.instrument.search.Search | ||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.gxf.soapbridge.monitoring.MonitoringService.Companion.CACHE_SIZE_METRIC | ||
import org.gxf.soapbridge.monitoring.MonitoringService.Companion.CONNECTION_TIMER_CONTEXT_TAG | ||
import org.gxf.soapbridge.monitoring.MonitoringService.Companion.CONNECTION_TIMER_METRIC | ||
import org.gxf.soapbridge.monitoring.MonitoringService.Companion.CONNECTION_TIMER_SUCCESSFUL_TAG | ||
import org.junit.jupiter.api.AfterEach | ||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import java.time.Instant | ||
import java.util.concurrent.TimeUnit | ||
|
||
|
||
class MonitoringServiceTest { | ||
|
||
private lateinit var meterRegistry: MeterRegistry | ||
private lateinit var monitoringService: MonitoringService | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
meterRegistry = SimpleMeterRegistry() | ||
monitoringService = MonitoringService(meterRegistry) | ||
} | ||
|
||
@AfterEach | ||
fun tearDown() { | ||
meterRegistry.clear() | ||
} | ||
|
||
@Test | ||
fun `cache size monitor matches map size`() { | ||
// Meter should not exist before creating it | ||
assertNull(meterRegistry.find(CACHE_SIZE_METRIC).gauge()) | ||
|
||
// Create cache map and gauge | ||
val cacheMap = mutableMapOf<String, String>() | ||
val gauge = monitoringService.monitorCacheSize(cacheMap) | ||
|
||
// Check if the meter exists and is 0 | ||
assertNotNull(meterRegistry.find(CACHE_SIZE_METRIC).gauge()) | ||
assertEquals(0.0, gauge.value()) | ||
|
||
// After adding an entry it should be 1 | ||
cacheMap["key"] = "value" | ||
assertEquals(1.0, gauge.value()) | ||
|
||
// After reassigning the key it should stay at 1 | ||
cacheMap["key"] = "new-value" | ||
assertEquals(1.0, gauge.value()) | ||
|
||
// After adding a second key it should be 2 | ||
cacheMap["new-key"] = "new-value" | ||
assertEquals(2.0, gauge.value()) | ||
} | ||
|
||
@Test | ||
fun `connection timer creates multiple timers`() { | ||
val startTime = Instant.now() | ||
val contextOne = "test-context-one" | ||
val successfulOne = true | ||
|
||
val expectedTagsOne = listOf( | ||
ImmutableTag(CONNECTION_TIMER_CONTEXT_TAG, contextOne), | ||
ImmutableTag(CONNECTION_TIMER_SUCCESSFUL_TAG, successfulOne.toString()) | ||
) | ||
|
||
val contextTwo = "test-context-two" | ||
val successfulTwo = false | ||
val expectedTagsTwo = listOf( | ||
ImmutableTag(CONNECTION_TIMER_CONTEXT_TAG, contextTwo), | ||
ImmutableTag(CONNECTION_TIMER_SUCCESSFUL_TAG, successfulTwo.toString()) | ||
) | ||
monitoringService.recordConnectionTime(startTime, contextOne, successfulOne) | ||
monitoringService.recordConnectionTime(startTime, contextTwo, successfulTwo) | ||
|
||
val timerOne = | ||
Search.`in`(meterRegistry) | ||
.name(CONNECTION_TIMER_METRIC) | ||
.tags(expectedTagsOne) | ||
.timer() | ||
|
||
assertNotNull(timerOne) | ||
|
||
val timerTwo = | ||
Search.`in`(meterRegistry) | ||
.name(CONNECTION_TIMER_METRIC) | ||
.tags(expectedTagsTwo) | ||
.timer() | ||
assertNotNull(timerTwo) | ||
|
||
} | ||
|
||
@Test | ||
fun `connection timer records request times`() { | ||
val startTime = Instant.now() | ||
val context = "test-context" | ||
val successful = true | ||
|
||
monitoringService.recordConnectionTime(startTime, context, successful) | ||
|
||
// Find the timer by name and tags | ||
val expectedTags = listOf( | ||
ImmutableTag(CONNECTION_TIMER_CONTEXT_TAG, context), | ||
ImmutableTag(CONNECTION_TIMER_SUCCESSFUL_TAG, successful.toString()) | ||
) | ||
val timer = Search.`in`(meterRegistry) | ||
.name(CONNECTION_TIMER_METRIC) | ||
.tags(expectedTags) | ||
.timer() | ||
assertNotNull(timer) | ||
check(timer != null) | ||
|
||
|
||
assertThat(timer.count()).isEqualTo(1) | ||
assertThat(timer.totalTime(TimeUnit.NANOSECONDS)).isNotEqualTo(0) | ||
assertThat(timer.max(TimeUnit.NANOSECONDS)).isNotEqualTo(0) | ||
assertThat(timer.mean(TimeUnit.NANOSECONDS)).isNotEqualTo(0) | ||
|
||
monitoringService.recordConnectionTime(startTime, context, successful) | ||
|
||
assertThat(timer.count()).isEqualTo(2) | ||
assertThat(timer.totalTime(TimeUnit.NANOSECONDS)).isNotEqualTo(0) | ||
assertThat(timer.max(TimeUnit.NANOSECONDS)).isNotEqualTo(0) | ||
assertThat(timer.mean(TimeUnit.NANOSECONDS)).isNotEqualTo(0) | ||
|
||
} | ||
|
||
} |