This repository has been archived by the owner on Sep 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
exists_test.go
112 lines (82 loc) · 2.95 KB
/
exists_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
package curator
import (
"sync"
"testing"
"github.com/samuel/go-zookeeper/zk"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type CheckExistsBuilderTestSuite struct {
mockContainerTestSuite
}
func TestCheckExistsBuilder(t *testing.T) {
suite.Run(t, new(CheckExistsBuilderTestSuite))
}
func (s *CheckExistsBuilderTestSuite) TestCheckExists() {
s.With(func(client CuratorFramework, conn *mockConn, stat *zk.Stat) {
conn.On("Exists", "/node").Return(true, stat, nil).Once()
stat2, err := client.CheckExists().ForPath("/node")
assert.Equal(s.T(), stat, stat2)
assert.NoError(s.T(), err)
})
s.With(func(client CuratorFramework, conn *mockConn) {
conn.On("Exists", "/node").Return(false, nil, nil).Once()
stat, err := client.CheckExists().ForPath("/node")
assert.Nil(s.T(), stat)
assert.Nil(s.T(), err)
})
s.With(func(client CuratorFramework, conn *mockConn) {
conn.On("Exists", "/node").Return(false, nil, zk.ErrAPIError).Once()
stat, err := client.CheckExists().ForPath("/node")
assert.Nil(s.T(), stat)
assert.EqualError(s.T(), err, zk.ErrAPIError.Error())
})
}
func (s *CheckExistsBuilderTestSuite) TestNamespace() {
s.WithNamespace("parent", func(client CuratorFramework, conn *mockConn) {
conn.On("Exists", "/parent").Return(true, nil, nil).Once()
conn.On("Exists", "/parent/child").Return(false, nil, nil).Once()
stat, err := client.CheckExists().ForPath("/child")
assert.Nil(s.T(), stat)
assert.Nil(s.T(), err)
})
}
func (s *CheckExistsBuilderTestSuite) TestBackground() {
s.WithNamespace("parent", func(client CuratorFramework, conn *mockConn, wg *sync.WaitGroup, stat *zk.Stat) {
ctxt := "context"
conn.On("Exists", "/parent").Return(true, nil, nil).Once()
conn.On("Exists", "/parent/child").Return(true, stat, nil).Once()
stat, err := client.CheckExists().InBackgroundWithCallbackAndContext(
func(client CuratorFramework, event CuratorEvent) error {
defer wg.Done()
assert.Equal(s.T(), EXISTS, event.Type())
assert.Equal(s.T(), "/child", event.Path())
assert.NotNil(s.T(), event.Stat())
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.Nil(s.T(), stat)
assert.NoError(s.T(), err)
})
}
func (s *CheckExistsBuilderTestSuite) TestWatcher() {
s.With(func(client CuratorFramework, conn *mockConn, wg *sync.WaitGroup) {
events := make(chan zk.Event)
defer close(events)
conn.On("ExistsW", "/node").Return(true, &zk.Stat{}, events, nil).Once()
stat, err := client.CheckExists().UsingWatcher(NewWatcher(func(event *zk.Event) {
defer wg.Done()
assert.NotNil(s.T(), event)
assert.Equal(s.T(), zk.EventNodeDeleted, event.Type)
assert.Equal(s.T(), "/node", event.Path)
})).ForPath("/node")
assert.NotNil(s.T(), stat)
assert.NoError(s.T(), err)
events <- zk.Event{
Type: zk.EventNodeDeleted,
Path: "/node",
}
})
}