Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scalatest Flatspec: Timeout for entire class

With the scalatest Flatspec and the TimeLimits trait I can set a timeout for a line of code like so:

import org.scalatest.time.SpanSugar._
import org.scalatest.concurrent.TimeLimits
import org.scalatest.FlatSpec

class MyTestClass extends Flatspec with TimeLimits {
  "My thread" must "complete on time" in { 
    failAfter(100 millis) { infiniteLoop() }
    // I have also tried cancelAfter
  }
}

This should fail due to a timeout. However, when I run this test in Intellij it just runs forever.

Furthermore, I do not want to have to rewrite the timeout for every method, instead I would like to configure it once for the entire class. The PatienceConfig claims to do that, but it does not seem to do anything. The test is still runs forever.

import org.scalatest.FlatSpec
import org.scalatest.time.{Millis, Span}
import org.scalatest.concurrent.{Eventually, IntegrationPatience}

class MyTestClass extends Flatspec with Eventually with IntegrationPatience {
 implicit val defaultPatience = PatienceConfig(timeout=Span(100, Millis))

 "My thread" must "complete on time" in { 
   inifiniteLoop()
 }
}
like image 675
icehawk Avatar asked Nov 19 '17 17:11

icehawk


1 Answers

I looked for a solution myself. came a cross this answer, it worked for me. I am using flatspec, added the trait TimeLimitedTests

with TimeLimitedTests

then inside the code I wrote my limit for each of the tests val timeLimit: Span = Span(2000, Millis) which is defined by the trait (we are overriding it).

Finally it didn't work until I overriden the interrupter as suggested by Rumoku in the referenced answer by

override val defaultTestInterruptor: Interruptor = new Interruptor {
override def apply(testThread: Thread): Unit = {
  println("Kindly die")
  testThread.stop() // deprecated. unsafe. do not use
}}

I hope this helps

like image 188
Eytan Avatar answered Nov 08 '22 14:11

Eytan