Skip to content

Commit

Permalink
Fix failing async fixture tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olafurpg committed Oct 16, 2021
1 parent 1b3854e commit 9e081b3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
21 changes: 4 additions & 17 deletions docs/fixtures.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,6 @@ class MySuite extends munit.FunSuite {
}
```

Next, extend `munitValueTransforms` to convert `Resource[T]` into `Future[T]`,
see [declare async tests](tests.md#declare-async-test) for more details.

## Ad-hoc test-local fixtures

Override `beforeEach()` and `afterEach()` to add custom logic that should run
Expand Down Expand Up @@ -198,7 +195,6 @@ import scala.concurrent.ExecutionContext.Implicits.global

class AsyncFilesSuite extends FunSuite {

// Test-local async fixture
val file = new FutureFixture[Path]("files") {
var file: Path = null
def apply() = file
Expand All @@ -211,19 +207,7 @@ class AsyncFilesSuite extends FunSuite {
}
}

// Suite-local async fixture
val db = new FutureFixture[Connection]("database") {
private var connection: Connection = null
def apply() = connection
override def beforeAll(): Future[Unit] = Future {
connection = DriverManager.getConnection("jdbc:h2:mem:", "sa", null)
}
override def afterAll(): Future[Unit] = Future {
connection.close()
}
}

override def munitFixtures = List(file, db)
override def munitFixtures = List(file)

test("exists") {
// `file` is the temporary file that was created for this test case.
Expand Down Expand Up @@ -259,6 +243,9 @@ abstract class ResourceFixture[T](name: String) extends munit.AnyFixture[T](name
}
```

Next, extend `munitValueTransforms` to convert `Resource[T]` into `Future[T]`,
see [declare async tests](tests.md#declare-async-test) for more details.

## Avoid stateful operations in the class constructor

Test classes may sometimes get initialized even if no tests run so it's best to
Expand Down
12 changes: 6 additions & 6 deletions tests/jvm/src/test/scala/munit/AsyncFixtureSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ class AsyncFixtureSuite extends BaseSuite {
}
)
)
class ScheduledMessage() extends Fixture[String]("AsyncFixture") {
class ScheduledMessage() extends AnyFixture[String]("AsyncFixture") {
val sh: ScheduledExecutorService =
Executors.newSingleThreadScheduledExecutor()
private var didBeforeAllEvaluateAsync = false
private var promise = Promise[String]()
private val timeout = 20
def apply(): String = promise.future.value.get.get
override def beforeAll(): Unit = {
override def beforeAll(): PromiseWrapper = {
val setBeforeAllBit = Promise[Unit]()
sh.schedule[Unit](
() => {
Expand All @@ -36,7 +36,7 @@ class AsyncFixtureSuite extends BaseSuite {
)
PromiseWrapper("beforeAll", setBeforeAllBit)
}
override def beforeEach(context: BeforeEach): Unit = {
override def beforeEach(context: BeforeEach): PromiseWrapper = {
assertEquals(
promise.future.value,
None,
Expand All @@ -53,7 +53,7 @@ class AsyncFixtureSuite extends BaseSuite {
)
PromiseWrapper("beforeEach", promise)
}
override def afterEach(context: AfterEach): Unit = {
override def afterEach(context: AfterEach): PromiseWrapper = {
val resetPromise = Promise[Unit]()
sh.schedule[Unit](
() => {
Expand All @@ -65,7 +65,7 @@ class AsyncFixtureSuite extends BaseSuite {
)
PromiseWrapper("afterEach", resetPromise)
}
override def afterAll(): Unit = {
override def afterAll(): PromiseWrapper = {
val shutdownPromise = Promise[Unit]()
ExecutionContext.global.execute(() => {
Thread.sleep(timeout)
Expand All @@ -88,7 +88,7 @@ class AsyncFixtureSuite extends BaseSuite {
}
}

override def munitFixtures: Seq[Fixture[_]] = List(message, latest)
override def munitFixtures: Seq[AnyFixture[_]] = List(message, latest)

1.to(3).foreach { i =>
test(s"test-$i") {
Expand Down

0 comments on commit 9e081b3

Please sign in to comment.