Skip to content

Commit

Permalink
zkconn should honor memory zk server logger type
Browse files Browse the repository at this point in the history
current zkconn logger is auto set to new context logger which is
incorrect as there are cases when the user could have set the memory zk
server logger to be of discard type
  • Loading branch information
jgheewala committed Oct 12, 2021
1 parent 58a037b commit 9c44556
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
15 changes: 14 additions & 1 deletion zkplus/zktest/zktest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package zktest

import (
"errors"
"reflect"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -134,15 +135,27 @@ func (z *MemoryZkServer) Conn() (ZkConnSupported, <-chan zk.Event, error) {
return z.Connect()
}

// based on MemoryZkServer logger type set the logger for ZkConn
// as we got to honor if the logger is set to discard
func (z *ZkConn) setZkConnLogger(logger log.Logger, nextID *int64) {
if reflect.TypeOf(logger) == reflect.TypeOf(log.Discard) {
z.Logger = log.Discard
} else {
z.Logger = log.NewContext(z.Logger).With("id", atomic.AddInt64(nextID, 1))
}
}

// Connect to this server
func (z *MemoryZkServer) Connect() (*ZkConn, <-chan zk.Event, error) {
r := &ZkConn{
connectedTo: z,
Logger: log.NewContext(z.Logger).With("id", atomic.AddInt64(&z.nextID, 1)),
events: make(chan zk.Event, 1000),
pathWatch: make(map[string]chan zk.Event),
chanTimeout: z.ChanTimeout,
}

r.setZkConnLogger(z.Logger, &z.nextID)

z.childrenConnectionsLock.Lock()
defer z.childrenConnectionsLock.Unlock()
z.childrenConnections[r] = struct{}{}
Expand Down
9 changes: 9 additions & 0 deletions zkplus/zktest/zktest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,12 @@ func testChildrenWNotHere(t *testing.T, z ZkConnSupported, z2 ZkConnSupported, _
case <-time.After(time.Microsecond):
}
}

func TestDiscardLoggerSetup(t *testing.T) {
zkConn := &ZkConn{}
nextID := int64(0)
zkConn.setZkConnLogger(log.Discard, &nextID)
assert.Equal(t, zkConn.Logger, log.Discard)
zkConn.setZkConnLogger(log.DefaultLogger, &nextID)
assert.NotEqual(t, zkConn.Logger, log.Discard)
}

0 comments on commit 9c44556

Please sign in to comment.