Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Draft
zach-klippenstein wants to merge1 commit intomain
base:main
Choose a base branch
Loading
fromzachklipp/wsd-android-tests
Draft
Show file tree
Hide file tree
Changes fromall 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 numberDiff line numberDiff 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 numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
packagecom.squareup.workflow1.internal

internalexpectclassLock()
publicexpectclassLock()

internalexpectinlinefun <R> Lock.withLock(block: ()->R):R
publicexpectinlinefun <R> Lock.withLock(block: ()->R):R
Comment on lines -3 to +5
Copy link
CollaboratorAuthor

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 numberDiff line numberDiff 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
CollaboratorAuthor

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 numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
packagecom.squareup.workflow1.internal

internalactualtypealiasLock=Any
publicactualtypealiasLock=Any

internalactualinlinefun <R> Lock.withLock(block: ()->R):R= synchronized(this, block)
publicactualinlinefun <R> Lock.withLock(block: ()->R):R= synchronized(this, block)
Comment on lines -3 to +5
Copy link
CollaboratorAuthor

Choose a reason for hiding this comment

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

  • Undo this

Loading

[8]ページ先頭

©2009-2025 Movatter.jp