Skip to content

Commit

Permalink
Resolve data races in tests utilizing go routines (#29)
Browse files Browse the repository at this point in the history
* Remove parallel execution for rapid tests

* Resolve data races in tests

* Add race test to workflow

* Resolve faulty path in test dir

* Remove double context cancels

* Add newline to the workflow file
  • Loading branch information
zivkovicmilos authored Oct 14, 2022
1 parent d583c51 commit 20516d0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,18 @@ jobs:
uses: codecov/codecov-action@v3
with:
files: coverage.out
test-race:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: 1.18.x

- name: Checkout code
uses: actions/checkout@v3
with:
submodules: recursive

- name: Go race test
run: go test -race -shuffle=on -timeout 28m ./...
17 changes: 15 additions & 2 deletions core/ibft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,8 @@ func TestRunCommit(t *testing.T) {
t.Parallel()

var (
wg sync.WaitGroup

proposal = []byte("block proposal")
proposalHash = []byte("proposal hash")
signer = []byte("signer")
Expand Down Expand Up @@ -1023,9 +1025,12 @@ func TestRunCommit(t *testing.T) {

ctx, cancelFn := context.WithCancel(context.Background())

wg.Add(1)

go func(i *IBFT) {
defer func() {
cancelFn()
wg.Done()
}()

select {
Expand Down Expand Up @@ -1054,6 +1059,7 @@ func TestRunCommit(t *testing.T) {
assert.Equal(t, insertedCommittedSeals, committedSeals)

// Make sure the proper done channel was notified
wg.Wait()
assert.True(t, doneReceived)
},
)
Expand Down Expand Up @@ -2089,6 +2095,8 @@ func TestIBFT_WatchForFutureRCC(t *testing.T) {
setRoundForMessages(roundChangeMessages, rccRound)

var (
wg sync.WaitGroup

receivedRound = uint64(0)
notifyCh = make(chan uint64, 1)

Expand Down Expand Up @@ -2126,10 +2134,14 @@ func TestIBFT_WatchForFutureRCC(t *testing.T) {
i.messages = messages

ctx, cancelFn := context.WithCancel(context.Background())
defer cancelFn()

wg.Add(1)

go func() {
defer cancelFn()
defer func() {
cancelFn()
wg.Done()
}()

select {
case r := <-i.roundCertificate:
Expand All @@ -2146,6 +2158,7 @@ func TestIBFT_WatchForFutureRCC(t *testing.T) {
i.wg.Wait()

// Make sure the notification round was correct
wg.Wait()
assert.Equal(t, rccRound, receivedRound)
}

Expand Down
2 changes: 1 addition & 1 deletion core/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func (wg *mockNodeWg) getDone() int64 {
}

func (wg *mockNodeWg) resetDone() {
wg.count = 0
atomic.StoreInt64(&wg.count, 0)
}

// mockCluster represents a mock IBFT cluster
Expand Down

0 comments on commit 20516d0

Please sign in to comment.