Skip to content

Commit

Permalink
Use primitive data type in Executor
Browse files Browse the repository at this point in the history
Executor is our core logic, so, We want it to have less dependency
to outer layers (in this case, json api) as much as possible
  • Loading branch information
lenguyenthanh committed Nov 21, 2023
1 parent 1f3964b commit 5a524c1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
22 changes: 11 additions & 11 deletions app/src/main/scala/Executor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ trait Executor:
// get a move from the queue return Work
def acquire(accquire: ClientKey): IO[Option[Work.RequestWithId]]
// get Work from Map => send to lila
def move(workId: WorkId, result: Fishnet.PostMove): IO[Unit]
def move(workId: WorkId, fishnetKey: ClientKey, move: BestMove): IO[Unit]
// add work to queue
def add(work: Lila.Request): IO[Unit]
def clean(before: Instant): IO[Unit]
Expand Down Expand Up @@ -56,24 +56,24 @@ object Executor:
(coll + (move.id -> move)) -> move.toRequestWithId.some
.getOrElse(coll -> none)

def move(workId: WorkId, data: Fishnet.PostMove): IO[Unit] =
def move(workId: WorkId, apikey: ClientKey, move: BestMove): IO[Unit] =
ref.flatModify: coll =>
coll get workId match
case None =>
coll -> notFound(workId, data.fishnet.apikey)
case Some(move) if move.isAcquiredBy(data.fishnet.apikey) =>
data.move.bestmove.uci match
coll -> notFound(workId, apikey)
case Some(work) if work.isAcquiredBy(apikey) =>
move.uci match
case Some(uci) =>
coll - move.id -> (success(move) >> client.send(Lila.Move(
move.request.id,
move.request.moves,
coll - work.id -> (success(work) >> client.send(Lila.Move(
work.request.id,
work.request.moves,
uci,
)))
case _ =>
updateOrGiveUp(coll, move.invalid) ->
failure(move, data.fishnet.apikey, new Exception("Missing move"))
updateOrGiveUp(coll, work.invalid) ->
failure(work, apikey, new Exception("Missing move"))
case Some(move) =>
coll -> notAcquired(move, data.fishnet.apikey)
coll -> notAcquired(move, apikey)

def clean(since: Instant): IO[Unit] =
ref.update: coll =>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/scala/http/FishnetRoutes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class FishnetRoutes(executor: Executor) extends Http4sDsl[IO]:
case req @ POST -> Root / "move" / WorkIdVar(id) =>
req
.decode[Fishnet.PostMove]: move =>
executor.move(id, move)
executor.move(id, move.fishnet.apikey, move.move.bestmove)
>> executor.acquire(move.fishnet.apikey)
.map(_.map(_.toResponse))
.flatMap(_.fold(NoContent())(Ok(_)))
Expand Down
18 changes: 9 additions & 9 deletions app/src/test/scala/ExecutorTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ object ExecutorTest extends SimpleIOSuite:

val key = ClientKey("key")

val validMove = Fishnet.PostMove(Fishnet.Fishnet("v1", key), Fishnet.Move(BestMove("e2e4")))
val invalidMove = Fishnet.PostMove(Fishnet.Fishnet("v1", key), Fishnet.Move(BestMove("2e4")))
val validMove = BestMove("e2e4")
val invalidMove = BestMove("2e4")

test("acquire when there is no work should return none"):
for
Expand Down Expand Up @@ -50,7 +50,7 @@ object ExecutorTest extends SimpleIOSuite:
executor <- Executor.instance(client)
_ <- executor.add(request)
acquired <- executor.acquire(key)
_ <- executor.move(acquired.get.id, validMove)
_ <- executor.move(acquired.get.id, key, validMove)
move <- ref.get.map(_.head)
yield assert(move == Lila.Move(request.id, request.moves, chess.format.Uci.Move("e2e4").get))

Expand All @@ -62,7 +62,7 @@ object ExecutorTest extends SimpleIOSuite:
_ <- executor.add(request)
acquired <- executor.acquire(key)
_ <- executor.clean(Instant.now.plusSeconds(37))
_ <- executor.move(acquired.get.id, validMove)
_ <- executor.move(acquired.get.id, key, validMove)
moves <- ref.get
yield assert(moves.isEmpty)

Expand All @@ -75,7 +75,7 @@ object ExecutorTest extends SimpleIOSuite:
_ <- executor.acquire(key)
_ <- executor.clean(Instant.now.plusSeconds(37))
acquired <- executor.acquire(key)
_ <- executor.move(acquired.get.id, validMove)
_ <- executor.move(acquired.get.id, key, validMove)
move <- ref.get.map(_.head)
yield assert(move == Lila.Move(request.id, request.moves, chess.format.Uci.Move("e2e4").get))

Expand All @@ -86,7 +86,7 @@ object ExecutorTest extends SimpleIOSuite:
executor <- Executor.instance(client)
_ <- executor.add(request)
acquired <- executor.acquire(key)
_ <- executor.move(acquired.get.id, invalidMove)
_ <- executor.move(acquired.get.id, key, invalidMove)
moves <- ref.get
yield assert(moves.isEmpty)

Expand All @@ -98,7 +98,7 @@ object ExecutorTest extends SimpleIOSuite:
_ <- executor.add(request)
acquired <- executor.acquire(key)
workId = acquired.get.id
_ <- executor.move(workId, invalidMove)
_ <- executor.move(workId, key, invalidMove)
acquiredOption <- executor.acquire(key)
acquired = acquiredOption.get
yield assert(acquired == Work.RequestWithId(workId, request))
Expand All @@ -109,7 +109,7 @@ object ExecutorTest extends SimpleIOSuite:
client = createLilaClient(ref)
executor <- Executor.instance(client)
_ <- executor.add(request)
_ <- (executor.acquire(key).flatMap(x => executor.move(x.get.id, invalidMove))).replicateA_(2)
_ <- (executor.acquire(key).flatMap(x => executor.move(x.get.id, key, invalidMove))).replicateA_(2)
acquired <- executor.acquire(key)
yield assert(acquired.isDefined)

Expand All @@ -119,7 +119,7 @@ object ExecutorTest extends SimpleIOSuite:
client = createLilaClient(ref)
executor <- Executor.instance(client)
_ <- executor.add(request)
_ <- (executor.acquire(key).flatMap(x => executor.move(x.get.id, invalidMove))).replicateA_(3)
_ <- (executor.acquire(key).flatMap(x => executor.move(x.get.id, key, invalidMove))).replicateA_(3)
acquired <- executor.acquire(key)
yield assert(acquired.isEmpty)

Expand Down
2 changes: 1 addition & 1 deletion app/src/test/scala/http/FishnetRoutesTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ object FishnetRoutesTest extends SimpleIOSuite:
def createExecutor(): Executor =
new Executor:
def acquire(key: ClientKey) = IO.pure(requestWithId.some)
def move(id: WorkId, move: Fishnet.PostMove): IO[Unit] =
def move(id: WorkId, key: ClientKey, move: BestMove): IO[Unit] =
if id == requestWithId.id then IO.unit
else IO.raiseError(new Exception("invalid work id"))
def add(request: Lila.Request): IO[Unit] = IO.unit
Expand Down

0 comments on commit 5a524c1

Please sign in to comment.