Skip to content

Commit 95a297e

Browse files
committed
recover possiblely close chan error
1 parent 01b177b commit 95a297e

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

sync_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ func TestClient_SyncJSON_MapObject(t *testing.T) {
126126
waitEventWatch()
127127

128128
assert.Equal(t, "wongoo", m["name"])
129-
assert.Equal(t, float64(1), m["sex"])
129+
130+
assert.Equal(t, 1, int(m["sex"].(float64)))
130131

131132
w.Close()
132133
}
@@ -153,14 +154,15 @@ func TestClient_SyncJSONMap(t *testing.T) {
153154

154155
waitEventWatch()
155156

156-
err = testClient.SetMapJSONValue(path, "u1", &user{Name: "wongoo", Sex: 1})
157+
sex := 1
158+
err = testClient.SetMapJSONValue(path, "u1", &user{Name: "wongoo", Sex: sex})
157159
assert.Nil(t, err)
158160

159161
waitEventWatch()
160162

161163
assert.Equal(t, 1, len(users))
162164
assert.Equal(t, "wongoo", users["u1"].Name)
163-
assert.Equal(t, 1, users["u1"].Sex)
165+
assert.Equal(t, sex, users["u1"].Sex)
164166

165167
err = testClient.SetMapJSONValue(path, "u1", &user{Name: "yang", Sex: 0})
166168
assert.Nil(t, err)

watcher.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ func (w *Watcher) Close() {
7070
w.Lock()
7171
defer w.Unlock()
7272

73+
defer func() {
74+
_ = recover()
75+
}()
76+
7377
select {
7478
case <-w.done:
7579
default:

zkclient.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import (
1515
"github.com/vogo/logger"
1616
)
1717

18+
const (
19+
connCheckInterval = time.Second * 20
20+
)
21+
1822
// AlarmTrigger alarm function
1923
type AlarmTrigger func(err error)
2024

@@ -61,7 +65,7 @@ func NewClient(servers []string, options ...ClientOption) *Client {
6165
// collectDeadWatchers start a goroutine to maintain the zk connection
6266
func (cli *Client) startConnMaintainer() {
6367
go func() {
64-
ticker := time.NewTicker(time.Second * 20)
68+
ticker := time.NewTicker(connCheckInterval)
6569

6670
for {
6771
select {

zkclient_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ func connectLocalZK(_ *testing.T) *Client {
5454
return c
5555
}
5656

57+
const (
58+
watchWaitInterval = time.Second * 2
59+
)
60+
5761
func waitEventWatch() {
58-
time.Sleep(time.Second * 2)
62+
time.Sleep(watchWaitInterval)
5963
}

0 commit comments

Comments
 (0)