-
Notifications
You must be signed in to change notification settings - Fork 0
/
tdag.go
387 lines (341 loc) · 8.78 KB
/
tdag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package tdag
import (
"fmt"
"os"
"strings"
"sync"
"testing"
)
type TDag struct {
Nodes []*TNode
Edges []*TEdge
Ctx *TestContext
SetupFns []func(ctx *TestContext)
TearDownFns []func(ctx *TestContext)
BeforeEachFns []func(ctx *TestContext)
AfterEachFns []func(ctx *TestContext)
}
type TNode struct {
ID string
Fn TestFn
}
type TEdge struct {
Left *TNode
Right *TNode
}
type TestContext struct {
Store *TStore
T *testing.T
}
type TestFn func(ctx *TestContext)
func NewTDag(t *testing.T) *TDag {
return &TDag{
Ctx: &TestContext{
Store: NewStore(),
T: t,
},
Nodes: []*TNode{},
Edges: []*TEdge{},
SetupFns: []func(ctx *TestContext){},
TearDownFns: []func(ctx *TestContext){},
BeforeEachFns: []func(ctx *TestContext){},
AfterEachFns: []func(ctx *TestContext){},
}
}
func (d *TDag) AddNode(id string, fn TestFn) *TNode {
node := &TNode{
ID: id,
Fn: fn,
}
d.Nodes = append(d.Nodes, node)
return node
}
func (d *TDag) AddEdge(from string, to ...string) ([]*TEdge, error) {
var edges []*TEdge
fromNode := d.findNodeByID(from)
if fromNode == nil {
return nil, fmt.Errorf("node %s does not exist", from)
}
for _, targetID := range to {
targetNode := d.findNodeByID(targetID)
if targetNode == nil {
return nil, fmt.Errorf("node %s does not exist", targetID)
}
if d.createsCycle(fromNode, targetNode) {
return nil, fmt.Errorf("adding edge from %s to %s would create a cycle", from, targetID)
}
edge := &TEdge{
Left: fromNode,
Right: targetNode,
}
d.Edges = append(d.Edges, edge)
edges = append(edges, edge)
}
return edges, nil
}
func (d *TDag) Setup(fn func(ctx *TestContext)) {
d.SetupFns = append(d.SetupFns, fn)
}
func (d *TDag) TearDown(fn func(ctx *TestContext)) {
d.TearDownFns = append(d.TearDownFns, fn)
}
func (d *TDag) BeforeEach(fn func(ctx *TestContext)) {
d.BeforeEachFns = append(d.BeforeEachFns, fn)
}
func (d *TDag) AfterEach(fn func(ctx *TestContext)) {
d.AfterEachFns = append(d.AfterEachFns, fn)
}
func (d *TDag) findNodeByID(id string) *TNode {
for _, node := range d.Nodes {
if node.ID == id {
return node
}
}
return nil
}
// RunTests runs the tests in topological order.
//
// Arguments:
// - t: The testing object.
func (d *TDag) RunTests(t *testing.T) {
// Create dependency graph and track in-degree for each node.
inDegree := make(map[string]int)
outEdges := make(map[string][]*TNode)
// Initialize in-degree counts and build adjacency list.
for _, node := range d.Nodes {
inDegree[node.ID] = 0
}
// Build adjacency list.
for _, edge := range d.Edges {
inDegree[edge.Right.ID]++
outEdges[edge.Left.ID] = append(outEdges[edge.Left.ID], edge.Right)
}
// Keep track of completed nodes.
completed := make(map[string]bool)
var completedMux sync.Mutex
// Run tests in topological order.
for {
// Find nodes with no dependencies.
var available []*TNode
for _, node := range d.Nodes {
completedMux.Lock()
if !completed[node.ID] && inDegree[node.ID] == 0 {
available = append(available, node)
}
completedMux.Unlock()
}
// If no nodes are available but we haven't processed all nodes, we have a cycle.
if len(available) == 0 {
var remaining []string
completedMux.Lock()
for _, node := range d.Nodes {
if !completed[node.ID] {
remaining = append(remaining, node.ID)
}
}
completedMux.Unlock()
if len(remaining) > 0 {
t.Fatalf("Dependency cycle detected. Remaining nodes: %v", remaining)
}
break
}
// Run available tests
var wg sync.WaitGroup
for _, node := range available {
wg.Add(1)
go func(n *TNode) {
defer wg.Done()
t.Run(n.ID, func(t *testing.T) {
// Run the test passed in.
for _, fn := range d.BeforeEachFns {
fn(d.Ctx)
}
n.Fn(d.Ctx)
for _, fn := range d.AfterEachFns {
fn(d.Ctx)
}
// Mark as completed and update dependencies.
completedMux.Lock()
completed[n.ID] = true
for _, dependent := range outEdges[n.ID] {
inDegree[dependent.ID]--
}
completedMux.Unlock()
})
}(node)
}
wg.Wait()
}
// Run teardown functions.
for _, fn := range d.TearDownFns {
fn(d.Ctx)
}
}
// RunTo runs tests up to the given node.
//
// Arguments:
// - id: The ID of the node to run tests up to.
// - t: The testing object.
func (d *TDag) RunTo(id string, t *testing.T) {
// First verify the target node exists
targetNode := d.findNodeByID(id)
if targetNode == nil {
t.Fatalf("Node %s does not exist", id)
return
}
// Build a set of nodes we need to run (target and its dependencies).
requiredNodes := make(map[string]bool)
d.collectDependencies(id, requiredNodes)
// Create dependency graph and track in-degree for required nodes
inDegree := make(map[string]int)
outEdges := make(map[string][]*TNode)
// Initialize in-degree counts only for required nodes.
for _, node := range d.Nodes {
if requiredNodes[node.ID] {
inDegree[node.ID] = 0
}
}
// Build adjacency list only for required nodes.
for _, edge := range d.Edges {
if requiredNodes[edge.Left.ID] && requiredNodes[edge.Right.ID] {
inDegree[edge.Right.ID]++
outEdges[edge.Left.ID] = append(outEdges[edge.Left.ID], edge.Right)
}
}
// Keep track of completed nodes
completed := make(map[string]bool)
var completedMux sync.Mutex
// Run setup functions.
for _, fn := range d.SetupFns {
fn(d.Ctx)
}
// Run tests in topological order.
for {
// Find nodes with no dependencies.
var available []*TNode
for _, node := range d.Nodes {
// Only consider nodes that are required.
if !requiredNodes[node.ID] {
continue
}
completedMux.Lock()
if !completed[node.ID] && inDegree[node.ID] == 0 {
available = append(available, node)
}
completedMux.Unlock()
}
// If no nodes are available but we haven't processed all required nodes, we have a cycle.
if len(available) == 0 {
var remaining []string
completedMux.Lock()
for nodeID := range requiredNodes {
if !completed[nodeID] {
remaining = append(remaining, nodeID)
}
}
completedMux.Unlock()
if len(remaining) > 0 {
t.Fatalf("Dependency cycle detected. Remaining nodes: %v", remaining)
}
break
}
// Run available tests.
var wg sync.WaitGroup
for _, node := range available {
wg.Add(1)
go func(n *TNode) {
defer wg.Done()
t.Run(n.ID, func(t *testing.T) {
for _, fn := range d.BeforeEachFns {
fn(d.Ctx)
}
n.Fn(d.Ctx)
for _, fn := range d.AfterEachFns {
fn(d.Ctx)
}
completedMux.Lock()
completed[n.ID] = true
for _, dependent := range outEdges[n.ID] {
inDegree[dependent.ID]--
}
completedMux.Unlock()
})
}(node)
}
wg.Wait()
}
// Run teardown functions.
for _, fn := range d.TearDownFns {
fn(d.Ctx)
}
}
// collectDependencies recursively collects all dependencies for a given node.
func (d *TDag) collectDependencies(nodeID string, collected map[string]bool) {
// Mark this node as required.
collected[nodeID] = true
// Find all edges where this node is on the right (dependencies).
for _, edge := range d.Edges {
if edge.Right.ID == nodeID && !collected[edge.Left.ID] {
d.collectDependencies(edge.Left.ID, collected)
}
}
}
func (d *TDag) createsCycle(from, to *TNode) bool {
visited := make(map[string]bool)
return d.detectCycle(from, to, visited)
}
func (d *TDag) detectCycle(start, target *TNode, visited map[string]bool) bool {
if start == nil || target == nil {
return false
}
if start.ID == target.ID {
return true
}
visited[start.ID] = true
for _, edge := range d.Edges {
if edge.Left.ID == start.ID && !visited[edge.Right.ID] {
if d.detectCycle(edge.Right, target, visited) {
return true
}
}
}
visited[start.ID] = false
return false
}
func (d *TDag) ToD2(path string) error {
var builder strings.Builder
visited := make(map[string]bool)
edgesPrinted := make(map[string]bool)
for _, node := range d.Nodes {
if _, inGroup := visited[node.ID]; !inGroup {
d.buildD2FromNode(node, visited, edgesPrinted, &builder)
}
}
// Write D2 output to file
f, err := os.OpenFile(path, os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return err
}
defer f.Close()
if _, err := f.WriteString(builder.String()); err != nil {
return err
}
return nil
}
func (d *TDag) buildD2FromNode(node *TNode, visited, edgesPrinted map[string]bool, builder *strings.Builder) {
if visited[node.ID] {
return
}
visited[node.ID] = true
for _, edge := range d.Edges {
if edge.Left.ID == node.ID {
edgeKey := fmt.Sprintf("%s->%s", edge.Left.ID, edge.Right.ID)
if !edgesPrinted[edgeKey] {
builder.WriteString(fmt.Sprintf("%s -> %s\n", edge.Left.ID, edge.Right.ID))
edgesPrinted[edgeKey] = true
}
d.buildD2FromNode(edge.Right, visited, edgesPrinted, builder)
}
}
visited[node.ID] = false
}