Skip to content

Commit fc45a96

Browse files
committed
fix(test): resolve data races in redis mock and nexmark
1. Fix data race in pkg/mock/test_source.go used by Redis tests. 2. Fix data race in global slice access in internal/io/nexmark/event.go. Signed-off-by: Jiyong Huang <huangjy@emqx.io>
1 parent 8513189 commit fc45a96

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

internal/io/nexmark/event.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"math/rand"
2020
"strings"
21+
"sync"
2122
)
2223

2324
var (
@@ -29,6 +30,7 @@ var (
2930
PersonIDs []uint64
3031
AuctionIDs []uint64
3132
categoriesCount int
33+
mu sync.RWMutex
3234
)
3335

3436
func init() {
@@ -68,11 +70,15 @@ func (p Person) ToMap() map[string]interface{} {
6870

6971
func genPersonID(r *rand.Rand) uint64 {
7072
id := uint64(r.Int())
73+
mu.Lock()
74+
defer mu.Unlock()
7175
PersonIDs = append(PersonIDs, id)
7276
return id
7377
}
7478

7579
func pickPersonID(r *rand.Rand) uint64 {
80+
mu.RLock()
81+
defer mu.RUnlock()
7682
return PersonIDs[r.Int()%len(PersonIDs)]
7783
}
7884

@@ -152,11 +158,15 @@ func NewAuction(eventID int64, time uint64) Auction {
152158

153159
func genAuctionID(r *rand.Rand) uint64 {
154160
id := uint64(r.Int())
161+
mu.Lock()
162+
defer mu.Unlock()
155163
AuctionIDs = append(AuctionIDs, id)
156164
return id
157165
}
158166

159167
func pickAuctionID(r *rand.Rand) uint64 {
168+
mu.RLock()
169+
defer mu.RUnlock()
160170
return AuctionIDs[r.Int()%len(AuctionIDs)]
161171
}
162172

pkg/mock/test_source.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,11 @@ func TestSourceConnectorCompare(t *testing.T, r api.Source, props map[string]any
149149
}
150150
}()
151151
case api.BytesSource:
152-
err = ss.Subscribe(ctx, ingestBytes, ingestErr)
152+
err := ss.Subscribe(ctx, ingestBytes, ingestErr)
153+
assert.NoError(t, err)
153154
case api.TupleSource:
154-
err = ss.Subscribe(ctx, ingestTuples, ingestErr)
155+
err := ss.Subscribe(ctx, ingestTuples, ingestErr)
156+
assert.NoError(t, err)
155157
default:
156158
panic("wrong source type")
157159
}

0 commit comments

Comments
 (0)