Skip to content

Commit 356a5e6

Browse files
committed
fix rand usage
1 parent 1ca4a08 commit 356a5e6

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

crdt.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ var (
5555
ErrNoMoreBroadcast = errors.New("receiving blocks aborted since no new blocks will be broadcasted")
5656
)
5757

58-
var randGen = rand.New(rand.NewSource(time.Now().UnixNano()))
59-
6058
// A Broadcaster provides a way to send (notify) an opaque payload to
6159
// all replicas and to retrieve payloads broadcasted.
6260
type Broadcaster interface {
@@ -465,6 +463,7 @@ func randomizeInterval(d time.Duration) time.Duration {
465463
// 30% of the configured interval
466464
leeway := (d * 30 / 100)
467465
// A random number between -leeway|+leeway
466+
randGen := rand.New(rand.NewSource(time.Now().UnixNano()))
468467
randomInterval := time.Duration(randGen.Int63n(int64(leeway*2))) - leeway
469468
return d + randomInterval
470469
}

crdt_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package crdt
33
import (
44
"context"
55
"fmt"
6+
"math/rand"
67
"os"
78
"sync"
89
"sync/atomic"
@@ -119,16 +120,20 @@ func newBroadcasters(t testing.TB, n int) ([]*mockBroadcaster, context.CancelFun
119120

120121
func (mb *mockBroadcaster) Broadcast(data []byte) error {
121122
var wg sync.WaitGroup
123+
124+
randg := rand.New(rand.NewSource(time.Now().UnixNano()))
125+
122126
for i, ch := range mb.chans {
123-
n := randGen.Intn(100)
127+
n := randg.Intn(100)
124128
if n < mb.dropProb {
125129
continue
126130
}
127131
wg.Add(1)
128-
go func() {
132+
go func(i int) {
129133
defer wg.Done()
134+
randg := rand.New(rand.NewSource(int64(i)))
130135
// randomize when we send a little bit
131-
if randGen.Intn(100) < 30 {
136+
if randg.Intn(100) < 30 {
132137
// Sleep for a very small time that will
133138
// effectively be pretty random
134139
time.Sleep(time.Nanosecond)
@@ -142,7 +147,7 @@ func (mb *mockBroadcaster) Broadcast(data []byte) error {
142147
case <-timer.C:
143148
mb.t.Errorf("broadcasting to %d timed out", i)
144149
}
145-
}()
150+
}(i)
146151
wg.Wait()
147152
}
148153
return nil
@@ -306,6 +311,7 @@ func TestCRDT(t *testing.T) {
306311
func TestCRDTReplication(t *testing.T) {
307312
ctx := context.Background()
308313
nItems := 50
314+
randGen := rand.New(rand.NewSource(time.Now().UnixNano()))
309315

310316
replicas, closeReplicas := makeReplicas(t, nil)
311317
defer closeReplicas()

heads_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package crdt
33
import (
44
"bytes"
55
"context"
6+
"math/rand"
67
"reflect"
78
"sort"
89
"testing"
10+
"time"
911

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

1618
var headsTestNS = ds.NewKey("headstest")
1719

20+
var randg = rand.New(rand.NewSource(time.Now().UnixNano()))
21+
1822
// TODO we should also test with a non-batching store
1923
func newTestHeads(t *testing.T) *heads {
2024
t.Helper()
@@ -32,7 +36,7 @@ func newTestHeads(t *testing.T) *heads {
3236
func newCID(t *testing.T) cid.Cid {
3337
t.Helper()
3438
var buf [32]byte
35-
_, _ = randGen.Read(buf[:])
39+
_, _ = randg.Read(buf[:])
3640

3741
mh, err := multihash.Sum(buf[:], multihash.SHA2_256, -1)
3842
if err != nil {
@@ -56,7 +60,7 @@ func TestHeadsBasic(t *testing.T) {
5660
cidHeights := make(map[cid.Cid]uint64)
5761
numHeads := 5
5862
for i := 0; i < numHeads; i++ {
59-
c, height := newCID(t), uint64(randGen.Int())
63+
c, height := newCID(t), uint64(randg.Int())
6064
cidHeights[c] = height
6165
err := heads.Add(ctx, c, height)
6266
if err != nil {
@@ -67,7 +71,7 @@ func TestHeadsBasic(t *testing.T) {
6771
assertHeads(t, heads, cidHeights)
6872

6973
for c := range cidHeights {
70-
newC, newHeight := newCID(t), uint64(randGen.Int())
74+
newC, newHeight := newCID(t), uint64(randg.Int())
7175
err := heads.Replace(ctx, c, newC, newHeight)
7276
if err != nil {
7377
t.Fatal(err)

0 commit comments

Comments
 (0)