Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dsa0x committed Jan 8, 2025
1 parent fb7b05d commit 38f192d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
5 changes: 3 additions & 2 deletions internal/dag/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,9 @@ func (g *AcyclicGraph) Cycles() [][]Vertex {
// This will walk nodes in parallel if it can. The resulting diagnostics
// contains problems from all graphs visited, in no particular order.
func (g *AcyclicGraph) Walk(cb WalkFunc) tfdiags.Diagnostics {
ctx, cancel := context.WithCancelCause(context.Background())
w := &Walker{Callback: cb, Reverse: true, walkContext: ctx, walkContextCancel: cancel}
w := NewWalker(cb, func(w *Walker) {
w.Reverse = true
})
g.walker = w

w.Update(g)
Expand Down
16 changes: 16 additions & 0 deletions internal/dag/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ func (w *Walker) init() {
}
}

// NewWalker creates a new walker with the given callback function.
func NewWalker(cb WalkFunc, opts ...func(*Walker)) *Walker {
ctx, cancel := context.WithCancelCause(context.Background())
w := &Walker{
Callback: cb,
walkContext: ctx,
walkContextCancel: cancel,
}
if opts != nil {

Check failure on line 91 in internal/dag/walk.go

View workflow job for this annotation

GitHub Actions / Code Consistency Checks

unnecessary nil check around range (S1031)
for _, opt := range opts {
opt(w)
}
}
return w
}

type walkerVertex struct {
// These should only be set once on initialization and never written again.
// They are not protected by a lock since they don't need to be since
Expand Down
21 changes: 10 additions & 11 deletions internal/dag/walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestWalker_basic(t *testing.T) {
// Run it a bunch of times since it is timing dependent
for i := 0; i < 50; i++ {
var order []interface{}
w := &Walker{Callback: walkCbRecord(&order)}
w := NewWalker(walkCbRecord(&order))
w.Update(&g)

// Wait
Expand All @@ -47,7 +47,7 @@ func TestWalker_updateNilGraph(t *testing.T) {
// Run it a bunch of times since it is timing dependent
for i := 0; i < 50; i++ {
var order []interface{}
w := &Walker{Callback: walkCbRecord(&order)}
w := NewWalker(walkCbRecord(&order))
w.Update(&g)
w.Update(nil)

Expand Down Expand Up @@ -83,7 +83,7 @@ func TestWalker_error(t *testing.T) {
return recordF(v)
}

w := &Walker{Callback: cb}
w := NewWalker(cb)
w.Update(&g)

// Wait
Expand All @@ -110,7 +110,6 @@ func TestWalker_newVertex(t *testing.T) {
done2 := make(chan int)

// Build a callback that notifies us when 2 has been walked
var w *Walker
cb := func(v Vertex) tfdiags.Diagnostics {
if v == 2 {
defer close(done2)
Expand All @@ -119,7 +118,7 @@ func TestWalker_newVertex(t *testing.T) {
}

// Add the initial vertices
w = &Walker{Callback: cb}
w := NewWalker(cb)
w.Update(&g)

// if 2 has been visited, the walk is complete so far
Expand Down Expand Up @@ -155,7 +154,7 @@ func TestWalker_removeVertex(t *testing.T) {
var order []interface{}
recordF := walkCbRecord(&order)

var w *Walker
w := NewWalker(nil)
cb := func(v Vertex) tfdiags.Diagnostics {
if v == 1 {
g.Remove(2)
Expand All @@ -166,7 +165,7 @@ func TestWalker_removeVertex(t *testing.T) {
}

// Add the initial vertices
w = &Walker{Callback: cb}
w.Callback = cb
w.Update(&g)

// Wait
Expand All @@ -191,7 +190,7 @@ func TestWalker_newEdge(t *testing.T) {
var order []interface{}
recordF := walkCbRecord(&order)

var w *Walker
w := NewWalker(nil)
cb := func(v Vertex) tfdiags.Diagnostics {
// record where we are first, otherwise the Updated vertex may get
// walked before the first visit.
Expand All @@ -206,7 +205,7 @@ func TestWalker_newEdge(t *testing.T) {
}

// Add the initial vertices
w = &Walker{Callback: cb}
w.Callback = cb
w.Update(&g)

// Wait
Expand Down Expand Up @@ -241,7 +240,7 @@ func TestWalker_removeEdge(t *testing.T) {
// forcing 2 before 3 via the callback (and not the graph). If
// 2 cannot execute before 3 (edge removal is non-functional), then
// this test will timeout.
var w *Walker
w := NewWalker(nil)
gateCh := make(chan struct{})
cb := func(v Vertex) tfdiags.Diagnostics {
t.Logf("visit vertex %#v", v)
Expand Down Expand Up @@ -274,7 +273,7 @@ func TestWalker_removeEdge(t *testing.T) {
}

// Add the initial vertices
w = &Walker{Callback: cb}
w.Callback = cb
w.Update(&g)

// Wait
Expand Down

0 comments on commit 38f192d

Please sign in to comment.