Remove sleep-based concurrency from newVertex test

Add a synchronization channel for the TestWalker_newVertex test, rather
than using a sleep and running it multiple times.
This commit is contained in:
James Bardin 2017-02-20 14:58:24 -05:00
parent 2fce519f57
commit 0fb24c1a7a
1 changed files with 41 additions and 28 deletions

View File

@ -92,19 +92,33 @@ func TestWalker_error(t *testing.T) {
} }
func TestWalker_newVertex(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 var g AcyclicGraph
g.Add(1) g.Add(1)
g.Add(2) g.Add(2)
g.Connect(BasicEdge(1, 2)) g.Connect(BasicEdge(1, 2))
// Record function
var order []interface{} 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 func() {
close(done2)
}()
}
return recordF(v)
}
// Add the initial vertices
w = &Walker{Callback: cb}
w.Update(&g) w.Update(&g)
// Wait a bit // if 2 has been visited, the walk is complete so far
time.Sleep(10 * time.Millisecond) <-done2
// Update the graph // Update the graph
g.Add(3) g.Add(3)
@ -125,7 +139,6 @@ func TestWalker_newVertex(t *testing.T) {
t.Fatalf("bad: %#v", order) t.Fatalf("bad: %#v", order)
} }
} }
}
func TestWalker_removeVertex(t *testing.T) { func TestWalker_removeVertex(t *testing.T) {
// Run it a bunch of times since it is timing dependent // Run it a bunch of times since it is timing dependent