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
+2 -2
View File
@@ -7,7 +7,7 @@ import (
"syscall"
"time"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/rand"
)
/* AutoFile usage
@@ -58,7 +58,7 @@ type AutoFile struct {
// permissions got changed (should be 0600)).
func OpenAutoFile(path string) (*AutoFile, error) {
af := &AutoFile{
ID: cmn.RandStr(12) + ":" + path,
ID: rand.RandStr(12) + ":" + path,
Path: path,
closeTicker: time.NewTicker(autoFileClosePeriod),
closeTickerStopc: make(chan struct{}),
+7 -6
View File
@@ -10,10 +10,11 @@ import (
"github.com/stretchr/testify/require"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/rand"
)
func createTestGroupWithHeadSizeLimit(t *testing.T, headSizeLimit int64) *Group {
testID := cmn.RandStr(12)
testID := rand.RandStr(12)
testDir := "_test_" + testID
err := cmn.EnsureDir(testDir, 0700)
require.NoError(t, err, "Error creating dir")
@@ -48,7 +49,7 @@ func TestCheckHeadSizeLimit(t *testing.T) {
// Write 1000 bytes 999 times.
for i := 0; i < 999; i++ {
err := g.WriteLine(cmn.RandStr(999))
err := g.WriteLine(rand.RandStr(999))
require.NoError(t, err, "Error appending to head")
}
g.FlushAndSync()
@@ -59,7 +60,7 @@ func TestCheckHeadSizeLimit(t *testing.T) {
assertGroupInfo(t, g.ReadGroupInfo(), 0, 0, 999000, 999000)
// Write 1000 more bytes.
err := g.WriteLine(cmn.RandStr(999))
err := g.WriteLine(rand.RandStr(999))
require.NoError(t, err, "Error appending to head")
g.FlushAndSync()
@@ -68,7 +69,7 @@ func TestCheckHeadSizeLimit(t *testing.T) {
assertGroupInfo(t, g.ReadGroupInfo(), 0, 1, 1000000, 0)
// Write 1000 more bytes.
err = g.WriteLine(cmn.RandStr(999))
err = g.WriteLine(rand.RandStr(999))
require.NoError(t, err, "Error appending to head")
g.FlushAndSync()
@@ -78,7 +79,7 @@ func TestCheckHeadSizeLimit(t *testing.T) {
// Write 1000 bytes 999 times.
for i := 0; i < 999; i++ {
err = g.WriteLine(cmn.RandStr(999))
err = g.WriteLine(rand.RandStr(999))
require.NoError(t, err, "Error appending to head")
}
g.FlushAndSync()
@@ -89,7 +90,7 @@ func TestCheckHeadSizeLimit(t *testing.T) {
assertGroupInfo(t, g.ReadGroupInfo(), 0, 2, 2000000, 0)
// Write 1000 more bytes.
_, err = g.Head.Write([]byte(cmn.RandStr(999) + "\n"))
_, err = g.Head.Write([]byte(rand.RandStr(999) + "\n"))
require.NoError(t, err, "Error appending to head")
g.FlushAndSync()
assertGroupInfo(t, g.ReadGroupInfo(), 0, 2, 2001000, 1000)
+4 -4
View File
@@ -8,7 +8,7 @@ import (
"time"
"github.com/stretchr/testify/assert"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/rand"
)
func TestPanicOnMaxLength(t *testing.T) {
@@ -147,7 +147,7 @@ func _TestGCRandom(t *testing.T) {
els = append(els, el)
}
for _, i := range cmn.RandPerm(numElements) {
for _, i := range rand.RandPerm(numElements) {
el := els[i]
l.Remove(el)
_ = el.Next()
@@ -205,7 +205,7 @@ func TestScanRightDeleteRandom(t *testing.T) {
// Remove an element, push back an element.
for i := 0; i < numTimes; i++ {
// Pick an element to remove
rmElIdx := cmn.RandIntn(len(els))
rmElIdx := rand.RandIntn(len(els))
rmEl := els[rmElIdx]
// Remove it
@@ -259,7 +259,7 @@ func TestWaitChan(t *testing.T) {
for i := 1; i < 100; i++ {
l.PushBack(i)
pushed++
time.Sleep(time.Duration(cmn.RandIntn(25)) * time.Millisecond)
time.Sleep(time.Duration(rand.RandIntn(25)) * time.Millisecond)
}
// apply a deterministic pause so the counter has time to catch up
time.Sleep(25 * time.Millisecond)
+3 -1
View File
@@ -6,6 +6,8 @@ import (
"regexp"
"strings"
"sync"
"github.com/tendermint/tendermint/libs/rand"
)
// BitArray is a thread-safe implementation of a bit array.
@@ -250,7 +252,7 @@ func (bA *BitArray) PickRandom() (int, bool) {
return 0, false
}
return trueIndices[RandIntn(len(trueIndices))], true
return trueIndices[rand.RandIntn(len(trueIndices))], true
}
func (bA *BitArray) getTrueIndices() []int {
+3 -1
View File
@@ -8,10 +8,12 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/rand"
)
func randBitArray(bits int) (*BitArray, []byte) {
src := RandBytes((bits + 7) / 8)
src := rand.RandBytes((bits + 7) / 8)
bA := NewBitArray(bits)
for i := 0; i < len(src); i++ {
for j := 0; j < 8; j++ {
+4 -2
View File
@@ -10,12 +10,14 @@ import (
testing "testing"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/rand"
)
func TestWriteFileAtomic(t *testing.T) {
var (
data = []byte(RandStr(RandIntn(2048)))
old = RandBytes(RandIntn(2048))
data = []byte(rand.RandStr(rand.RandIntn(2048)))
old = rand.RandBytes(rand.RandIntn(2048))
perm os.FileMode = 0600
)
+3 -3
View File
@@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/rand"
)
// TestAddListenerForEventFireOnce sets up an EventSwitch, subscribes a single
@@ -356,7 +356,7 @@ func TestRemoveListenersAsync(t *testing.T) {
// collect received events for event2
go sumReceivedNumbers(numbers2, doneSum2)
addListenersStress := func() {
r1 := cmn.NewRand()
r1 := rand.NewRand()
r1.Seed(time.Now().UnixNano())
for k := uint16(0); k < 400; k++ {
listenerNumber := r1.Intn(100) + 3
@@ -367,7 +367,7 @@ func TestRemoveListenersAsync(t *testing.T) {
}
}
removeListenersStress := func() {
r2 := cmn.NewRand()
r2 := rand.NewRand()
r2.Seed(time.Now().UnixNano())
for k := uint16(0); k < 80; k++ {
listenerNumber := r2.Intn(100) + 3
@@ -1,4 +1,4 @@
package common
package rand
import (
crand "crypto/rand"
@@ -1,4 +1,4 @@
package common
package rand
import (
"bytes"
+4 -4
View File
@@ -1,7 +1,7 @@
package test
import (
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/rand"
)
// Contract: !bytes.Equal(input, output) && len(input) >= len(output)
@@ -17,11 +17,11 @@ func MutateByteSlice(bytez []byte) []byte {
bytez = mBytez
// Try a random mutation
switch cmn.RandInt() % 2 {
switch rand.RandInt() % 2 {
case 0: // Mutate a single byte
bytez[cmn.RandInt()%len(bytez)] += byte(cmn.RandInt()%255 + 1)
bytez[rand.RandInt()%len(bytez)] += byte(rand.RandInt()%255 + 1)
case 1: // Remove an arbitrary byte
pos := cmn.RandInt() % len(bytez)
pos := rand.RandInt() % len(bytez)
bytez = append(bytez[:pos], bytez[pos+1:]...)
}
return bytez