Skip to content

Commit

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

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.
type Broadcaster interface {
Expand Down Expand Up @@ -465,6 +463,7 @@ func randomizeInterval(d time.Duration) time.Duration {
// 30% of the configured interval
leeway := (d * 30 / 100)
// A random number between -leeway|+leeway
randGen := rand.New(rand.NewSource(time.Now().UnixNano()))
randomInterval := time.Duration(randGen.Int63n(int64(leeway*2))) - leeway
return d + randomInterval
}
Expand Down
14 changes: 10 additions & 4 deletions crdt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package crdt
import (
"context"
"fmt"
"math/rand"
"os"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -119,16 +120,20 @@ func newBroadcasters(t testing.TB, n int) ([]*mockBroadcaster, context.CancelFun

func (mb *mockBroadcaster) Broadcast(data []byte) error {
var wg sync.WaitGroup

randg := rand.New(rand.NewSource(time.Now().UnixNano()))

for i, ch := range mb.chans {
n := randGen.Intn(100)
n := randg.Intn(100)
if n < mb.dropProb {
continue
}
wg.Add(1)
go func() {
go func(i int) {
defer wg.Done()
randg := rand.New(rand.NewSource(int64(i)))
// randomize when we send a little bit
if randGen.Intn(100) < 30 {
if randg.Intn(100) < 30 {
// Sleep for a very small time that will
// effectively be pretty random
time.Sleep(time.Nanosecond)
Expand All @@ -142,7 +147,7 @@ func (mb *mockBroadcaster) Broadcast(data []byte) error {
case <-timer.C:
mb.t.Errorf("broadcasting to %d timed out", i)
}
}()
}(i)
wg.Wait()
}
return nil
Expand Down Expand Up @@ -306,6 +311,7 @@ func TestCRDT(t *testing.T) {
func TestCRDTReplication(t *testing.T) {
ctx := context.Background()
nItems := 50
randGen := rand.New(rand.NewSource(time.Now().UnixNano()))

replicas, closeReplicas := makeReplicas(t, nil)
defer closeReplicas()
Expand Down
10 changes: 7 additions & 3 deletions heads_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package crdt
import (
"bytes"
"context"
"math/rand"
"reflect"
"sort"
"testing"
"time"

"github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
Expand All @@ -15,6 +17,8 @@ import (

var headsTestNS = ds.NewKey("headstest")

var randg = rand.New(rand.NewSource(time.Now().UnixNano()))

// TODO we should also test with a non-batching store
func newTestHeads(t *testing.T) *heads {
t.Helper()
Expand All @@ -32,7 +36,7 @@ func newTestHeads(t *testing.T) *heads {
func newCID(t *testing.T) cid.Cid {
t.Helper()
var buf [32]byte
_, _ = randGen.Read(buf[:])
_, _ = randg.Read(buf[:])

mh, err := multihash.Sum(buf[:], multihash.SHA2_256, -1)
if err != nil {
Expand All @@ -56,7 +60,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(randGen.Int())
c, height := newCID(t), uint64(randg.Int())
cidHeights[c] = height
err := heads.Add(ctx, c, height)
if err != nil {
Expand All @@ -67,7 +71,7 @@ func TestHeadsBasic(t *testing.T) {
assertHeads(t, heads, cidHeights)

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

0 comments on commit 356a5e6

Please sign in to comment.