Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
package com.squareup.workflow1.internal

internal expect class Lock()
public expect class Lock()

internal expect inline fun <R> Lock.withLock(block: () -> R): R
public expect inline fun <R> Lock.withLock(block: () -> R): R
Comment on lines -3 to +5
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Undo this

Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
internal open class WorkStealingDispatcher protected constructor(
public open class WorkStealingDispatcher protected constructor(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Undo this

private val delegateInterceptor: ContinuationInterceptor,
lock: Lock?,
queue: LinkedHashSet<DelegateDispatchedContinuation>?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.squareup.workflow1.internal

internal actual typealias Lock = Any
public actual typealias Lock = Any

internal actual inline fun <R> Lock.withLock(block: () -> R): R = synchronized(this, block)
public actual inline fun <R> Lock.withLock(block: () -> R): R = synchronized(this, block)
Comment on lines -3 to +5
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Undo this

Loading