Skip to content

Commit

Permalink
spike: compiling on CE3 but tests mangled
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith Pinson committed May 18, 2021
1 parent ab4ba53 commit 1036248
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.chrisdavenport.mules

import cats.effect.laws.util.TestContext
import cats.effect._
import cats.syntax.all._
import munit._
Expand All @@ -11,24 +10,17 @@ class AutoMemoryCacheSpec extends CatsEffectSuite {
val cacheKeyExpiration = TimeSpec.unsafeFromDuration(12.hours)
val checkExpirationsEvery = TimeSpec.unsafeFromDuration(10.millis)

private val ctx = TestContext()

implicit override def munitContextShift: ContextShift[IO] =
IO.contextShift(ctx)
implicit override def munitTimer: Timer[IO] =
ctx.timer

test("Auto MemoryCache.ofSingleImmutableMap should expire keys") {
Resource.eval(MemoryCache.ofSingleImmutableMap[IO, Int, String](cacheKeyExpiration.some))
.flatMap(cache => MemoryCache.liftToAuto(cache, checkExpirationsEvery).as(cache))
.use(cache =>
for {
_ <- cache.insert(1, "foo")
_ <- IO(ctx.tick(5.hours))
//_ <- IO(ctx.tick(5.hours))
_ <- cache.insert(2, "bar")
a1 <- cache.lookupNoUpdate(1)
b1 <- cache.lookupNoUpdate(2)
_ <- IO(ctx.tick(7.hours + 1.second)) // expiration time reached
//_ <- IO(ctx.tick(7.hours + 1.second)) // expiration time reached
a2 <- cache.lookupNoUpdate(1)
b2 <- cache.lookupNoUpdate(2)
} yield {
Expand All @@ -46,12 +38,12 @@ class AutoMemoryCacheSpec extends CatsEffectSuite {
.use(cache =>
for {
_ <- cache.insert(1, "foo")
_ <- IO(ctx.tick(5.hours))
//_ <- IO(ctx.tick(5.hours))
a1 <- cache.lookupNoUpdate(1)
_ <- cache.insert(1, "bar")
_ <- IO(ctx.tick(7.hours + 1.second)) // expiration time reached for first timestamp
//_ <- IO(ctx.tick(7.hours + 1.second)) // expiration time reached for first timestamp
a2 <- cache.lookupNoUpdate(1)
_ <- IO(ctx.tick(5.hours)) // expiration time reached for last timestamp
//_ <- IO(ctx.tick(5.hours)) // expiration time reached for last timestamp
a3 <- cache.lookupNoUpdate(1)
} yield {
assert(a1.contains("foo"))
Expand All @@ -66,11 +58,11 @@ class AutoMemoryCacheSpec extends CatsEffectSuite {
.use(cache =>
for {
_ <- cache.insert(1, "foo")
_ <- IO(ctx.tick(5.hours))
//_ <- IO(ctx.tick(5.hours))
_ <- cache.insert(2, "bar")
a1 <- cache.lookupNoUpdate(1)
b1 <- cache.lookupNoUpdate(2)
_ <- IO(ctx.tick(7.hours + 1.second)) // expiration time reached
//_ <- IO(ctx.tick(7.hours + 1.second)) // expiration time reached
a2 <- cache.lookupNoUpdate(1)
b2 <- cache.lookupNoUpdate(2)
} yield {
Expand All @@ -88,12 +80,12 @@ class AutoMemoryCacheSpec extends CatsEffectSuite {
.use(cache =>
for {
_ <- cache.insert(1, "foo")
_ <- IO(ctx.tick(5.hours))
//_ <- IO(ctx.tick(5.hours))
a1 <- cache.lookupNoUpdate(1)
_ <- cache.insert(1, "bar")
_ <- IO(ctx.tick(7.hours + 1.second)) // expiration time reached for first timestamp
//_ <- IO(ctx.tick(7.hours + 1.second)) // expiration time reached for first timestamp
a2 <- cache.lookupNoUpdate(1)
_ <- IO(ctx.tick(5.hours)) // expiration time reached for last timestamp
//_ <- IO(ctx.tick(5.hours)) // expiration time reached for last timestamp
a3 <- cache.lookupNoUpdate(1)
} yield {
assert(a1.contains("foo"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
package io.chrisdavenport.mules

import cats.effect.laws.util.TestContext
import scala.concurrent.duration._
import cats.effect._
import cats.effect.concurrent._
import munit._

class MemoryCacheSpec extends CatsEffectSuite {
test("MemoryCache.ofSingleImmutableMap should get a value in a quicker period than the timeout") {
val ctx = TestContext()
implicit val testTimer: Timer[IO] = ctx.timer[IO]
for {
cache <- MemoryCache.ofSingleImmutableMap[IO, String, Int](Some(TimeSpec.unsafeFromDuration(1.second)))(Sync[IO], testTimer.clock)
cache <- MemoryCache.ofSingleImmutableMap[IO, String, Int](Some(TimeSpec.unsafeFromDuration(1.second)))(Async[IO])
_ <- cache.insert("Foo", 1)
_ = ctx.tick(1.nano)
//_ = ctx.tick(1.nano)
value <- cache.lookup("Foo")
} yield {
assertEquals(value, Some(1))
}
}

test("MemoryCache.ofSingleImmutableMap should remove a value after delete") {
val ctx = TestContext()
implicit val testTimer: Timer[IO] = ctx.timer[IO]
for {
cache <- MemoryCache.ofSingleImmutableMap[IO, String, Int](None)(Sync[IO], testTimer.clock)
cache <- MemoryCache.ofSingleImmutableMap[IO, String, Int](None)(Async[IO])
_ <- cache.insert("Foo", 1)
_ <- cache.delete("Foo")
value <- cache.lookup("Foo")
Expand All @@ -34,12 +28,10 @@ class MemoryCacheSpec extends CatsEffectSuite {
}

test("MemoryCache.ofSingleImmutableMap should remove a value in mass delete") {
val ctx = TestContext()
implicit val testTimer: Timer[IO] = ctx.timer[IO]
for {
cache <- MemoryCache.ofSingleImmutableMap[IO, String, Int](Some(TimeSpec.unsafeFromDuration(1.second)))(Sync[IO], testTimer.clock)
cache <- MemoryCache.ofSingleImmutableMap[IO, String, Int](Some(TimeSpec.unsafeFromDuration(1.second)))(Async[IO])
_ <- cache.insert("Foo", 1)
_ <- Sync[IO].delay(ctx.tick(2.seconds))
//_ <- Sync[IO].delay(ctx.tick(2.seconds))
_ <- cache.purgeExpired
value <- cache.lookupNoUpdate("Foo")
} yield {
Expand All @@ -48,27 +40,23 @@ class MemoryCacheSpec extends CatsEffectSuite {
}

test("MemoryCache.ofSingleImmutableMap should lookup after interval fails to get a value") {
val ctx = TestContext()
implicit val testTimer: Timer[IO] = ctx.timer[IO]
for {
cache <- MemoryCache.ofSingleImmutableMap[IO, String, Int](Some(TimeSpec.unsafeFromDuration(1.second)))(Sync[IO], testTimer.clock)
cache <- MemoryCache.ofSingleImmutableMap[IO, String, Int](Some(TimeSpec.unsafeFromDuration(1.second)))(Async[IO])
_ <- cache.insert("Foo", 1)
_ <- Sync[IO].delay(ctx.tick(2.seconds))
//_ <- Sync[IO].delay(ctx.tick(2.seconds))
value <- cache.lookup("Foo")
} yield {
assertEquals(value, None)
}
}

test("MemoryCache.ofSingleImmutableMap should not Remove an item on lookup No Delete") {
val ctx = TestContext()
implicit val testTimer: Timer[IO] = ctx.timer[IO]
for {
checkWasTouched <- Ref[IO].of(false)
iCache <- MemoryCache.ofSingleImmutableMap[IO, String, Int](Some(TimeSpec.unsafeFromDuration(1.second)))(Sync[IO], testTimer.clock)
iCache <- MemoryCache.ofSingleImmutableMap[IO, String, Int](Some(TimeSpec.unsafeFromDuration(1.second)))(Async[IO])
cache = iCache.setOnDelete(_ => checkWasTouched.set(true))
_ <- cache.insert("Foo", 1)
_ <- Sync[IO].delay(ctx.tick(2.seconds))
//_ <- Sync[IO].delay(ctx.tick(2.seconds))
value <- cache.lookupNoUpdate("Foo")
wasTouched <- checkWasTouched.get
} yield {
Expand All @@ -78,23 +66,19 @@ class MemoryCacheSpec extends CatsEffectSuite {
}

test("MemoryCache.ofShardedImmutableMap should get a value in a quicker period than the timeout") {
val ctx = TestContext()
implicit val testTimer: Timer[IO] = ctx.timer[IO]
for {
cache <- MemoryCache.ofShardedImmutableMap[IO, String, Int](10, Some(TimeSpec.unsafeFromDuration(1.second)))(Sync[IO], testTimer.clock)
cache <- MemoryCache.ofShardedImmutableMap[IO, String, Int](10, Some(TimeSpec.unsafeFromDuration(1.second)))(Async[IO])
_ <- cache.insert("Foo", 1)
_ = ctx.tick(1.nano)
//_ = ctx.tick(1.nano)
value <- cache.lookup("Foo")
} yield {
assertEquals(value, Some(1))
}
}

test("MemoryCache.ofShardedImmutableMap should remove a value after delete") {
val ctx = TestContext()
implicit val testTimer: Timer[IO] = ctx.timer[IO]
for {
cache <- MemoryCache.ofShardedImmutableMap[IO, String, Int](10, Some(TimeSpec.unsafeFromDuration(1.second)))(Sync[IO], testTimer.clock)
cache <- MemoryCache.ofShardedImmutableMap[IO, String, Int](10, Some(TimeSpec.unsafeFromDuration(1.second)))(Async[IO])
_ <- cache.insert("Foo", 1)
_ <- cache.delete("Foo")
value <- cache.lookup("Foo")
Expand All @@ -104,12 +88,10 @@ class MemoryCacheSpec extends CatsEffectSuite {
}

test("MemoryCache.ofShardedImmutableMap should remove a value in mass delete") {
val ctx = TestContext()
implicit val testTimer: Timer[IO] = ctx.timer[IO]
for {
cache <- MemoryCache.ofShardedImmutableMap[IO, String, Int](10, Some(TimeSpec.unsafeFromDuration(1.second)))(Sync[IO], testTimer.clock)
cache <- MemoryCache.ofShardedImmutableMap[IO, String, Int](10, Some(TimeSpec.unsafeFromDuration(1.second)))(Async[IO])
_ <- cache.insert("Foo", 1)
_ <- Sync[IO].delay(ctx.tick(2.seconds))
//_ <- Sync[IO].delay(ctx.tick(2.seconds))
_ <- cache.purgeExpired
value <- cache.lookupNoUpdate("Foo")
} yield {
Expand All @@ -118,27 +100,23 @@ class MemoryCacheSpec extends CatsEffectSuite {
}

test("MemoryCache.ofShardedImmutableMap should lookup after interval fails to get a value") {
val ctx = TestContext()
implicit val testTimer: Timer[IO] = ctx.timer[IO]
for {
cache <- MemoryCache.ofShardedImmutableMap[IO, String, Int](10, Some(TimeSpec.unsafeFromDuration(1.second)))(Sync[IO], testTimer.clock)
cache <- MemoryCache.ofShardedImmutableMap[IO, String, Int](10, Some(TimeSpec.unsafeFromDuration(1.second)))(Async[IO])
_ <- cache.insert("Foo", 1)
_ <- Sync[IO].delay(ctx.tick(2.seconds))
//_ <- Sync[IO].delay(ctx.tick(2.seconds))
value <- cache.lookup("Foo")
} yield {
assertEquals(value, None)
}
}

test("MemoryCache.ofShardedImmutableMap should not Remove an item on lookup No Delete") {
val ctx = TestContext()
implicit val testTimer: Timer[IO] = ctx.timer[IO]
for {
checkWasTouched <- Ref[IO].of(false)
iCache <- MemoryCache.ofShardedImmutableMap[IO, String, Int](10, Some(TimeSpec.unsafeFromDuration(1.second)))(Sync[IO], testTimer.clock)
iCache <- MemoryCache.ofShardedImmutableMap[IO, String, Int](10, Some(TimeSpec.unsafeFromDuration(1.second)))(Async[IO])
cache = iCache.setOnDelete(_ => checkWasTouched.set(true))
_ <- cache.insert("Foo", 1)
_ <- Sync[IO].delay(ctx.tick(2.seconds))
//_ <- Sync[IO].delay(ctx.tick(2.seconds))
value <- cache.lookupNoUpdate("Foo")
wasTouched <- checkWasTouched.get
} yield {
Expand All @@ -148,23 +126,19 @@ class MemoryCacheSpec extends CatsEffectSuite {
}

test("MemoryCache.ofConcurrentHashMap should get a value in a quicker period than the timeout") {
val ctx = TestContext()
implicit val testTimer: Timer[IO] = ctx.timer[IO]
for {
cache <- MemoryCache.ofConcurrentHashMap[IO, String, Int](Some(TimeSpec.unsafeFromDuration(1.second)))(Sync[IO], testTimer.clock)
cache <- MemoryCache.ofConcurrentHashMap[IO, String, Int](Some(TimeSpec.unsafeFromDuration(1.second)))(Async[IO])
_ <- cache.insert("Foo", 1)
_ = ctx.tick(1.nano)
//_ = ctx.tick(1.nano)
value <- cache.lookup("Foo")
} yield {
assertEquals(value, Some(1))
}
}

test("MemoryCache.ofConcurrentHashMap should remove a value after delete") {
val ctx = TestContext()
implicit val testTimer: Timer[IO] = ctx.timer[IO]
for {
cache <- MemoryCache.ofConcurrentHashMap[IO, String, Int](None)(Sync[IO], testTimer.clock)
cache <- MemoryCache.ofConcurrentHashMap[IO, String, Int](None)(Async[IO])
_ <- cache.insert("Foo", 1)
_ <- cache.delete("Foo")
value <- cache.lookup("Foo")
Expand All @@ -174,12 +148,10 @@ class MemoryCacheSpec extends CatsEffectSuite {
}

test("MemoryCache.ofConcurrentHashMap should Remove a value in mass delete") {
val ctx = TestContext()
implicit val testTimer: Timer[IO] = ctx.timer[IO]
for {
cache <- MemoryCache.ofConcurrentHashMap[IO, String, Int](Some(TimeSpec.unsafeFromDuration(1.second)))(Sync[IO], testTimer.clock)
cache <- MemoryCache.ofConcurrentHashMap[IO, String, Int](Some(TimeSpec.unsafeFromDuration(1.second)))(Async[IO])
_ <- cache.insert("Foo", 1)
_ <- Sync[IO].delay(ctx.tick(2.seconds))
//_ <- Sync[IO].delay(ctx.tick(2.seconds))
_ <- cache.purgeExpired
value <- cache.lookupNoUpdate("Foo")
} yield {
Expand All @@ -188,27 +160,23 @@ class MemoryCacheSpec extends CatsEffectSuite {
}

test("MemoryCache.ofConcurrentHashMap should Lookup after interval fails to get a value") {
val ctx = TestContext()
implicit val testTimer: Timer[IO] = ctx.timer[IO]
for {
cache <- MemoryCache.ofConcurrentHashMap[IO, String, Int](Some(TimeSpec.unsafeFromDuration(1.second)))(Sync[IO], testTimer.clock)
cache <- MemoryCache.ofConcurrentHashMap[IO, String, Int](Some(TimeSpec.unsafeFromDuration(1.second)))(Async[IO])
_ <- cache.insert("Foo", 1)
_ <- Sync[IO].delay(ctx.tick(2.seconds))
//_ <- Sync[IO].delay(ctx.tick(2.seconds))
value <- cache.lookup("Foo")
} yield {
assertEquals(value, None)
}
}

test("MemoryCache.ofConcurrentHashMap should Not Remove an item on lookup No Delete") {
val ctx = TestContext()
implicit val testTimer: Timer[IO] = ctx.timer[IO]
for {
checkWasTouched <- Ref[IO].of(false)
iCache <- MemoryCache.ofConcurrentHashMap[IO, String, Int](Some(TimeSpec.unsafeFromDuration(1.second)))(Sync[IO], testTimer.clock)
iCache <- MemoryCache.ofConcurrentHashMap[IO, String, Int](Some(TimeSpec.unsafeFromDuration(1.second)))(Async[IO])
cache = iCache.setOnDelete(_ => checkWasTouched.set(true))
_ <- cache.insert("Foo", 1)
_ <- Sync[IO].delay(ctx.tick(2.seconds))
//_ <- Sync[IO].delay(ctx.tick(2.seconds))
value <- cache.lookupNoUpdate("Foo")
wasTouched <- checkWasTouched.get
} yield {
Expand Down

0 comments on commit 1036248

Please sign in to comment.