2019-12-08 02:14:42 +01:00
|
|
|
/*
|
|
|
|
|
* sbt
|
2023-06-20 13:42:07 +02:00
|
|
|
* Copyright 2023, Scala center
|
|
|
|
|
* Copyright 2011 - 2022, Lightbend, Inc.
|
2019-12-08 02:14:42 +01:00
|
|
|
* Copyright 2008 - 2010, Mark Harrah
|
|
|
|
|
* Licensed under Apache License 2.0 (see LICENSE)
|
|
|
|
|
*/
|
|
|
|
|
|
2017-04-17 09:20:41 +02:00
|
|
|
package sbt.util
|
2016-06-27 15:27:42 +02:00
|
|
|
|
|
|
|
|
import sbt.io.IO
|
2025-01-02 03:16:32 +01:00
|
|
|
import sbt.io.syntax.*
|
2026-01-11 13:19:22 +01:00
|
|
|
import verify.BasicTestSuite
|
2016-06-27 15:27:42 +02:00
|
|
|
|
2024-11-17 04:39:08 +01:00
|
|
|
import CacheImplicits.given
|
2016-06-27 15:27:42 +02:00
|
|
|
|
2026-01-11 13:19:22 +01:00
|
|
|
object CacheSpec extends BasicTestSuite:
|
2016-06-27 15:27:42 +02:00
|
|
|
|
2026-01-11 13:19:22 +01:00
|
|
|
test("A cache should NOT throw an exception if read without being written previously"):
|
2025-02-22 00:45:18 +01:00
|
|
|
testCache[String, Int] { (cache, store) =>
|
2026-01-11 13:19:22 +01:00
|
|
|
cache(store)("missing") match
|
|
|
|
|
case Hit(_) => assert(false, "Expected Miss but got Hit")
|
2021-12-20 08:00:30 +01:00
|
|
|
case Miss(_) => ()
|
2016-06-27 15:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
2026-01-11 13:19:22 +01:00
|
|
|
test("A cache should write a very simple value"):
|
2025-02-22 00:45:18 +01:00
|
|
|
testCache[String, Int] { (cache, store) =>
|
2026-01-11 13:19:22 +01:00
|
|
|
cache(store)("missing") match
|
|
|
|
|
case Hit(_) => assert(false, "Expected Miss but got Hit")
|
2021-12-20 08:00:30 +01:00
|
|
|
case Miss(update) => update(5)
|
2016-06-27 15:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
2026-01-11 13:19:22 +01:00
|
|
|
test("A cache should be updatable"):
|
2025-02-22 00:45:18 +01:00
|
|
|
testCache[String, Int] { (cache, store) =>
|
2021-12-20 08:00:30 +01:00
|
|
|
val value = 5
|
2026-01-11 13:19:22 +01:00
|
|
|
cache(store)("someKey") match
|
|
|
|
|
case Hit(_) => assert(false, "Expected Miss but got Hit")
|
2021-12-20 08:00:30 +01:00
|
|
|
case Miss(update) => update(value)
|
2016-06-27 15:27:42 +02:00
|
|
|
|
2026-01-11 13:19:22 +01:00
|
|
|
cache(store)("someKey") match
|
|
|
|
|
case Hit(read) => assert(read == value)
|
|
|
|
|
case Miss(_) => assert(false, "Expected Hit but got Miss")
|
2016-06-27 15:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
2026-01-11 13:19:22 +01:00
|
|
|
test("A cache should return the value that has been previously written"):
|
2025-02-22 00:45:18 +01:00
|
|
|
testCache[String, Int] { (cache, store) =>
|
2021-12-20 08:00:30 +01:00
|
|
|
val key = "someKey"
|
|
|
|
|
val value = 5
|
2026-01-11 13:19:22 +01:00
|
|
|
cache(store)(key) match
|
|
|
|
|
case Hit(_) => assert(false, "Expected Miss but got Hit")
|
2021-12-20 08:00:30 +01:00
|
|
|
case Miss(update) => update(value)
|
2016-06-27 15:27:42 +02:00
|
|
|
|
2026-01-11 13:19:22 +01:00
|
|
|
cache(store)(key) match
|
|
|
|
|
case Hit(read) => assert(read == value)
|
|
|
|
|
case Miss(_) => assert(false, "Expected Hit but got Miss")
|
2016-06-27 15:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
2024-11-17 01:26:16 +01:00
|
|
|
private def testCache[K, V](f: (Cache[K, V], CacheStore) => Unit)(using
|
2021-12-20 08:00:30 +01:00
|
|
|
cache: Cache[K, V]
|
2017-08-10 12:24:29 +02:00
|
|
|
): Unit =
|
2016-06-27 15:27:42 +02:00
|
|
|
IO.withTemporaryDirectory { tmp =>
|
2020-04-26 17:07:10 +02:00
|
|
|
val store = new FileBasedStore(tmp / "cache-store")
|
2016-06-27 15:27:42 +02:00
|
|
|
f(cache, store)
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-11 13:19:22 +01:00
|
|
|
end CacheSpec
|