backtester/engine: Fix TestStart race (#2024)

* Fix TestStart race

TestStart does 3 tests
test1 = Start() with no error
test2 = Start() on an already started struct
test3 = Start() on nil

Previously, test1 and test2 were on the same struct. Depending on
the go scheduler, we could have test2 fail because the field "started"
could go from 1 to 0 just after the atomic compare and swap.
So we would Start() a second time on the same struct but the field "started"
would already be back to 0.

* Divide TestStart in 3 tests

* Resolve comments

---------

Co-authored-by: mathieu cesbron <mathieucesbron@mathieus-MacBook-Pro.local>
This commit is contained in:
Mathieu Cesbron
2025-09-05 15:26:30 +08:00
committed by GitHub
parent 454de17bf4
commit 60f7cb37f2

View File

@@ -2,7 +2,6 @@ package engine
import (
"sync"
"sync/atomic"
"testing"
"time"
@@ -65,21 +64,25 @@ func TestSetupLiveDataHandler(t *testing.T) {
func TestStart(t *testing.T) {
t.Parallel()
dc := &dataChecker{
var dc *dataChecker
err := dc.Start()
assert.ErrorIs(t, err, gctcommon.ErrNilPointer)
dc = &dataChecker{
shutdown: make(chan bool),
}
err := dc.Start()
assert.NoError(t, err)
err = dc.Start()
require.NoError(t, err)
close(dc.shutdown)
dc.wg.Wait()
atomic.CompareAndSwapUint32(&dc.started, 0, 1)
dc = &dataChecker{
started: 1,
}
err = dc.Start()
assert.ErrorIs(t, err, engine.ErrSubSystemAlreadyStarted)
var dh *dataChecker
err = dh.Start()
assert.ErrorIs(t, err, gctcommon.ErrNilPointer)
}
func TestDataCheckerIsRunning(t *testing.T) {