-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_test.go
212 lines (173 loc) · 4.58 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"strings"
"testing"
)
func TestDetectCircularDependencySimple(t *testing.T) {
a := NewNode(Job{}, "a")
b := NewNode(Job{}, "b")
a.Dependents[b] = struct{}{}
b.Dependents[a] = struct{}{}
root := NewNode(Job{}, "root")
root.Dependents[a] = struct{}{}
expectedCycle := "a->b->a"
err := detectCircularDependency(root)
if err == nil {
t.Fatalf("failed to detect a circular dependency: %s", expectedCycle)
}
msg := err.Error()
if !strings.Contains(msg, expectedCycle) {
t.Fatalf("epexcting the error message to contain \"%s\", but got \"%s\"", expectedCycle, msg)
}
}
func TestDetectCircularDependencyMoreThanOneDependents(t *testing.T) {
a := NewNode(Job{}, "a")
b := NewNode(Job{}, "b")
c := NewNode(Job{}, "c")
d := NewNode(Job{}, "d")
e := NewNode(Job{}, "e")
root := NewNode(Job{}, "root")
root.Dependents[a] = struct{}{}
root.Dependents[b] = struct{}{}
a.Dependents[c] = struct{}{}
a.Dependents[d] = struct{}{}
d.Dependents[e] = struct{}{}
b.Dependents[e] = struct{}{}
e.Dependents[d] = struct{}{}
expectedCycles := []string{"d->e->d->a", "e->d->e->b"}
err := detectCircularDependency(root)
if err == nil {
t.Fatalf("failed to detect a circular dependency: \"%s\" or \"%s\"", expectedCycles[0], expectedCycles[1])
}
msg := err.Error()
var found bool
for _, expectedCycle := range expectedCycles {
if strings.Contains(msg, expectedCycle) {
found = true
}
}
if !found {
t.Fatalf("epexcting the error message to contain \"%s\" or \"%s\", but got \"%s\"", expectedCycles[0], expectedCycles[1], msg)
}
}
func TestDetectCircularDependencyNoCycle(t *testing.T) {
a := NewNode(Job{}, "a")
b := NewNode(Job{}, "b")
c := NewNode(Job{}, "c")
d := NewNode(Job{}, "d")
e := NewNode(Job{}, "e")
f := NewNode(Job{}, "f")
root := NewNode(Job{}, "root")
root.Dependents[a] = struct{}{}
root.Dependents[b] = struct{}{}
a.Dependents[c] = struct{}{}
a.Dependents[d] = struct{}{}
d.Dependents[e] = struct{}{}
b.Dependents[e] = struct{}{}
e.Dependents[f] = struct{}{}
err := detectCircularDependency(root)
if err != nil {
t.Fatalf("expected no cycle, but got \"%s\"", err.Error())
}
}
func TestDetectCircularDependencyWhenNil(t *testing.T) {
err := detectCircularDependency(nil)
if err != nil {
t.Fatal(err)
}
}
func TestNewGraphWithoutJobs(t *testing.T) {
var cfg Config
_, err := NewGraph(cfg)
if err == nil {
t.Fatal("expected to get an error due to no jobs")
}
}
func TestNewGraphWithNoDependencies(t *testing.T) {
var cfg Config
var job Job
cfg.Jobs = map[string]Job{
"job1": job,
"job2": job,
}
graph, err := NewGraph(cfg)
if err != nil {
t.Fatal(err)
}
if len(graph.Dependents) != 2 {
t.Fatalf("expected to have 2 dependents, but got %d", len(graph.Dependents))
}
expectedIDs := map[string]struct{}{
"job1": {},
"job2": {},
}
for dependent := range graph.Dependents {
if _, ok := expectedIDs[dependent.ID]; !ok {
t.Fatalf("%s is not an expected label", dependent.ID)
}
delete(expectedIDs, dependent.ID)
}
}
func TestNewGraphWithDependencies(t *testing.T) {
var cfg Config
var job Job
cfg.Jobs = map[string]Job{
"job1": job,
"job2": job,
}
job.Needs = []string{"job1"}
cfg.Jobs["job3"] = job
graph, err := NewGraph(cfg)
if err != nil {
t.Fatal(err)
}
if len(graph.Dependents) != 2 {
t.Fatalf("expected to have 2 dependents, but got %d", len(graph.Dependents))
}
expectedIDs := map[string][]string{
"job1": {"job3"},
"job2": nil,
}
for dependent := range graph.Dependents {
children, ok := expectedIDs[dependent.ID]
if !ok {
t.Fatalf("%s is not an expected label", dependent.ID)
}
if len(dependent.Dependents) != len(children) {
t.Fatalf("expected to have %d dependents, but got %d dependents", len(children), len(dependent.Dependents))
}
if len(children) > 0 {
child := children[0]
for d := range dependent.Dependents {
if d.ID != child {
t.Fatalf("expected the child to be labeled \"%s\", but instead \"%s\"", child, d.ID)
}
}
}
delete(expectedIDs, dependent.ID)
}
}
func TestNewGraphWithCircularDependency(t *testing.T) {
var cfg Config
var job Job
cfg.Jobs = make(map[string]Job)
job.Needs = []string{"job2"}
cfg.Jobs["job1"] = job
job.Needs = []string{"job1"}
cfg.Jobs["job2"] = job
_, err := NewGraph(cfg)
if err == nil {
t.Fatal("expected to get an error about circular dependency")
}
}
func TestNewGraphDependencyNotExist(t *testing.T) {
var cfg Config
var job Job
cfg.Jobs = make(map[string]Job)
job.Needs = []string{"job2"}
cfg.Jobs["job1"] = job
_, err := NewGraph(cfg)
if err == nil {
t.Fatal("expected to get an error")
}
}