aboutsummaryrefslogtreecommitdiff
path: root/kamon-core-tests
diff options
context:
space:
mode:
authorIvan Topolnjak <ivantopo@gmail.com>2017-12-18 12:50:41 +0100
committerIvan Topolnjak <ivantopo@gmail.com>2017-12-18 13:27:37 +0100
commita97c7dac0748732700d3a98ee44bd2fdf847ffbc (patch)
tree7e90436c56d59aea5c201d00614a1505ca88762b /kamon-core-tests
parent72537e7a94d90131f8945cd78b6299e97e4cd027 (diff)
downloadKamon-a97c7dac0748732700d3a98ee44bd2fdf847ffbc.tar.gz
Kamon-a97c7dac0748732700d3a98ee44bd2fdf847ffbc.tar.bz2
Kamon-a97c7dac0748732700d3a98ee44bd2fdf847ffbc.zip
move alignment and duration utilities to the companion object
Diffstat (limited to 'kamon-core-tests')
-rw-r--r--kamon-core-tests/src/test/scala/kamon/util/ClockSpec.scala39
1 files changed, 39 insertions, 0 deletions
diff --git a/kamon-core-tests/src/test/scala/kamon/util/ClockSpec.scala b/kamon-core-tests/src/test/scala/kamon/util/ClockSpec.scala
new file mode 100644
index 00000000..5f83d22c
--- /dev/null
+++ b/kamon-core-tests/src/test/scala/kamon/util/ClockSpec.scala
@@ -0,0 +1,39 @@
+package kamon.util
+
+import java.time.{Duration, Instant}
+
+import kamon.util
+import org.scalatest.{Matchers, WordSpec}
+
+class ClockSpec extends WordSpec with Matchers {
+ "the Clock" should {
+ "generate nanosecond precision Instants" in {
+ newClock().instant().getNano() % MicrosInSecond shouldNot be(0)
+ }
+
+ "turn Instants into micros" in {
+ Clock.toEpochMicros(Instant.parse("2017-12-18T08:39:59.000000000Z")) shouldBe 1513586399000000L
+ Clock.toEpochMicros(Instant.parse("2017-12-18T08:39:59.000000010Z")) shouldBe 1513586399000000L
+ Clock.toEpochMicros(Instant.parse("2017-12-18T08:39:59.987654321Z")) shouldBe 1513586399987654L
+ Clock.toEpochMicros(Instant.parse("2017-12-18T08:39:59.987000000Z")) shouldBe 1513586399987000L
+ }
+
+ "calculate nanos between two Instants" in {
+ Clock.nanosBetween(Instant.parse("2017-12-18T08:39:59.987654321Z"), Instant.parse("2017-12-18T08:39:59.987654322Z")) shouldBe 1
+ Clock.nanosBetween(Instant.parse("2017-12-18T08:39:59.987654322Z"), Instant.parse("2017-12-18T08:39:59.987654321Z")) shouldBe -1
+ Clock.nanosBetween(Instant.parse("2017-12-18T08:39:59.987Z"), Instant.parse("2017-12-18T08:39:59.988Z")) shouldBe 1000000
+ Clock.nanosBetween(Instant.parse("2017-12-18T08:39:59.987654Z"), Instant.parse("2017-12-18T08:39:59.987Z")) shouldBe -654000
+ }
+
+ "calculate ticks aligned to rounded boundaries" in {
+ Clock.nextTick(Instant.parse("2017-12-18T08:39:59.999Z"), Duration.ofSeconds(10)).toString shouldBe "2017-12-18T08:40:00Z"
+ Clock.nextTick(Instant.parse("2017-12-18T08:40:00.000Z"), Duration.ofSeconds(10)).toString shouldBe "2017-12-18T08:40:10Z"
+ Clock.nextTick(Instant.parse("2017-12-18T08:39:14.906Z"), Duration.ofSeconds(10)).toString shouldBe "2017-12-18T08:39:20Z"
+ }
+ }
+
+ val MicrosInSecond = 1000000
+
+ def newClock(): Clock =
+ new util.Clock.Default()
+}