Skip to content

Commit

Permalink
Add sync.Mutex to read/write MockBackend startedAt var
Browse files Browse the repository at this point in the history
Signed-off-by: James Ryans <[email protected]>
  • Loading branch information
james-ryans committed Jan 14, 2024
1 parent 1de0e36 commit 243c2e9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion testbed/runtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ TEST_COLORIZE="${SED} 's/PASS/${PASS_COLOR}/' | ${SED} 's/FAIL/${FAIL_COLOR}/'"

mkdir -p results/junit

RUN_TESTBED=1 go test -v ${TEST_ARGS} 2>&1 | tee results/testoutput.log | bash -c "${TEST_COLORIZE}"
RUN_TESTBED=1 go test -race -v ${TEST_ARGS} 2>&1 | tee results/testoutput.log | bash -c "${TEST_COLORIZE}"

testStatus=${PIPESTATUS[0]}

Expand Down
11 changes: 8 additions & 3 deletions testbed/testbed/mock_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ type MockBackend struct {
logFile *os.File

// Start/stop flags
isStarted bool
stopOnce sync.Once
startedAt time.Time
isStarted bool
stopOnce sync.Once
startedAt time.Time
startMutex sync.Mutex

// Recording fields.
isRecording bool
Expand Down Expand Up @@ -100,6 +101,8 @@ func (mb *MockBackend) Start() error {
}

mb.isStarted = true
mb.startMutex.Lock()
defer mb.startMutex.Unlock()
mb.startedAt = time.Now()
return nil
}
Expand Down Expand Up @@ -130,6 +133,8 @@ func (mb *MockBackend) EnableRecording() {
}

func (mb *MockBackend) GetStats() string {
mb.startMutex.Lock()
defer mb.startMutex.Unlock()
received := mb.DataItemsReceived()
return printer.Sprintf("Received:%10d items (%d/sec)", received, int(float64(received)/time.Since(mb.startedAt).Seconds()))
}
Expand Down

0 comments on commit 243c2e9

Please sign in to comment.