0

I have a several Scala test with Future and with Await time and the same structure. Tests check some method which write file to file system.

I want to execute tests strictly consistent each test have to start after future in previous test will be finished. Could u help me find the way?

Now I have a failed test, because next test cant check result.

class Test1 extends AsyncFunSuite with BeforeAndAfterAll with Eventually with Matchers {
       test("test 1") {
          // clean file system

          Try(
             Await.ready(
             Future(TestedMethod1.run(someArgs)), 10 seconds)
          )

           // checking file
      }

       test("test 2") {
         ..... // the same tests
      }
}

class Test2 extends AsyncFunSuite with BeforeAndAfterAll with Eventually with Matchers {
       test("test 1") {
         ..... // the same tests
      }

       test("test 2") {
         ..... // the same tests
      }
}
Vlad
  • 35
  • 3

1 Answers1

2

AsyncFunSuite by default provides serial execution context

private final val serialExecutionContext: ExecutionContext = new concurrent.SerialExecutionContext
implicit def executionContext: ExecutionContext = serialExecutionContext

which only has a single thread available so if that thread is blocked by Await then further tests cannot proceed. The solution is to refactor your tests to not use Await but instead simply map over the Future as usual

Future(TestedMethod1.run(someArgs)).map { result =>
   // assert on result
}

Note it is likely you can avoid using Eventually.

Mario Galic
  • 47,285
  • 6
  • 56
  • 98
  • Thank u fro response, I rewrite test with map, but test execution did not finished. Is it cause that I use implicit val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(20)) ? – Vlad Jun 19 '20 at 04:39