-
Notifications
You must be signed in to change notification settings - Fork 101
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
910: Integrate WorkerTester with TestScope #918
Conversation
f927ad7
to
d1c2fac
Compare
* This can be used to advance virtual time for the [CoroutineDispatcher] that the the Worker's | ||
* flow is flowing on. | ||
*/ | ||
public val testCoroutineScheduler: TestCoroutineScheduler |
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.
This is added to control virtual time on the scope in which the Turbine is launched and thus where the Worker's upstream flow is producing values.
@@ -31,7 +39,7 @@ public interface WorkerTester<T> { | |||
/** | |||
* Suspends until the worker emits an output or finishes. | |||
* | |||
* Throws an [AssertionError] if an output was emitted. | |||
* Throws an [AssertionError] if an output was emitted or the Worker has an error. |
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.
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.
Yep, I agree.
if (!channel.isEmpty && !channel.isClosedForReceive) { | ||
try { | ||
expectNoEvents() | ||
} catch (e: AssertionError) { | ||
throw AssertionError("Expected no output to have been emitted.") | ||
} | ||
} | ||
|
||
override suspend fun assertFinished() { |
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.
This mapping is the most complicated in order to get the same error messages in the AssertionError as before.
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.
Seems worth saying so in a comment, e.g. "Complicated because these messages predate our Turbine integration and we wanted to keep them stable."
if (!channel.isEmpty && !channel.isClosedForReceive) { | ||
try { | ||
expectNoEvents() | ||
} catch (e: AssertionError) { | ||
throw AssertionError("Expected no output to have been emitted.") | ||
} | ||
} | ||
|
||
override suspend fun assertFinished() { |
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.
Seems worth saying so in a comment, e.g. "Complicated because these messages predate our Turbine integration and we wanted to keep them stable."
|
||
worker.test { | ||
assertNoOutput() | ||
assertNotFinished() | ||
|
||
testDispatcher.scheduler.advanceTimeBy(999) | ||
testDispatcher.scheduler.runCurrent() | ||
testCoroutineScheduler.advanceTimeBy(999) |
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.
Nice. Great argument in favor of the API change.
**Note this contains minor BREAKING changes to the WorkerTester API** Closes #910
d1c2fac
to
00b3789
Compare
Closes #910. We do not allow a
CoroutineContext
/CoroutineDispatcher
to be passed in, instead favouring keeping the API forWorkerTester.test
self contained. Within the test though you can now access theTestCoroutineScheduler
to control virtual time.We drive the flow backing the Worker with a Turbine rather than our own handle around a channel.