-
Notifications
You must be signed in to change notification settings - Fork 0
/
children_test.go
99 lines (73 loc) · 2.91 KB
/
children_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
package curator
import (
"sync"
"testing"
"github.com/go-zookeeper/zk"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type GetChildrenBuilderTestSuite struct {
mockContainerTestSuite
}
func TestGetChildrenBuilder(t *testing.T) {
suite.Run(t, new(GetChildrenBuilderTestSuite))
}
func (s *GetChildrenBuilderTestSuite) TestGetChildren() {
s.With(func(builder *CuratorFrameworkBuilder, client CuratorFramework, conn *mockConn, stat *zk.Stat) {
conn.On("Children", "/parent").Return([]string{"child"}, stat, nil).Once()
var parentStat zk.Stat
children, err := client.GetChildren().StoringStatIn(&parentStat).ForPath("/parent")
assert.Equal(s.T(), []string{"child"}, children)
assert.NoError(s.T(), err)
})
}
func (s *GetChildrenBuilderTestSuite) TestNamespace() {
s.WithNamespace("parent", func(builder *CuratorFrameworkBuilder, client CuratorFramework, conn *mockConn, stat *zk.Stat, acls []zk.ACL) {
conn.On("Exists", "/parent").Return(false, nil, nil).Once()
conn.On("Create", "/parent", []byte{}, int32(PERSISTENT), OPEN_ACL_UNSAFE).Return("/parent", nil).Once()
conn.On("Children", "/parent/child").Return([]string{"node"}, stat, nil).Once()
var parentStat zk.Stat
children, err := client.GetChildren().StoringStatIn(&parentStat).ForPath("/child")
assert.Equal(s.T(), []string{"node"}, children)
assert.NoError(s.T(), err)
})
}
func (s *GetChildrenBuilderTestSuite) TestBackground() {
s.With(func(client CuratorFramework, conn *mockConn, wg *sync.WaitGroup, stat *zk.Stat) {
ctxt := "context"
children := []string{"child"}
conn.On("Children", "/parent").Return(children, stat, nil).Once()
_, err := client.GetChildren().InBackgroundWithCallbackAndContext(
func(client CuratorFramework, event CuratorEvent) error {
defer wg.Done()
assert.Equal(s.T(), CHILDREN, event.Type())
assert.Equal(s.T(), "/parent", event.Path())
assert.Equal(s.T(), stat, event.Stat())
assert.NoError(s.T(), event.Err())
assert.Equal(s.T(), "parent", event.Name())
assert.Equal(s.T(), children, event.Children())
assert.Equal(s.T(), ctxt, event.Context())
return nil
}, ctxt).ForPath("/parent")
assert.NoError(s.T(), err)
})
}
func (s *GetChildrenBuilderTestSuite) TestWatcher() {
s.With(func(client CuratorFramework, conn *mockConn, wg *sync.WaitGroup, data []byte, stat *zk.Stat) {
events := make(chan zk.Event)
defer close(events)
conn.On("ChildrenW", "/parent").Return([]string{"child"}, stat, events, nil).Once()
children, err := client.GetChildren().UsingWatcher(NewWatcher(func(event *zk.Event) {
defer wg.Done()
assert.NotNil(s.T(), event)
assert.Equal(s.T(), zk.EventNodeChildrenChanged, event.Type)
assert.Equal(s.T(), "/parent", event.Path)
})).ForPath("/parent")
assert.Equal(s.T(), []string{"child"}, children)
assert.NoError(s.T(), err)
events <- zk.Event{
Type: zk.EventNodeChildrenChanged,
Path: "/parent",
}
})
}