- Notifications
You must be signed in to change notification settings - Fork112
WIP Android-specific tests for WorkStealingDispatcher.#1371
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package com.squareup.workflow1.android | ||
| import com.squareup.workflow1.internal.WorkStealingDispatcher | ||
| import kotlinx.coroutines.CoroutineDispatcher | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.test.runTest | ||
| import kotlinx.coroutines.withContext | ||
| import kotlinx.coroutines.yield | ||
| import org.junit.Test | ||
| import kotlin.test.assertEquals | ||
| class WorkStealingDispatcherAndroidDispatchersTest { | ||
| @Test fun dispatch_runsImmediatelyWhenDelegateIsMainImmediate_onMainThread() = runTest { | ||
| val dispatcher = WorkStealingDispatcher(Dispatchers.Main.immediate) | ||
| runOnMainThread { | ||
| expect(0) | ||
| dispatcher.dispatch { | ||
| expect(1) | ||
| } | ||
| expect(2) | ||
| } | ||
| } | ||
| @Test fun dispatchNested_enqueuesWhenDelegateIsMainImmediate_onMainThread() = runTest { | ||
| val dispatcher = WorkStealingDispatcher(Dispatchers.Main.immediate) | ||
| runOnMainThread { | ||
| expect(0) | ||
| dispatcher.dispatch { | ||
| expect(1) | ||
| // This dispatch should get enqueued to Unconfined's threadlocal queue. | ||
| dispatcher.dispatch { | ||
| expect(3) | ||
| } | ||
| expect(2) | ||
| } | ||
| expect(4) | ||
| } | ||
| } | ||
| @Test fun dispatch_queues_whenDelegateisMain_onMainThread() = runTest { | ||
| val dispatcher = WorkStealingDispatcher(Dispatchers.Main) | ||
| runOnMainThread { | ||
| expect(0) | ||
| dispatcher.dispatch { | ||
| expect(2) | ||
| } | ||
| expect(1) | ||
| yield() | ||
| expect(3) | ||
| } | ||
| } | ||
| @Test fun dispatch_runsMultipleTasksInOrder_whenDelegateIsMain_onMainThread() = runTest { | ||
| val dispatcher = WorkStealingDispatcher(Dispatchers.Main) | ||
| runOnMainThread { | ||
| expect(0) | ||
| dispatcher.dispatch { | ||
| expect(3) | ||
| } | ||
| expect(1) | ||
| dispatcher.dispatch { | ||
| expect(4) | ||
| } | ||
| expect(2) | ||
| yield() | ||
| expect(5) | ||
| } | ||
| } | ||
| private suspend fun runOnMainThread(block: suspend CoroutineScope.() -> Unit) { | ||
| withContext(Dispatchers.Main, block) | ||
| } | ||
| private fun CoroutineDispatcher.dispatch(block: () -> Unit) { | ||
| dispatch(this) { block() } | ||
| } | ||
| private val expectLock = Any() | ||
| private var current = 0 | ||
| private fun expect(expected: Int) { | ||
| synchronized(expectLock) { | ||
| assertEquals(expected, current, "Expected to be at step $expected but was at $current") | ||
| current++ | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| packagecom.squareup.workflow1.internal | ||
| publicexpectclassLock() | ||
| publicexpectinlinefun <R> Lock.withLock(block: ()->R):R | ||
Comment on lines -3 to +5 CollaboratorAuthor 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.
| ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -38,7 +38,7 @@ import kotlin.coroutines.resume | ||
| * delegate scheduling behavior to. This can either be a confined or unconfined dispatcher, and its | ||
| * behavior will be preserved transparently. | ||
| */ | ||
| public open class WorkStealingDispatcher protected constructor( | ||
CollaboratorAuthor 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.
| ||
| private val delegateInterceptor: ContinuationInterceptor, | ||
| lock: Lock?, | ||
| queue: LinkedHashSet<DelegateDispatchedContinuation>? | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| packagecom.squareup.workflow1.internal | ||
| publicactualtypealiasLock=Any | ||
| publicactualinlinefun <R> Lock.withLock(block: ()->R):R= synchronized(this, block) | ||
Comment on lines -3 to +5 CollaboratorAuthor 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.
| ||
Uh oh!
There was an error while loading.Please reload this page.