-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph_test.go
73 lines (56 loc) · 1.04 KB
/
graph_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"context"
"sort"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGraph(t *testing.T) {
g := newGraph()
a := &testTarget{name: "a"}
b := &testTarget{name: "b"}
r := &rootTarget{}
var wg sync.WaitGroup
wg.Add(3)
add := func(t Target) {
defer wg.Done()
g.Add(t)
}
// Catches data races with -race flag.
go add(a)
go add(b)
go add(r)
wg.Wait()
g.Connect(b, a)
g.Connect(r, b)
var mu sync.Mutex
var targets []string
g.Walk(func(t Target) error {
mu.Lock()
defer mu.Unlock()
targets = append(targets, t.Name())
return nil
})
assert.Equal(t, []string{"a", "b"}, targets)
}
type testTarget struct {
name string
}
func (t *testTarget) Name() string {
return t.name
}
func (t *testTarget) Exec(_ context.Context) error {
return nil
}
func (t *testTarget) Dependencies(_ context.Context) ([]string, error) {
return nil, nil
}
func targetNames(targets []Target) []string {
var n sort.StringSlice
for _, d := range targets {
n = append(n, d.Name())
}
sort.Sort(n)
return n
}