I've got a rest service that launches a couroutine...
# coroutines
p
I've got a rest service that launches a couroutine via GlobalScope.launch. The coroutine is in turn calling 2 3rd party services (by lauching child corotuintes) which may take 15 minutes to complete, so I want my rest service to return straight away. When my coroutine eventually finishes I send a coroutine message back to the originator and everyone is happy. I have multiple tests for the above. I'm using mockk and junit 5 to mock the 3d part services, and clearing mocks before each tests. Because of this I'm experiencing errors in the coroutines kicked off by one test, because the next test is coming along and clearing the mocks that are waiting to be consumed by the coroutine. I can't not clear the mocks, because I want to guarantee behaviour of each test. Is there any way I can wait for a GlobalScope launched coroutine to finish before I launch the next test ?
g
Why do you use GlobalScope for tests? You can just create scope per test, so no interference between tests (simple
runBlocking { launch {} }
should be enough for this)
p
Good point. Thanks,