Skip to content

Commit ee8b977

Browse files
Add Dispatch Tracking
1 parent 6249905 commit ee8b977

File tree

12 files changed

+69
-15
lines changed

12 files changed

+69
-15
lines changed

workflow-runtime/api/workflow-runtime.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public abstract interface class com/squareup/workflow1/WorkflowInterceptor$Workf
123123
public abstract fun getParent ()Lcom/squareup/workflow1/WorkflowInterceptor$WorkflowSession;
124124
public abstract fun getRenderKey ()Ljava/lang/String;
125125
public abstract fun getRuntimeConfig ()Ljava/util/Set;
126+
public abstract fun getRuntimeContext ()Lkotlin/coroutines/CoroutineContext;
126127
public abstract fun getSessionId ()J
127128
public abstract fun getWorkflowTracer ()Lcom/squareup/workflow1/WorkflowTracer;
128129
public abstract fun isRootWorkflow ()Z

workflow-runtime/src/commonMain/kotlin/com/squareup/workflow1/WorkflowInterceptor.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ public interface WorkflowInterceptor {
223223
/** The [RuntimeConfig] of the runtime this session is executing in. */
224224
public val runtimeConfig: RuntimeConfig
225225

226+
/** The [CoroutineContext] of the runtime this session is executing in. */
227+
public val runtimeContext: CoroutineContext
228+
226229
/** The optional [WorkflowTracer] of the runtime this session is executing in. */
227230
public val workflowTracer: WorkflowTracer?
228231
}

workflow-runtime/src/commonMain/kotlin/com/squareup/workflow1/internal/WorkflowNode.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ internal class WorkflowNode<PropsT, StateT, OutputT, RenderingT>(
7171
*/
7272
override val coroutineContext = baseContext + Job(baseContext[Job]) + CoroutineName(id.toString())
7373

74+
override val runtimeContext: CoroutineContext
75+
get() = coroutineContext
76+
7477
// WorkflowInstance properties
7578
override val identifier: WorkflowIdentifier get() = id.identifier
7679
override val renderKey: String get() = id.name

workflow-runtime/src/commonTest/kotlin/com/squareup/workflow1/SimpleLoggingWorkflowInterceptorTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.squareup.workflow1
33
import com.squareup.workflow1.WorkflowInterceptor.WorkflowSession
44
import kotlinx.coroutines.CoroutineScope
55
import kotlinx.coroutines.cancel
6+
import kotlin.coroutines.CoroutineContext
67
import kotlin.coroutines.EmptyCoroutineContext
78
import kotlin.reflect.KType
89
import kotlin.reflect.typeOf
@@ -90,6 +91,7 @@ internal class SimpleLoggingWorkflowInterceptorTest {
9091
override val parent: WorkflowSession? get() = null
9192
override val runtimeConfig: RuntimeConfig = RuntimeConfigOptions.DEFAULT_CONFIG
9293
override val workflowTracer: WorkflowTracer? = null
94+
override val runtimeContext: CoroutineContext = EmptyCoroutineContext
9395
}
9496

9597
private object FakeRenderContext : BaseRenderContext<Unit, Unit, Nothing> {

workflow-runtime/src/commonTest/kotlin/com/squareup/workflow1/WorkflowInterceptorTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ internal class WorkflowInterceptorTest {
183183
override val parent: WorkflowSession? = null
184184
override val runtimeConfig: RuntimeConfig = RuntimeConfigOptions.DEFAULT_CONFIG
185185
override val workflowTracer: WorkflowTracer? = null
186+
override val runtimeContext: CoroutineContext = EmptyCoroutineContext
186187
}
187188

188189
private object TestWorkflow : StatefulWorkflow<String, String, String, String>() {

workflow-runtime/src/commonTest/kotlin/com/squareup/workflow1/internal/ChainedWorkflowInterceptorTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import kotlinx.coroutines.Job
2424
import kotlinx.coroutines.launch
2525
import kotlinx.coroutines.test.advanceUntilIdle
2626
import kotlinx.coroutines.test.runTest
27+
import kotlin.coroutines.CoroutineContext
2728
import kotlin.coroutines.EmptyCoroutineContext
2829
import kotlin.reflect.KType
2930
import kotlin.test.Test
@@ -359,5 +360,6 @@ internal class ChainedWorkflowInterceptorTest {
359360
override val parent: WorkflowSession? = null
360361
override val runtimeConfig: RuntimeConfig = RuntimeConfigOptions.DEFAULT_CONFIG
361362
override val workflowTracer: WorkflowTracer? = null
363+
override val runtimeContext: CoroutineContext = EmptyCoroutineContext
362364
}
363365
}

workflow-runtime/src/commonTest/kotlin/com/squareup/workflow1/internal/WorkflowNodeTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import kotlinx.coroutines.suspendCancellableCoroutine
4242
import kotlinx.coroutines.test.runTest
4343
import kotlinx.coroutines.withTimeout
4444
import kotlin.coroutines.CoroutineContext
45+
import kotlin.coroutines.EmptyCoroutineContext
4546
import kotlin.reflect.typeOf
4647
import kotlin.test.AfterTest
4748
import kotlin.test.Test
@@ -1418,5 +1419,6 @@ internal class WorkflowNodeTest {
14181419
override val parent: WorkflowSession? = null
14191420
override val runtimeConfig: RuntimeConfig = RuntimeConfigOptions.DEFAULT_CONFIG
14201421
override val workflowTracer: WorkflowTracer? = null
1422+
override val runtimeContext: CoroutineContext = EmptyCoroutineContext
14211423
}
14221424
}

workflow-tracing-papa/src/test/java/com/squareup/workflow1/tracing/papa/WorkflowPapaTracerTest.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import com.squareup.workflow1.tracing.RuntimeTraceContext
1616
import com.squareup.workflow1.tracing.RuntimeUpdateLogLine
1717
import com.squareup.workflow1.tracing.WorkflowSessionInfo
1818
import kotlinx.coroutines.test.TestScope
19+
import kotlin.coroutines.CoroutineContext
20+
import kotlin.coroutines.EmptyCoroutineContext
1921
import kotlin.test.Test
2022
import kotlin.test.assertEquals
2123
import kotlin.test.assertNotNull
@@ -246,7 +248,8 @@ internal class WorkflowPapaTracerTest {
246248
private val workflow: TestWorkflow,
247249
override val sessionId: Long,
248250
override val renderKey: String,
249-
override val parent: WorkflowSession?
251+
override val parent: WorkflowSession?,
252+
override val runtimeContext: CoroutineContext = EmptyCoroutineContext
250253
) : WorkflowSession {
251254
override val identifier = workflow.identifier
252255
override val runtimeConfig = TestRuntimeConfig()

workflow-tracing/api/workflow-tracing.api

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ public final class com/squareup/workflow1/tracing/ChainedWorkflowRuntimeTracerKt
2525
}
2626

2727
public final class com/squareup/workflow1/tracing/ConfigSnapshot {
28-
public fun <init> (Ljava/util/Set;)V
28+
public fun <init> (Ljava/util/Set;Lkotlinx/coroutines/CoroutineDispatcher;)V
29+
public synthetic fun <init> (Ljava/util/Set;Lkotlinx/coroutines/CoroutineDispatcher;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
2930
public final fun getConfigAsString ()Ljava/lang/String;
31+
public final fun getRuntimeDispatch ()Lkotlinx/coroutines/CoroutineDispatcher;
3032
public final fun getShortConfigAsString ()Ljava/lang/String;
3133
}
3234

workflow-tracing/src/main/java/com/squareup/workflow1/tracing/ConfigSnapshot.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@ import com.squareup.workflow1.RuntimeConfigOptions.RENDER_ONLY_WHEN_STATE_CHANGE
88
import com.squareup.workflow1.RuntimeConfigOptions.STABLE_EVENT_HANDLERS
99
import com.squareup.workflow1.RuntimeConfigOptions.WORK_STEALING_DISPATCHER
1010
import com.squareup.workflow1.WorkflowExperimentalRuntime
11+
import kotlinx.coroutines.CoroutineDispatcher
1112

1213
/**
1314
* Snapshot of the current [RuntimeConfig]
1415
*/
1516
@OptIn(WorkflowExperimentalRuntime::class)
16-
public class ConfigSnapshot(config: RuntimeConfig) {
17-
public val configAsString: String = config.toString()
17+
public class ConfigSnapshot(
18+
config: RuntimeConfig,
19+
public val runtimeDispatch: CoroutineDispatcher? = null
20+
) {
21+
22+
public val configAsString: String = "$config, $runtimeDispatch"
1823

1924
public val shortConfigAsString: String by lazy {
2025
buildString {
@@ -40,6 +45,7 @@ public class ConfigSnapshot(config: RuntimeConfig) {
4045
if (config.isEmpty()) {
4146
append("Base, ")
4247
}
48+
append("Dispatch: ${runtimeDispatch?.toString()?.take(6)}")
4349
}
4450
}
4551
}

0 commit comments

Comments
 (0)