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

Introduce TestScope.test(RibCoroutineWorker) test helper utility. #620

Merged
merged 1 commit into from
Oct 6, 2023

Conversation

psteiger
Copy link
Contributor

@psteiger psteiger commented Oct 3, 2023

This helper utility is meant to be used in tests inside runTest { } blocks and should facilitate RibCoroutineWorker testing by automatically binding and unbinding the worker.

  1. A test dispatcher is used to bind the worker (StandardTestDispatcher), so the test shall work even when RibCoroutinesRule is not used to override the RibDispatchers.Default dispatcher with a test dispatcher.
  2. Worker is only bound in the scope of the lambda passed to test(RibCoroutineWorker), hence tests never timeout with uncompleted coroutines because of a bound worker.
@Test fun test() = runTest {
  test(worker) {
    // Worker is bound. Make assertions
  }
  // Worker is unbound.
}

@psteiger psteiger force-pushed the testrbw branch 6 times, most recently from 6b7511f to 82c1f2b Compare October 3, 2023 21:31
* or completed, otherwise, the aforementioned time advancing API must be used.
*/
@OptIn(ExperimentalCoroutinesApi::class)
public inline fun <T : RibCoroutineWorker> TestScope.test(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious, why not place this function somewhere in rib-coroutines-test instead of here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this file has the same name as the private class TestRibCoroutineWorker in RibCouroutineWorkerTest.kt (no immediate suggestion, just saying that I had to do a double-take.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious, why not place this function somewhere in rib-coroutines-test instead of here?

Because RibCoroutineWorker is in rib-base, not rib-coroutines.

Also, this file has the same name as the private class TestRibCoroutineWorker in RibCouroutineWorkerTest.kt (no immediate suggestion, just saying that I had to do a double-take.)

Hm. Maybe it's reasonable to rename the private class to something like TestWorker

This helper utility is meant to be used in tests inside `runTest { }` blocks
and should facilitate `RibCoroutineWorker` testing by automatically binding
and unbinding the worker in the scope of the lambda.

```
@test fun test() = runTest {
  test(worker) {
    // Worker is bound. Make assertions
  }
  // Worker is unbound.
}
```
@psteiger psteiger merged commit 73a5958 into uber:main Oct 6, 2023
2 checks passed
@psteiger psteiger deleted the testrbw branch October 6, 2023 18:11
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

Successfully merging this pull request may close these issues.

3 participants