retry the random func to minimize tests flakiness
This commit is contained in:
@@ -3,6 +3,7 @@ package security_test
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/pocketbase/pocketbase/tools/security"
|
||||
@@ -38,55 +39,73 @@ func testRandomStringWithAlphabet(t *testing.T, randomFunc func(n int, alphabet
|
||||
|
||||
for i, s := range scenarios {
|
||||
t.Run(fmt.Sprintf("%d_%q", i, s.alphabet), func(t *testing.T) {
|
||||
generated := make([]string, 0, 1000)
|
||||
generated := make([]string, 0, 500)
|
||||
length := 10
|
||||
|
||||
for j := 0; j < 1000; j++ {
|
||||
result := randomFunc(length, s.alphabet)
|
||||
for j := 0; j < 500; j++ {
|
||||
var run func(int)
|
||||
run = func(attempt int) {
|
||||
result := randomFunc(length, s.alphabet)
|
||||
|
||||
if len(result) != length {
|
||||
t.Fatalf("(%d) Expected the length of the string to be %d, got %d", j, length, len(result))
|
||||
}
|
||||
|
||||
reg := regexp.MustCompile(s.expectPattern)
|
||||
if match := reg.MatchString(result); !match {
|
||||
t.Fatalf("(%d) The generated string should have only %s characters, got %q", j, s.expectPattern, result)
|
||||
}
|
||||
|
||||
for _, str := range generated {
|
||||
if str == result {
|
||||
t.Fatalf("(%d) Repeating random string - found %q in %q", j, result, generated)
|
||||
if len(result) != length {
|
||||
t.Fatalf("(%d) Expected the length of the string to be %d, got %d", j, length, len(result))
|
||||
}
|
||||
|
||||
reg := regexp.MustCompile(s.expectPattern)
|
||||
if match := reg.MatchString(result); !match {
|
||||
t.Fatalf("(%d) The generated string should have only %s characters, got %q", j, s.expectPattern, result)
|
||||
}
|
||||
|
||||
if slices.Contains(generated, result) {
|
||||
if attempt > 3 {
|
||||
t.Fatalf("(%d) Repeating random string - found %q in %q", j, result, generated)
|
||||
}
|
||||
|
||||
// rerun
|
||||
run(attempt + 1)
|
||||
return
|
||||
}
|
||||
|
||||
generated = append(generated, result)
|
||||
}
|
||||
|
||||
generated = append(generated, result)
|
||||
run(1)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func testRandomString(t *testing.T, randomFunc func(n int) string) {
|
||||
generated := make([]string, 0, 1000)
|
||||
generated := make([]string, 0, 500)
|
||||
reg := regexp.MustCompile(`[a-zA-Z0-9]+`)
|
||||
length := 10
|
||||
|
||||
for i := 0; i < 1000; i++ {
|
||||
result := randomFunc(length)
|
||||
for i := 0; i < 500; i++ {
|
||||
var run func(int)
|
||||
run = func(attempt int) {
|
||||
result := randomFunc(length)
|
||||
|
||||
if len(result) != length {
|
||||
t.Fatalf("(%d) Expected the length of the string to be %d, got %d", i, length, len(result))
|
||||
}
|
||||
|
||||
if match := reg.MatchString(result); !match {
|
||||
t.Fatalf("(%d) The generated string should have only [a-zA-Z0-9]+ characters, got %q", i, result)
|
||||
}
|
||||
|
||||
for _, str := range generated {
|
||||
if str == result {
|
||||
t.Fatalf("(%d) Repeating random string - found %q in \n%v", i, result, generated)
|
||||
if len(result) != length {
|
||||
t.Fatalf("(%d) Expected the length of the string to be %d, got %d", i, length, len(result))
|
||||
}
|
||||
|
||||
if match := reg.MatchString(result); !match {
|
||||
t.Fatalf("(%d) The generated string should have only [a-zA-Z0-9]+ characters, got %q", i, result)
|
||||
}
|
||||
|
||||
if slices.Contains(generated, result) {
|
||||
if attempt > 3 {
|
||||
t.Fatalf("(%d) Repeating random string - found %q in \n%v", i, result, generated)
|
||||
}
|
||||
|
||||
// rerun
|
||||
run(attempt + 1)
|
||||
return
|
||||
}
|
||||
|
||||
generated = append(generated, result)
|
||||
}
|
||||
|
||||
generated = append(generated, result)
|
||||
run(1)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user