Last active
March 21, 2019 03:16
-
-
Save izmailoff/fb343c93b29e73ab4c9ac870b3c6e312 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import org.scalatest.{Assertion, AsyncFlatSpec} | |
import scala.concurrent.{Future, Await} | |
import org.scalatest.Matchers._ | |
import scala.concurrent.duration._ | |
import scala.language.postfixOps | |
class FutureSpec extends AsyncFlatSpec { | |
// Overriding execution context is not required, unless you want to use Await. | |
// To avoid deadlocking the main thread you need to have multithreaded context. | |
implicit override val executionContext = scala.concurrent.ExecutionContext.Implicits.global | |
def assertAll(futures: Future[Assertion]*): Future[Assertion] = | |
Future.sequence(futures) map (_.foreach(a => a)) map (_ => succeed) | |
"non-future assertions" should "be all checked as expected" in { | |
1 shouldEqual 2 // fails here | |
2 shouldEqual 2 | |
} | |
"all future assertions" should "be checked BUT this test doesn't fail" in { | |
Future { 1 shouldEqual 2 } // doesn't fail here as expected | |
Future { 2 shouldEqual 2 } | |
} | |
"all future assertions" should "be checked BUT this test checks only last one" in { | |
Future { 1 shouldEqual 2 } // doesn't fail here as expected | |
Future { 3 shouldEqual 4 } // only last Future[Assertion] is checked | |
} | |
"all future assertions" should "be checked with our helper and this test fails as it should" in { | |
val a1 = Future { 1 shouldEqual 2 } | |
// you can do more things here | |
val a2 = Future { 2 shouldEqual 2 } | |
assertAll(a1, a2) | |
} | |
"all future assertions" should "be checked with Await, but this introduces blocking" in { | |
Await.result(Future { 1 shouldEqual 2 }, 2 seconds) | |
Await.result(Future { 2 shouldEqual 2 }, 2 seconds) | |
} | |
} | |
(new FutureSpec).execute(color = false, shortstacks = true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
testOnly *FutureSpec
[info] Compiling 1 Scala source to ...
[info] Done compiling.
[info] FutureSpec:
[info] non-future assertions
[info] - should be all checked as expected *** FAILED ***
[info] 1 did not equal 2 (FutureSpec.scala:122)
[info] all future assertions
[info] - should be checked BUT this test doesn't fail
[info] all future assertions
[info] - should be checked BUT this test checks only last one *** FAILED ***
[info] 3 did not equal 4 (FutureSpec.scala:133)
[info] all future assertions
[info] all future assertions
[info] - should be checked with our helper and this test fails as it should *** FAILED ***
[info] 1 did not equal 2 (FutureSpec.scala:137)
[info] - should be checked with Await, but this introduces blocking *** FAILED ***
[info] 1 did not equal 2 (FutureSpec.scala:145)
[info] Run completed in 885 milliseconds.
[info] Total number of tests run: 5
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 4, canceled 0, ignored 0, pending 0
[info] *** 4 TESTS FAILED ***
[error] Failed tests:
[error] FutureSpec
[error] (Test / testOnly) sbt.TestsFailedException: Tests unsuccessful