Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove munit deps #326

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 49 additions & 42 deletions app/src/test/scala/AppStateTest.scala
Original file line number Diff line number Diff line change
@@ -1,98 +1,105 @@
package lila.fishnet

import munit.ScalaCheckSuite
import org.scalacheck.Prop.{ forAll, propBoolean }
import cats.Show
import weaver.scalacheck.Checkers

import java.time.Instant

import Arbitraries.given

class AppStateTest extends ScalaCheckSuite:
object AppStateTest extends weaver.SimpleIOSuite with Checkers:

given Show[AppState] = Show.fromToString
given Show[ClientKey] = Show.fromToString
given Show[Instant] = Show.fromToString
given Show[Work.Task] = Show.fromToString
given Show[Work.Task => Boolean] = Show.fromToString

test("tasks.fromTasks == identity"):
forAll: (state: AppState) =>
assertEquals(AppState.fromTasks(state.tasks), state)
forall: (state: AppState) =>
expect(AppState.fromTasks(state.tasks) == state)

test("tasks.fromTasks == tasks"):
forAll: (ts: List[Work.Task]) =>
forall: (ts: List[Work.Task]) =>
val tasks = ts.distinctBy(_.id)
assertEquals(AppState.fromTasks(tasks).tasks.toSet, tasks.toSet)
expect(AppState.fromTasks(tasks).tasks.toSet == tasks.toSet)

test("isFull"):
forAll: (state: AppState) =>
state.isFull(10) || state.size < 10
forall: (state: AppState) =>
expect(state.isFull(10) || state.size < 10)

test("add = remove.add"):
forAll: (state: AppState, task: Work.Task) =>
state.remove(task.id).add(task) == state.add(task)
forall: (state: AppState, task: Work.Task) =>
expect(state.remove(task.id).add(task) == state.add(task))

test("add.remove == remove"):
forAll: (state: AppState, task: Work.Task) =>
state.add(task).remove(task.id) == state.remove(task.id)
forall: (state: AppState, task: Work.Task) =>
expect(state.add(task).remove(task.id) == state.remove(task.id))

test("count(p) + count(!p) == size"):
forAll: (state: AppState, p: Work.Task => Boolean) =>
state.count(p) + state.count(x => !p(x)) == state.size
forall: (state: AppState, p: Work.Task => Boolean) =>
expect(state.count(p) + state.count(x => !p(x)) == state.size)

test("count(true) == size"):
forAll: (state: AppState) =>
state.count(_ => true) == state.size
forall: (state: AppState) =>
expect(state.count(_ => true) == state.size)

test("count(false) == 0"):
forAll: (state: AppState) =>
state.count(_ => false) == 0
forall: (state: AppState) =>
expect(state.count(_ => false) == 0)

test("earliestNonAcquired is always non acquired"):
forAll: (state: AppState) =>
state.earliestNonAcquired.forall(_.nonAcquired)
forall: (state: AppState) =>
expect(state.earliestNonAcquired.forall(_.nonAcquired))

test("if earliestNonAcquired is defined then tryAcquire is defined"):
forAll: (state: AppState, key: ClientKey, at: Instant) =>
state.tryAcquire(key, at)._2.isDefined == state.earliestNonAcquired.isDefined
forall: (state: AppState, key: ClientKey, at: Instant) =>
expect(state.tryAcquire(key, at)._2.isDefined == state.earliestNonAcquired.isDefined)

test("tryAcquire always acquires the earliest non acquired task"):
forAll: (state: AppState, key: ClientKey, at: Instant) =>
state.earliestNonAcquired.forall: nonAcquiredTask =>
val (newState, ot) = state.tryAcquire(key, at)
val task = ot.get
state.add(task) == newState && task == nonAcquiredTask.assignTo(key, at)
forall: (state: AppState, key: ClientKey, at: Instant) =>
expect:
state.earliestNonAcquired.forall: nonAcquiredTask =>
val (newState, ot) = state.tryAcquire(key, at)
val task = ot.get
state.add(task) == newState && task == nonAcquiredTask.assignTo(key, at)

test("tryAcquire then apply = Found"):
forAll: (state: AppState, key: ClientKey, at: Instant) =>
forall: (state: AppState, key: ClientKey, at: Instant) =>
val (newState, task) = state.tryAcquire(key, at)
task.forall(t => newState.apply(t.id, key) == GetTaskResult.Found(t))
expect(task.forall(t => newState.apply(t.id, key) == GetTaskResult.Found(t)))

test("acquiredBefore is acquiredBefore"):
forAll: (state: AppState, since: Instant) =>
state.acquiredBefore(since).forall(_.acquiredBefore(since))
forall: (state: AppState, since: Instant) =>
expect(state.acquiredBefore(since).forall(_.acquiredBefore(since)))

test("updateOrGiveUp is a subset of given tasks"):
forAll: (state: AppState, before: Instant) =>
forall: (state: AppState, before: Instant) =>
val candidates = state.acquiredBefore(before)
val (_, givenUp) = state.unassignOrGiveUp(candidates)
givenUp.toSet.subsetOf(candidates.toSet)
expect(givenUp.toSet.subsetOf(candidates.toSet))

test("updateOrGiveUp preserves size"):
forAll: (state: AppState, before: Instant) =>
forall: (state: AppState, before: Instant) =>
val candidates = state.acquiredBefore(before)
val (newState, givenUp) = state.unassignOrGiveUp(candidates)
newState.size + givenUp.size == state.size
expect(newState.size + givenUp.size == state.size)

test("all given up tasks are outOfTries"):
forAll: (state: AppState, before: Instant) =>
forall: (state: AppState, before: Instant) =>
val candidates = state.acquiredBefore(before)
val (_, givenUp) = state.unassignOrGiveUp(candidates)
givenUp.forall(_.isOutOfTries)
expect(givenUp.forall(_.isOutOfTries))

test("all candidates that are not given up are not outOfTries"):
forAll: (state: AppState, before: Instant) =>
forall: (state: AppState, before: Instant) =>
val candidates = state.acquiredBefore(before)
val (_, givenUp) = state.unassignOrGiveUp(candidates)
val rest = candidates.filterNot(givenUp.contains)
rest.forall(!_.isOutOfTries)
expect(rest.forall(!_.isOutOfTries))

test("after cleanup, acquiredBefore is empty"):
forAll: (state: AppState, before: Instant) =>
forall: (state: AppState, before: Instant) =>
val candidates = state.acquiredBefore(before)
val (newState, _) = state.unassignOrGiveUp(candidates)
newState.acquiredBefore(before).isEmpty
expect(newState.acquiredBefore(before).isEmpty)
3 changes: 0 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ lazy val app = project
testContainers,
http4sClient,
catsEffectTestKit,
munit,
munitScalaCheck,
scalacheck
),
javaAgents += kamonAgent,
Docker / packageName := "lichess-org/lila-fishnet",
Expand Down
3 changes: 0 additions & 3 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,4 @@ object Dependencies {
val weaver = "com.disneystreaming" %% "weaver-cats" % "0.8.4" % Test
val weaverScalaCheck = "com.disneystreaming" %% "weaver-scalacheck" % "0.8.4" % Test
val catsEffectTestKit = "org.typelevel" %% "cats-effect-testkit" % V.catsEffect % Test
val scalacheck = "org.scalacheck" %% "scalacheck" % "1.18.0" % Test
val munit = "org.scalameta" %% "munit" % "1.0.0" % Test
val munitScalaCheck = "org.scalameta" %% "munit-scalacheck" % "1.0.0" % Test
}