Skip to content

Commit

Permalink
Add unit tests for GenerateRandomString (#5532)
Browse files Browse the repository at this point in the history
* add unit tests for GenerateRandomString

* remove deprecated call
  • Loading branch information
arzonus authored Dec 27, 2023
1 parent ef1dd86 commit 02a9dd8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions common/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}

0 comments on commit 02a9dd8

Please sign in to comment.