Skip to content

Commit

Permalink
Fix syncache test
Browse files Browse the repository at this point in the history
  • Loading branch information
WandererXII committed Jan 20, 2025
1 parent b095ddb commit b9a8245
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
4 changes: 3 additions & 1 deletion modules/memo/src/main/Syncache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ final private[memo] class Syncache[K, V](
case _ =>
incMiss()
strategy match {
case NeverWait => default(k)
case NeverWait => default(k)
case AlwaysWait(duration) => waitForResult(k, future, duration)
case WaitAfterUptime(duration, uptime) =>
if (Uptime startedSinceSeconds uptime) waitForResult(k, future, duration)
else default(k)
Expand Down Expand Up @@ -106,6 +107,7 @@ object Syncache {

sealed trait Strategy
case object NeverWait extends Strategy
case class AlwaysWait(duration: FiniteDuration) extends Strategy
case class WaitAfterUptime(duration: FiniteDuration, uptimeSeconds: Int = 20) extends Strategy

sealed trait ExpireAfter
Expand Down
13 changes: 7 additions & 6 deletions modules/memo/src/test/SyncacheTest.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package lila.memo

import scala.concurrent.Await
import scala.concurrent.Future
import scala.concurrent.duration._

Expand All @@ -10,7 +11,7 @@ import org.scalatest.BeforeAndAfterAll
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike

class MySpec()
class SyncacheTest()
extends TestKit(ActorSystem())
with ImplicitSender
with AnyWordSpecLike
Expand All @@ -23,6 +24,8 @@ class MySpec()

implicit def ec = system.dispatcher

lila.mon.start(false)

"syncache" must {

"be thread safe" in {
Expand All @@ -37,13 +40,11 @@ class MySpec()
},
default = s => s"default $s",
strategy = Syncache.AlwaysWait(1 second),
expireAfter = Syncache.ExpireAfterWrite(10 seconds),
logger = lila.log("syncache"),
resultTimeout = 5 seconds,
expireAfter = Syncache.ExpireAfterWrite(20 seconds),
)
val threads = 20
val keys = 50
(1 to threads) foreach { _ =>
val fs = (1 to threads).map { _ =>
Future {
(1 to 5) foreach { _ =>
(1 to keys) foreach { i =>
Expand All @@ -52,7 +53,7 @@ class MySpec()
}
}
}
Thread sleep 500
Await.result(Future.sequence(fs), 20.seconds)
computeCount should be(keys)
}

Expand Down

0 comments on commit b9a8245

Please sign in to comment.