forked from curator-go/curator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync_test.go
66 lines (50 loc) · 1.74 KB
/
sync_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
package curator
import (
"sync"
"testing"
"github.com/go-zookeeper/zk"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type SyncBuilderTestSuite struct {
mockContainerTestSuite
}
func TestSyncBuilder(t *testing.T) {
suite.Run(t, new(SyncBuilderTestSuite))
}
func (s *SyncBuilderTestSuite) TestSync() {
s.With(func(client CuratorFramework, conn *mockConn, stat *zk.Stat) {
conn.On("Sync", "/node").Return("/node", nil).Once()
path, err := client.Sync().ForPath("/node")
assert.Equal(s.T(), "/node", path)
assert.NoError(s.T(), err)
})
}
func (s *SyncBuilderTestSuite) TestNamespace() {
s.WithNamespace("parent", func(client CuratorFramework, conn *mockConn) {
conn.On("Exists", "/parent").Return(true, nil, nil).Once()
conn.On("Sync", "/parent/child").Return("", zk.ErrNoNode).Once()
path, err := client.Sync().ForPath("/child")
assert.Equal(s.T(), "/child", path)
assert.EqualError(s.T(), err, zk.ErrNoNode.Error())
})
}
func (s *SyncBuilderTestSuite) TestBackground() {
s.WithNamespace("parent", func(client CuratorFramework, conn *mockConn, wg *sync.WaitGroup) {
ctxt := "context"
conn.On("Exists", "/parent").Return(true, nil, nil).Once()
conn.On("Sync", "/parent/child").Return("/parent/child", nil).Once()
path, err := client.Sync().InBackgroundWithCallbackAndContext(
func(client CuratorFramework, event CuratorEvent) error {
defer wg.Done()
assert.Equal(s.T(), SYNC, event.Type())
assert.Equal(s.T(), "/child", event.Path())
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.Equal(s.T(), "/child", path)
assert.NoError(s.T(), err)
})
}