Skip to content

Commit

Permalink
Implement test about refreshValue
Browse files Browse the repository at this point in the history
  • Loading branch information
kajebiii committed Sep 22, 2023
1 parent 481871e commit 3df9381
Showing 1 changed file with 64 additions and 2 deletions.
66 changes: 64 additions & 2 deletions zio-cache/shared/src/test/scala/zio/cache/CacheSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,17 @@ object CacheSpec extends ZIOSpecDefault {
_ <- cache.refresh(key)
val1 <- cache.get(key).either
_ <- cache.refresh(key)
val2 <- cache.get(key).either
failure2 <- cache.refresh(key).either
failure3 <- cache.get(key).either
_ <- cache.refresh(key)
val2 <- cache.get(key).either
val3 <- cache.get(key).either
} yield assert(failure1)(isLeft(equalTo(error))) &&
assert(failure2)(isLeft(equalTo(error))) &&
assert(failure3)(isLeft(equalTo(error))) &&
assert(val1)(isRight(equalTo(4))) &&
assert(val2)(isRight(equalTo(7)))
assert(val2)(isRight(equalTo(5))) &&
assert(val3)(isRight(equalTo(7)))
},
test("should get the value if the key doesn't exist in the cache") {
check(Gen.int) { salt =>
Expand All @@ -134,6 +138,64 @@ object CacheSpec extends ZIOSpecDefault {
}
}
),
suite("`refreshValue` method")(
test("should update the cache with a new value") {
def inc(n: Int) = n * 10

def retrieve(multiplier: Ref[Int])(key: Int) =
multiplier
.updateAndGet(inc)
.map(key * _)

val seed = 1
val key = 123
for {
ref <- Ref.make(seed)
cache <- Cache.make(1, Duration.Infinity, Lookup(retrieve(ref)))
val1 <- cache.get(key)
_ <- cache.refreshValue(key)
_ <- cache.get(key)
val2 <- cache.get(key)
} yield assertTrue(val1 == inc(key)) && assertTrue(val2 == inc(val1))
},
test("should update the cache only if lookup return a value, not an error.") {

val error = new RuntimeException("Must be a multiple of 3")

def inc(n: Int) = n + 1

def retrieve(number: Ref[Int])(key: Int) =
number
.updateAndGet(inc)
.flatMap {
case n if n % 3 == 0 =>
ZIO.fail(error)
case n =>
ZIO.succeed(key * n)
}

val seed = 2
val key = 1
for {
ref <- Ref.make(seed)
cache <- Cache.make(1, Duration.Infinity, Lookup(retrieve(ref)))
failure1 <- cache.get(key).either
_ <- cache.refreshValue(key)
val1 <- cache.get(key).either
_ <- cache.refreshValue(key)
val2 <- cache.get(key).either
failure2 <- cache.refreshValue(key).either
val3 <- cache.get(key).either
_ <- cache.refreshValue(key)
val4 <- cache.get(key).either
} yield assert(failure1)(isLeft(equalTo(error))) &&
assert(failure2)(isLeft(equalTo(error))) &&
assert(val1)(isRight(equalTo(4))) &&
assert(val2)(isRight(equalTo(5))) &&
assert(val3)(isRight(equalTo(5))) &&
assert(val4)(isRight(equalTo(7)))
}
),
test("size") {
check(Gen.int) { salt =>
for {
Expand Down

0 comments on commit 3df9381

Please sign in to comment.