libs/common: refactor libs/common 2 (#4231)

* libs/common: refactor libs/common 2

- move random function to there own pkg

Signed-off-by: Marko Baricevic <marbar3778@yahoo.com>

* change imports and usage throughout repo

* fix goimports

* add changelog entry
This commit is contained in:
Marko
2019-12-10 18:38:34 +01:00
committed by GitHub
parent dfebac86f7
commit afc4d7a61f
55 changed files with 221 additions and 196 deletions
+3 -3
View File
@@ -8,8 +8,8 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/libs/rand"
"github.com/tendermint/tendermint/p2p"
"github.com/tendermint/tendermint/types"
)
@@ -65,8 +65,8 @@ func (ps testPeers) stop() {
func makePeers(numPeers int, minHeight, maxHeight int64) testPeers {
peers := make(testPeers, numPeers)
for i := 0; i < numPeers; i++ {
peerID := p2p.ID(cmn.RandStr(12))
height := minHeight + cmn.RandInt63n(maxHeight-minHeight)
peerID := p2p.ID(rand.RandStr(12))
height := minHeight + rand.RandInt63n(maxHeight-minHeight)
peers[peerID] = testPeer{peerID, height, make(chan inputData, 10)}
}
return peers
+9 -9
View File
@@ -8,15 +8,15 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/libs/rand"
"github.com/tendermint/tendermint/p2p"
"github.com/tendermint/tendermint/types"
)
func TestPeerMonitor(t *testing.T) {
peer := NewBpPeer(
p2p.ID(cmn.RandStr(12)), 10,
p2p.ID(rand.RandStr(12)), 10,
func(err error, _ p2p.ID) {},
nil)
peer.SetLogger(log.TestingLogger())
@@ -35,7 +35,7 @@ func TestPeerResetBlockResponseTimer(t *testing.T) {
params := &BpPeerParams{timeout: 2 * time.Millisecond}
peer := NewBpPeer(
p2p.ID(cmn.RandStr(12)), 10,
p2p.ID(rand.RandStr(12)), 10,
func(err error, _ p2p.ID) {
peerTestMtx.Lock()
defer peerTestMtx.Unlock()
@@ -75,7 +75,7 @@ func TestPeerRequestSent(t *testing.T) {
params := &BpPeerParams{timeout: 2 * time.Millisecond}
peer := NewBpPeer(
p2p.ID(cmn.RandStr(12)), 10,
p2p.ID(rand.RandStr(12)), 10,
func(err error, _ p2p.ID) {},
params)
@@ -94,7 +94,7 @@ func TestPeerRequestSent(t *testing.T) {
func TestPeerGetAndRemoveBlock(t *testing.T) {
peer := NewBpPeer(
p2p.ID(cmn.RandStr(12)), 100,
p2p.ID(rand.RandStr(12)), 100,
func(err error, _ p2p.ID) {},
nil)
@@ -142,7 +142,7 @@ func TestPeerGetAndRemoveBlock(t *testing.T) {
func TestPeerAddBlock(t *testing.T) {
peer := NewBpPeer(
p2p.ID(cmn.RandStr(12)), 100,
p2p.ID(rand.RandStr(12)), 100,
func(err error, _ p2p.ID) {},
nil)
@@ -189,7 +189,7 @@ func TestPeerOnErrFuncCalledDueToExpiration(t *testing.T) {
)
peer := NewBpPeer(
p2p.ID(cmn.RandStr(12)), 10,
p2p.ID(rand.RandStr(12)), 10,
func(err error, _ p2p.ID) {
peerTestMtx.Lock()
defer peerTestMtx.Unlock()
@@ -215,7 +215,7 @@ func TestPeerCheckRate(t *testing.T) {
minRecvRate: int64(100), // 100 bytes/sec exponential moving average
}
peer := NewBpPeer(
p2p.ID(cmn.RandStr(12)), 10,
p2p.ID(rand.RandStr(12)), 10,
func(err error, _ p2p.ID) {},
params)
peer.SetLogger(log.TestingLogger())
@@ -249,7 +249,7 @@ func TestPeerCleanup(t *testing.T) {
params := &BpPeerParams{timeout: 2 * time.Millisecond}
peer := NewBpPeer(
p2p.ID(cmn.RandStr(12)), 10,
p2p.ID(rand.RandStr(12)), 10,
func(err error, _ p2p.ID) {},
params)
peer.SetLogger(log.TestingLogger())
+7 -6
View File
@@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/assert"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/libs/rand"
"github.com/tendermint/tendermint/p2p"
"github.com/tendermint/tendermint/types"
)
@@ -735,7 +736,7 @@ func makeCorrectTransitionSequence(startingHeight int64, numBlocks int64, numPee
continue
}
if randomPeerHeights {
peerHeights[i] = int64(cmn.MaxInt(cmn.RandIntn(int(numBlocks)), int(startingHeight)+1))
peerHeights[i] = int64(cmn.MaxInt(rand.RandIntn(int(numBlocks)), int(startingHeight)+1))
} else {
peerHeights[i] = numBlocks
}
@@ -825,19 +826,19 @@ const (
func makeCorrectTransitionSequenceWithRandomParameters() testFields {
// Generate a starting height for fast sync.
startingHeight := int64(cmn.RandIntn(maxStartingHeightTest) + 1)
startingHeight := int64(rand.RandIntn(maxStartingHeightTest) + 1)
// Generate the number of requests per peer.
maxRequestsPerPeer := cmn.RandIntn(maxRequestsPerPeerTest) + 1
maxRequestsPerPeer := rand.RandIntn(maxRequestsPerPeerTest) + 1
// Generate the maximum number of total pending requests, >= maxRequestsPerPeer.
maxPendingRequests := cmn.RandIntn(maxTotalPendingRequestsTest-maxRequestsPerPeer) + maxRequestsPerPeer
maxPendingRequests := rand.RandIntn(maxTotalPendingRequestsTest-maxRequestsPerPeer) + maxRequestsPerPeer
// Generate the number of blocks to be synced.
numBlocks := int64(cmn.RandIntn(maxNumBlocksInChainTest)) + startingHeight
numBlocks := int64(rand.RandIntn(maxNumBlocksInChainTest)) + startingHeight
// Generate a number of peers.
numPeers := cmn.RandIntn(maxNumPeersTest) + 1
numPeers := rand.RandIntn(maxNumPeersTest) + 1
return makeCorrectTransitionSequence(startingHeight, numBlocks, numPeers, true, maxRequestsPerPeer, maxPendingRequests)
}