From 226f8cb215687f60ccd72876146a646a93b1c162 Mon Sep 17 00:00:00 2001 From: Philip O'Toole Date: Thu, 1 Feb 2024 19:55:20 -0500 Subject: [PATCH] Minor improvements to random module --- CHANGELOG.md | 6 +++++- random/random.go | 17 ++++++++--------- random/random_test.go | 19 +++++++++++-------- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8cba0f74..d240d1e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ -## 8.18.7 (unreleased) +## 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. diff --git a/random/random.go b/random/random.go index e2793089..8a224b3c 100644 --- a/random/random.go +++ b/random/random.go @@ -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)) } diff --git a/random/random_test.go b/random/random_test.go index e841b97c..09aef114 100644 --- a/random/random_test.go +++ b/random/random_test.go @@ -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) } } }