2017-02-02 20:41:46 +01:00
|
|
|
package dag
|
|
|
|
|
|
|
|
import (
|
2017-02-02 23:38:47 +01:00
|
|
|
"fmt"
|
2017-02-02 20:41:46 +01:00
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
2017-02-02 21:09:26 +01:00
|
|
|
"time"
|
2017-02-02 20:41:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestWalker_basic(t *testing.T) {
|
2017-02-03 11:48:09 +01:00
|
|
|
var g AcyclicGraph
|
2017-02-02 20:41:46 +01:00
|
|
|
g.Add(1)
|
|
|
|
g.Add(2)
|
|
|
|
g.Connect(BasicEdge(1, 2))
|
|
|
|
|
|
|
|
// Run it a bunch of times since it is timing dependent
|
|
|
|
for i := 0; i < 50; i++ {
|
|
|
|
var order []interface{}
|
2017-02-03 11:22:26 +01:00
|
|
|
w := &Walker{Callback: walkCbRecord(&order)}
|
2017-02-03 11:11:37 +01:00
|
|
|
w.Update(&g)
|
2017-02-02 20:41:46 +01:00
|
|
|
|
|
|
|
// Wait
|
|
|
|
if err := w.Wait(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check
|
|
|
|
expected := []interface{}{1, 2}
|
|
|
|
if !reflect.DeepEqual(order, expected) {
|
|
|
|
t.Fatalf("bad: %#v", order)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-03 11:26:58 +01:00
|
|
|
func TestWalker_updateNilGraph(t *testing.T) {
|
2017-02-03 11:48:09 +01:00
|
|
|
var g AcyclicGraph
|
2017-02-03 11:26:58 +01:00
|
|
|
g.Add(1)
|
|
|
|
g.Add(2)
|
|
|
|
g.Connect(BasicEdge(1, 2))
|
|
|
|
|
|
|
|
// 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.Update(&g)
|
|
|
|
w.Update(nil)
|
|
|
|
|
|
|
|
// Wait
|
|
|
|
if err := w.Wait(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-03 11:04:39 +01:00
|
|
|
func TestWalker_error(t *testing.T) {
|
2017-02-03 11:48:09 +01:00
|
|
|
var g AcyclicGraph
|
2017-02-03 11:04:39 +01:00
|
|
|
g.Add(1)
|
|
|
|
g.Add(2)
|
|
|
|
g.Add(3)
|
|
|
|
g.Add(4)
|
|
|
|
g.Connect(BasicEdge(1, 2))
|
|
|
|
g.Connect(BasicEdge(2, 3))
|
|
|
|
g.Connect(BasicEdge(3, 4))
|
|
|
|
|
|
|
|
// Record function
|
|
|
|
var order []interface{}
|
|
|
|
recordF := walkCbRecord(&order)
|
|
|
|
|
|
|
|
// Build a callback that delays until we close a channel
|
|
|
|
cb := func(v Vertex) error {
|
|
|
|
if v == 2 {
|
|
|
|
return fmt.Errorf("error!")
|
|
|
|
}
|
|
|
|
|
|
|
|
return recordF(v)
|
|
|
|
}
|
|
|
|
|
2017-02-03 11:22:26 +01:00
|
|
|
w := &Walker{Callback: cb}
|
2017-02-03 11:11:37 +01:00
|
|
|
w.Update(&g)
|
2017-02-03 11:04:39 +01:00
|
|
|
|
|
|
|
// Wait
|
|
|
|
if err := w.Wait(); err == nil {
|
|
|
|
t.Fatal("expect error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check
|
|
|
|
expected := []interface{}{1}
|
|
|
|
if !reflect.DeepEqual(order, expected) {
|
|
|
|
t.Fatalf("bad: %#v", order)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 21:09:26 +01:00
|
|
|
func TestWalker_newVertex(t *testing.T) {
|
2017-02-20 20:58:24 +01:00
|
|
|
var g AcyclicGraph
|
|
|
|
g.Add(1)
|
|
|
|
g.Add(2)
|
|
|
|
g.Connect(BasicEdge(1, 2))
|
2017-02-02 21:09:26 +01:00
|
|
|
|
2017-02-20 20:58:24 +01:00
|
|
|
// Record function
|
|
|
|
var order []interface{}
|
|
|
|
recordF := walkCbRecord(&order)
|
|
|
|
done2 := make(chan int)
|
2017-02-02 21:09:26 +01:00
|
|
|
|
2017-02-20 20:58:24 +01:00
|
|
|
// Build a callback that notifies us when 2 has been walked
|
|
|
|
var w *Walker
|
|
|
|
cb := func(v Vertex) error {
|
|
|
|
if v == 2 {
|
2017-02-20 21:07:09 +01:00
|
|
|
defer close(done2)
|
2017-02-20 20:58:24 +01:00
|
|
|
}
|
|
|
|
return recordF(v)
|
|
|
|
}
|
2017-02-02 21:09:26 +01:00
|
|
|
|
2017-02-20 20:58:24 +01:00
|
|
|
// Add the initial vertices
|
|
|
|
w = &Walker{Callback: cb}
|
|
|
|
w.Update(&g)
|
2017-02-02 21:09:26 +01:00
|
|
|
|
2017-02-20 20:58:24 +01:00
|
|
|
// if 2 has been visited, the walk is complete so far
|
|
|
|
<-done2
|
2017-02-02 21:09:26 +01:00
|
|
|
|
2017-02-20 20:58:24 +01:00
|
|
|
// Update the graph
|
|
|
|
g.Add(3)
|
|
|
|
w.Update(&g)
|
2017-02-02 21:09:26 +01:00
|
|
|
|
2017-02-20 20:58:24 +01:00
|
|
|
// 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)
|
2017-02-02 21:09:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWalker_removeVertex(t *testing.T) {
|
2017-02-20 21:07:09 +01:00
|
|
|
var g AcyclicGraph
|
|
|
|
g.Add(1)
|
|
|
|
g.Add(2)
|
|
|
|
g.Connect(BasicEdge(1, 2))
|
2017-02-02 21:09:26 +01:00
|
|
|
|
2017-02-20 21:07:09 +01:00
|
|
|
// Record function
|
|
|
|
var order []interface{}
|
|
|
|
recordF := walkCbRecord(&order)
|
2017-02-02 21:09:26 +01:00
|
|
|
|
2017-02-20 21:07:09 +01:00
|
|
|
var w *Walker
|
|
|
|
cb := func(v Vertex) error {
|
|
|
|
if v == 1 {
|
|
|
|
g.Remove(2)
|
|
|
|
w.Update(&g)
|
2017-02-02 21:09:26 +01:00
|
|
|
}
|
|
|
|
|
2017-02-20 21:07:09 +01:00
|
|
|
return recordF(v)
|
|
|
|
}
|
2017-02-02 21:09:26 +01:00
|
|
|
|
2017-02-20 21:07:09 +01:00
|
|
|
// Add the initial vertices
|
|
|
|
w = &Walker{Callback: cb}
|
|
|
|
w.Update(&g)
|
2017-02-02 21:09:26 +01:00
|
|
|
|
2017-02-20 21:07:09 +01:00
|
|
|
// Wait
|
|
|
|
if err := w.Wait(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check
|
|
|
|
expected := []interface{}{1}
|
|
|
|
if !reflect.DeepEqual(order, expected) {
|
|
|
|
t.Fatalf("bad: %#v", order)
|
2017-02-02 21:09:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWalker_newEdge(t *testing.T) {
|
|
|
|
// Run it a bunch of times since it is timing dependent
|
|
|
|
for i := 0; i < 50; i++ {
|
2017-02-03 11:48:09 +01:00
|
|
|
var g AcyclicGraph
|
2017-02-02 21:09:26 +01:00
|
|
|
g.Add(1)
|
|
|
|
g.Add(2)
|
|
|
|
g.Connect(BasicEdge(1, 2))
|
|
|
|
|
|
|
|
// Record function
|
|
|
|
var order []interface{}
|
|
|
|
recordF := walkCbRecord(&order)
|
|
|
|
|
|
|
|
// Build a callback that delays until we close a channel
|
2017-02-03 11:22:26 +01:00
|
|
|
var w *Walker
|
2017-02-02 21:09:26 +01:00
|
|
|
cb := func(v Vertex) error {
|
|
|
|
if v == 1 {
|
|
|
|
g.Add(3)
|
|
|
|
g.Connect(BasicEdge(3, 2))
|
2017-02-03 11:11:37 +01:00
|
|
|
w.Update(&g)
|
2017-02-02 21:09:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return recordF(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the initial vertices
|
2017-02-03 11:22:26 +01:00
|
|
|
w = &Walker{Callback: cb}
|
2017-02-03 11:11:37 +01:00
|
|
|
w.Update(&g)
|
2017-02-02 21:09:26 +01:00
|
|
|
|
|
|
|
// Wait
|
|
|
|
if err := w.Wait(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check
|
|
|
|
expected := []interface{}{1, 3, 2}
|
|
|
|
if !reflect.DeepEqual(order, expected) {
|
|
|
|
t.Fatalf("bad: %#v", order)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 23:38:47 +01:00
|
|
|
func TestWalker_removeEdge(t *testing.T) {
|
|
|
|
// Run it a bunch of times since it is timing dependent
|
|
|
|
for i := 0; i < 50; i++ {
|
2017-02-03 11:48:09 +01:00
|
|
|
var g AcyclicGraph
|
2017-02-02 23:38:47 +01:00
|
|
|
g.Add(1)
|
|
|
|
g.Add(2)
|
|
|
|
g.Add(3)
|
|
|
|
g.Connect(BasicEdge(1, 2))
|
|
|
|
g.Connect(BasicEdge(3, 2))
|
|
|
|
|
|
|
|
// Record function
|
|
|
|
var order []interface{}
|
|
|
|
recordF := walkCbRecord(&order)
|
|
|
|
|
|
|
|
// The way this works is that our original graph forces
|
|
|
|
// the order of 1 => 3 => 2. During the execution of 1, we
|
|
|
|
// remove the edge forcing 3 before 2. Then, during the execution
|
|
|
|
// of 3, we wait on a channel that is only closed by 2, implicitly
|
|
|
|
// 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.
|
2017-02-03 11:22:26 +01:00
|
|
|
var w *Walker
|
2017-02-02 23:38:47 +01:00
|
|
|
gateCh := make(chan struct{})
|
|
|
|
cb := func(v Vertex) error {
|
|
|
|
if v == 1 {
|
|
|
|
g.RemoveEdge(BasicEdge(3, 2))
|
2017-02-03 11:11:37 +01:00
|
|
|
w.Update(&g)
|
2017-02-02 23:38:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if v == 2 {
|
|
|
|
close(gateCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v == 3 {
|
|
|
|
select {
|
|
|
|
case <-gateCh:
|
|
|
|
case <-time.After(50 * time.Millisecond):
|
|
|
|
return fmt.Errorf("timeout 3 waiting for 2")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return recordF(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the initial vertices
|
2017-02-03 11:22:26 +01:00
|
|
|
w = &Walker{Callback: cb}
|
2017-02-03 11:11:37 +01:00
|
|
|
w.Update(&g)
|
2017-02-02 23:38:47 +01:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 20:41:46 +01:00
|
|
|
// walkCbRecord is a test helper callback that just records the order called.
|
|
|
|
func walkCbRecord(order *[]interface{}) WalkFunc {
|
|
|
|
var l sync.Mutex
|
|
|
|
return func(v Vertex) error {
|
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
*order = append(*order, v)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|