-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Improve TestCoroutineScope
#2975
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
Improve TestCoroutineScope
#2975
Conversation
3e872b4
to
f90ba4c
Compare
15788a3
to
25a7fe0
Compare
f90ba4c
to
5ea966d
Compare
e2e2d10
to
2f818a3
Compare
5ea966d
to
4cdda36
Compare
} | ||
this ?: TestCoroutineExceptionHandler() | ||
} | ||
val job: Job |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm okay with the changes in general.
What puzzles me is the overall sense of passing your own job to TestCoroutineScope
that does not complete when the scope semantically completes.
Let's not prevent my comment from merging it though, I'll return to it later (it's much easier to review the whole picture again when one has all the details incorporated)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point of this approach was to avoid breakage with an unclear migration path. It's possible that someone shared a Job
among several TestCoroutineScope
instances, which would break if we were to complete the passed job on each cleanup.
However, I did not encounter such usage in the wild—in fact, the only instances of someone passing a Job
to TestCoroutineScope
that I observed were to work around the bug where, before, TestCoroutineScope
was constructed without one.
Another approach to fix this particular problem would be to treat the passed job as the parent. However, it could also break some code.
Not much depends on this particular choice, so maybe we can reach a resolution right in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep the things consistent then:
TestCoroutineScope
provides its own job when it's missing from the context or uses the existing ones. It's perfectly consistent with ourCoroutineScope()
builder (andMainScope()
)runTest
coroutine uses the job from the scope as a parent, no matter whether it is the default one or a user-defined one. Also consistent with our builders (e.g.runBlocking
orwithContext
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean that, given how unlikely breakage would be, we need to avoid discerning between a user-supplied Job
and the one TestCoroutineScope
creates on its own?
7d004d6
to
0a60a3b
Compare
67661d9
to
5509e90
Compare
0a60a3b
to
2cddb28
Compare
Otherwise looks good and is good to go |
runBlockingTest
tocleanupTestCoroutines
Fixes cleanupTestCoroutines does not detect awaiting CompletableDeferred #1749