Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runBlockingTestProvided() must add the TestCoroutineDispatcher to the context #15

Closed
ralfstuckert opened this issue Jan 12, 2020 · 5 comments

Comments

@ralfstuckert
Copy link

ralfstuckert commented Jan 12, 2020

The TestCoroutineDispatcher used to create the TestDispatcherProvider is not added to the context. Since there is no existing dispatcher in the context, the called runBlockingTest() will create its own TestCoroutineDispatcher.

As a consequence, any calls to advanceTime...() will not work correctly since they are performed on a different dispatcher. The following test fails with an UncompletedCoroutinesError

    @Test
    @ExperimentalCoroutinesApi
    fun advanceTimeIssue() = runBlockingTestProvided {
        delayOnProvidedMain()
        advanceTimeBy(1000)
    }

    fun CoroutineScope.delayOnProvidedMain() {
        launch(dispatcherProvider.main) {
            delay(1000)
        }
    }

In order to fix this situation, you must add the testDispatcher used for the provider also to the context, see this working example:

    @ExperimentalCoroutinesApi
    private fun contextWithProvider(): CoroutineContext {
        val testDispatcher = TestCoroutineDispatcher()
        val provider = TestDispatcherProvider(testDispatcher)
        return EmptyCoroutineContext + testDispatcher + provider
    }

    @Test
    @ExperimentalCoroutinesApi
    fun advanceTimeIssueFixed() = runBlockingTest(contextWithProvider()) {
        delayOnProvidedMain()
        advanceTimeBy(1000)
    }
@RBusarow
Copy link
Owner

RBusarow commented Jan 12, 2020

Thanks for the report and minimum repro! I'll have a fix PR'd shortly.

RBusarow pushed a commit that referenced this issue Jan 12, 2020
RBusarow added a commit that referenced this issue Jan 13, 2020
…to use TestCoroutineDispatcher (#16)

* 1.0.0-beta01 (#12)

* update copyright for 2020

* update codestyle

* update versions for coroutines, kotlin, mockK, and misc. androidx

* set jvm target to 1.6

* add Kdocs and ensure a blank line after copyright headers

* add flowOn operators (#7)

* add flowOn operators

* add flowOn operators

* scripts for publishing to maven central (#8)

* kotlin gradle dsl (#9)

* add kdocs to the test api functions (#11)

* update copyright block in README

* #15 Make TestBasicDispatcherProvider, update runBlockingProvided and runBlockingProvidedTest
RBusarow added a commit that referenced this issue Jan 13, 2020
* update copyright for 2020

* update codestyle

* update versions for coroutines, kotlin, mockK, and misc. androidx

* set jvm target to 1.6

* add Kdocs and ensure a blank line after copyright headers

* add flowOn operators (#7)

* add flowOn operators

* add flowOn operators

* scripts for publishing to maven central (#8)

* kotlin gradle dsl (#9)

* add kdocs to the test api functions (#11)

* update Sample to use Maven Central versions of the project (#13)

* CoroutineContext args for CoroutineScope factories (#14)

* 1.0.0-beta01 (#12)

* update copyright for 2020

* update codestyle

* update versions for coroutines, kotlin, mockK, and misc. androidx

* set jvm target to 1.6

* add Kdocs and ensure a blank line after copyright headers

* add flowOn operators (#7)

* add flowOn operators

* add flowOn operators

* scripts for publishing to maven central (#8)

* kotlin gradle dsl (#9)

* add kdocs to the test api functions (#11)

* CoroutineContext args for CoroutineScope builders

* #15 Make TestBasicDispatcherProvider, update runBlockingProvidedTest to use TestCoroutineDispatcher (#16)

* 1.0.0-beta01 (#12)

* update copyright for 2020

* update codestyle

* update versions for coroutines, kotlin, mockK, and misc. androidx

* set jvm target to 1.6

* add Kdocs and ensure a blank line after copyright headers

* add flowOn operators (#7)

* add flowOn operators

* add flowOn operators

* scripts for publishing to maven central (#8)

* kotlin gradle dsl (#9)

* add kdocs to the test api functions (#11)

* update copyright block in README

* #15 Make TestBasicDispatcherProvider, update runBlockingProvided and runBlockingProvidedTest

* update kdoc to use `@see foo` instead of `see also [foo]` (#18)

* add change log (#19)

* update version to 1.0.0-beta2 (#17)

* update version to 1.0.0-beta2

* change library version in the sample dependencies to be "1.+"
@RBusarow
Copy link
Owner

It should be fixed now in 1.0.0-beta02. Would you mind giving it a shot and confirming?

Thanks again!

@ralfstuckert
Copy link
Author

Yep, perfect. Thanks for releasing a fix at warp speed ;-)

By the way: you only released beta02 for dispatcher-provider-test, right? At least my gradle complained about not finding dispatcher-provider with version beta02.

@RBusarow
Copy link
Owner

Thanks for checking it out!

I released both artifacts, and I'm able to import both of them now on my machine. I think someone at maven central just needed to flip their floppy disk over.

@ralfstuckert
Copy link
Author

Yep, seemed to be a maven central problem, by now it succeeded.
Thanks again for your support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants