Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix the set of events received by ChildrenW #66

Merged
merged 2 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 30 additions & 23 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const (
type watchType int

const (
watchTypeData = iota
watchTypeData watchType = iota
watchTypeExist
watchTypeChild
)
Expand Down Expand Up @@ -530,6 +530,33 @@ func (c *Conn) flushRequests(err error) {
c.requestsLock.Unlock()
}

// Send event to all interested watchers
func (c *Conn) notifyWatches(ev Event) {
var wTypes []watchType
switch ev.Type {
case EventNodeCreated:
wTypes = []watchType{watchTypeExist}
case EventNodeDataChanged:
wTypes = []watchType{watchTypeExist, watchTypeData}
case EventNodeChildrenChanged:
wTypes = []watchType{watchTypeChild}
case EventNodeDeleted:
wTypes = []watchType{watchTypeExist, watchTypeData, watchTypeChild}
}
c.watchersLock.Lock()
defer c.watchersLock.Unlock()
for _, t := range wTypes {
wpt := watchPathType{ev.Path, t}
if watchers := c.watchers[wpt]; len(watchers) > 0 {
for _, ch := range watchers {
ch <- ev
close(ch)
}
delete(c.watchers, wpt)
}
}
}

// Send error to all watchers and clear watchers map
func (c *Conn) invalidateWatches(err error) {
c.watchersLock.Lock()
Expand Down Expand Up @@ -801,7 +828,7 @@ func (c *Conn) recvLoop(conn net.Conn) error {

if res.Xid == -1 {
res := &watcherEvent{}
_, err := decodePacket(buf[16:blen], res)
_, err = decodePacket(buf[16:blen], res)
if err != nil {
return err
}
Expand All @@ -812,27 +839,7 @@ func (c *Conn) recvLoop(conn net.Conn) error {
Err: nil,
}
c.sendEvent(ev)
wTypes := make([]watchType, 0, 2)
switch res.Type {
case EventNodeCreated:
wTypes = append(wTypes, watchTypeExist)
case EventNodeDeleted, EventNodeDataChanged:
wTypes = append(wTypes, watchTypeExist, watchTypeData, watchTypeChild)
case EventNodeChildrenChanged:
wTypes = append(wTypes, watchTypeChild)
}
c.watchersLock.Lock()
for _, t := range wTypes {
wpt := watchPathType{res.Path, t}
if watchers := c.watchers[wpt]; watchers != nil && len(watchers) > 0 {
for _, ch := range watchers {
ch <- ev
close(ch)
}
delete(c.watchers, wpt)
}
}
c.watchersLock.Unlock()
c.notifyWatches(ev)
} else if res.Xid == -2 {
// Ping response. Ignore.
} else if res.Xid < 0 {
Expand Down
86 changes: 86 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package zk

import (
"context"
"fmt"
"io/ioutil"
"sync"
"testing"
Expand Down Expand Up @@ -80,3 +81,88 @@ func TestDeadlockInClose(t *testing.T) {
t.Fatal("apparent deadlock!")
}
}

func TestNotifyWatches(t *testing.T) {
cases := []struct {
eType EventType
path string
watches map[watchPathType]bool
}{
{
EventNodeCreated, "/",
map[watchPathType]bool{
{"/", watchTypeExist}: true,
{"/", watchTypeChild}: false,
{"/", watchTypeData}: false,
},
},
{
EventNodeCreated, "/a",
map[watchPathType]bool{
{"/b", watchTypeExist}: false,
},
},
{
EventNodeDataChanged, "/",
map[watchPathType]bool{
{"/", watchTypeExist}: true,
{"/", watchTypeData}: true,
{"/", watchTypeChild}: false,
},
},
{
EventNodeChildrenChanged, "/",
map[watchPathType]bool{
{"/", watchTypeExist}: false,
{"/", watchTypeData}: false,
{"/", watchTypeChild}: true,
},
},
{
EventNodeDeleted, "/",
map[watchPathType]bool{
{"/", watchTypeExist}: true,
{"/", watchTypeData}: true,
{"/", watchTypeChild}: true,
},
},
}

conn := &Conn{watchers: make(map[watchPathType][]chan Event)}

for idx, c := range cases {
t.Run(fmt.Sprintf("#%d %s", idx, c.eType), func(t *testing.T) {
c := c

notifications := make([]struct {
path string
notify bool
ch <-chan Event
}, len(c.watches))

var idx int
for wpt, expectEvent := range c.watches {
ch := conn.addWatcher(wpt.path, wpt.wType)
notifications[idx].path = wpt.path
notifications[idx].notify = expectEvent
notifications[idx].ch = ch
idx++
}
ev := Event{Type: c.eType, Path: c.path}
conn.notifyWatches(ev)

for _, res := range notifications {
select {
case e := <-res.ch:
if !res.notify || e.Path != res.path {
t.Fatal("unexpeted notification received")
}
default:
if res.notify {
t.Fatal("expected notification not received")
}
}
}
})
}
}