Skip to content

Commit

Permalink
Add more tests & one test is failed
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Nov 16, 2023
1 parent 9b1c0bb commit 792bf4c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 12 deletions.
1 change: 1 addition & 0 deletions app/src/main/scala/Executor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ trait Executor:
def acquire(accquire: MoveDb.Acquire): IO[Option[Work.Move]]
// get Work from Map => send to lila
def move(workId: Work.Id, result: Fishnet.PostMove): IO[Unit]
// add work to queue
def add(work: Work.Move): IO[Unit]

object Executor:
Expand Down
65 changes: 59 additions & 6 deletions app/src/test/scala/ExecutorTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import monocle.syntax.all.*

object ExecutorTest extends SimpleIOSuite with Checkers:

val workId = Work.Id("id")
val work = Work.Move(
_id = Work.Id("id"),
_id = workId,
game = Work.Game(
id = "id",
initialFen = None,
Expand All @@ -27,20 +28,72 @@ object ExecutorTest extends SimpleIOSuite with Checkers:

val key = ClientKey("key")

test("add => acqurired => work"):
test("acqurired => none"):
for
executor <- createExecutor()
acquired <- executor.acquire(MoveDb.Acquire(ClientKey("key")))
yield assert(acquired.isEmpty)

test("add => acqurired => work.some"):
for
executor <- createExecutor()
_ <- executor.add(work)
acquiredOption <- executor.acquire(MoveDb.Acquire(ClientKey("key")))
acquired = acquiredOption.get
yield assert(acquired.acquired.get.clientKey == key) `and` assert(acquired.copy(acquired = None) == work)

test("add => acqurired => acquired => none"):
for
executor <- createExecutor()
_ <- executor.add(work)
_ <- executor.acquire(MoveDb.Acquire(ClientKey("key")))
acquired <- executor.acquire(MoveDb.Acquire(ClientKey("key")))
yield assert(acquired.isEmpty)

test("add => acqurired => move => done"):
for
ref <- Ref.of[IO, List[Lila.Move]](Nil)
client = createLilaClient(ref)
executor <- Executor.instance(client)
_ <- executor.add(work)
_ <- executor.acquire(MoveDb.Acquire(ClientKey("key")))
_ <- executor.move(workId, Fishnet.PostMove(Fishnet.Fishnet(key), Fishnet.MoveResult("e2e4")))
move <- ref.get.map(_.head)
yield assert(move == Lila.Move(work.game, chess.format.Uci.Move("e2e4").get))

test("add => acqurired => post invalid move => no send"):
for
ref <- Ref.of[IO, List[Lila.Move]](Nil)
client = createLilaClient(ref)
executor <- Executor.instance(client)
_ <- executor.add(work)
_ <- executor.acquire(MoveDb.Acquire(ClientKey("key")))
_ <- executor.move(workId, Fishnet.PostMove(Fishnet.Fishnet(key), Fishnet.MoveResult("ee4")))
moves <- ref.get
yield assert(moves.isEmpty)

test("add => acqurired => post invalid move => acquired => work.some"):
for
ref <- Ref.of[IO, List[Lila.Move]](Nil)
client = createLilaClient(ref)
executor <- Executor.instance(client)
_ <- executor.add(work)
_ <- executor.acquire(MoveDb.Acquire(ClientKey("key")))
_ <- executor.move(workId, Fishnet.PostMove(Fishnet.Fishnet(key), Fishnet.MoveResult("ee4")))
acquiredOption <- executor.acquire(MoveDb.Acquire(ClientKey("key")))
acquired = acquiredOption.get
yield assert(acquired.acquired.get.clientKey == key) `and` assert(acquired.tries == 1) `and` assert(
acquired.copy(acquired = None, tries = 0) == work
)

def createExecutor(): IO[Executor] =
createLilaClient.flatMap(Executor.instance)

def createLilaClient: IO[LilaClient] =
Ref.of[IO, List[Lila.Move]](Nil)
.map: ref =>
new LilaClient:
def send(move: Lila.Move): IO[Unit] =
ref.update(_ :+ move)
.map(createLilaClient)

def createLilaClient(ref: Ref[IO, List[Lila.Move]]): LilaClient =
new LilaClient:
def send(move: Lila.Move): IO[Unit] =
ref.update(_ :+ move)
11 changes: 5 additions & 6 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ object Dependencies {
val circe = "0.14.6"
val http4s = "0.23.23"
val ciris = "3.4.0"
val kamon = "2.5.11"
val kamon = "2.5.11"
}

def http4s(artifact: String) = "org.http4s" %% s"http4s-$artifact" % V.http4s
Expand All @@ -34,11 +34,9 @@ object Dependencies {

val jodaTime = "joda-time" % "joda-time" % "2.12.5"

val kamonCore = "io.kamon" %% "kamon-core" % V.kamon
val kamonInflux = "io.kamon" %% "kamon-influxdb" % V.kamon
val kamonSystemMetrics = "io.kamon" %% "kamon-system-metrics" % V.kamon

val monocleCore = "dev.optics" %% "monocle-core" % "3.2.0"
val kamonCore = "io.kamon" %% "kamon-core" % V.kamon
val kamonInflux = "io.kamon" %% "kamon-influxdb" % V.kamon
val kamonSystemMetrics = "io.kamon" %% "kamon-system-metrics" % V.kamon

val http4sDsl = http4s("dsl")
val http4sServer = http4s("ember-server")
Expand All @@ -48,6 +46,7 @@ object Dependencies {
val log4Cats = "org.typelevel" %% "log4cats-slf4j" % "2.6.0"
val logback = "ch.qos.logback" % "logback-classic" % "1.4.11"

val monocleCore = "dev.optics" %% "monocle-core" % "3.2.0" % Test
val log4CatsNoop = "org.typelevel" %% "log4cats-noop" % "2.6.0" % Test
val testContainers = "com.dimafeng" %% "testcontainers-scala-redis" % "0.41.0" % Test
val weaver = "com.disneystreaming" %% "weaver-cats" % "0.8.3" % Test
Expand Down

0 comments on commit 792bf4c

Please sign in to comment.