Util: Fix panic when generating UIDs concurrently (#69476)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -16,6 +17,25 @@ func TestAllowedCharMatchesUidPattern(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Run with "go test -race -run ^TestThreadSafe$ github.com/grafana/grafana/pkg/util"
|
||||
func TestThreadSafe(t *testing.T) {
|
||||
// This test was used to showcase the bug, unfortunately there is
|
||||
// no way to enable the -race flag programmatically.
|
||||
t.Skip()
|
||||
// Use 1000 go routines to create 100 UIDs each at roughly the same time.
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 1000; i++ {
|
||||
go func() {
|
||||
for ii := 0; ii < 100; ii++ {
|
||||
_ = GenerateShortUID()
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
wg.Add(1)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func TestRandomUIDs(t *testing.T) {
|
||||
for i := 0; i < 100; i++ {
|
||||
v := GenerateShortUID()
|
||||
|
||||
Reference in New Issue
Block a user