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 e26315b
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 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 @@ -123,6 +126,7 @@ func (s *testTSODispatcherSuite) SetupTest() {
s.streamInner.generateNext()
s.reqMustReady(req)
}
s.re.True(created.Load())
s.re.NotNil(s.stream)
}

Expand Down

0 comments on commit e26315b

Please sign in to comment.