Skip to content

Commit

Permalink
Merge pull request #284 from Masynchin/master
Browse files Browse the repository at this point in the history
Use default executor constructor where possible
  • Loading branch information
lenguyenthanh authored Mar 11, 2024
2 parents 87b7a2b + fa202e1 commit 8c7f667
Showing 1 changed file with 20 additions and 30 deletions.
50 changes: 20 additions & 30 deletions app/src/test/scala/ExecutorTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ object ExecutorTest extends SimpleIOSuite:

test("post move after acquire should send move"):
for
ref <- Ref.of[IO, List[Lila.Move]](Nil)
client = createLilaClient(ref)
executor <- ioExecutor(client)(noopMonitor, ExecutorConfig(2))
ref <- emptyMovesRef
executor <- createExecutor(ref)(ExecutorConfig(2))
_ <- executor.add(request)
acquired <- executor.acquire(key)
_ <- executor.move(acquired.get.id, key, validMove)
Expand All @@ -73,9 +72,8 @@ object ExecutorTest extends SimpleIOSuite:

test("post move after timeout should not send move"):
for
ref <- Ref.of[IO, List[Lila.Move]](Nil)
client = createLilaClient(ref)
executor <- ioExecutor(client)(noopMonitor, ExecutorConfig(2))
ref <- emptyMovesRef
executor <- createExecutor(ref)(ExecutorConfig(2))
_ <- executor.add(request)
acquired <- executor.acquire(key)
_ <- executor.clean(Instant.now.plusSeconds(37))
Expand All @@ -85,9 +83,8 @@ object ExecutorTest extends SimpleIOSuite:

test("after timeout move should be able to acquired again"):
for
ref <- Ref.of[IO, List[Lila.Move]](Nil)
client = createLilaClient(ref)
executor <- ioExecutor(client)(noopMonitor, ExecutorConfig(2))
ref <- emptyMovesRef
executor <- createExecutor(ref)(ExecutorConfig(2))
_ <- executor.add(request)
_ <- executor.acquire(key)
_ <- executor.clean(Instant.now.plusSeconds(37))
Expand All @@ -98,9 +95,8 @@ object ExecutorTest extends SimpleIOSuite:

test("post an invalid move should not send move"):
for
ref <- Ref.of[IO, List[Lila.Move]](Nil)
client = createLilaClient(ref)
executor <- ioExecutor(client)(noopMonitor, ExecutorConfig(2))
ref <- emptyMovesRef
executor <- createExecutor(ref)(ExecutorConfig(2))
_ <- executor.add(request)
acquired <- executor.acquire(key)
_ <- executor.move(acquired.get.id, key, invalidMove)
Expand All @@ -109,9 +105,7 @@ object ExecutorTest extends SimpleIOSuite:

test("after post an invalid move, acquire again should return work.some"):
for
ref <- Ref.of[IO, List[Lila.Move]](Nil)
client = createLilaClient(ref)
executor <- ioExecutor(client)(noopMonitor, ExecutorConfig(2))
executor <- createExecutor(ExecutorConfig(2))
_ <- executor.add(request)
acquired <- executor.acquire(key)
workId = acquired.get.id
Expand All @@ -122,29 +116,23 @@ object ExecutorTest extends SimpleIOSuite:

test("should not give up after 2 tries"):
for
ref <- Ref.of[IO, List[Lila.Move]](Nil)
client = createLilaClient(ref)
executor <- ioExecutor(client)(noopMonitor, ExecutorConfig(2))
executor <- createExecutor(ExecutorConfig(2))
_ <- executor.add(request)
_ <- (executor.acquire(key).flatMap(x => executor.move(x.get.id, key, invalidMove))).replicateA_(2)
acquired <- executor.acquire(key)
yield assert(acquired.isDefined)

test("should give up after 3 tries"):
for
ref <- Ref.of[IO, List[Lila.Move]](Nil)
client = createLilaClient(ref)
executor <- ioExecutor(client)(noopMonitor, ExecutorConfig(2))
executor <- createExecutor(ExecutorConfig(2))
_ <- executor.add(request)
_ <- (executor.acquire(key).flatMap(x => executor.move(x.get.id, key, invalidMove))).replicateA_(3)
acquired <- executor.acquire(key)
yield assert(acquired.isEmpty)

test("give up due to invalid move should reduce task's size"):
for
ref <- Ref.of[IO, List[Lila.Move]](Nil)
client = createLilaClient(ref)
executor <- ioExecutor(client)(noopMonitor, ExecutorConfig(2))
executor <- createExecutor(ExecutorConfig(2))
_ <- executor.add(request)
_ <- executor.add(request.copy(id = GameId("2")))
_ <- (executor.acquire(key).flatMap(x => executor.move(x.get.id, key, invalidMove))).replicateA_(3)
Expand All @@ -155,9 +143,7 @@ object ExecutorTest extends SimpleIOSuite:

test("give up due to cleaning should reduce task's size"):
for
ref <- Ref.of[IO, List[Lila.Move]](Nil)
client = createLilaClient(ref)
executor <- ioExecutor(client)(noopMonitor, ExecutorConfig(2))
executor <- createExecutor(ExecutorConfig(2))
_ <- executor.add(request)
_ <- executor.add(request.copy(id = GameId("2")))
_ <- (executor.acquire(key).flatMap(x => executor.move(x.get.id, key, invalidMove))).replicateA_(2)
Expand All @@ -182,17 +168,21 @@ object ExecutorTest extends SimpleIOSuite:
def createExecutor(config: ExecutorConfig = ExecutorConfig(300)): IO[Executor] =
createLilaClient.flatMap(ioExecutor(_)(noopMonitor, config))

def createExecutor(ref: Ref[IO, List[Lila.Move]])(config: ExecutorConfig): IO[Executor] =
ioExecutor(createLilaClient(ref))(noopMonitor, config)

def ioExecutor(client: LilaClient)(monitor: Monitor, config: ExecutorConfig): IO[Executor] =
Ref
.of[IO, AppState](AppState.empty)
.map(Executor.instance(_, client, monitor, config))

def createLilaClient: IO[LilaClient] =
Ref
.of[IO, List[Lila.Move]](Nil)
.map(createLilaClient)
emptyMovesRef.map(createLilaClient)

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

def emptyMovesRef: IO[Ref[IO, List[Lila.Move]]] =
Ref.of[IO, List[Lila.Move]](Nil)

0 comments on commit 8c7f667

Please sign in to comment.