Wesley Hartford
08/25/2023, 5:06 PMWesley Hartford
08/25/2023, 5:08 PMbeforeSpec
using the GlobalScope
, and cancelling that job in an afterSpec
. Is there a cleaner way to do that without GlobalScope
?sam
08/25/2023, 11:57 PMsam
08/25/2023, 11:58 PMWesley Hartford
08/26/2023, 12:15 AMbeforeSpec
block. Ideally, there would be a scope that ends after the last test ends, or after all the afterSpec
blocks or something.sam
08/26/2023, 12:15 AMsam
08/26/2023, 12:18 AMWesley Hartford
08/26/2023, 12:20 AMOleksii Malovanyi
08/27/2023, 12:49 PMbackgroundScope
(kotlinlang.org)
kotest has it too via own TestScope
impl. The only thing to be aware of is to understand the lifecycle of particular TestCope you gonna use – if you acquire backgroundScope
from the ContainerScope
it would have a lifecycle of the container, not a particular test, and probably this is not something you gonna wantOleksii Malovanyi
08/27/2023, 12:51 PMUnconfinedTestDispatcherImpl
is what you want, but then you'll have to dance a bit with the single instance of TestCoroutineScheduler
fun Spec.SpecCancellableScope(dispatcher: CoroutineDispatcher = Dispatchers.Default) =
CoroutineScope(Job() + dispatcher).also { scope ->
val doCancel = { scope.cancel("Cleanup scope with test finishes") }
when (isolationMode ?: AppKotestProjectConfig.isolationMode) {
IsolationMode.SingleInstance -> {
afterSpec { doCancel() }
}
IsolationMode.InstancePerLeaf, IsolationMode.InstancePerTest -> {
afterTest { doCancel() }
}
}
}