https://kotlinlang.org logo
#coroutines
Title
# coroutines
m

mkrussel

11/02/2023, 2:48 PM
When running my android unit test (not instrumented). I get this stack trace printed when trying to set the dispatcher in my setup function. The test passes, but this makes the build output very noisy. Anyone know if I'm doing something wrong. The exception says to call
Dispatchers.setMain
, but that is the function creating the stack trace.
c

Chrimaeon

11/02/2023, 3:21 PM
the exception is from
TestDispatchers
not
Dispatchers
m

mkrussel

11/02/2023, 3:25 PM
I understand how to use
setMain
and I'm trying to use it. What seems to be happening is
setMain
grabs a copy of the current Main to support
resetMain
, but getting the current version fails and that failure gets logged. Is there something that I can do to prevent this behavior.
c

Chrimaeon

11/02/2023, 3:26 PM
a code snipped how you use it might help 😉
m

mkrussel

11/02/2023, 3:28 PM
Copy code
@Before
    override fun setUp() {
        Dispatchers.setMain(Dispatchers.Unconfined)
        viewportManager.setUp()
    }

    @After
    override fun tearDown() {
        if (this::viewport.isInitialized) {
            viewport.dispose()
        }

        viewportManager.tearDown()
        Dispatchers.resetMain()
    }
Copy code
The stack trace points to my call of `Dispatchers.setMain(Dispatchers.Unconfined)`
c

Chrimaeon

11/02/2023, 3:29 PM
as mentioned in the docs
Using TestDispatcher as an argument has special behavior: subsequently-called runTest, as well as TestScope and test dispatcher constructors, will use the TestCoroutineScheduler of the provided dispatcher.
m

mkrussel

11/02/2023, 3:37 PM
I don't think that will make a difference because the
setMain
function doesn't look at the dispatcher type. Trying to run again, but it is no longer printing this stack trace even though I didn't change anything.
👍 1
2 Views