Skip to content

Commit

Permalink
Rename Clock to MonotonicClock
Browse files Browse the repository at this point in the history
  • Loading branch information
voidcontext committed Mar 21, 2020
1 parent 9c7aa14 commit b87c4e0
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions examples/src/main/scala/vdx/fetchfile/examples/Main.scala
@@ -1,6 +1,6 @@
package vdx.fetchfile.examples

import cats.effect.{Clock => _, _}
import cats.effect._
import cats.syntax.functor._
import vdx.fetchfile._

Expand All @@ -10,7 +10,7 @@ import java.io.{File, FileOutputStream}
object Main extends IOApp {
def run(args: List[String]): IO[ExitCode] = {

implicit val clock: Clock = Clock.system
implicit val clock: MonotonicClock = MonotonicClock.system

val outFile = new File("/tmp/100MB.bin")

Expand Down
Expand Up @@ -5,15 +5,15 @@ package vdx.fetchfile
* In case of cats.effect.Clock[F], the evaluation of IO makes the stream slightly slower.
* To avoid this performance impact, but still keep the progress tracker testable we introduce our own clock interface.
*/
trait Clock {
trait MonotonicClock {
def nanoTime(): Long
}

object Clock {
object MonotonicClock {
/**
* Simple clock implementation based on System.nanoTime()
*/
def system(): Clock = new Clock {
def system(): MonotonicClock = new MonotonicClock {
def nanoTime(): Long = System.nanoTime()
}
}
Expand Down
4 changes: 2 additions & 2 deletions fetch-file/src/main/scala/vdx/fetchfile/Progress.scala
Expand Up @@ -9,7 +9,7 @@ import java.util.Locale
object Progress {
def noop[F[_]]: Int => Pipe[F, Byte, Unit] = _ => _.map(_ => ())

def consoleProgress[F[_]: Sync](implicit clock: Clock): Int => Pipe[F, Byte, Unit] =
def consoleProgress[F[_]: Sync](implicit clock: MonotonicClock): Int => Pipe[F, Byte, Unit] =
custom[F] { (downloadedBytes, contentLength, elapsedTime, downloadSpeed) =>
println(
s"\u001b[1A\u001b[100D\u001b[0KDownloaded ${bytesToString(downloadedBytes)} of " +
Expand All @@ -22,7 +22,7 @@ object Progress {
def custom[F[_]: Sync](
f: (Long, Int, Long, Long) => Unit,
chunkLimit: Option[Int] = None
)(implicit clock: Clock): Int => Pipe[F, Byte, Unit] =
)(implicit clock: MonotonicClock): Int => Pipe[F, Byte, Unit] =
contentLength => { s =>
Stream.eval(Sync[F].delay(clock.nanoTime()))
.flatMap { startTime =>
Expand Down
2 changes: 1 addition & 1 deletion fetch-file/src/test/scala/vdx/fetchfile/ProgressSpec.scala
Expand Up @@ -20,7 +20,7 @@ class ProgressSpec extends AnyFlatSpec with Checkers {

implicit val cs: ContextShift[IO] = IO.contextShift(ExecutionContext.global)
var elapsedTime: Long = 0
implicit val clock: Clock = new Clock {
implicit val clock: MonotonicClock = new MonotonicClock {
def nanoTime(): Long = {
elapsedTime += 1000000
elapsedTime
Expand Down

0 comments on commit b87c4e0

Please sign in to comment.