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

CoroutineTestExtension does not always use Dispatchers.setMain() #130

Closed
RBusarow opened this issue Jun 8, 2020 · 0 comments · Fixed by #132
Closed

CoroutineTestExtension does not always use Dispatchers.setMain() #130

RBusarow opened this issue Jun 8, 2020 · 0 comments · Fixed by #132
Labels
bug Something isn't working
Milestone

Comments

@RBusarow
Copy link
Owner

RBusarow commented Jun 8, 2020

Any time the extension injects a new scope via constructor or function argument, it puts that scope into a MutableMap where the key its the ExtensionContext. This map is used in beforeEach to call setMain(...) and in afterEach to call resetMain() and cleanUpTestCoroutines().

This is the existing beforeEach:

  override fun beforeEach(context: ExtensionContext) {
    contextScopeMap[context]?.dispatcherProvider?.main?.let {
      Dispatchers.setMain(it)
    }
  }

Here's a simplified timeline of a JUnit5 test execution with parameter resolution:

  1. Initialize extension (no context here for the internal scope property).
  2. Create constructor dependencies and add to map.
  3. Create class.
  4. beforeEach()
  5. Create function dependencies and add to map.
  6. Execute test function with parameters.
  7. afterEach()

Dispatchers.setMain(...) works with constructor parameters because they've been added to the map before beforeEach() is invoked. It doesn't work with the internal scope property because it's never added to the map. It also doesn't work for injected function parameters because they're constructed and added to the map after beforeEach() is invoked.

@RBusarow RBusarow added the bug Something isn't working label Jun 8, 2020
@RBusarow RBusarow added this to the 1.0.0-beta04 milestone Jun 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant