diff options
author | Chris Rebert <code@rebertia.com> | 2015-04-14 02:01:47 +0300 |
---|---|---|
committer | Chris Rebert <code@rebertia.com> | 2015-04-14 02:01:47 +0300 |
commit | 7df2016b06f99ed59a934d9dfccf4ab85db8e246 (patch) | |
tree | 7e5ebfc1d15d7f82e70aa28abb42a8443e3f5290 /src | |
parent | ced824f9746a304c44acd138012fe59cd89883c4 (diff) |
Add & use StoppedClock in unit tests
Diffstat (limited to 'src')
-rw-r--r-- | src/main/scala/com/getbootstrap/no_carrier/util/package.scala | 10 | ||||
-rw-r--r-- | src/test/scala/RichInstantSpec.scala | 22 |
2 files changed, 24 insertions, 8 deletions
diff --git a/src/main/scala/com/getbootstrap/no_carrier/util/package.scala b/src/main/scala/com/getbootstrap/no_carrier/util/package.scala index 88a811d..1645d84 100644 --- a/src/main/scala/com/getbootstrap/no_carrier/util/package.scala +++ b/src/main/scala/com/getbootstrap/no_carrier/util/package.scala @@ -1,6 +1,6 @@ package com.getbootstrap.no_carrier -import java.time.{Duration, Instant} +import java.time.{Clock, Instant, Duration} package object util { val InstantOrdering = implicitly[Ordering[Instant]] @@ -8,8 +8,12 @@ package object util { implicit class RichInstant(instant: Instant) { import InstantOrdering._ - def +(duration: Duration) = instant.plus(duration) - def isBeyondTimeout(timeout: Duration): Boolean = (instant + timeout) < Instant.now() + def +(duration: Duration): Instant = instant.plus(duration) + def isBeyondTimeout(timeout: Duration)(implicit clock: Clock): Boolean = { + val now = Instant.now(clock) + val deadline = (instant + timeout) + deadline < now + } } implicit class RichTraversableOnce[T](trav: TraversableOnce[T]) { diff --git a/src/test/scala/RichInstantSpec.scala b/src/test/scala/RichInstantSpec.scala index 41ded96..59c3c69 100644 --- a/src/test/scala/RichInstantSpec.scala +++ b/src/test/scala/RichInstantSpec.scala @@ -3,6 +3,9 @@ import org.specs2.mutable._ import com.getbootstrap.no_carrier.util.RichInstant class RichInstantSpec extends Specification { + val utc = ZoneId.of("UTC") + val pseudoNow = ZonedDateTime.of(LocalDateTime.of(2015, 3, 17, 1, 2), utc).toInstant + implicit val clock = StoppedClock(pseudoNow) val oneDay = Duration.ofDays(1) val twoDays = Duration.ofDays(2) val twoDaysAndOneSec = twoDays.plusSeconds(1) @@ -10,17 +13,26 @@ class RichInstantSpec extends Specification { "isBeyondTimeout" should { "be false when no time has elapsed" in { - (Instant.now() isBeyondTimeout twoDays) must beFalse + (pseudoNow isBeyondTimeout twoDays) must beFalse } "be false when just a bit of time has elapsed" in { - (Instant.now().minus(oneDay) isBeyondTimeout twoDays) must beFalse + (pseudoNow.minus(oneDay) isBeyondTimeout twoDays) must beFalse } "be false at the exact instant that the timeout has elapsed" in { - (Instant.now().minus(twoDays) isBeyondTimeout twoDays) must beFalse + (pseudoNow.minus(twoDays) isBeyondTimeout twoDays) must beFalse } "be true after the timeout has expired" in { - (Instant.now().minus(twoDaysAndOneSec) isBeyondTimeout twoDays) must beTrue - (Instant.now().minus(threeDays) isBeyondTimeout twoDays) must beTrue + (pseudoNow.minus(twoDaysAndOneSec) isBeyondTimeout twoDays) must beTrue + (pseudoNow.minus(threeDays) isBeyondTimeout twoDays) must beTrue } } } + +case class StoppedClock(atInstant: Instant, timeZone: ZoneId = ZoneId.of("UTC")) extends Clock { + @Override + def instant: Instant = atInstant + @Override + val getZone: ZoneId = timeZone + @Override + def withZone(zone: ZoneId) = StoppedClock(atInstant, zone) +} |