-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from 2m/feature/async-fixture-2m
Add async setup/teardown support to FunFixture
- Loading branch information
Showing
11 changed files
with
243 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
tests/jvm/src/test/scala/munit/AsyncFixtureOrderSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package munit | ||
|
||
import scala.concurrent.Future | ||
import scala.concurrent.Promise | ||
|
||
class AsyncFixtureOrderSuite extends FunSuite { | ||
val latch = Promise[Unit] | ||
var completedFromTest = Option.empty[Boolean] | ||
var completedFromTeardown = Option.empty[Boolean] | ||
|
||
val latchOnTeardown = FunFixture.async[String]( | ||
setup = { test => Future.successful(test.name) }, | ||
teardown = { name => | ||
implicit val ec = munitExecutionContext | ||
Future { | ||
completedFromTeardown = Some(latch.trySuccess(())); | ||
} | ||
} | ||
) | ||
|
||
override def afterAll(): Unit = { | ||
// promise was completed first by the test | ||
assertEquals(completedFromTest, Some(true)) | ||
// and then there was a completion attempt by the teardown | ||
assertEquals(completedFromTeardown, Some(false)) | ||
} | ||
|
||
latchOnTeardown.test("teardown runs only after test completes") { _ => | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
Future { | ||
// Simulate some work here, which increases the certainty that this test | ||
// will fail by design and not by lucky scheduling if the happens-before | ||
// relationship between the test and teardown is removed. | ||
Thread.sleep(50) | ||
completedFromTest = Some(latch.trySuccess(())) | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
tests/shared/src/main/scala/munit/AsyncFixtureFrameworkSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package munit | ||
|
||
import scala.concurrent.Future | ||
|
||
class AsyncFixtureFrameworkSuite extends FunSuite { | ||
val failingSetup = FunFixture.async[Unit]( | ||
_ => Future.failed(new Error("failure in setup")), | ||
_ => Future.successful(()) | ||
) | ||
|
||
val failingTeardown = FunFixture.async[Unit]( | ||
_ => Future.successful(()), | ||
_ => Future.failed(new Error("failure in teardown")) | ||
) | ||
|
||
val unitFixture = FunFixture.async[Unit]( | ||
_ => Future.successful(()), | ||
_ => Future.successful(()) | ||
) | ||
|
||
failingSetup.test("fail when setup fails") { _ => | ||
fail("failing setup did not fail the test") | ||
} | ||
|
||
failingTeardown.test("fail when teardown fails") { _ => () } | ||
|
||
failingTeardown.test("fail when test and teardown fail") { _ => | ||
fail("failure in test") | ||
} | ||
|
||
FunFixture | ||
.map2(unitFixture, failingSetup) | ||
.test("fail when mapped setup fails") { _ => | ||
fail("failing setup did not fail the test") | ||
} | ||
|
||
FunFixture | ||
.map3(unitFixture, unitFixture, failingSetup) | ||
.test("fail when even more nested mapped setup fails") { _ => | ||
fail("failing setup did not fail the test") | ||
} | ||
|
||
FunFixture | ||
.map2(unitFixture, failingTeardown) | ||
.test("fail when mapped teardown fails") { _ => () } | ||
|
||
FunFixture | ||
.map3(unitFixture, unitFixture, failingTeardown) | ||
.test("fail when even more nested mapped teardown fails") { _ => () } | ||
} | ||
|
||
object AsyncFixtureFrameworkSuite | ||
extends FrameworkTest( | ||
classOf[AsyncFixtureFrameworkSuite], | ||
"""|==> failure munit.AsyncFixtureFrameworkSuite.fail when setup fails - failure in setup | ||
|==> failure munit.AsyncFixtureFrameworkSuite.fail when teardown fails - failure in teardown | ||
|==> failure munit.AsyncFixtureFrameworkSuite.fail when test and teardown fail - /scala/munit/AsyncFixtureFrameworkSuite.scala:28 failure in test | ||
|27: failingTeardown.test("fail when test and teardown fail") { _ => | ||
|28: fail("failure in test") | ||
|29: } | ||
|==> failure munit.AsyncFixtureFrameworkSuite.fail when mapped setup fails - failure in setup | ||
|==> failure munit.AsyncFixtureFrameworkSuite.fail when even more nested mapped setup fails - failure in setup | ||
|==> failure munit.AsyncFixtureFrameworkSuite.fail when mapped teardown fails - failure in teardown | ||
|==> failure munit.AsyncFixtureFrameworkSuite.fail when even more nested mapped teardown fails - failure in teardown | ||
|""".stripMargin | ||
) |
32 changes: 32 additions & 0 deletions
32
tests/shared/src/main/scala/munit/AsyncFixtureTeardownFrameworkSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package munit | ||
|
||
import scala.concurrent.Future | ||
|
||
class AsyncFixtureTeardownFrameworkSuite extends FunSuite { | ||
@volatile var cleanedUp: Boolean = _ | ||
|
||
val cleanupInTeardown = FunFixture.async[Unit]( | ||
_ => { cleanedUp = false; Future.successful(()) }, | ||
_ => { cleanedUp = true; Future.successful(()) } | ||
) | ||
|
||
override def afterAll(): Unit = { | ||
assert(cleanedUp) | ||
} | ||
|
||
cleanupInTeardown.test("calls teardown when test throws") { _ => | ||
throw new Error("failure in test") | ||
} | ||
|
||
cleanupInTeardown.test("calls teardown when test returns failed Future") { | ||
_ => Future.failed(new Error("failure in test")) | ||
} | ||
} | ||
|
||
object AsyncFixtureTeardownFrameworkSuite | ||
extends FrameworkTest( | ||
classOf[AsyncFixtureTeardownFrameworkSuite], | ||
"""|==> failure munit.AsyncFixtureTeardownFrameworkSuite.calls teardown when test throws - failure in test | ||
|==> failure munit.AsyncFixtureTeardownFrameworkSuite.calls teardown when test returns failed Future - failure in test | ||
|""".stripMargin | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters