-
Notifications
You must be signed in to change notification settings - Fork 14
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
Track batch execution via inspector. #361
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1732,6 +1732,8 @@ class FormulaRuntimeTest(val runtime: TestableRuntime, val name: String) { | |
|
||
|
||
@Test fun `batched formulas are executed as part of a single evaluation`() { | ||
|
||
|
||
val childFormulaCount = 100 | ||
|
||
val batchScheduler = StateBatchScheduler() | ||
|
@@ -1864,5 +1866,64 @@ class FormulaRuntimeTest(val runtime: TestableRuntime, val name: String) { | |
|
||
assertThat(batchScheduler.batchesOutsideOfScope).hasSize(0) | ||
} | ||
|
||
@Test fun `batched events notify the inspector of start and stop`() { | ||
val globalInspector = TestInspector() | ||
FormulaPlugins.setPlugin(object : Plugin { | ||
override fun inspector(type: KClass<*>): Inspector { | ||
return globalInspector | ||
} | ||
}) | ||
val localInspector = TestInspector() | ||
|
||
val childFormulaCount = 3 | ||
|
||
val batchScheduler = StateBatchScheduler() | ||
val relay = runtime.newRelay() | ||
|
||
val childFormula = IncrementActionFormula( | ||
incrementRelay = relay, | ||
executionType = Transition.Batched(batchScheduler) | ||
) | ||
|
||
val rootFormula = object : StatelessFormula<Unit, Int>() { | ||
override fun Snapshot<Unit, Unit>.evaluate(): Evaluation<Int> { | ||
val sum = (0 until childFormulaCount).sumOf { id -> | ||
context.key(id) { | ||
context.child(childFormula, input) | ||
} | ||
} | ||
return Evaluation( | ||
output = sum, | ||
) | ||
} | ||
} | ||
|
||
val subject = runtime.test(rootFormula, Unit, localInspector) | ||
batchScheduler.performUpdate { relay.triggerEvent() } | ||
|
||
// Inspect! | ||
for (inspector in listOf(localInspector, globalInspector)) { | ||
// Filtering out logs before "batch-started" | ||
val events = inspector.events.dropWhile { !it.contains("batch-started") } | ||
assertThat(events).containsExactly( | ||
"batch-started: 3 updates", | ||
"state-changed: com.instacart.formula.types.IncrementActionFormula", | ||
"state-changed: com.instacart.formula.types.IncrementActionFormula", | ||
"state-changed: com.instacart.formula.types.IncrementActionFormula", | ||
"formula-run-started", | ||
"evaluate-started: null", | ||
"evaluate-started: com.instacart.formula.types.IncrementActionFormula", | ||
"evaluate-finished: com.instacart.formula.types.IncrementActionFormula", | ||
"evaluate-started: com.instacart.formula.types.IncrementActionFormula", | ||
"evaluate-finished: com.instacart.formula.types.IncrementActionFormula", | ||
"evaluate-started: com.instacart.formula.types.IncrementActionFormula", | ||
"evaluate-finished: com.instacart.formula.types.IncrementActionFormula", | ||
"evaluate-finished: null", | ||
"formula-run-finished", | ||
"batch-finished", | ||
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. Same here with |
||
).inOrder() | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,27 @@ | ||
package com.instacart.formula.types | ||
|
||
import com.instacart.formula.Evaluation | ||
import com.instacart.formula.Formula | ||
import com.instacart.formula.Snapshot | ||
import com.instacart.formula.StatelessFormula | ||
import com.instacart.formula.Transition | ||
import com.instacart.formula.test.Relay | ||
|
||
class IncrementActionFormula( | ||
private val incrementRelay: Relay, | ||
private val executionType: Transition.ExecutionType? = null, | ||
) : StatelessFormula<Unit, Int>() { | ||
) : Formula<Unit, Int, Int>() { | ||
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. Does it need to be stateful now? 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. Yeah, we increment integer state as part of the batched event. |
||
override fun initialState(input: Unit): Int { | ||
return 0 | ||
} | ||
|
||
private val actionInput = ActionDelegateFormula.Input( | ||
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. Simplifying this test class, no need to delegate to another formula for this. |
||
delegateAction = { | ||
if (executionType == null) { | ||
incrementRelay.action().onEvent { | ||
transition(state + 1) | ||
} | ||
} else { | ||
override fun Snapshot<Unit, Int>.evaluate(): Evaluation<Int> { | ||
return Evaluation( | ||
output = state, | ||
actions = context.actions { | ||
incrementRelay.action().onEventWithExecutionType(executionType) { | ||
transition(state + 1) | ||
} | ||
} | ||
}, | ||
onAction = {} | ||
) | ||
|
||
private val actionFormula = ActionDelegateFormula() | ||
|
||
override fun Snapshot<Unit, Unit>.evaluate(): Evaluation<Int> { | ||
return Evaluation( | ||
output = context.child(actionFormula, actionInput) | ||
) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I wonder if we should pass
batchStartedEventPrefix
asTestInspector
field - to decipher this hidden"batch-started"
dependency?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While for this use case, it's a small lift, generally, this pattern would be hard to manage when verifying multiple events such as
evaluate-started,
state-change, etc. In my opinion, it's just easier to have a single
TestInspector` with all the cases managed there (with no customizations across tests).