Skip to content

Commit 4a0b76f

Browse files
Add Tests for WorkflowTracer
1 parent b2e9f1e commit 4a0b76f

File tree

7 files changed

+201
-100
lines changed

7 files changed

+201
-100
lines changed

workflow-core/api/workflow-core.api

-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,6 @@ public abstract interface class com/squareup/workflow1/WorkflowTracer {
317317

318318
public final class com/squareup/workflow1/WorkflowTracerKt {
319319
public static final fun trace (Lcom/squareup/workflow1/WorkflowTracer;Ljava/lang/String;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;
320-
public static final fun trace (Lcom/squareup/workflow1/WorkflowTracer;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;
321320
}
322321

323322
public final class com/squareup/workflow1/Workflows {

workflow-core/src/commonMain/kotlin/com/squareup/workflow1/WorkerWorkflow.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ internal class WorkerWorkflow<OutputT>(
3636
ImpostorWorkflow {
3737

3838
override val realIdentifier: WorkflowIdentifier =
39-
workflowTracer.trace("ComputeRealIdentifier" ) {
39+
workflowTracer.trace("ComputeRealIdentifier") {
4040
unsnapshottableIdentifier(workerType)
4141
}
4242

workflow-core/src/commonMain/kotlin/com/squareup/workflow1/WorkflowTracer.kt

+15-20
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,22 @@ public interface WorkflowTracer {
1010
}
1111

1212
/**
13-
* Convenience function to wrap [block] with a trace span as defined by [WorkflowTracer].
14-
* Only calls [label] if there is an active [WorkflowTracer] use this for any label other than
15-
* a constant.
13+
* Convenience function to wrap [block] with a trace span as defined by [WorkflowTracer]. This
14+
* wraps very frequently evaluated code and we should only use constants for [label], with no
15+
* interpolation.
1616
*/
17-
public inline fun <T> WorkflowTracer?.trace(label: () -> String, block: () -> T): T {
18-
val optimizedLabel = if (this !== null) {
19-
label()
17+
public inline fun <T> WorkflowTracer?.trace(
18+
label: String,
19+
block: () -> T
20+
): T {
21+
return if (this == null) {
22+
block()
2023
} else {
21-
""
22-
}
23-
return trace(optimizedLabel, block)
24-
}
25-
26-
/**
27-
* Convenience function to wrap [block] with a trace span as defined by [WorkflowTracer].
28-
*/
29-
public inline fun <T> WorkflowTracer?.trace(label: String, block: () -> T): T {
30-
this?.beginSection(label)
31-
try {
32-
return block()
33-
} finally {
34-
this?.endSection()
24+
beginSection(label)
25+
try {
26+
return block()
27+
} finally {
28+
endSection()
29+
}
3530
}
3631
}

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

+9-10
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,15 @@ public fun <PropsT, OutputT, RenderingT> renderWorkflowIn(
115115
): StateFlow<RenderingAndSnapshot<RenderingT>> {
116116
val chainedInterceptor = interceptors.chained()
117117

118-
val runner =
119-
WorkflowRunner(
120-
scope,
121-
workflow,
122-
props,
123-
initialSnapshot,
124-
chainedInterceptor,
125-
runtimeConfig,
126-
workflowTracer
127-
)
118+
val runner = WorkflowRunner(
119+
scope,
120+
workflow,
121+
props,
122+
initialSnapshot,
123+
chainedInterceptor,
124+
runtimeConfig,
125+
workflowTracer
126+
)
128127

129128
// Rendering is synchronous, so we can run the first render pass before launching the runtime
130129
// coroutine to calculate the initial rendering.

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ internal class SubtreeManager<PropsT, StateT, OutputT>(
126126
// Prevent duplicate workflows with the same key.
127127
workflowTracer.trace("CheckingUniqueMatches") {
128128
children.forEachStaging {
129-
require(!(it.matches(child, key))) {
129+
require(!(it.matches(child, key, workflowTracer))) {
130130
"Expected keys to be unique for ${child.identifier}: key=\"$key\""
131131
}
132132
}
@@ -136,7 +136,7 @@ internal class SubtreeManager<PropsT, StateT, OutputT>(
136136
val stagedChild =
137137
workflowTracer.trace("RetainingChildren") {
138138
children.retainOrCreate(
139-
predicate = { it.matches(child, key) },
139+
predicate = { it.matches(child, key, workflowTracer) },
140140
create = { createChildNode(child, props, key, handler) }
141141
)
142142
}

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package com.squareup.workflow1.internal
33
import com.squareup.workflow1.StatefulWorkflow
44
import com.squareup.workflow1.Workflow
55
import com.squareup.workflow1.WorkflowAction
6+
import com.squareup.workflow1.WorkflowTracer
67
import com.squareup.workflow1.internal.InlineLinkedList.InlineListNode
8+
import com.squareup.workflow1.trace
79

810
/**
911
* Representation of a child workflow that has been rendered by another workflow.
@@ -32,8 +34,9 @@ internal class WorkflowChildNode<
3234
*/
3335
fun matches(
3436
otherWorkflow: Workflow<*, *, *>,
35-
key: String
36-
): Boolean = id.matches(otherWorkflow, key)
37+
key: String,
38+
workflowTracer: WorkflowTracer?
39+
): Boolean = workflowTracer.trace("matches") { id.matches(otherWorkflow, key) }
3740

3841
/**
3942
* Updates the handler function that will be invoked by [acceptChildOutput].

0 commit comments

Comments
 (0)