-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement simple progress tracker that prints to stdout
- Loading branch information
1 parent
c221f79
commit ac2a8bb
Showing
6 changed files
with
103 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package vdx.fetchfile | ||
|
||
/** | ||
* Representation of a clock. | ||
* 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 { | ||
def nanoTime(): Long | ||
} | ||
|
||
object Clock { | ||
/** | ||
* Simple clock implementation based on System.nanoTime() | ||
*/ | ||
def system(): Clock = new Clock { | ||
def nanoTime(): Long = System.nanoTime() | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
fetch-file/src/test/scala/vdx/fetchfile/ProgressSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package vdx.fetchfile | ||
|
||
import cats.effect.IO | ||
import fs2.Stream | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatestplus.scalacheck.Checkers | ||
|
||
import cats.effect.ContextShift | ||
import org.scalacheck.{Gen, Prop} | ||
import scala.concurrent.ExecutionContext | ||
|
||
class ProgressSpec extends AnyFlatSpec with Checkers { | ||
|
||
val byte = Gen.choose(1, 255).map(_.toByte) | ||
val bytes = Gen.containerOf[List, Byte](byte) | ||
|
||
"custom" should "create a custom progress tracker that calls the given function after each chunk" in { | ||
check( | ||
Prop.forAll(bytes) { bs => | ||
|
||
implicit val cs: ContextShift[IO] = IO.contextShift(ExecutionContext.global) | ||
var elapsedTime: Long = 0 | ||
implicit val clock: Clock = new Clock { | ||
def nanoTime(): Long = { | ||
elapsedTime += 1000000 | ||
elapsedTime | ||
} | ||
|
||
} | ||
|
||
var chunksOK = true | ||
|
||
val progress = (downloadedBytes: Long, contentLength: Int, elapsedTime: Long, downloadSpeed: Long) => { | ||
chunksOK = chunksOK && | ||
contentLength == bs.length && | ||
elapsedTime == downloadedBytes && | ||
downloadSpeed == (downloadedBytes * 1000) / elapsedTime | ||
} | ||
|
||
val pipe = Progress.custom[IO](progress, Some(1)) | ||
|
||
Stream.emits[IO, Byte](bs) | ||
.observe(pipe(bs.length)) | ||
.compile | ||
.drain | ||
.unsafeRunSync() | ||
|
||
chunksOK | ||
} | ||
) | ||
} | ||
} |