Skip to content

Commit aa46d2d

Browse files
authored
Merge pull request #13 from navikt/timeUnit
Time unit
2 parents 7b8781d + 2f463ed commit aa46d2d

File tree

12 files changed

+179
-91
lines changed

12 files changed

+179
-91
lines changed

buildSrc/src/main/kotlin/recommendedVersions.kt

+3-6
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@ object Jjwt {
1919
const val jackson = "$groupId:jjwt-jackson:$version"
2020
}
2121

22-
object Kluent {
23-
private const val version = "1.68"
24-
const val kluent = "org.amshove.kluent:kluent:$version"
25-
}
26-
2722
object Kotest {
28-
const val version = "4.3.1"
23+
private const val version = "4.3.1"
2924
private const val groupId = "io.kotest"
3025

26+
const val runnerJunit5 = "$groupId:kotest-runner-junit5:$version"
27+
const val assertionsCore = "$groupId:kotest-assertions-core:$version"
3128
const val extensions = "$groupId:kotest-extensions:$version"
3229
}
3330

dittnav-common-influxdb/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ dependencies {
3636
testImplementation(kotlin("test-junit5"))
3737
testImplementation(Junit.api)
3838
testImplementation(Junit.engine)
39-
testImplementation(Kluent.kluent)
4039
testImplementation(Mockk.mockk)
40+
testImplementation(Kotest.assertionsCore)
4141
}
4242

4343
tasks {

dittnav-common-influxdb/src/main/kotlin/no/nav/personbruker/dittnav/common/metrics/influxdb/InfluxConfig.kt

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package no.nav.personbruker.dittnav.common.metrics.influxdb
22

3+
import java.util.concurrent.TimeUnit
4+
import java.util.concurrent.TimeUnit.*
5+
36
data class InfluxConfig (
47
val hostName: String,
58
val userName: String,
@@ -11,4 +14,11 @@ data class InfluxConfig (
1114
val clusterName: String,
1215
val namespace: String,
1316
val enableEventBatching: Boolean = true,
14-
)
17+
val timePrecision: TimeUnit = MILLISECONDS
18+
) {
19+
init {
20+
require( timePrecision in listOf( SECONDS, MILLISECONDS, MICROSECONDS, NANOSECONDS ) ) {
21+
"timePrecision må være en av [SECONDS, MILLISECONDS, MICROSECONDS, NANOSECONDS]."
22+
}
23+
}
24+
}

dittnav-common-influxdb/src/main/kotlin/no/nav/personbruker/dittnav/common/metrics/influxdb/InfluxMetricsReporter.kt

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ package no.nav.personbruker.dittnav.common.metrics.influxdb
22

33
import no.nav.personbruker.dittnav.common.metrics.MetricsReporter
44
import org.influxdb.dto.Point
5-
import java.util.concurrent.TimeUnit
65

76
class InfluxMetricsReporter internal constructor(influxConfig: InfluxConfig, private val dataPointRelay: DataPointRelay) : MetricsReporter {
87

8+
private val timePrecision = influxConfig.timePrecision
9+
910
constructor(influxConfig: InfluxConfig) : this(influxConfig, DataPointRelayFactory.createDataPointRelay(influxConfig))
1011

1112
override suspend fun registerDataPoint(measurementName: String, fields: Map<String, Any>, tags: Map<String, String>) {
1213
val point = Point.measurement(measurementName)
13-
.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
14+
.setTime()
1415
.tag(tags)
1516
.tag(DEFAULT_TAGS)
1617
.fields(fields)
@@ -19,6 +20,8 @@ class InfluxMetricsReporter internal constructor(influxConfig: InfluxConfig, pri
1920
dataPointRelay.submitDataPoint(point)
2021
}
2122

23+
private fun Point.Builder.setTime() = time(UnitsSinceEpochHelper.unitsSinceEpoch(timePrecision), timePrecision)
24+
2225
private val DEFAULT_TAGS = listOf(
2326
"application" to influxConfig.applicationName,
2427
"cluster" to influxConfig.clusterName,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package no.nav.personbruker.dittnav.common.metrics.influxdb
2+
3+
import java.time.Instant
4+
import java.time.temporal.ChronoUnit
5+
import java.util.concurrent.TimeUnit
6+
7+
internal object UnitsSinceEpochHelper {
8+
fun unitsSinceEpoch(unit: TimeUnit, toDate: Instant = Instant.now()): Long {
9+
return when(unit) {
10+
TimeUnit.SECONDS -> ChronoUnit.SECONDS.between(Instant.EPOCH, toDate)
11+
TimeUnit.MILLISECONDS -> ChronoUnit.MILLIS.between(Instant.EPOCH, toDate)
12+
TimeUnit.MICROSECONDS -> ChronoUnit.MICROS.between(Instant.EPOCH, toDate)
13+
TimeUnit.NANOSECONDS -> ChronoUnit.NANOS.between(Instant.EPOCH, toDate)
14+
else -> throw IllegalStateException("Invalid time precision")
15+
}
16+
}
17+
}

dittnav-common-influxdb/src/test/kotlin/no/nav/personbruker/dittnav/common/metrics/influxdb/InfluxMetricsReporterTest.kt

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package no.nav.personbruker.dittnav.common.metrics.influxdb
22

3+
import io.kotest.matchers.collections.shouldContainAll
4+
import io.kotest.matchers.longs.shouldBeGreaterThanOrEqual
5+
import io.kotest.matchers.longs.shouldBeLessThanOrEqual
6+
import io.kotest.matchers.shouldBe
37
import io.mockk.coEvery
48
import io.mockk.mockk
59
import io.mockk.slot
610
import kotlinx.coroutines.runBlocking
7-
import org.amshove.kluent.*
811
import org.influxdb.dto.Point
912
import org.junit.jupiter.api.Test
1013
import java.util.concurrent.TimeUnit
@@ -67,12 +70,12 @@ internal class InfluxMetricsReporterTest {
6770
val end = System.currentTimeMillis()
6871

6972

70-
resultMeasurement `should be equal to` measurementName
71-
resultFields `should be equal to` fields
72-
resultTags.values `should contain same` listOf(application, cluster, namespace, tagVal)
73-
resultTime `should be greater or equal to` start
74-
resultTime `should be less or equal to` end
75-
resultPrecision `should be equal to` TimeUnit.MILLISECONDS
73+
resultMeasurement shouldBe measurementName
74+
resultFields shouldBe fields
75+
resultTags.values shouldContainAll listOf(application, cluster, namespace, tagVal)
76+
resultTime shouldBeGreaterThanOrEqual start
77+
resultTime shouldBeLessThanOrEqual end
78+
resultPrecision shouldBe TimeUnit.MILLISECONDS
7679
}
7780

7881
private inline fun <reified T: Any> Point.getPrivateField(fieldName: String): T {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package no.nav.personbruker.dittnav.common.metrics.influxdb
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.jupiter.api.Test
5+
import java.time.Instant
6+
import java.time.temporal.ChronoUnit
7+
import java.util.concurrent.TimeUnit.*
8+
9+
internal class UnitsSinceEpochHelperTest {
10+
private val epochSeconds = 100200300L
11+
private val nanos = 400500600L
12+
13+
private val endDate = Instant.ofEpochSecond(epochSeconds, nanos)
14+
15+
@Test
16+
fun `Finner riktig antall sekunder siden epoch`() {
17+
val secondsSinceEpoch = UnitsSinceEpochHelper.unitsSinceEpoch(SECONDS, endDate)
18+
19+
secondsSinceEpoch shouldBe 100200300L
20+
}
21+
22+
@Test
23+
fun `Finner riktig antall millisekunder siden epoch`() {
24+
val millisecondsSinceEpoch = UnitsSinceEpochHelper.unitsSinceEpoch(MILLISECONDS, endDate)
25+
26+
millisecondsSinceEpoch shouldBe 100200300400L
27+
}
28+
29+
@Test
30+
fun `Finner riktig antall mikrosekunder siden epoch`() {
31+
val microsecondsSinceEpoch = UnitsSinceEpochHelper.unitsSinceEpoch(MICROSECONDS, endDate)
32+
33+
microsecondsSinceEpoch shouldBe 100200300400500L
34+
}
35+
36+
@Test
37+
fun `Finner riktig antall nanosekunder siden epoch`() {
38+
val nanosecondsSinceEpoch = UnitsSinceEpochHelper.unitsSinceEpoch(NANOSECONDS, endDate)
39+
40+
nanosecondsSinceEpoch shouldBe 100200300400500600L
41+
}
42+
43+
}

dittnav-common-utils/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ dependencies {
1414
testImplementation(Jjwt.impl)
1515
testImplementation(Jjwt.jackson)
1616
testImplementation(Junit.engine)
17-
testImplementation(Kluent.kluent)
17+
testImplementation(Kotest.assertionsCore)
1818
testImplementation(Mockk.mockk)
1919
}
2020

dittnav-common-utils/src/test/kotlin/no/nav/personbruker/dittnav/common/util/config/EnvVarAsTypeKtTest.kt

+33-24
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
package no.nav.personbruker.dittnav.common.util.config
22

3+
import io.kotest.assertions.throwables.shouldThrow
4+
import io.kotest.matchers.shouldBe
35
import io.mockk.every
46
import io.mockk.mockkObject
57
import io.mockk.unmockkObject
68
import no.nav.personbruker.dittnav.common.util.config.TypedEnvVar.getEnvVarAsType
79
import no.nav.personbruker.dittnav.common.util.config.TypedEnvVar.getEnvVarAsTypedList
810
import no.nav.personbruker.dittnav.common.util.config.TypedEnvVar.getOptionalEnvVarAsType
911
import no.nav.personbruker.dittnav.common.util.config.TypedEnvVar.getOptionalEnvVarAsTypedList
10-
import org.amshove.kluent.`should be equal to`
11-
import org.amshove.kluent.`should throw`
12-
import org.amshove.kluent.invoking
1312
import org.junit.jupiter.api.AfterEach
1413
import org.junit.jupiter.api.BeforeEach
1514
import org.junit.jupiter.api.Test
16-
import java.lang.IllegalArgumentException
17-
import java.lang.IllegalStateException
15+
import kotlin.IllegalArgumentException
16+
import kotlin.IllegalStateException
1817

1918
internal class EnvVarAsTypeKtTest {
2019

@@ -43,7 +42,7 @@ internal class EnvVarAsTypeKtTest {
4342

4443
val result = getEnvVarAsType(envName, default, String::toInt)
4544

46-
result `should be equal to` envVal
45+
result shouldBe envVal
4746
}
4847

4948
@Test
@@ -54,21 +53,25 @@ internal class EnvVarAsTypeKtTest {
5453

5554
val result = getEnvVarAsType(envName, default, String::toInt)
5655

57-
result `should be equal to` default
56+
result shouldBe default
5857
}
5958

6059
@Test
6160
fun `Function getEnvVarAsType should throw exception if variable was not found and no default was specified`() {
6261
every { SystemWrapper.getEnvVar(envName) } returns null
6362

64-
invoking { getEnvVarAsType(envName, mapper = String::toInt) } `should throw` IllegalStateException::class
63+
shouldThrow<IllegalStateException> {
64+
getEnvVarAsType(envName, mapper = String::toInt)
65+
}
6566
}
6667

6768
@Test
6869
fun `Function getEnvVarAsType should throw exception if variable could not be mapped`() {
6970
every { SystemWrapper.getEnvVar(envName) } returns "onetwothree"
7071

71-
invoking { getEnvVarAsType(envName, mapper = String::toInt) } `should throw` IllegalArgumentException::class
72+
shouldThrow<IllegalArgumentException> {
73+
getEnvVarAsType(envName, mapper = String::toInt)
74+
}
7275
}
7376

7477
@Test
@@ -79,7 +82,7 @@ internal class EnvVarAsTypeKtTest {
7982

8083
val result = getOptionalEnvVarAsType(envName, default, String::toInt)
8184

82-
result `should be equal to` envVal
85+
result shouldBe envVal
8386
}
8487

8588
@Test
@@ -90,7 +93,7 @@ internal class EnvVarAsTypeKtTest {
9093

9194
val result = getOptionalEnvVarAsType(envName, default, String::toInt)
9295

93-
result `should be equal to` default
96+
result shouldBe default
9497
}
9598

9699
@Test
@@ -99,14 +102,14 @@ internal class EnvVarAsTypeKtTest {
99102

100103
val result = getOptionalEnvVarAsType(envName, mapper = String::toInt)
101104

102-
result `should be equal to` null
105+
result shouldBe null
103106
}
104107

105108
@Test
106109
fun `Function getOptionalEnvVarAsType should throw exception if variable could not be mapped`() {
107110
every { SystemWrapper.getEnvVar(envName) } returns "onetwo"
108111

109-
invoking { getEnvVarAsType(envName, mapper = String::toInt) } `should throw` IllegalArgumentException::class
112+
shouldThrow<IllegalArgumentException> { getEnvVarAsType(envName, mapper = String::toInt) }
110113
}
111114

112115
@Test
@@ -117,7 +120,7 @@ internal class EnvVarAsTypeKtTest {
117120

118121
val result = getEnvVarAsTypedList(envName, default, mapper = String::toInt)
119122

120-
result `should be equal to` listEnvVal
123+
result shouldBe listEnvVal
121124
}
122125

123126
@Test
@@ -128,21 +131,25 @@ internal class EnvVarAsTypeKtTest {
128131

129132
val result = getEnvVarAsTypedList(envName, default, mapper = String::toInt)
130133

131-
result `should be equal to` default
134+
result shouldBe default
132135
}
133136

134137
@Test
135138
fun `Function getEnvVarAsTypedList should throw exception if variable was not found and no default was specified`() {
136139
every { SystemWrapper.getEnvVar(envName) } returns null
137140

138-
invoking { getEnvVarAsTypedList(envName, mapper = String::toInt) } `should throw` IllegalStateException::class
141+
shouldThrow<IllegalStateException> {
142+
getEnvVarAsTypedList(envName, mapper = String::toInt)
143+
}
139144
}
140145

141146
@Test
142147
fun `Function getEnvVarAsTypedList should throw exception if variable could not be mapped`() {
143148
every { SystemWrapper.getEnvVar(envName) } returns "one,two"
144149

145-
invoking { getEnvVarAsTypedList(envName, mapper = String::toInt) } `should throw` IllegalArgumentException::class
150+
shouldThrow<IllegalArgumentException> {
151+
getEnvVarAsTypedList(envName, mapper = String::toInt)
152+
}
146153
}
147154

148155
@Test
@@ -153,7 +160,7 @@ internal class EnvVarAsTypeKtTest {
153160

154161
val expected = listOf(4, 5, 6)
155162

156-
getEnvVarAsTypedList(envName, separator = "|", mapper = String::toInt) `should be equal to` expected
163+
getEnvVarAsTypedList(envName, separator = "|", mapper = String::toInt) shouldBe expected
157164
}
158165

159166
@Test
@@ -164,7 +171,7 @@ internal class EnvVarAsTypeKtTest {
164171

165172
val result = getOptionalEnvVarAsTypedList(envName, default, mapper = String::toInt)
166173

167-
result `should be equal to` listEnvVal
174+
result shouldBe listEnvVal
168175
}
169176

170177
@Test
@@ -175,7 +182,7 @@ internal class EnvVarAsTypeKtTest {
175182

176183
val result = getOptionalEnvVarAsTypedList(envName, default, mapper = String::toInt)
177184

178-
result `should be equal to` default
185+
result shouldBe default
179186
}
180187

181188
@Test
@@ -184,14 +191,16 @@ internal class EnvVarAsTypeKtTest {
184191

185192
val result = getOptionalEnvVarAsTypedList(envName, mapper = String::toInt)
186193

187-
result `should be equal to` emptyList()
194+
result shouldBe emptyList()
188195
}
189196

190197
@Test
191198
fun `Function getOptionalEnvVarAsTypedList should throw exception if variable could not be mapped`() {
192199
every { SystemWrapper.getEnvVar(envName) } returns "one,two"
193200

194-
invoking { getOptionalEnvVarAsTypedList(envName, mapper = String::toInt) } `should throw` IllegalArgumentException::class
201+
shouldThrow<IllegalArgumentException> {
202+
getOptionalEnvVarAsTypedList(envName, mapper = String::toInt)
203+
}
195204
}
196205

197206
@Test
@@ -202,6 +211,6 @@ internal class EnvVarAsTypeKtTest {
202211

203212
val expected = listOf(7, 8, 9)
204213

205-
getOptionalEnvVarAsTypedList(envName, separator = "|", mapper = String::toInt) `should be equal to` expected
214+
getOptionalEnvVarAsTypedList(envName, separator = "|", mapper = String::toInt) shouldBe expected
206215
}
207-
}
216+
}

0 commit comments

Comments
 (0)