-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Adding a test-helper class TestCoroutineContext. #297
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
Conversation
I've reviewed implementation, rebased it onto the latest
The reason that I have not yet merged it into
I suggest to have some kind of "testing DSL" around this context implementation, so that tests can be written like this:
If has any unhandled exception, then the test should fail. For expected exception something like this looks reasonable:
I would suggest that any any exception recorded by |
Good points. Ad 1) I think you are right. The 'virtual time' can be a plain variable. I will change that. Ad 2) I based handling exceptions on the way the RxJava2's |
I think that Rx approach is error-prone. However, in my own test I sometimes write test where I expect to have some unhandled exception and want to examine them. We can support this use-case. I propose something like:
The way |
Also, note that normally all descendants of |
Thank you @elizarov |
@elizarov I made the changes on the |
@streetsofboston I've fetched the code your from your Now, I see you've added |
@elizarov Thank you for the review. I'll push the changes to this branch and PR. About the three extension functions: I'll remove them from the |
82eb9e8
to
289f3ba
Compare
289f3ba
to
afb3dea
Compare
@elizarov I added the suggested changes to this branch and PR. |
Thanks. Merged. |
The purpose of this PR is to add a component that allows unit-tests that exercise code with Coroutines with delays and timeouts to run as fast as possible in near zero time.
Running a unit-test for code that contains one or more Coroutine with delays or timeouts requires the unit-test to wait and delay until the delays and timeouts of the various Coroutines come to pass.
This can cause unit-tests to take too much time to finish.
The addition of a new Coroutine context, called
TestCoroutineContext
alleviates this problem much like aTestScheduler
does for RxJava2. When Coroutines use an instance ofTestCoroutineContext
, delays and timeouts can be fast-forwarded by calling its methods such asadvanceTimeBy
andadvanceTimeTo
and this will speed up the execution of these Coroutines.