Skip to content

Commit

Permalink
Merge branch 'alxzh-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel committed May 18, 2016
2 parents 5250732 + b3a64c3 commit 0c47a49
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
4 changes: 2 additions & 2 deletions zk/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ func (c *Conn) recvLoop(conn net.Conn) error {

if res.Xid == -1 {
res := &watcherEvent{}
_, err := decodePacket(buf[16:16+blen], res)
_, err := decodePacket(buf[16:blen], res)
if err != nil {
return err
}
Expand Down Expand Up @@ -653,7 +653,7 @@ func (c *Conn) recvLoop(conn net.Conn) error {
if res.Err != 0 {
err = res.Err.toError()
} else {
_, err = decodePacket(buf[16:16+blen], req.recvStruct)
_, err = decodePacket(buf[16:blen], req.recvStruct)
}
if req.recvFunc != nil {
req.recvFunc(req, &res, err)
Expand Down
57 changes: 57 additions & 0 deletions zk/zk_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package zk

import (
"crypto/rand"
"encoding/hex"
"fmt"
"io"
"net"
"strings"
"testing"
"time"

"go4.org/net/throttle"
)

func TestCreate(t *testing.T) {
Expand Down Expand Up @@ -181,6 +185,59 @@ func TestAuth(t *testing.T) {
}
}

func TestChildren(t *testing.T) {
ts, err := StartTestCluster(1, nil, logWriter{t: t, p: "[ZKERR] "})
if err != nil {
t.Fatal(err)
}
defer ts.Stop()
zk, _, err := ts.ConnectAll()
if err != nil {
t.Fatalf("Connect returned error: %+v", err)
}
defer zk.Close()

deleteNode := func(node string) {
if err := zk.Delete(node, -1); err != nil && err != ErrNoNode {
t.Fatalf("Delete returned error: %+v", err)
}
}

deleteNode("/gozk-test-big")

if path, err := zk.Create("/gozk-test-big", []byte{1, 2, 3, 4}, 0, WorldACL(PermAll)); err != nil {
t.Fatalf("Create returned error: %+v", err)
} else if path != "/gozk-test-big" {
t.Fatalf("Create returned different path '%s' != '/gozk-test-big'", path)
}

rb := make([]byte, 1000)
hb := make([]byte, 2000)
prefix := []byte("/gozk-test-big/")
for i := 0; i < 10000; i++ {
_, err := rand.Read(rb)
if err != nil {
t.Fatal("Cannot create random znode name")
}
hex.Encode(hb, rb)

expect := string(append(prefix, hb...))
if path, err := zk.Create(expect, []byte{1, 2, 3, 4}, 0, WorldACL(PermAll)); err != nil {
t.Fatalf("Create returned error: %+v", err)
} else if path != expect {
t.Fatalf("Create returned different path '%s' != '%s'", path, expect)
}
defer deleteNode(string(expect))
}

children, _, err := zk.Children("/gozk-test-big")
if err != nil {
t.Fatalf("Children returned error: %+v", err)
} else if len(children) != 10000 {
t.Fatal("Children returned wrong number of nodes")
}
}

func TestChildWatch(t *testing.T) {
ts, err := StartTestCluster(1, nil, logWriter{t: t, p: "[ZKERR] "})
if err != nil {
Expand Down

0 comments on commit 0c47a49

Please sign in to comment.