Skip to content

Commit

Permalink
Fix data race
Browse files Browse the repository at this point in the history
Signed-off-by: MyonKeminta <[email protected]>
  • Loading branch information
MyonKeminta committed Sep 25, 2024
1 parent 0bfe55f commit 0bfe9a6
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion client/tso_dispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -120,9 +123,14 @@ func (s *testTSODispatcherSuite) SetupTest() {
ctx := context.Background()
req := s.sendReq(ctx)
s.reqMustNotReady(req)
// Wait until created
for !created.Load() {
time.Sleep(time.Millisecond)
}
s.streamInner.generateNext()
s.reqMustReady(req)
}
s.re.True(created.Load())
s.re.NotNil(s.stream)
}

Expand Down

0 comments on commit 0bfe9a6

Please sign in to comment.