Skip to content

Commit

Permalink
Add unit test to check for data race when accessing 'currentState' co…
Browse files Browse the repository at this point in the history
…ncurrently.
  • Loading branch information
urvisavla committed Jun 2, 2023
1 parent b8ddf99 commit 0ec5441
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions services/horizon/internal/ingest/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"database/sql"
"testing"
"time"

"github.com/jmoiron/sqlx"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -242,6 +243,46 @@ func TestMaybeVerifyInternalDBErrCancelOrContextCanceled(t *testing.T) {
historyQ.AssertExpectations(t)
}

func TestCurrentStateRaceCondition(t *testing.T) {
historyQ := &mockDBQ{}
s := &system{
historyQ: historyQ,
ctx: context.Background(),
}

historyQ.On("GetTx").Return(nil)
historyQ.On("Begin").Return(nil)
historyQ.On("Rollback").Return(nil)
historyQ.On("GetLastLedgerIngest", s.ctx).Return(uint32(1), nil)
historyQ.On("GetIngestVersion", s.ctx).Return(CurrentVersion, nil)

timer := time.NewTimer(2000 * time.Millisecond)
getCh := make(chan bool, 1)
doneCh := make(chan bool, 1)
go func() {
var state = buildState{checkpointLedger: 8,
skipChecks: true,
stop: true}
for range getCh {
s.runStateMachine(state)

This comment has been minimized.

Copy link
@tsachiherman

tsachiherman Jun 2, 2023

Contributor

check the return value of runStateMachine here to avoid the nagging warning massage.

}
close(doneCh)
}()

loop:
for {
s.GetCurrentState()
select {
case <-timer.C:
break loop
default:
}
getCh <- true
}
close(getCh)
<-doneCh
}

type mockDBQ struct {
mock.Mock

Expand Down

0 comments on commit 0ec5441

Please sign in to comment.