-
Notifications
You must be signed in to change notification settings - Fork 109
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
Internal cleanups #318
Merged
Merged
Internal cleanups #318
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,7 @@ public interface TurbineContext : CoroutineScope { | |
public interface TurbineTestContext<T> : TurbineContext, ReceiveTurbine<T> | ||
|
||
internal class TurbineTestContextImpl<T>( | ||
turbine: Turbine<T>, | ||
turbine: ReceiveTurbine<T>, | ||
turbineContext: CoroutineContext, | ||
) : TurbineContext by TurbineContextImpl(turbineContext), ReceiveTurbine<T> by turbine, TurbineTestContext<T> | ||
|
||
|
@@ -180,7 +180,7 @@ public fun <T> Flow<T>.testIn( | |
return testInInternal(this, timeout, scope, name) | ||
} | ||
|
||
private fun <T> testInInternal(flow: Flow<T>, timeout: Duration?, scope: CoroutineScope, name: String?): Turbine<T> { | ||
private fun <T> testInInternal(flow: Flow<T>, timeout: Duration?, scope: CoroutineScope, name: String?): ReceiveTurbine<T> { | ||
if (timeout != null) { | ||
// Eager check to throw early rather than in a subsequent 'await' call. | ||
checkTimeout(timeout) | ||
|
@@ -204,27 +204,15 @@ private fun <T> testInInternal(flow: Flow<T>, timeout: Duration?, scope: Corouti | |
return turbine | ||
} | ||
|
||
private fun <T> Flow<T>.collectTurbineIn(scope: CoroutineScope, timeout: Duration?, name: String?): Turbine<T> { | ||
lateinit var channel: Channel<T> | ||
|
||
private fun <T> Flow<T>.collectTurbineIn(scope: CoroutineScope, timeout: Duration?, name: String?): ReceiveTurbine<T> { | ||
// Use test-specific unconfined if test scheduler is in use to inherit its virtual time. | ||
@OptIn(ExperimentalCoroutinesApi::class) // UnconfinedTestDispatcher is still experimental. | ||
val unconfined = scope.coroutineContext[TestCoroutineScheduler] | ||
?.let(::UnconfinedTestDispatcher) | ||
?: Unconfined | ||
|
||
val job = scope.launch(unconfined, start = UNDISPATCHED) { | ||
channel = collectIntoChannel(this) | ||
} | ||
|
||
return ChannelTurbine(channel, job, timeout, name).also { | ||
scope.reportTurbine(it) | ||
} | ||
} | ||
|
||
private fun <T> Flow<T>.collectIntoChannel(scope: CoroutineScope): Channel<T> { | ||
val output = Channel<T>(UNLIMITED) | ||
val job = scope.launch(start = UNDISPATCHED) { | ||
val job = scope.launch(unconfined, start = UNDISPATCHED) { | ||
try { | ||
collect { output.trySend(it) } | ||
output.close() | ||
|
@@ -233,15 +221,7 @@ private fun <T> Flow<T>.collectIntoChannel(scope: CoroutineScope): Channel<T> { | |
} | ||
} | ||
|
||
return object : Channel<T> by output { | ||
override fun cancel(cause: CancellationException?) { | ||
job.cancel() | ||
output.close(cause) | ||
} | ||
|
||
override fun close(cause: Throwable?): Boolean { | ||
job.cancel() | ||
return output.close(cause) | ||
} | ||
Comment on lines
-236
to
-245
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was wrapping to mix-in behavior only to be re-wrapped in |
||
return ChannelTurbine(output, job, timeout, name).also { | ||
scope.reportTurbine(it) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Now that tests aren't using
collectIntoChannel
, we can inline it and remove this pointless double launch.