-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathdag_test.go
More file actions
122 lines (91 loc) · 4.13 KB
/
dag_test.go
File metadata and controls
122 lines (91 loc) · 4.13 KB
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
package do
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// TestNewServiceDescription checks the creation of a new ServiceDescription.
func TestNewServiceDescription(t *testing.T) {
t.Parallel()
testWithTimeout(t, 100*time.Millisecond)
is := assert.New(t)
expected := ServiceDescription{"foo", "bar", "baz"}
actual := newServiceDescription("foo", "bar", "baz")
is.Equal(expected, actual)
}
// TestNewDAG checks the initialization of a new DAG.
func TestNewDAG(t *testing.T) {
t.Parallel()
testWithTimeout(t, 100*time.Millisecond)
is := assert.New(t)
dag := newDAG()
expectedDependencies := map[ServiceDescription]map[ServiceDescription]struct{}{}
expectedDependents := map[ServiceDescription]map[ServiceDescription]struct{}{}
is.Equal(expectedDependencies, dag.dependencies)
is.Equal(expectedDependents, dag.dependents)
}
// TestDAG_addDependency checks the addition of dependencies to the DAG.
func TestDAG_addDependency(t *testing.T) {
t.Parallel()
testWithTimeout(t, 100*time.Millisecond)
is := assert.New(t)
edge1 := newServiceDescription("scope1", "scope1", "service1")
edge2 := newServiceDescription("scope2", "scope2", "service2")
edge3 := newServiceDescription("scope3", "scope3", "service3")
dag := newDAG()
dag.addDependency("scope1", "scope1", "service1", "scope2", "scope2", "service2")
expectedDependencies := map[ServiceDescription]map[ServiceDescription]struct{}{edge1: {edge2: {}}}
expectedDependents := map[ServiceDescription]map[ServiceDescription]struct{}{edge2: {edge1: {}}}
is.Equal(expectedDependencies, dag.dependencies)
is.Equal(expectedDependents, dag.dependents)
dag.addDependency("scope3", "scope3", "service3", "scope2", "scope2", "service2")
expectedDependencies = map[ServiceDescription]map[ServiceDescription]struct{}{edge1: {edge2: {}}, edge3: {edge2: {}}}
expectedDependents = map[ServiceDescription]map[ServiceDescription]struct{}{edge2: {edge1: {}, edge3: {}}}
is.Equal(expectedDependencies, dag.dependencies)
is.Equal(expectedDependents, dag.dependents)
}
// TestDAG_removeService checks the removal of dependencies to the DAG.
func TestDAG_removeService(t *testing.T) {
t.Parallel()
testWithTimeout(t, 100*time.Millisecond)
is := assert.New(t)
edge1 := newServiceDescription("scope1", "scope1", "service1")
// edge2 := newServiceDescription("scope2", "scope2", "service2")
edge3 := newServiceDescription("scope3", "scope3", "service3")
dag := newDAG()
dag.addDependency("scope1", "scope1", "service1", "scope2", "scope2", "service2")
dag.addDependency("scope3", "scope3", "service3", "scope2", "scope2", "service2")
dag.removeService("scope2", "scope2", "service2")
expectedDependencies := map[ServiceDescription]map[ServiceDescription]struct{}{edge1: {}, edge3: {}}
expectedDependents := map[ServiceDescription]map[ServiceDescription]struct{}{}
is.Equal(expectedDependencies, dag.dependencies)
is.Equal(expectedDependents, dag.dependents)
}
// TestDAG_explainService checks the explanation of dependencies for a service in the DAG.
func TestDAG_explainService(t *testing.T) {
t.Parallel()
testWithTimeout(t, 100*time.Millisecond)
is := assert.New(t)
edge1 := newServiceDescription("scope1", "scope1", "service1")
edge2 := newServiceDescription("scope2", "scope2", "service2")
edge3 := newServiceDescription("scope3", "scope3", "service3")
dag := newDAG()
dag.addDependency("scope1", "scope1", "service1", "scope2", "scope2", "service2")
dag.addDependency("scope3", "scope3", "service3", "scope2", "scope2", "service2")
// edge1
a, b := dag.explainService("scope1", "scope1", "service1")
is.ElementsMatch([]ServiceDescription{edge2}, a)
is.ElementsMatch([]ServiceDescription{}, b)
// edge2
a, b = dag.explainService("scope2", "scope2", "service2")
is.ElementsMatch([]ServiceDescription{}, a)
is.ElementsMatch([]ServiceDescription{edge1, edge3}, b)
// edge3
a, b = dag.explainService("scope3", "scope3", "service3")
is.ElementsMatch([]ServiceDescription{edge2}, a)
is.ElementsMatch([]ServiceDescription{}, b)
// not found
a, b = dag.explainService("scopeX", "scopeX", "serviceX")
is.ElementsMatch([]ServiceDescription{}, a)
is.ElementsMatch([]ServiceDescription{}, b)
}