Skip to content
Snippets Groups Projects
TestUtils.scala 936 B
Newer Older
Matt Bovel's avatar
Matt Bovel committed
package instrumentation

Matt Bovel's avatar
Matt Bovel committed
import scala.concurrent.*
import scala.concurrent.duration.*
Matt Bovel's avatar
Matt Bovel committed
import scala.concurrent.ExecutionContext.Implicits.global
import org.junit.Assert.*

object TestUtils:
  def failsOrTimesOut[T](action: => T): Boolean =
    val asyncAction = Future {
      action
    }
    try
      Await.result(asyncAction, 2000.millisecond)
    catch
      case _: Throwable => return true
    return false
Matt Bovel's avatar
Matt Bovel committed

Matt Bovel's avatar
Matt Bovel committed
  def assertDeadlock[T](action: => T): Unit =
    try
      action
      throw new AssertionError("No error detected.")
    catch
Matt Bovel's avatar
Matt Bovel committed
      case e: AssertionError =>
        assert(e.getMessage.contains("Deadlock"), "No deadlock detected.")

Matt Bovel's avatar
Matt Bovel committed
  def assertMaybeDeadlock[T](action: => T): Unit =
    try
      action
      throw new AssertionError("No error detected.")
    catch
Matt Bovel's avatar
Matt Bovel committed
      case e: AssertionError =>
        assert(
          e.getMessage.contains("A possible deadlock!"),
          "No deadlock detected."
        )