From a97c7dac0748732700d3a98ee44bd2fdf847ffbc Mon Sep 17 00:00:00 2001 From: Ivan Topolnjak Date: Mon, 18 Dec 2017 12:50:41 +0100 Subject: move alignment and duration utilities to the companion object --- .../src/test/scala/kamon/util/ClockSpec.scala | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 kamon-core-tests/src/test/scala/kamon/util/ClockSpec.scala (limited to 'kamon-core-tests') 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() +} -- cgit v1.2.3