Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { D2 } from '../src/d2.js'
3import { DifferenceStreamWriter, UnaryOperator } from '../src/graph.js'
4import { MultiSet } from '../src/multiset.js'
5
6/**
7 * # Does a graph update execute disconnected idle operators?
8 *
9 * D2 operators consume queued input. A direct input on one branch must not run
10 * operators on disconnected branches. The independent reference is a per-branch
11 * input ledger: a branch with no messages has zero operator work, while a branch
12 * with one message forwards that message once. The production driver constructs
13 * a finalized D2 graph with the active branch before, among, or after idle
14 * branches. A second case reverses a connected pair’s registration order so
15 * the graph must make another pass. At each graph.run() checkpoint it compares
16 * exact output messages and operator invocations. This bounded oracle does not
17 * measure elapsed time or prove a general graph complexity bound.
18 */
19class CountingOperator extends UnaryOperator<number> {
20 calls = 0
21
22 run(): void {
23 this.calls++
24 for (const message of this.inputMessages()) {
25 this.output.sendData(message)
26 }
27 }
28}
29
30function addBranch(graph: D2) {
31 const input = graph.newInput<number>()
32 const output = new DifferenceStreamWriter<number>()
33 const operator = new CountingOperator(
34 graph.getNextOperatorId(),
35 input.connectReader(),
36 output,
37 )
38 graph.addOperator(operator)
39 return { input, output: output.newReader(), operator }
40}
41
42describe(`D2 operator work oracle`, () => {
43 it(`rejects an unfinalized graph with no pending input`, () => {
44 expect(() => new D2().run()).toThrow(`Graph not finalized`)
45 })
46
47 it.each([0, 32, 64])(
48 `runs only branches with queued input when the active branch is at position %i`,
49 (activePosition) => {
50 const graph = new D2()
51 const branches = Array.from({ length: 65 }, () => addBranch(graph))
52 const active = branches[activePosition]!
53 const expectedCalls = Array<number>(branches.length).fill(0)
54 graph.finalize()
55
56 active.input.sendData(new MultiSet([[7, 1]]))
57 graph.run()
58 expect(
59 active.output.drain().map((message) => message.getInner()),
60 ).toEqual([[[7, 1]]])
61 expectedCalls[activePosition] = 1
62 expect(branches.map((branch) => branch.operator.calls)).toEqual(
63 expectedCalls,
64 )
65
66 const secondPosition = (activePosition + 1) % branches.length
67 const second = branches[secondPosition]!
68 second.input.sendData(new MultiSet([[9, 1]]))
69 graph.run()
70 expect(
71 second.output.drain().map((message) => message.getInner()),
72 ).toEqual([[[9, 1]]])
73 expectedCalls[secondPosition] = 1
74 expect(branches.map((branch) => branch.operator.calls)).toEqual(
75 expectedCalls,
76 )
77 },
78 )
79
80 it(`drains work that a later registered operator sends to an earlier one`, () => {
81 const graph = new D2()
82 const middle = new DifferenceStreamWriter<number>()
83 const output = new DifferenceStreamWriter<number>()
84 const outputReader = output.newReader()
85 const downstream = new CountingOperator(
86 graph.getNextOperatorId(),
87 middle.newReader(),
88 output,
89 )
90 graph.addOperator(downstream)
91 const input = graph.newInput<number>()
92 const upstream = new CountingOperator(
93 graph.getNextOperatorId(),
94 input.connectReader(),
95 middle,
96 )
97 graph.addOperator(upstream)
98 graph.finalize()
99
100 input.sendData(new MultiSet([[3, 1]]))
101 graph.run()
102 expect(outputReader.drain().map((message) => message.getInner())).toEqual([
103 [[3, 1]],
104 ])
105 expect([upstream.calls, downstream.calls]).toEqual([1, 1])
106 })
107})