mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-25 14:52:17 +00:00
Switch usage of math/rand to cmn's rand (#1980)
This commit switches all usage of math/rand to cmn's rand. The only exceptions are within the random file itself, the tools package, and the crypto package. In tools you don't want it to lock between the go-routines. The crypto package doesn't use it so the crypto package have no other dependencies within tendermint/tendermint for easier portability. Crypto/rand usage is unadjusted. Closes #1343
This commit is contained in:
parent
14cebd181d
commit
dae7dc30e0
@ -1,7 +1,6 @@
|
|||||||
package blockchain
|
package blockchain
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -25,7 +24,7 @@ func makePeers(numPeers int, minHeight, maxHeight int64) map[p2p.ID]testPeer {
|
|||||||
peers := make(map[p2p.ID]testPeer, numPeers)
|
peers := make(map[p2p.ID]testPeer, numPeers)
|
||||||
for i := 0; i < numPeers; i++ {
|
for i := 0; i < numPeers; i++ {
|
||||||
peerID := p2p.ID(cmn.RandStr(12))
|
peerID := p2p.ID(cmn.RandStr(12))
|
||||||
height := minHeight + rand.Int63n(maxHeight-minHeight)
|
height := minHeight + cmn.RandInt63n(maxHeight-minHeight)
|
||||||
peers[peerID] = testPeer{peerID, height}
|
peers[peerID] = testPeer{peerID, height}
|
||||||
}
|
}
|
||||||
return peers
|
return peers
|
||||||
|
@ -2,11 +2,12 @@ package clist
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
cmn "github.com/tendermint/tendermint/libs/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSmall(t *testing.T) {
|
func TestSmall(t *testing.T) {
|
||||||
@ -131,7 +132,7 @@ func _TestGCRandom(t *testing.T) {
|
|||||||
els = append(els, el)
|
els = append(els, el)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, i := range rand.Perm(numElements) {
|
for _, i := range cmn.RandPerm(numElements) {
|
||||||
el := els[i]
|
el := els[i]
|
||||||
l.Remove(el)
|
l.Remove(el)
|
||||||
_ = el.Next()
|
_ = el.Next()
|
||||||
@ -189,7 +190,7 @@ func TestScanRightDeleteRandom(t *testing.T) {
|
|||||||
// Remove an element, push back an element.
|
// Remove an element, push back an element.
|
||||||
for i := 0; i < numTimes; i++ {
|
for i := 0; i < numTimes; i++ {
|
||||||
// Pick an element to remove
|
// Pick an element to remove
|
||||||
rmElIdx := rand.Intn(len(els))
|
rmElIdx := cmn.RandIntn(len(els))
|
||||||
rmEl := els[rmElIdx]
|
rmEl := els[rmElIdx]
|
||||||
|
|
||||||
// Remove it
|
// Remove it
|
||||||
@ -243,7 +244,7 @@ func TestWaitChan(t *testing.T) {
|
|||||||
for i := 1; i < 100; i++ {
|
for i := 1; i < 100; i++ {
|
||||||
l.PushBack(i)
|
l.PushBack(i)
|
||||||
pushed++
|
pushed++
|
||||||
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
|
time.Sleep(time.Duration(cmn.RandIntn(100)) * time.Millisecond)
|
||||||
}
|
}
|
||||||
close(done)
|
close(done)
|
||||||
}()
|
}()
|
||||||
|
@ -3,17 +3,14 @@ package common
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math/rand"
|
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestWriteFileAtomic(t *testing.T) {
|
func TestWriteFileAtomic(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
seed = rand.New(rand.NewSource(time.Now().UnixNano()))
|
data = []byte(RandStr(RandIntn(2048)))
|
||||||
data = []byte(RandStr(seed.Intn(2048)))
|
old = RandBytes(RandIntn(2048))
|
||||||
old = RandBytes(seed.Intn(2048))
|
|
||||||
perm os.FileMode = 0600
|
perm os.FileMode = 0600
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -131,7 +130,7 @@ func TestRepeatTimerReset(t *testing.T) {
|
|||||||
|
|
||||||
// just random calls
|
// just random calls
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < 100; i++ {
|
||||||
time.Sleep(time.Duration(rand.Intn(40)) * time.Millisecond)
|
time.Sleep(time.Duration(RandIntn(40)) * time.Millisecond)
|
||||||
timer.Reset()
|
timer.Reset()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,11 +2,11 @@ package events
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
cmn "github.com/tendermint/tendermint/libs/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestAddListenerForEventFireOnce sets up an EventSwitch, subscribes a single
|
// TestAddListenerForEventFireOnce sets up an EventSwitch, subscribes a single
|
||||||
@ -306,8 +306,8 @@ func TestRemoveListenersAsync(t *testing.T) {
|
|||||||
// collect received events for event2
|
// collect received events for event2
|
||||||
go sumReceivedNumbers(numbers2, doneSum2)
|
go sumReceivedNumbers(numbers2, doneSum2)
|
||||||
addListenersStress := func() {
|
addListenersStress := func() {
|
||||||
s1 := rand.NewSource(time.Now().UnixNano())
|
r1 := cmn.NewRand()
|
||||||
r1 := rand.New(s1)
|
r1.Seed(time.Now().UnixNano())
|
||||||
for k := uint16(0); k < 400; k++ {
|
for k := uint16(0); k < 400; k++ {
|
||||||
listenerNumber := r1.Intn(100) + 3
|
listenerNumber := r1.Intn(100) + 3
|
||||||
eventNumber := r1.Intn(3) + 1
|
eventNumber := r1.Intn(3) + 1
|
||||||
@ -317,8 +317,8 @@ func TestRemoveListenersAsync(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
removeListenersStress := func() {
|
removeListenersStress := func() {
|
||||||
s2 := rand.NewSource(time.Now().UnixNano())
|
r2 := cmn.NewRand()
|
||||||
r2 := rand.New(s2)
|
r2.Seed(time.Now().UnixNano())
|
||||||
for k := uint16(0); k < 80; k++ {
|
for k := uint16(0); k < 80; k++ {
|
||||||
listenerNumber := r2.Intn(100) + 3
|
listenerNumber := r2.Intn(100) + 3
|
||||||
go evsw.RemoveListener(fmt.Sprintf("listener%v", listenerNumber))
|
go evsw.RemoveListener(fmt.Sprintf("listener%v", listenerNumber))
|
||||||
|
@ -2,13 +2,12 @@ package lite
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
cmn "github.com/tendermint/tendermint/libs/common"
|
||||||
liteErr "github.com/tendermint/tendermint/lite/errors"
|
liteErr "github.com/tendermint/tendermint/lite/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -280,7 +279,11 @@ func BenchmarkMemStoreProviderGetByHeightBinarySearch1000(b *testing.B) {
|
|||||||
benchmarkMemStoreProvidergetByHeight(b, fcs1000, h1000, binarySearch)
|
benchmarkMemStoreProvidergetByHeight(b, fcs1000, h1000, binarySearch)
|
||||||
}
|
}
|
||||||
|
|
||||||
var rng = rand.New(rand.NewSource(10))
|
var rng = cmn.NewRand()
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rng.Seed(10)
|
||||||
|
}
|
||||||
|
|
||||||
func benchmarkMemStoreProvidergetByHeight(b *testing.B, fcs []FullCommit, fHeights []int64, algo algo) {
|
func benchmarkMemStoreProvidergetByHeight(b *testing.B, fcs []FullCommit, fHeights []int64, algo algo) {
|
||||||
lazyGenerateFullCommits(b)
|
lazyGenerateFullCommits(b)
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package p2p
|
package p2p
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
@ -22,7 +21,7 @@ func randPeer(ip net.IP) *peer {
|
|||||||
p := &peer{
|
p := &peer{
|
||||||
nodeInfo: NodeInfo{
|
nodeInfo: NodeInfo{
|
||||||
ID: nodeKey.ID(),
|
ID: nodeKey.ID(),
|
||||||
ListenAddr: cmn.Fmt("%v.%v.%v.%v:26656", rand.Int()%256, rand.Int()%256, rand.Int()%256, rand.Int()%256),
|
ListenAddr: cmn.Fmt("%v.%v.%v.%v:26656", cmn.RandInt()%256, cmn.RandInt()%256, cmn.RandInt()%256, cmn.RandInt()%256),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,14 +4,13 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math/rand"
|
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/tendermint/tendermint/p2p"
|
|
||||||
cmn "github.com/tendermint/tendermint/libs/common"
|
cmn "github.com/tendermint/tendermint/libs/common"
|
||||||
"github.com/tendermint/tendermint/libs/log"
|
"github.com/tendermint/tendermint/libs/log"
|
||||||
|
"github.com/tendermint/tendermint/p2p"
|
||||||
)
|
)
|
||||||
|
|
||||||
func createTempFileName(prefix string) string {
|
func createTempFileName(prefix string) string {
|
||||||
@ -202,12 +201,12 @@ func randNetAddressPairs(t *testing.T, n int) []netAddressPair {
|
|||||||
func randIPv4Address(t *testing.T) *p2p.NetAddress {
|
func randIPv4Address(t *testing.T) *p2p.NetAddress {
|
||||||
for {
|
for {
|
||||||
ip := fmt.Sprintf("%v.%v.%v.%v",
|
ip := fmt.Sprintf("%v.%v.%v.%v",
|
||||||
rand.Intn(254)+1,
|
cmn.RandIntn(254)+1,
|
||||||
rand.Intn(255),
|
cmn.RandIntn(255),
|
||||||
rand.Intn(255),
|
cmn.RandIntn(255),
|
||||||
rand.Intn(255),
|
cmn.RandIntn(255),
|
||||||
)
|
)
|
||||||
port := rand.Intn(65535-1) + 1
|
port := cmn.RandIntn(65535-1) + 1
|
||||||
id := p2p.ID(hex.EncodeToString(cmn.RandBytes(p2p.IDByteLength)))
|
id := p2p.ID(hex.EncodeToString(cmn.RandBytes(p2p.IDByteLength)))
|
||||||
idAddr := p2p.IDAddressString(id, fmt.Sprintf("%v:%v", ip, port))
|
idAddr := p2p.IDAddressString(id, fmt.Sprintf("%v:%v", ip, port))
|
||||||
addr, err := p2p.NewNetAddressString(idAddr)
|
addr, err := p2p.NewNetAddressString(idAddr)
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
crand "crypto/rand"
|
crand "crypto/rand"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
@ -206,7 +205,7 @@ func testWithHTTPClient(t *testing.T, cl client.HTTPClient) {
|
|||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
assert.Equal(t, got3, val3)
|
assert.Equal(t, got3, val3)
|
||||||
|
|
||||||
val4 := rand.Intn(10000)
|
val4 := cmn.RandIntn(10000)
|
||||||
got4, err := echoIntViaHTTP(cl, val4)
|
got4, err := echoIntViaHTTP(cl, val4)
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
assert.Equal(t, got4, val4)
|
assert.Equal(t, got4, val4)
|
||||||
@ -370,7 +369,7 @@ func TestWSClientPingPong(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func randBytes(t *testing.T) []byte {
|
func randBytes(t *testing.T) []byte {
|
||||||
n := rand.Intn(10) + 2
|
n := cmn.RandIntn(10) + 2
|
||||||
buf := make([]byte, n)
|
buf := make([]byte, n)
|
||||||
_, err := crand.Read(buf)
|
_, err := crand.Read(buf)
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
|
@ -3,7 +3,6 @@ package types
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -150,7 +149,7 @@ func BenchmarkEventBus(b *testing.B) {
|
|||||||
|
|
||||||
func benchmarkEventBus(numClients int, randQueries bool, randEvents bool, b *testing.B) {
|
func benchmarkEventBus(numClients int, randQueries bool, randEvents bool, b *testing.B) {
|
||||||
// for random* functions
|
// for random* functions
|
||||||
rand.Seed(time.Now().Unix())
|
cmn.Seed(time.Now().Unix())
|
||||||
|
|
||||||
eventBus := NewEventBusWithBufferCapacity(0) // set buffer capacity to 0 so we are not testing cache
|
eventBus := NewEventBusWithBufferCapacity(0) // set buffer capacity to 0 so we are not testing cache
|
||||||
eventBus.Start()
|
eventBus.Start()
|
||||||
@ -199,7 +198,7 @@ var events = []string{
|
|||||||
EventVote}
|
EventVote}
|
||||||
|
|
||||||
func randEvent() string {
|
func randEvent() string {
|
||||||
return events[rand.Intn(len(events))]
|
return events[cmn.RandIntn(len(events))]
|
||||||
}
|
}
|
||||||
|
|
||||||
var queries = []tmpubsub.Query{
|
var queries = []tmpubsub.Query{
|
||||||
@ -217,5 +216,5 @@ var queries = []tmpubsub.Query{
|
|||||||
EventQueryVote}
|
EventQueryVote}
|
||||||
|
|
||||||
func randQuery() tmpubsub.Query {
|
func randQuery() tmpubsub.Query {
|
||||||
return queries[rand.Intn(len(queries))]
|
return queries[cmn.RandIntn(len(queries))]
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user