Skip to content

Commit 8be4b48

Browse files
committed
feat: Add test for validating the fix
1 parent e5c8361 commit 8be4b48

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package batchsender
2+
3+
import (
4+
"sync"
5+
"testing"
6+
"time"
7+
)
8+
9+
// This test verifies there is no data race between Send() and the timer-triggered flush.
10+
func TestSend_ConcurrentWithTimerFlush(t *testing.T) {
11+
// The race occurs when:
12+
// 1. a Send() call schedules a timer via time.AfterFunc
13+
// 2. the timer fires and calls flush() on a separate goroutine
14+
// 3. another Send() reads bs.items concurrently.
15+
//
16+
// To trigger this, we send items from multiple goroutines with delays around batchTimeout so the timer fires between Sends.
17+
var mu sync.Mutex
18+
var received []any
19+
20+
const numGoroutines = 5
21+
const sendsPerGoroutine = 20
22+
23+
bs := NewBatchSender(func(items any) {
24+
mu.Lock()
25+
defer mu.Unlock()
26+
received = append(received, items)
27+
})
28+
29+
var wg sync.WaitGroup
30+
wg.Add(numGoroutines)
31+
for range numGoroutines {
32+
go func() {
33+
defer wg.Done()
34+
for range sendsPerGoroutine {
35+
bs.Send("item")
36+
time.Sleep(batchTimeout + 10*time.Millisecond)
37+
}
38+
}()
39+
}
40+
41+
wg.Wait()
42+
bs.Close()
43+
}

0 commit comments

Comments
 (0)