Fixes Tracked.inputChanged

Tracked.inputChanged stores and reads hash correctly.
Fixes #96
This commit is contained in:
Eugene Yokota 2017-07-16 18:48:28 -04:00
parent 5b85002a69
commit 24d97aa104
2 changed files with 12 additions and 8 deletions

View File

@ -96,16 +96,20 @@ object Tracked {
private final class CacheHelp[I: JsonFormat](val sc: SingletonCache[Long]) {
import CacheImplicits.implicitHashWriter
import CacheImplicits.LongJsonFormat
def save(store: CacheStore, value: I): Unit = {
store.write(value)
Hasher.hash(value) match {
case Success(keyHash) => store.write[Long](keyHash.toLong)
case Failure(e) => ()
}
}
def changed(store: CacheStore, value: I): Boolean =
Try { store.read[I] } match {
case Success(prev) =>
Try { store.read[Long] } match {
case Success(prev: Long) =>
Hasher.hash(value) match {
case Success(keyHash) => keyHash.toLong != prev
case Failure(_) => true
case Success(keyHash: Int) => keyHash.toLong != prev
case Failure(_) => true
}
case Failure(_) => true
}

View File

@ -51,10 +51,10 @@ class TrackedSpec extends UnitSpec {
"inputChanged" should "detect that the input has not changed" in {
withStore { store =>
val input0 = 0
val input0 = "foo"
val res0 =
Tracked.inputChanged[Int, Int](store) {
Tracked.inputChanged[String, String](store) {
case (true, in) =>
assert(in === input0)
in
@ -64,7 +64,7 @@ class TrackedSpec extends UnitSpec {
assert(res0 === input0)
val res1 =
Tracked.inputChanged[Int, Int](store) {
Tracked.inputChanged[String, String](store) {
case (true, in) =>
fail()
case (false, in) =>