-
Notifications
You must be signed in to change notification settings - Fork 15
/
random.go
34 lines (28 loc) · 1.02 KB
/
random.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package memongo
import (
"crypto/rand"
"fmt"
"math/big"
)
// DBNameLen is the length of a database name generated by RandomDatabase().
// It's OK to change this, but not concurrently with calls to RandomDatabase.
const DBNameLen = 15
// DBNameChars is the set of characters used by RandomDatabase().
// It's OK to change this, but not concurrently with calls to RandomDatabase.
const DBNameChars = "abcdefghijklmnopqrstuvwxyz"
// RandomDatabase returns a random valid mongo database name. You can use to
// to pick a new database name for each test to isolate tests from each other
// without having to tear down the whole server.
//
// This function will panic if it cannot generate a random number.
func RandomDatabase() string {
dbChars := make([]byte, DBNameLen)
for i := 0; i < DBNameLen; i++ {
bigN, err := rand.Int(rand.Reader, big.NewInt(int64(len(DBNameChars))))
if err != nil {
panic(fmt.Errorf("error getting a random int: %s", err))
}
dbChars[i] = DBNameChars[int(bigN.Int64())]
}
return string(dbChars)
}