I need a hack to run this TestCaseExtension `inter...
# kotest
l
I need a hack to run this TestCaseExtension
intercept
inside a place that for some reason I don't have a suspension context
runBlocking { execute(testCase)
executes too late.
runBlocking(ctx) { execute(testCase) }
deadlocks (I think) the execution
I can`t really clone it to the project, it's a giant from somewhere in Android
e
Could you inline the
runTest
function?
l
It's not my code 😞 So I believe it's not possible, unless I copy the whole structure supporting it
Any hidden coroutine context I could try to use? @sam maybe you have a pointer on that
s
would have to be run blcoking inside a final clause
l
I don't think that's possible. I need to run it inside the
runTest
block, before the callback hell is unwrapped.
Or I didn't understand you
s
finally { runBlocking { ... } }
l
I don't see how to apply that suggestion. I'll try to simplify the code:
Copy code
fun notMyFun(block: () -> Unit) {}
  
  override suspend fun intercept(testCase: TestCase, execute: suspend (TestCase) -> TestResult): TestResult {
    var result: TestResult? = null
    
    notMyFun { 
      result = execute(testCase)
    }
    
    return result!!
  }
I need to necessarily run the test block inside
notMyFun
, which isn't suspendable and isn't inlined
s
run blocking gives you a coroutine from a non suspendable function
l
I tried to do this:
Copy code
fun notMyFun(block: () -> Unit) {}

override suspend fun intercept(testCase: TestCase, execute: suspend (TestCase) -> TestResult): TestResult {
  var result: TestResult? = null

  notMyFun {
    result = runBlocking { execute(testCase) }
  }

  return result!!
}
But it gets run after
notMyFun
have already finished
I also tried
Copy code
fun notMyFun(block: () -> Unit) {}

  override suspend fun intercept(testCase: TestCase, execute: suspend (TestCase) -> TestResult): TestResult {
    val ctx = currentCoroutineContext()
    var result: TestResult? = null

    notMyFun {
      result = runBlocking(ctx) { execute(testCase) }
    }

    return result!!
  }
But this leaves me in a locked running state
Copy code
override suspend fun intercept(spec: Spec, execute: suspend (Spec) -> Unit) {
  notMyFun {
    runBlocking { execute(spec) }
  }
}
Interesting, on Spec it worked
But the testcase is ran outside of
notMyFun
anyway
Android is probably switching to another context during the extension execution
Would that make any sense?