spek icon indicating copy to clipboard operation
spek copied to clipboard

Spek executes tests off-thread

Open edwinRNDR opened this issue 4 years ago • 3 comments

I have a number of tests that require to be executed on a known thread (one that has an OpenGL context associated to it). Currently it doesn't seem possible to change Spek's execution behaviour.

edwinRNDR avatar Dec 12 '19 07:12 edwinRNDR

Thanks for the report @edwinRNDR! This looks like related to #805. With #835 (coroutine support), I think you can create your own dispatcher (from a single thread) and use launch or async for it:

launch(MySingleThreadedDispatcher) {
  // code here will execute in that thread.
}

raniejade avatar Dec 12 '19 09:12 raniejade

I don't think that would solve my problem in cases in which code execution is constrained to take place on the main thread (like LWJGL's window event handling code on macOS). This is indeed similar to #805 but with an additional constraint that code needs to be executed on the main thread.

edwinRNDR avatar Dec 12 '19 11:12 edwinRNDR

The current implementation of #835 runs on the main thread actually but that will change (will probably use Dispatchers.Default). Bootstrapping of spek happens on the main thread (gradle and IJ), which gets blocked when the tests are executed (via runBlocking). I can probably expose some API (something like runOnMain { ... } ) that will be run that thread. Implementing it might be possible using coroutine's selection expressions.

// main thread
runBlocking {
   val run = launch { runtime.executeTests(...) }
   var isActive = true
   while (isActive) {
     select<Unit> {
        run.onJoin { isActive = false }
        execChannel.onReceive { block ->
          block()
        }
     }
   }
}

raniejade avatar Dec 12 '19 12:12 raniejade