diff --git a/services/horizon/internal/ingest/main_test.go b/services/horizon/internal/ingest/main_test.go index 65c9f47e25..920942b593 100644 --- a/services/horizon/internal/ingest/main_test.go +++ b/services/horizon/internal/ingest/main_test.go @@ -5,6 +5,7 @@ import ( "context" "database/sql" "testing" + "time" "github.com/jmoiron/sqlx" "github.com/prometheus/client_golang/prometheus" @@ -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) + } + close(doneCh) + }() + +loop: + for { + s.GetCurrentState() + select { + case <-timer.C: + break loop + default: + } + getCh <- true + } + close(getCh) + <-doneCh +} + type mockDBQ struct { mock.Mock