retry the random func to minimize tests flakiness
This commit is contained in:
parent
172b1f96f7
commit
9e13418565
|
|
@ -35,31 +35,41 @@ func TestRandomStringByRegex(t *testing.T) {
|
||||||
|
|
||||||
for i, s := range scenarios {
|
for i, s := range scenarios {
|
||||||
t.Run(fmt.Sprintf("%d_%q", i, s.pattern), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%d_%q", i, s.pattern), func(t *testing.T) {
|
||||||
str, err := security.RandomStringByRegex(s.pattern, s.flags...)
|
var run func(int)
|
||||||
|
run = func(attempt int) {
|
||||||
|
str, err := security.RandomStringByRegex(s.pattern, s.flags...)
|
||||||
|
|
||||||
hasErr := err != nil
|
hasErr := err != nil
|
||||||
if hasErr != s.expectError {
|
if hasErr != s.expectError {
|
||||||
t.Fatalf("Expected hasErr %v, got %v (%v)", s.expectError, hasErr, err)
|
t.Fatalf("Expected hasErr %v, got %v (%v)", s.expectError, hasErr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if hasErr {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
r, err := regexp.Compile(s.pattern)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !r.Match([]byte(str)) {
|
||||||
|
t.Fatalf("Expected %q to match pattern %v", str, s.pattern)
|
||||||
|
}
|
||||||
|
|
||||||
|
if slices.Contains(generated, str) {
|
||||||
|
if attempt > 3 {
|
||||||
|
t.Fatalf("The generated string %q already exists in\n%v", str, generated)
|
||||||
|
}
|
||||||
|
|
||||||
|
// rerun
|
||||||
|
run(attempt + 1)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
generated = append(generated, str)
|
||||||
}
|
}
|
||||||
|
|
||||||
if hasErr {
|
run(1)
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
r, err := regexp.Compile(s.pattern)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !r.Match([]byte(str)) {
|
|
||||||
t.Fatalf("Expected %q to match pattern %v", str, s.pattern)
|
|
||||||
}
|
|
||||||
|
|
||||||
if slices.Contains(generated, str) {
|
|
||||||
t.Fatalf("The generated string %q already exists in\n%v", str, generated)
|
|
||||||
}
|
|
||||||
|
|
||||||
generated = append(generated, str)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package security_test
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"slices"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/pocketbase/pocketbase/tools/security"
|
"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 {
|
for i, s := range scenarios {
|
||||||
t.Run(fmt.Sprintf("%d_%q", i, s.alphabet), func(t *testing.T) {
|
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
|
length := 10
|
||||||
|
|
||||||
for j := 0; j < 1000; j++ {
|
for j := 0; j < 500; j++ {
|
||||||
result := randomFunc(length, s.alphabet)
|
var run func(int)
|
||||||
|
run = func(attempt int) {
|
||||||
|
result := randomFunc(length, s.alphabet)
|
||||||
|
|
||||||
if len(result) != length {
|
if len(result) != length {
|
||||||
t.Fatalf("(%d) Expected the length of the string to be %d, got %d", j, length, len(result))
|
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
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]+`)
|
reg := regexp.MustCompile(`[a-zA-Z0-9]+`)
|
||||||
length := 10
|
length := 10
|
||||||
|
|
||||||
for i := 0; i < 1000; i++ {
|
for i := 0; i < 500; i++ {
|
||||||
result := randomFunc(length)
|
var run func(int)
|
||||||
|
run = func(attempt int) {
|
||||||
|
result := randomFunc(length)
|
||||||
|
|
||||||
if len(result) != length {
|
if len(result) != length {
|
||||||
t.Fatalf("(%d) Expected the length of the string to be %d, got %d", i, length, len(result))
|
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 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue