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:
parent
2fce519f57
commit
0fb24c1a7a
|
@ -92,38 +92,51 @@ 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
|
var g AcyclicGraph
|
||||||
for i := 0; i < 50; i++ {
|
g.Add(1)
|
||||||
var g AcyclicGraph
|
g.Add(2)
|
||||||
g.Add(1)
|
g.Connect(BasicEdge(1, 2))
|
||||||
g.Add(2)
|
|
||||||
g.Connect(BasicEdge(1, 2))
|
|
||||||
|
|
||||||
var order []interface{}
|
// Record function
|
||||||
w := &Walker{Callback: walkCbRecord(&order)}
|
var order []interface{}
|
||||||
w.Update(&g)
|
recordF := walkCbRecord(&order)
|
||||||
|
done2 := make(chan int)
|
||||||
|
|
||||||
// Wait a bit
|
// Build a callback that notifies us when 2 has been walked
|
||||||
time.Sleep(10 * time.Millisecond)
|
var w *Walker
|
||||||
|
cb := func(v Vertex) error {
|
||||||
// Update the graph
|
if v == 2 {
|
||||||
g.Add(3)
|
defer func() {
|
||||||
w.Update(&g)
|
close(done2)
|
||||||
|
}()
|
||||||
// Update the graph again but with the same vertex
|
|
||||||
g.Add(3)
|
|
||||||
w.Update(&g)
|
|
||||||
|
|
||||||
// Wait
|
|
||||||
if err := w.Wait(); err != nil {
|
|
||||||
t.Fatalf("err: %s", err)
|
|
||||||
}
|
}
|
||||||
|
return recordF(v)
|
||||||
|
}
|
||||||
|
|
||||||
// Check
|
// Add the initial vertices
|
||||||
expected := []interface{}{1, 2, 3}
|
w = &Walker{Callback: cb}
|
||||||
if !reflect.DeepEqual(order, expected) {
|
w.Update(&g)
|
||||||
t.Fatalf("bad: %#v", order)
|
|
||||||
}
|
// if 2 has been visited, the walk is complete so far
|
||||||
|
<-done2
|
||||||
|
|
||||||
|
// Update the graph
|
||||||
|
g.Add(3)
|
||||||
|
w.Update(&g)
|
||||||
|
|
||||||
|
// Update the graph again but with the same vertex
|
||||||
|
g.Add(3)
|
||||||
|
w.Update(&g)
|
||||||
|
|
||||||
|
// Wait
|
||||||
|
if err := w.Wait(); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check
|
||||||
|
expected := []interface{}{1, 2, 3}
|
||||||
|
if !reflect.DeepEqual(order, expected) {
|
||||||
|
t.Fatalf("bad: %#v", order)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue