Skip to content

Commit

Permalink
CE3: Use twitter-util Time and Stopwatch for Clock implementation.
Browse files Browse the repository at this point in the history
Allows using the twitter-util testing facilities.
  • Loading branch information
dangerousben committed Jun 14, 2021
1 parent 66a0ac8 commit bea885c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package io.catbird.util.effect

import cats.effect.Clock
import cats.effect.kernel.{ MonadCancel, Outcome, Sync }
import com.twitter.util.{ Future, Monitor }
import com.twitter.util.{ Future, Monitor, Stopwatch, Time }
import io.catbird.util.{ Rerunnable, RerunnableMonadError }

import java.lang.Throwable
import java.util.concurrent.TimeUnit
import java.lang.System

import scala.Unit
import scala.concurrent.duration.FiniteDuration
Expand All @@ -24,10 +23,10 @@ trait RerunnableInstances {
Rerunnable(thunk)

final override def realTime: Rerunnable[FiniteDuration] =
Rerunnable(FiniteDuration(System.currentTimeMillis(), TimeUnit.MILLISECONDS))
Rerunnable(FiniteDuration(Time.nowNanoPrecision.inNanoseconds, TimeUnit.NANOSECONDS))

final override def monotonic: Rerunnable[FiniteDuration] =
Rerunnable(FiniteDuration(System.nanoTime(), TimeUnit.NANOSECONDS))
Rerunnable(FiniteDuration(Stopwatch.timeNanos(), TimeUnit.NANOSECONDS))

final override def forceR[A, B](fa: Rerunnable[A])(fb: Rerunnable[B]): Rerunnable[B] =
fa.liftToTry.flatMap { resultA =>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package io.catbird.util.effect

import cats.effect.MonadCancel
import cats.effect.kernel.{ Clock, MonadCancel, Outcome }
import cats.effect.kernel.testkit.SyncTypeGenerators
import cats.effect.laws.SyncTests
import cats.instances.either._
import cats.instances.int._
import cats.instances.tuple._
import cats.instances.unit._
import cats.laws.discipline.arbitrary._
import com.twitter.util.{ Await, Monitor, Throw }
import com.twitter.util.{ Await, Monitor, Throw, Time }
import io.catbird.util.Rerunnable

class RerunnableSuite
Expand All @@ -19,6 +19,22 @@ class RerunnableSuite
// This includes tests for Clock, MonadCancel, and MonadError
checkAll("Rerunnable[Int]", SyncTests[Rerunnable].sync[Int, Int, Int])

test("Retrieval of real time") {
val nanos = 123456789L
val result = Time.withTimeAt(Time.fromNanoseconds(nanos)) { _ =>
unsafeRun(Clock[Rerunnable].realTime.map(_.toNanos))
}
assert(result == Outcome.succeeded(Some(nanos)))
}

test("Retrieval of monotonic time") {
val nanos = 123456789L
val result = Time.withTimeAt(Time.fromNanoseconds(nanos)) { _ =>
unsafeRun(Clock[Rerunnable].monotonic.map(_.toNanos))
}
assert(result == Outcome.succeeded(Some(nanos)))
}

test("Exceptions thrown by release are handled by Monitor") {
val useException = new Exception("thrown by use")
val releaseException = new Exception("thrown by release")
Expand Down

0 comments on commit bea885c

Please sign in to comment.