From e26315bc04996a4ff0d8eaa01fdef8692ba03ce5 Mon Sep 17 00:00:00 2001 From: MyonKeminta Date: Wed, 25 Sep 2024 12:51:11 +0800 Subject: [PATCH] Fix data race Signed-off-by: MyonKeminta --- client/tso_dispatcher_test.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/client/tso_dispatcher_test.go b/client/tso_dispatcher_test.go index f22823d9698..5bdfd309f64 100644 --- a/client/tso_dispatcher_test.go +++ b/client/tso_dispatcher_test.go @@ -90,13 +90,16 @@ func (s *testTSODispatcherSuite) SetupTest() { s.option.timeout = time.Hour // As the internal logic of the tsoDispatcher allows it to create streams multiple times, but our tests needs // single stable access to the inner stream, we do not allow it to create it more than once in these tests. + creating := new(atomic.Bool) + // To avoid data race on reading `stream` and `streamInner` fields. created := new(atomic.Bool) createStream := func(ctx context.Context) *tsoStream { - if !created.CompareAndSwap(false, true) { + if !creating.CompareAndSwap(false, true) { s.re.FailNow("testTSODispatcherSuite: trying to create stream more than once, which is unsupported in this tests") } s.streamInner = newMockTSOStreamImpl(ctx, resultModeGenerateOnSignal) s.stream = newTSOStream(ctx, mockStreamURL, s.streamInner) + created.Store(true) return s.stream } s.dispatcher = newTSODispatcher(context.Background(), globalDCLocation, defaultMaxTSOBatchSize, newMockTSOServiceProvider(s.option, createStream)) @@ -123,6 +126,7 @@ func (s *testTSODispatcherSuite) SetupTest() { s.streamInner.generateNext() s.reqMustReady(req) } + s.re.True(created.Load()) s.re.NotNil(s.stream) }