diff --git a/common/util.go b/common/util.go index 97fe8bb0781..ae5ef54aec6 100644 --- a/common/util.go +++ b/common/util.go @@ -360,7 +360,10 @@ func CreateChildContext( // GenerateRandomString is used for generate test string func GenerateRandomString(n int) string { - rand.Seed(time.Now().UnixNano()) + if n <= 0 { + return "" + } + letterRunes := []rune("random") b := make([]rune, n) for i := range b { diff --git a/common/util_test.go b/common/util_test.go index 533e0a46fac..61e3eee26a5 100644 --- a/common/util_test.go +++ b/common/util_test.go @@ -757,3 +757,16 @@ func TestDomainIDToHistoryShard(t *testing.T) { }) } } + +func TestGenerateRandomString(t *testing.T) { + for input, wantSize := range map[int]int{ + -1: 0, + 0: 0, + 10: 10, + } { + t.Run(fmt.Sprintf("%d", input), func(t *testing.T) { + got := GenerateRandomString(input) + require.Len(t, got, wantSize) + }) + } +}