Skip to content

Commit

Permalink
Avoid using deprecated global rand functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hsanjuan committed Oct 3, 2023
1 parent a36a5d8 commit 1ca4a08
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
6 changes: 2 additions & 4 deletions crdt.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ var (
ErrNoMoreBroadcast = errors.New("receiving blocks aborted since no new blocks will be broadcasted")
)

func init() {
rand.Seed(time.Now().UnixNano())
}
var randGen = rand.New(rand.NewSource(time.Now().UnixNano()))

// A Broadcaster provides a way to send (notify) an opaque payload to
// all replicas and to retrieve payloads broadcasted.
Expand Down Expand Up @@ -467,7 +465,7 @@ func randomizeInterval(d time.Duration) time.Duration {
// 30% of the configured interval
leeway := (d * 30 / 100)
// A random number between -leeway|+leeway
randomInterval := time.Duration(rand.Int63n(int64(leeway*2))) - leeway
randomInterval := time.Duration(randGen.Int63n(int64(leeway*2))) - leeway
return d + randomInterval
}

Expand Down
10 changes: 4 additions & 6 deletions crdt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package crdt
import (
"context"
"fmt"
"math/rand"
"os"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -36,7 +35,6 @@ const (
var store int = mapStore

func init() {
rand.Seed(time.Now().UnixNano())
dstest.ElemCount = 10
}

Expand Down Expand Up @@ -122,15 +120,15 @@ func newBroadcasters(t testing.TB, n int) ([]*mockBroadcaster, context.CancelFun
func (mb *mockBroadcaster) Broadcast(data []byte) error {
var wg sync.WaitGroup
for i, ch := range mb.chans {
n := rand.Intn(100)
n := randGen.Intn(100)
if n < mb.dropProb {
continue
}
wg.Add(1)
go func() {
defer wg.Done()
// randomize when we send a little bit
if rand.Intn(100) < 30 {
if randGen.Intn(100) < 30 {
// Sleep for a very small time that will
// effectively be pretty random
time.Sleep(time.Nanosecond)
Expand Down Expand Up @@ -316,7 +314,7 @@ func TestCRDTReplication(t *testing.T) {
for i := 0; i < nItems; i++ {
k := ds.RandomKey()
v := []byte(fmt.Sprintf("%d", i))
n := rand.Intn(len(replicas))
n := randGen.Intn(len(replicas))
err := replicas[n].Put(ctx, k, v)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -357,7 +355,7 @@ func TestCRDTReplication(t *testing.T) {

// give a new value for each item
for _, r := range rest {
n := rand.Intn(len(replicas))
n := randGen.Intn(len(replicas))
err := replicas[n].Put(ctx, ds.NewKey(r.Key), []byte("hola"))
if err != nil {
t.Error(err)
Expand Down
7 changes: 3 additions & 4 deletions heads_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package crdt
import (
"bytes"
"context"
"math/rand"
"reflect"
"sort"
"testing"
Expand Down Expand Up @@ -33,7 +32,7 @@ func newTestHeads(t *testing.T) *heads {
func newCID(t *testing.T) cid.Cid {
t.Helper()
var buf [32]byte
_, _ = rand.Read(buf[:])
_, _ = randGen.Read(buf[:])

mh, err := multihash.Sum(buf[:], multihash.SHA2_256, -1)
if err != nil {
Expand All @@ -57,7 +56,7 @@ func TestHeadsBasic(t *testing.T) {
cidHeights := make(map[cid.Cid]uint64)
numHeads := 5
for i := 0; i < numHeads; i++ {
c, height := newCID(t), uint64(rand.Int())
c, height := newCID(t), uint64(randGen.Int())
cidHeights[c] = height
err := heads.Add(ctx, c, height)
if err != nil {
Expand All @@ -68,7 +67,7 @@ func TestHeadsBasic(t *testing.T) {
assertHeads(t, heads, cidHeights)

for c := range cidHeights {
newC, newHeight := newCID(t), uint64(rand.Int())
newC, newHeight := newCID(t), uint64(randGen.Int())
err := heads.Replace(ctx, c, newC, newHeight)
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 1ca4a08

Please sign in to comment.