Skip to content

Commit

Permalink
Fixed unit test race condition in TestEleSession PollerPrepare
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoff Bourne committed Apr 25, 2017
1 parent d7a9ea7 commit 0e20321
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
24 changes: 12 additions & 12 deletions poller/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (fm frameMatcher) Matches(in interface{}) bool {
return true
}

func setupConnStreamExpectations(ctrl *gomock.Controller, expectAuth bool) (eleConn *MockConnection,
func setupConnStreamExpectations(ctrl *gomock.Controller, guid string, expectAuth bool) (eleConn *MockConnection,
reconciler *MockChecksReconciler,
writesHere *utils.BlockingReadBuffer, readsHere *utils.BlockingReadBuffer) {
eleConn = NewMockConnection(ctrl)
Expand All @@ -72,11 +72,11 @@ func setupConnStreamExpectations(ctrl *gomock.Controller, expectAuth bool) (eleC

eleConn.EXPECT().GetFarendWriter().AnyTimes().Return(writesHere)
eleConn.EXPECT().GetFarendReader().AnyTimes().Return(readsHere)
eleConn.EXPECT().GetGUID().AnyTimes().Return("1-2-3")
eleConn.EXPECT().GetLogPrefix().AnyTimes().Return("1-2-3")
eleConn.EXPECT().GetGUID().AnyTimes().Return(guid)
eleConn.EXPECT().GetLogPrefix().AnyTimes().Return(guid)
eleConn.EXPECT().SetReadDeadline(gomock.Any()).AnyTimes()
eleConn.EXPECT().SetWriteDeadline(gomock.Any()).AnyTimes()
eleConn.EXPECT().GetLogPrefix().AnyTimes().Return("1-2-3")
eleConn.EXPECT().GetLogPrefix().AnyTimes().Return(guid)
if expectAuth {
authCh := make(chan struct{}, 1)
eleConn.EXPECT().SetAuthenticated().Do(func() {
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestEleSession_HeartbeatSending(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

eleConn, reconciler, writesHere, readsHere := setupConnStreamExpectations(ctrl, true)
eleConn, reconciler, writesHere, readsHere := setupConnStreamExpectations(ctrl, "HeartbeatSending", true)
defer readsHere.Close()

origTimestamper := installDeterministicTimestamper(1000, 2000)
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestEleSession_HeartbeatConsumption(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

eleConn, reconciler, writesHere, readsHere := setupConnStreamExpectations(ctrl, true)
eleConn, reconciler, writesHere, readsHere := setupConnStreamExpectations(ctrl, "HeartbeatConsumption", true)
defer readsHere.Close()

origTimestamper := installDeterministicTimestamper(1000, 2000)
Expand Down Expand Up @@ -229,7 +229,7 @@ func TestEleSession_HandshakeError(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

eleConn, reconciler, writesHere, readsHere := setupConnStreamExpectations(ctrl, false)
eleConn, reconciler, writesHere, readsHere := setupConnStreamExpectations(ctrl, "HandshakeError", false)
defer readsHere.Close()

eventConsumer := newPhasingEventConsumer()
Expand All @@ -247,7 +247,7 @@ func TestEleSession_HandshakeTimeout(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

eleConn, reconciler, _, readsHere := setupConnStreamExpectations(ctrl, false)
eleConn, reconciler, _, readsHere := setupConnStreamExpectations(ctrl, "HandshakeTimeout", false)
defer readsHere.Close()

eleConn.EXPECT().Close()
Expand All @@ -266,7 +266,7 @@ func TestEleSession_HandshakeTimeoutStoppedOnSuccess(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

eleConn, reconciler, writesHere, readsHere := setupConnStreamExpectations(ctrl, true)
eleConn, reconciler, writesHere, readsHere := setupConnStreamExpectations(ctrl, "HandshakeTimeoutStoppedOnSuccess", true)
defer readsHere.Close()

origTimestamper := installDeterministicTimestamper(1000, 2000)
Expand Down Expand Up @@ -567,7 +567,7 @@ func TestEleSession_PollerPrepare(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

eleConn, reconciler, writesHere, readsHere := setupConnStreamExpectations(ctrl, true)
eleConn, reconciler, writesHere, readsHere := setupConnStreamExpectations(ctrl, tt.name, true)
defer readsHere.Close()

origTimestamper := installDeterministicTimestamper(1000, 2000)
Expand Down Expand Up @@ -658,7 +658,7 @@ func TestEleSession_PollerPrepareTimeout(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

eleConn, reconciler, writesHere, readsHere := setupConnStreamExpectations(ctrl, true)
eleConn, reconciler, writesHere, readsHere := setupConnStreamExpectations(ctrl, tt.name, true)
defer readsHere.Close()

origTimestamper := installDeterministicTimestamper(1000, 2000)
Expand Down Expand Up @@ -727,7 +727,7 @@ func TestEleSession_CheckTest(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

eleConn, reconciler, writesHere, readsHere := setupConnStreamExpectations(ctrl, true)
eleConn, reconciler, writesHere, readsHere := setupConnStreamExpectations(ctrl, tt.name, true)
defer readsHere.Close()

origTimestamper := installDeterministicTimestamper(1000, 2000)
Expand Down
12 changes: 11 additions & 1 deletion utils/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
type BlockingReadBuffer struct {
bytes.Buffer
blockingCond *sync.Cond
bufferGuard sync.Mutex
closed bool
}

Expand All @@ -50,7 +51,9 @@ func (bb *BlockingReadBuffer) Read(p []byte) (n int, err error) {
return 0, io.EOF
}

bb.bufferGuard.Lock()
n, err = bb.Buffer.Read(p)
bb.bufferGuard.Unlock()
if err != io.EOF {
return
}
Expand All @@ -59,12 +62,19 @@ func (bb *BlockingReadBuffer) Read(p []byte) (n int, err error) {

// ReadReady is a non-blocking operation to see if any bytes are ready to be read.
func (bb *BlockingReadBuffer) ReadReady() bool {
return bb.Buffer.Len() != 0
bb.bufferGuard.Lock()
ready := bb.Buffer.Len() != 0
bb.bufferGuard.Unlock()

return ready
}

// Write places more content in the internal buffer and
func (bb *BlockingReadBuffer) Write(p []byte) (n int, err error) {
bb.bufferGuard.Lock()
n, err = bb.Buffer.Write(p)
bb.bufferGuard.Unlock()

bb.blockingCond.Signal()
return
}
Expand Down

0 comments on commit 0e20321

Please sign in to comment.