forked from zabawaba99/firetest
/
tree_test.go
129 lines (114 loc) · 2.67 KB
/
tree_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
package firetest
import (
"regexp"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTreeAdd(t *testing.T) {
for _, test := range []struct {
path string
node *node
}{
{
path: "scalars/string",
node: newNode("foo"),
},
{
path: "s/c/a/l/a/r/s/s/t/r/i/n/g",
node: newNode([]interface{}{"foo", "bar"}),
},
} {
tree := newTree()
// listen for notifications
notifications := tree.watch(test.path)
exited := make(chan struct{})
go func() {
n, ok := <-notifications
assert.True(t, ok)
assert.Equal(t, "put", n.Name)
assert.Equal(t, test.path, n.Data.Path)
assert.Equal(t, test.node, n.Data.Data)
close(exited)
}()
tree.add(test.path, test.node)
rabbitHole := strings.Split(test.path, "/")
previous := tree.rootNode
for i := 0; i < len(rabbitHole); i++ {
var ok bool
previous, ok = previous.children[rabbitHole[i]]
assert.True(t, ok, test.path)
}
assert.NoError(t, equalNodes(test.node, previous), test.path)
select {
case <-exited:
case <-time.After(250 * time.Millisecond):
}
tree.stopWatching(test.path, notifications)
}
}
func TestTreeGet(t *testing.T) {
for _, test := range []struct {
path string
node *node
}{
{
path: "scalars/string",
node: newNode("foo"),
},
{
path: "s/c/a/l/a/r/s/s/t/r/i/n/g",
node: newNode([]interface{}{"foo", "bar"}),
},
} {
tree := newTree()
tree.add(test.path, test.node)
assert.NoError(t, equalNodes(test.node, tree.get(test.path)), test.path)
}
}
func TestTreeDel(t *testing.T) {
existingNodes := []string{
"root/only/two",
"root/only/three",
"root/only/one/child/here",
}
tree := newTree()
for _, p := range existingNodes {
tree.add(p, newNode(1))
}
// listen for notifications
notifications := tree.watch("")
exited := make(chan struct{})
go func() {
regex := regexp.MustCompile("(root/only/one/child|root)")
n, ok := <-notifications
assert.True(t, ok)
assert.Equal(t, "put", n.Name)
assert.Regexp(t, regex, n.Data.Path)
n, ok = <-notifications
assert.True(t, ok)
assert.Equal(t, "put", n.Name)
assert.Regexp(t, regex, n.Data.Path)
close(exited)
}()
tree.del("root/only/one/child")
assert.Nil(t, tree.get("root/only/one/child/here"))
assert.Nil(t, tree.get("root/only/one/child"))
assert.Nil(t, tree.get("root/only/one"))
n := tree.get("root/only")
require.NotNil(t, n)
assert.Len(t, n.children, 2)
_, exists := n.children["one"]
assert.False(t, exists)
tree.del("root")
n = tree.get("")
require.NotNil(t, n)
assert.Len(t, n.children, 0)
select {
case <-exited:
case <-time.After(250 * time.Millisecond):
}
tree.stopWatching("", notifications)
}