Skip to content

Commit

Permalink
Add test for cleaning job
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Nov 25, 2023
1 parent 890e3ed commit 63a3823
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 18 deletions.
34 changes: 34 additions & 0 deletions app/src/test/scala/CleaningJobTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package lila.fishnet

import cats.effect.IO
import cats.effect.kernel.{ Ref, Resource }
import cats.effect.testkit.TestControl
import java.time.Instant
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.noop.NoOpLogger
import weaver.*

import scala.concurrent.duration.*

object CleaningJobTest extends SimpleIOSuite:

given Logger[IO] = NoOpLogger[IO]

val times = (60 - 5) / 3 + 1
test(s"cleaning run $times times in 1 minute"):
val res = for
ref <- Resource.eval(Ref.of[IO, Int](0))
executor = createExcutor(ref)
_ <- WorkCleaningJob(executor).run().background
_ <- Resource.eval(IO.sleep(1.minute))
count <- Resource.eval(ref.get)
yield count
val program = res.use(count => IO(expect.same(count, times)))
TestControl.executeEmbed(program)

def createExcutor(ref: Ref[IO, Int]): Executor =
new Executor:
def acquire(accquire: ClientKey) = IO.none
def move(workId: WorkId, fishnetKey: ClientKey, move: BestMove) = IO.unit
def add(work: Lila.Request) = IO.unit
def clean(before: Instant) = ref.update(_ + 1)
10 changes: 2 additions & 8 deletions app/src/test/scala/ExecutorTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import cats.effect.IO
import cats.effect.kernel.Ref
import java.time.Instant

import Helper.*

object ExecutorTest extends SimpleIOSuite:

val request: Lila.Request = Lila.Request(
Expand Down Expand Up @@ -145,11 +147,3 @@ object ExecutorTest extends SimpleIOSuite:
new LilaClient:
def send(move: Lila.Move): IO[Unit] =
ref.update(_ :+ move)

def noopMonitor: Monitor =
new Monitor:
def success(work: Work.Move): IO[Unit] = IO.unit
def failure(work: Work.Move, clientKey: ClientKey, e: Exception): IO[Unit] = IO.unit
def notFound(id: WorkId, clientKey: ClientKey): IO[Unit] = IO.unit
def notAcquired(work: Work.Move, clientKey: ClientKey): IO[Unit] = IO.unit
def updateSize(map: Map[WorkId, Work.Move]): IO[Unit] = IO.unit
17 changes: 17 additions & 0 deletions app/src/test/scala/Helper.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package lila.fishnet

import cats.effect.IO

object Helper:

val noopMonitor: Monitor =
new Monitor:
def success(work: Work.Move): IO[Unit] = IO.unit
def failure(work: Work.Move, clientKey: ClientKey, e: Exception): IO[Unit] = IO.unit
def notFound(id: WorkId, clientKey: ClientKey): IO[Unit] = IO.unit
def notAcquired(work: Work.Move, clientKey: ClientKey): IO[Unit] = IO.unit
def updateSize(map: Map[WorkId, Work.Move]): IO[Unit] = IO.unit

val noopLilaClient: LilaClient =
new LilaClient:
def send(move: Lila.Move): IO[Unit] = IO.unit
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ lazy val app = project
weaverScalaCheck,
testContainers,
log4CatsNoop,
http4sClient
http4sClient,
catsEffectTestKit,
)
)
.enablePlugins(JavaAppPackaging)
Expand Down
19 changes: 10 additions & 9 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ object Dependencies {
val kamonHttp4s = "2.6.1"
val chess = "15.6.11"
val munit = "1.0.0-M8"
val catsEffect = "3.5.2"
}

def http4s(artifact: String) = "org.http4s" %% s"http4s-$artifact" % V.http4s
Expand All @@ -20,7 +21,7 @@ object Dependencies {
val chess = "org.lichess" %% "scalachess" % V.chess

val catsCore = "org.typelevel" %% "cats-core" % "2.10.0"
val catsEffect = "org.typelevel" %% "cats-effect" % "3.5.0"
val catsEffect = "org.typelevel" %% "cats-effect" % V.catsEffect

val circeCore = circe("core")
val circeLiteral = circe("literal") % Test
Expand All @@ -43,12 +44,12 @@ object Dependencies {

val redis = "io.chrisdavenport" %% "rediculous" % "0.5.1"

val chessTestKit = "org.lichess" %% "scalachess-test-kit" % V.chess % Test
val log4CatsNoop = "org.typelevel" %% "log4cats-noop" % "2.6.0" % Test
val munit = "org.scalameta" %% "munit" % V.munit % Test
val munitScalacheck = "org.scalameta" %% "munit-scalacheck" % V.munit % Test
val testContainers = "com.dimafeng" %% "testcontainers-scala-core" % "0.41.0" % Test
val weaver = "com.disneystreaming" %% "weaver-cats" % "0.8.3" % Test
val weaverScalaCheck = "com.disneystreaming" %% "weaver-scalacheck" % "0.8.3" % Test

val chessTestKit = "org.lichess" %% "scalachess-test-kit" % V.chess % Test
val log4CatsNoop = "org.typelevel" %% "log4cats-noop" % "2.6.0" % Test
val munit = "org.scalameta" %% "munit" % V.munit % Test
val munitScalacheck = "org.scalameta" %% "munit-scalacheck" % V.munit % Test
val testContainers = "com.dimafeng" %% "testcontainers-scala-core" % "0.41.0" % Test
val weaver = "com.disneystreaming" %% "weaver-cats" % "0.8.3" % Test
val weaverScalaCheck = "com.disneystreaming" %% "weaver-scalacheck" % "0.8.3" % Test
val catsEffectTestKit = "org.typelevel" %% "cats-effect-testkit" % V.catsEffect % Test
}

0 comments on commit 63a3823

Please sign in to comment.