Merge pull request #12121 from hashicorp/jbardin/dag-tests

Fix some intermittent dag test failures
This commit is contained in:
James Bardin 2017-02-21 11:14:07 -05:00 committed by GitHub
commit be5230c673
1 changed files with 144 additions and 138 deletions

View File

@ -92,19 +92,31 @@ func TestWalker_error(t *testing.T) {
}
func TestWalker_newVertex(t *testing.T) {
// Run it a bunch of times since it is timing dependent
for i := 0; i < 50; i++ {
var g AcyclicGraph
g.Add(1)
g.Add(2)
g.Connect(BasicEdge(1, 2))
// Record function
var order []interface{}
w := &Walker{Callback: walkCbRecord(&order)}
recordF := walkCbRecord(&order)
done2 := make(chan int)
// Build a callback that notifies us when 2 has been walked
var w *Walker
cb := func(v Vertex) error {
if v == 2 {
defer close(done2)
}
return recordF(v)
}
// Add the initial vertices
w = &Walker{Callback: cb}
w.Update(&g)
// Wait a bit
time.Sleep(10 * time.Millisecond)
// if 2 has been visited, the walk is complete so far
<-done2
// Update the graph
g.Add(3)
@ -124,12 +136,9 @@ func TestWalker_newVertex(t *testing.T) {
if !reflect.DeepEqual(order, expected) {
t.Fatalf("bad: %#v", order)
}
}
}
func TestWalker_removeVertex(t *testing.T) {
// Run it a bunch of times since it is timing dependent
for i := 0; i < 50; i++ {
var g AcyclicGraph
g.Add(1)
g.Add(2)
@ -139,7 +148,6 @@ func TestWalker_removeVertex(t *testing.T) {
var order []interface{}
recordF := walkCbRecord(&order)
// Build a callback that delays until we close a channel
var w *Walker
cb := func(v Vertex) error {
if v == 1 {
@ -164,12 +172,9 @@ func TestWalker_removeVertex(t *testing.T) {
if !reflect.DeepEqual(order, expected) {
t.Fatalf("bad: %#v", order)
}
}
}
func TestWalker_newEdge(t *testing.T) {
// Run it a bunch of times since it is timing dependent
for i := 0; i < 50; i++ {
var g AcyclicGraph
g.Add(1)
g.Add(2)
@ -179,16 +184,18 @@ func TestWalker_newEdge(t *testing.T) {
var order []interface{}
recordF := walkCbRecord(&order)
// Build a callback that delays until we close a channel
var w *Walker
cb := func(v Vertex) error {
// record where we are first, otherwise the Updated vertex may get
// walked before the first visit.
err := recordF(v)
if v == 1 {
g.Add(3)
g.Connect(BasicEdge(3, 2))
w.Update(&g)
}
return recordF(v)
return err
}
// Add the initial vertices
@ -205,17 +212,15 @@ func TestWalker_newEdge(t *testing.T) {
if !reflect.DeepEqual(order, expected) {
t.Fatalf("bad: %#v", order)
}
}
}
func TestWalker_removeEdge(t *testing.T) {
// Run it a bunch of times since it is timing dependent
for i := 0; i < 50; i++ {
var g AcyclicGraph
g.Add(1)
g.Add(2)
g.Add(3)
g.Connect(BasicEdge(1, 2))
g.Connect(BasicEdge(1, 3))
g.Connect(BasicEdge(3, 2))
// Record function
@ -232,16 +237,18 @@ func TestWalker_removeEdge(t *testing.T) {
var w *Walker
gateCh := make(chan struct{})
cb := func(v Vertex) error {
if v == 1 {
switch v {
case 1:
g.RemoveEdge(BasicEdge(3, 2))
w.Update(&g)
}
if v == 2 {
close(gateCh)
}
case 2:
// this visit isn't completed until we've recorded it
// Once the visit is official, we can then close the gate to
// let 3 continue.
defer close(gateCh)
if v == 3 {
case 3:
select {
case <-gateCh:
case <-time.After(50 * time.Millisecond):
@ -266,7 +273,6 @@ func TestWalker_removeEdge(t *testing.T) {
if !reflect.DeepEqual(order, expected) {
t.Fatalf("bad: %#v", order)
}
}
}
// walkCbRecord is a test helper callback that just records the order called.