-
Notifications
You must be signed in to change notification settings - Fork 19
/
data_test.go
154 lines (116 loc) · 4.94 KB
/
data_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
package curator
import (
"sync"
"testing"
"github.com/samuel/go-zookeeper/zk"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type GetDataBuilderTestSuite struct {
mockContainerTestSuite
}
func TestGetDataBuilder(t *testing.T) {
suite.Run(t, new(GetDataBuilderTestSuite))
}
func (s *GetDataBuilderTestSuite) TestGetData() {
s.With(func(builder *CuratorFrameworkBuilder, client CuratorFramework, conn *mockConn, compress *mockCompressionProvider, data []byte, stat *zk.Stat) {
conn.On("Get", "/node").Return([]byte("compressed(data)"), stat, nil).Once()
compress.On("Decompress", "/node", []byte("compressed(data)")).Return(data, nil).Once()
var stat2 zk.Stat
data2, err := client.GetData().StoringStatIn(&stat2).Decompressed().ForPath("/node")
assert.Equal(s.T(), data, data2)
assert.Equal(s.T(), stat, &stat2)
assert.NoError(s.T(), err)
})
}
func (s *GetDataBuilderTestSuite) TestNamespace() {
s.WithNamespace("parent", func(client CuratorFramework, conn *mockConn, data []byte, stat *zk.Stat) {
conn.On("Exists", "/parent").Return(true, nil, nil).Once()
conn.On("Get", "/parent/child").Return(data, stat, nil).Once()
data2, err := client.GetData().ForPath("/child")
assert.Equal(s.T(), data, data2)
assert.NoError(s.T(), err)
})
}
func (s *GetDataBuilderTestSuite) TestBackground() {
s.WithNamespace("parent", func(client CuratorFramework, conn *mockConn, wg *sync.WaitGroup, data []byte, stat *zk.Stat) {
ctxt := "context"
conn.On("Exists", "/parent").Return(true, nil, nil).Once()
conn.On("Get", "/parent/child").Return(data, stat, nil).Once()
_, err := client.GetData().InBackgroundWithCallbackAndContext(
func(client CuratorFramework, event CuratorEvent) error {
defer wg.Done()
assert.Equal(s.T(), GET_DATA, event.Type())
assert.Equal(s.T(), "/child", event.Path())
assert.Equal(s.T(), data, event.Data())
assert.NoError(s.T(), event.Err())
assert.Equal(s.T(), "child", event.Name())
assert.Equal(s.T(), ctxt, event.Context())
return nil
}, ctxt).ForPath("/child")
assert.NoError(s.T(), err)
})
}
func (s *GetDataBuilderTestSuite) 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("GetW", "/node").Return(data, stat, events, nil).Once()
data2, err := client.GetData().UsingWatcher(NewWatcher(func(event *zk.Event) {
defer wg.Done()
assert.NotNil(s.T(), event)
assert.Equal(s.T(), zk.EventNodeDataChanged, event.Type)
assert.Equal(s.T(), "/node", event.Path)
})).ForPath("/node")
assert.Equal(s.T(), data, data2)
assert.NoError(s.T(), err)
events <- zk.Event{
Type: zk.EventNodeDataChanged,
Path: "/node",
}
})
}
type SetDataBuilderTestSuite struct {
mockContainerTestSuite
}
func TestSetDataBuilder(t *testing.T) {
suite.Run(t, new(SetDataBuilderTestSuite))
}
func (s *SetDataBuilderTestSuite) TestSetData() {
s.With(func(builder *CuratorFrameworkBuilder, client CuratorFramework, conn *mockConn, compress *mockCompressionProvider, data []byte, version int32, stat *zk.Stat) {
compress.On("Compress", "/node", data).Return([]byte("compressed(data)"), nil).Once()
conn.On("Set", "/node", []byte("compressed(data)"), version).Return(stat, nil).Once()
stat2, err := client.SetData().WithVersion(version).Compressed().ForPathWithData("/node", data)
assert.Equal(s.T(), stat, stat2)
assert.NoError(s.T(), err)
})
}
func (s *SetDataBuilderTestSuite) TestNamespace() {
s.WithNamespace("parent", func(client CuratorFramework, conn *mockConn, data []byte, stat *zk.Stat) {
conn.On("Exists", "/parent").Return(true, nil, nil).Once()
conn.On("Set", "/parent/child", data, AnyVersion).Return(stat, nil).Once()
stat2, err := client.SetData().ForPathWithData("/child", data)
assert.Equal(s.T(), stat, stat2)
assert.NoError(s.T(), err)
})
}
func (s *SetDataBuilderTestSuite) TestBackground() {
s.WithNamespace("parent", func(client CuratorFramework, conn *mockConn, wg *sync.WaitGroup, data []byte, stat *zk.Stat) {
ctxt := "context"
conn.On("Exists", "/parent").Return(true, nil, nil).Once()
conn.On("Set", "/parent/child", data, AnyVersion).Return(stat, zk.ErrAPIError).Once()
_, err := client.SetData().InBackgroundWithCallbackAndContext(
func(client CuratorFramework, event CuratorEvent) error {
defer wg.Done()
assert.Equal(s.T(), SET_DATA, event.Type())
assert.Equal(s.T(), "/child", event.Path())
assert.Equal(s.T(), data, event.Data())
assert.Equal(s.T(), stat, event.Stat())
assert.EqualError(s.T(), event.Err(), zk.ErrAPIError.Error())
assert.Equal(s.T(), "child", event.Name())
assert.Equal(s.T(), ctxt, event.Context())
return nil
}, ctxt).ForPathWithData("/child", data)
assert.NoError(s.T(), err)
})
}