fix data racing in the test
@@ -209,6 +209,13 @@ func TestBatchWriter(t *testing.T) {
}
}()
+ isBatchNil := func(writer *batchWriter) bool {
+ writer.mu.Lock()
+ ret := writer.curBatch == nil
+ writer.mu.Unlock()
+ return ret
+ }
+
t.Run("msgWithSizelimit", func(t *testing.T) {
writer := newBatchWriter(
&writerConfig{
@@ -231,28 +238,28 @@ func TestBatchWriter(t *testing.T) {
return atomic.LoadInt32(&batchesCount) == 1, nil
})
require.NoError(err)
- require.Nil(writer.curBatch)
+ require.True(isBatchNil(writer))
writer.Put(_messages[7])
err = testutil.WaitUntil(50*time.Millisecond, 1*time.Second, func() (bool, error) {
return atomic.LoadInt32(&batchesCount) == 1, nil
})
require.NoError(err)
- require.NotNil(writer.curBatch)
+ require.False(isBatchNil(writer))
writer.Put(_messages[3])
err = testutil.WaitUntil(50*time.Millisecond, 1*time.Second, func() (bool, error) {
return atomic.LoadInt32(&batchesCount) == 2, nil
})
require.NoError(err)
- require.Nil(writer.curBatch)
+ require.True(isBatchNil(writer))
writer.Put(_messages[3])
err = testutil.WaitUntil(50*time.Millisecond, 1*time.Second, func() (bool, error) {
return atomic.LoadInt32(&batchesCount) == 2, nil
})
require.NoError(err)
- require.NotNil(writer.curBatch)
+ require.False(isBatchNil(writer))
writer.Close()
require.Error(writer.Put(_messages[3]))
err = testutil.WaitUntil(50*time.Millisecond, 1*time.Second, func() (bool, error) {
@@ -286,7 +293,7 @@ func TestBatchWriter(t *testing.T) {
return atomic.LoadInt32(&batchesCount) == 1, nil
})
require.NoError(err)
- require.Nil(writer.curBatch)
+ require.True(isBatchNil(writer))
writer.Put(_messages[2])
writer.Put(_messages[3])
@@ -295,7 +302,7 @@ func TestBatchWriter(t *testing.T) {
return atomic.LoadInt32(&batchesCount) == 2, nil
})
require.NoError(err)
- require.Nil(writer.curBatch)
+ require.True(isBatchNil(writer))
writer.Put(_messages[4])
writer.Close()
@@ -306,7 +313,7 @@ func TestBatchWriter(t *testing.T) {
return atomic.LoadInt32(&batchesCount) == 3, nil
})
require.NoError(err)
- require.Nil(writer.curBatch)
+ require.True(isBatchNil(writer))
})
}
