spek
spek copied to clipboard
Spek executes tests off-thread
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.
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.
}
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.
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()
}
}
}
}