Search code examples
specs2

Specs2 await doesn't fail inside of a Scope


When using Specs2(v 2.3.13) await, I have noticed that in some cases it doesn't fail. Specifically I have observed that if the "await" times out inside of a Scope, then it doesn't fail.

"Awaiting a failed future should fail" in {
  Future.failed(throw new Exception()).map(_ => success).await // Fails Correctly
}
"Awaiting a timed out future should fail" in {
  Future(Thread.sleep(5000)).map(_ => success).await // Fails Correctly
}
"Awaiting a failed scope should fail" in new Scope {
  failure // Fails Correctly
}
"Awaiting a failed future in a scope should fail" in new Scope {
  Future.failed(throw new Exception()).map(_ => success).await // Fails Correctly
}
"Awaiting a timed out future in a scope should fail" in new Scope {
  Future(Thread.sleep(5000)).map(_ => success).await  // DOES NOT FAIL
}

Am I misunderstanding the usage, or is this a bug?


Solution

  • The .await implicit on a Future[T] just creates a Result which will fail if there is a time-out. However no exception will be thrown hence that result will not escape that Scope trait.

    To workaround this you need to use Matchers and specify what you expect of your value of type T in Future[T]:

    Future(Thread.sleep(5000)).map(_ => 1) must be_==(1).await
    

    This will correctly show

    [info] x Awaiting a timed out future in a scope should fail
    [error]  Timeout after 1 second (TestSpec.scala:35)