Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel committed Aug 26, 2016
1 parent 797c865 commit 44e46c2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 55 deletions.
8 changes: 4 additions & 4 deletions zk/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ const (
type watchType int

const (
watchTypeData = iota
watchTypeExist = iota
watchTypeChild = iota
watchTypeData = iota
watchTypeExist
watchTypeChild
)

type watchPathType struct {
Expand Down Expand Up @@ -251,7 +251,7 @@ func (c *Conn) State() State {
return State(atomic.LoadInt32((*int32)(&c.state)))
}

// SessionId returns the current session id of the connection.
// SessionID returns the current session id of the connection.
func (c *Conn) SessionID() int64 {
return atomic.LoadInt64(&c.sessionID)
}
Expand Down
55 changes: 27 additions & 28 deletions zk/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ const (
)

const (
EventNodeCreated = EventType(1)
EventNodeDeleted = EventType(2)
EventNodeDataChanged = EventType(3)
EventNodeChildrenChanged = EventType(4)
EventNodeCreated EventType = 1
EventNodeDeleted EventType = 2
EventNodeDataChanged EventType = 3
EventNodeChildrenChanged EventType = 4

EventSession = EventType(-1)
EventNotWatching = EventType(-2)
EventSession EventType = -1
EventNotWatching EventType = -2
)

var (
Expand All @@ -55,14 +55,13 @@ var (
)

const (
StateUnknown = State(-1)
StateDisconnected = State(0)
StateConnecting = State(1)
StateAuthFailed = State(4)
StateConnectedReadOnly = State(5)
StateSaslAuthenticated = State(6)
StateExpired = State(-112)
// StateAuthFailed = State(-113)
StateUnknown State = -1
StateDisconnected State = 0
StateConnecting State = 1
StateAuthFailed State = 4
StateConnectedReadOnly State = 5
StateSaslAuthenticated State = 6
StateExpired State = -112

StateConnected = State(100)
StateHasSession = State(101)
Expand Down Expand Up @@ -155,20 +154,20 @@ const (
errBadArguments = -8
errInvalidState = -9
// API errors
errAPIError = ErrCode(-100)
errNoNode = ErrCode(-101) // *
errNoAuth = ErrCode(-102)
errBadVersion = ErrCode(-103) // *
errNoChildrenForEphemerals = ErrCode(-108)
errNodeExists = ErrCode(-110) // *
errNotEmpty = ErrCode(-111)
errSessionExpired = ErrCode(-112)
errInvalidCallback = ErrCode(-113)
errInvalidAcl = ErrCode(-114)
errAuthFailed = ErrCode(-115)
errClosing = ErrCode(-116)
errNothing = ErrCode(-117)
errSessionMoved = ErrCode(-118)
errAPIError ErrCode = -100
errNoNode ErrCode = -101 // *
errNoAuth ErrCode = -102
errBadVersion ErrCode = -103 // *
errNoChildrenForEphemerals ErrCode = -108
errNodeExists ErrCode = -110 // *
errNotEmpty ErrCode = -111
errSessionExpired ErrCode = -112
errInvalidCallback ErrCode = -113
errInvalidAcl ErrCode = -114
errAuthFailed ErrCode = -115
errClosing ErrCode = -116
errNothing ErrCode = -117
errSessionMoved ErrCode = -118
)

// Constants for ACL permissions
Expand Down
38 changes: 19 additions & 19 deletions zk/server_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,41 +99,41 @@ func StartTestCluster(size int, stdout, stderr io.Writer) (*TestCluster, error)
return cluster, nil
}

func (ts *TestCluster) Connect(idx int) (*Conn, error) {
zk, _, err := Connect([]string{fmt.Sprintf("127.0.0.1:%d", ts.Servers[idx].Port)}, time.Second*15)
func (tc *TestCluster) Connect(idx int) (*Conn, error) {
zk, _, err := Connect([]string{fmt.Sprintf("127.0.0.1:%d", tc.Servers[idx].Port)}, time.Second*15)
return zk, err
}

func (ts *TestCluster) ConnectAll() (*Conn, <-chan Event, error) {
return ts.ConnectAllTimeout(time.Second * 15)
func (tc *TestCluster) ConnectAll() (*Conn, <-chan Event, error) {
return tc.ConnectAllTimeout(time.Second * 15)
}

func (ts *TestCluster) ConnectAllTimeout(sessionTimeout time.Duration) (*Conn, <-chan Event, error) {
return ts.ConnectWithOptions(sessionTimeout)
func (tc *TestCluster) ConnectAllTimeout(sessionTimeout time.Duration) (*Conn, <-chan Event, error) {
return tc.ConnectWithOptions(sessionTimeout)
}

func (ts *TestCluster) ConnectWithOptions(sessionTimeout time.Duration, options ...connOption) (*Conn, <-chan Event, error) {
hosts := make([]string, len(ts.Servers))
for i, srv := range ts.Servers {
func (tc *TestCluster) ConnectWithOptions(sessionTimeout time.Duration, options ...connOption) (*Conn, <-chan Event, error) {
hosts := make([]string, len(tc.Servers))
for i, srv := range tc.Servers {
hosts[i] = fmt.Sprintf("127.0.0.1:%d", srv.Port)
}
zk, ch, err := Connect(hosts, sessionTimeout, options...)
return zk, ch, err
}

func (ts *TestCluster) Stop() error {
for _, srv := range ts.Servers {
func (tc *TestCluster) Stop() error {
for _, srv := range tc.Servers {
srv.Srv.Stop()
}
defer os.RemoveAll(ts.Path)
return ts.waitForStop(5, time.Second)
defer os.RemoveAll(tc.Path)
return tc.waitForStop(5, time.Second)
}

// waitForStart blocks until the cluster is up
func (ts *TestCluster) waitForStart(maxRetry int, interval time.Duration) error {
func (tc *TestCluster) waitForStart(maxRetry int, interval time.Duration) error {
// verify that the servers are up with SRVR
serverAddrs := make([]string, len(ts.Servers))
for i, s := range ts.Servers {
serverAddrs := make([]string, len(tc.Servers))
for i, s := range tc.Servers {
serverAddrs[i] = fmt.Sprintf("127.0.0.1:%d", s.Port)
}

Expand All @@ -148,10 +148,10 @@ func (ts *TestCluster) waitForStart(maxRetry int, interval time.Duration) error
}

// waitForStop blocks until the cluster is down
func (ts *TestCluster) waitForStop(maxRetry int, interval time.Duration) error {
func (tc *TestCluster) waitForStop(maxRetry int, interval time.Duration) error {
// verify that the servers are up with RUOK
serverAddrs := make([]string, len(ts.Servers))
for i, s := range ts.Servers {
serverAddrs := make([]string, len(tc.Servers))
for i, s := range tc.Servers {
serverAddrs[i] = fmt.Sprintf("127.0.0.1:%d", s.Port)
}

Expand Down
6 changes: 2 additions & 4 deletions zk/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ func encodeDecodeTest(t *testing.T, r interface{}) {

func TestEncodeShortBuffer(t *testing.T) {
t.Parallel()
buf := make([]byte, 0)
_, err := encodePacket(buf, &requestHeader{1, 2})
_, err := encodePacket([]byte{}, &requestHeader{1, 2})
if err != ErrShortBuffer {
t.Errorf("encodePacket should return ErrShortBuffer on a short buffer instead of '%+v'", err)
return
Expand All @@ -64,8 +63,7 @@ func TestEncodeShortBuffer(t *testing.T) {

func TestDecodeShortBuffer(t *testing.T) {
t.Parallel()
buf := make([]byte, 0)
_, err := decodePacket(buf, &responseHeader{})
_, err := decodePacket([]byte{}, &responseHeader{})
if err != ErrShortBuffer {
t.Errorf("decodePacket should return ErrShortBuffer on a short buffer instead of '%+v'", err)
return
Expand Down

0 comments on commit 44e46c2

Please sign in to comment.