mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-11 00:02:28 +08:00
fix(random): avoid concurrent rand.Seed causing panic (#345)
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/duke-git/lancet/v2/internal"
|
||||
@@ -376,3 +377,31 @@ func TestRandNumberOfLength(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestRandNumberOfLength")
|
||||
assert.Equal(6, len(strconv.Itoa(randi)))
|
||||
}
|
||||
|
||||
// TestRandStringConcurrent verifies RandString is safe under high concurrency.
|
||||
// Before the fix, this test may panic or trigger data races.
|
||||
// After the fix, it should always pass.
|
||||
func TestRandStringConcurrent(t *testing.T) {
|
||||
const (
|
||||
goroutines = 100
|
||||
iterations = 1000
|
||||
length = 32
|
||||
)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(goroutines)
|
||||
|
||||
for g := 0; g < goroutines; g++ {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for i := 0; i < iterations; i++ {
|
||||
s := RandString(length)
|
||||
if len(s) != length {
|
||||
t.Fatalf("unexpected string length: got %d, want %d", len(s), length)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user