1
0
Fork 0

Merge pull request #1665 from rqlite/improve-random-code-test

Minor improvements to random module
master
Philip O'Toole 8 months ago committed by GitHub
commit 2b98dfac1f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,3 +1,7 @@
## 8.18.8 (unreleased)
### Implementation changes and bug fixes
- [PR #1665](https://github.com/rqlite/rqlite/pull/1665): Minor improvements to `random` module.
## 8.18.7 (February 1st 2024)
### Implementation changes and bug fixes
- [PR #1663](https://github.com/rqlite/rqlite/pull/1663): Remove unnecessary WAL-close during Raft snapshotting.

@ -14,16 +14,15 @@ func init() {
r = rand.New(rand.NewSource(time.Now().UnixNano()))
}
// String returns a random string of 20 characters
// String returns a random string, 20 characters long.
func String() string {
mu.Lock()
defer mu.Unlock()
var output strings.Builder
chars := "abcdedfghijklmnopqrstABCDEFGHIJKLMNOP"
chars := "abcdedfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i := 0; i < 20; i++ {
random := r.Intn(len(chars))
randomChar := chars[random]
output.WriteString(string(randomChar))
output.WriteString(string(chars[random]))
}
return output.String()
}
@ -35,16 +34,16 @@ func Float64() float64 {
return r.Float64()
}
// Intn returns a random int
// Intn returns a random int >=0 and < n.
func Intn(n int) int {
mu.Lock()
defer mu.Unlock()
return r.Intn(n)
}
// Jitter adds a little bit of randomness to a given duration. This is
// useful to prevent nodes across the cluster performing certain operations
// all at the same time.
// Jitter returns a randomly-chosen duration between d and 2d.
func Jitter(d time.Duration) time.Duration {
return d + time.Duration(Float64()*float64(d))
mu.Lock()
defer mu.Unlock()
return d + time.Duration(r.Float64()*float64(d))
}

@ -7,13 +7,13 @@ import (
func Test_StringLength(t *testing.T) {
str := String()
if len(str) != 20 {
t.Errorf("String() returned a string of length %d; want 20", len(str))
if exp, got := 20, len(str); exp != got {
t.Errorf("String() returned a string of length %d; want %d", got, exp)
}
}
func Test_StringUniqueness(t *testing.T) {
const numStrings = 10
const numStrings = 100
strs := make(map[string]bool, numStrings)
for i := 0; i < numStrings; i++ {
@ -26,7 +26,7 @@ func Test_StringUniqueness(t *testing.T) {
}
func Test_Float64Uniqueness(t *testing.T) {
const numFloat64s = 10
const numFloat64s = 100
floats := make(map[float64]bool, numFloat64s)
for i := 0; i < numFloat64s; i++ {
@ -39,7 +39,7 @@ func Test_Float64Uniqueness(t *testing.T) {
}
func Test_IntnUniqueness(t *testing.T) {
const numIntns = 10
const numIntns = 100
intns := make(map[int]bool, numIntns)
for i := 0; i < numIntns; i++ {
@ -53,9 +53,12 @@ func Test_IntnUniqueness(t *testing.T) {
func Test_Jitter(t *testing.T) {
for n := 0; n < 100; n++ {
d := Jitter(100 * time.Millisecond)
if d < 100*time.Millisecond || d >= 200*time.Millisecond {
t.Errorf("Jitter(100ms) returned a duration of %s; want between 0 and 200ms", d)
dur := 100 * time.Millisecond
lower := dur
upper := 2 * dur
if got := Jitter(dur); got < lower || got >= upper {
t.Errorf("Jitter(%s) returned a duration of %s; want between %s and %s",
dur, got, lower, upper)
}
}
}

Loading…
Cancel
Save