Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for GenerateRandomString #5532

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,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 {
Expand Down
13 changes: 13 additions & 0 deletions common/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,3 +707,16 @@ func TestConvertGetTaskFailedCauseToErr(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)
})
}
}