mirror of
https://github.com/coder/coder.git
synced 2025-07-13 21:36:50 +00:00
chore(testutil): add testutil.GetRandomName that does not return duplicates (#14020)
Fixes #13910 Adds testutil.GetRandomName that replaces namesgenerator.GetRandomName but instead appends a monotonically increasing integer instead of a number between 1 and 10.
This commit is contained in:
23
testutil/names.go
Normal file
23
testutil/names.go
Normal file
@ -0,0 +1,23 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/moby/moby/pkg/namesgenerator"
|
||||
)
|
||||
|
||||
var n atomic.Int64
|
||||
|
||||
// GetRandomName returns a random name using moby/pkg/namesgenerator.
|
||||
// namesgenerator.GetRandomName exposes a retry parameter that appends
|
||||
// a pseudo-random number between 1 and 10 to its return value.
|
||||
// While this reduces the probability of collisions, it does not negate them.
|
||||
// This function calls namesgenerator.GetRandomName without the retry
|
||||
// parameter and instead increments a monotonically increasing integer
|
||||
// to the return value.
|
||||
func GetRandomName(t testing.TB) string {
|
||||
t.Helper()
|
||||
return namesgenerator.GetRandomName(0) + strconv.FormatInt(n.Add(1), 10)
|
||||
}
|
Reference in New Issue
Block a user