Fix removeEdge test failures

The removeEdge test could fail intermittently with the wrong order.

The precondition of a 1->2->3 order wasn't met, because there was no
edge from 1->3, so 3->1->2 was also a valid ordering.

The other failure was a bookkeeping error, were the recorded order may
not match the visited order. What happened in this case was the gateCh
was closed by V2, allowing V3 to run which could beat V2 to recording
its visit. Now the visit is recorded as part of the vertex walk, and the
gate is released as the final operation.

The order is deterministic now, so remove the brute-force test loop.
This commit is contained in:
James Bardin 2017-02-20 15:36:15 -05:00
parent d01b0b0647
commit bfa6ab4617
1 changed files with 50 additions and 50 deletions

View File

@ -215,13 +215,12 @@ func TestWalker_newEdge(t *testing.T) {
}
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
@ -238,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):
@ -273,7 +274,6 @@ func TestWalker_removeEdge(t *testing.T) {
t.Fatalf("bad: %#v", order)
}
}
}
// walkCbRecord is a test helper callback that just records the order called.
func walkCbRecord(order *[]interface{}) WalkFunc {