[#587] Do not use math/rand.Read

Fix staticcheck warnings after go1.20 update.

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2023-08-09 16:01:44 +03:00
parent 33c11be0cf
commit d641cba2fc
9 changed files with 33 additions and 27 deletions

View file

@ -2,8 +2,8 @@ package blobovnicza
import (
"context"
"crypto/rand"
"errors"
"math/rand"
"os"
"testing"
@ -54,8 +54,6 @@ func testGet(t *testing.T, blz *Blobovnicza, addr oid.Address, expObj []byte, as
}
func TestBlobovnicza(t *testing.T) {
rand.Seed(1024)
p := "./test_blz"
sizeLim := uint64(256 * 1 << 10) // 256KB

View file

@ -2,7 +2,8 @@ package blobstortest
import (
"context"
"math/rand"
"crypto/rand"
mrand "math/rand"
"testing"
objectCore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
@ -53,8 +54,9 @@ func TestInfo(t *testing.T, cons Constructor, expectedType string, expectedPath
func prepare(t *testing.T, count int, s common.Storage, min, max uint64) []objectDesc {
objects := make([]objectDesc, count)
r := mrand.New(mrand.NewSource(0))
for i := range objects {
objects[i].obj = NewObject(min + uint64(rand.Intn(int(max-min+1)))) // not too large
objects[i].obj = NewObject(min + uint64(r.Intn(int(max-min+1)))) // not too large
objects[i].addr = objectCore.AddressOf(objects[i].obj)
raw, err := objects[i].obj.Marshal()

View file

@ -1,9 +1,11 @@
package meta
import (
"crypto/rand"
"math"
"math/rand"
mrand "math/rand"
"testing"
"time"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/stretchr/testify/require"
@ -39,9 +41,10 @@ func Test_decodeList(t *testing.T) {
require.Error(t, err)
})
t.Run("random", func(t *testing.T) {
r := mrand.New(mrand.NewSource(time.Now().Unix()))
expected := make([][]byte, 20)
for i := range expected {
expected[i] = make([]byte, rand.Uint32()%10)
expected[i] = make([]byte, r.Uint32()%10)
rand.Read(expected[i])
}

View file

@ -2,12 +2,14 @@ package pilorama
import (
"context"
"crypto/rand"
"fmt"
"math/rand"
mrand "math/rand"
"path/filepath"
"strconv"
"sync"
"testing"
"time"
cidSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
@ -767,9 +769,10 @@ func prepareRandomTree(nodeCount, opCount int) []Move {
rand.Read(ops[i].Meta.Items[1].Value)
}
r := mrand.New(mrand.NewSource(time.Now().Unix()))
for i := nodeCount; i < len(ops); i++ {
ops[i] = Move{
Parent: rand.Uint64() % uint64(nodeCount+12),
Parent: r.Uint64() % uint64(nodeCount+12),
Meta: Meta{
Time: Timestamp(i + nodeCount),
Items: []KeyValue{
@ -777,9 +780,9 @@ func prepareRandomTree(nodeCount, opCount int) []Move {
{Value: make([]byte, 10)},
},
},
Child: rand.Uint64() % uint64(nodeCount+10),
Child: r.Uint64() % uint64(nodeCount+10),
}
if rand.Uint32()%5 == 0 {
if r.Uint32()%5 == 0 {
ops[i].Parent = TrashID
}
rand.Read(ops[i].Meta.Items[1].Value)
@ -813,7 +816,7 @@ func compareForests(t *testing.T, expected, actual Forest, cid cidSDK.ID, treeID
}
func testForestTreeParallelApply(t *testing.T, constructor func(t testing.TB, _ ...Option) Forest, batchSize, opCount, iterCount int) {
rand.Seed(42)
r := mrand.New(mrand.NewSource(42))
const nodeCount = 5
@ -829,7 +832,7 @@ func testForestTreeParallelApply(t *testing.T, constructor func(t testing.TB, _
for i := 0; i < iterCount; i++ {
// Shuffle random operations, leave initialization in place.
rand.Shuffle(len(ops), func(i, j int) { ops[i], ops[j] = ops[j], ops[i] })
r.Shuffle(len(ops), func(i, j int) { ops[i], ops[j] = ops[j], ops[i] })
actual := constructor(t, WithMaxBatchSize(batchSize))
wg := new(sync.WaitGroup)
@ -855,7 +858,7 @@ func testForestTreeParallelApply(t *testing.T, constructor func(t testing.TB, _
}
func testForestTreeApplyRandom(t *testing.T, constructor func(t testing.TB, _ ...Option) Forest) {
rand.Seed(42)
r := mrand.New(mrand.NewSource(42))
const (
nodeCount = 5
@ -875,7 +878,7 @@ func testForestTreeApplyRandom(t *testing.T, constructor func(t testing.TB, _ ..
const iterCount = 200
for i := 0; i < iterCount; i++ {
// Shuffle random operations, leave initialization in place.
rand.Shuffle(len(ops), func(i, j int) { ops[i], ops[j] = ops[j], ops[i] })
r.Shuffle(len(ops), func(i, j int) { ops[i], ops[j] = ops[j], ops[i] })
actual := constructor(t)
for i := range ops {
@ -897,17 +900,18 @@ func BenchmarkApplySequential(b *testing.B) {
b.Run(providers[i].name, func(b *testing.B) {
for _, bs := range batchSizes {
b.Run("batchsize="+strconv.Itoa(bs), func(b *testing.B) {
r := mrand.New(mrand.NewSource(time.Now().Unix()))
s := providers[i].construct(b, WithMaxBatchSize(bs))
benchmarkApply(b, s, func(opCount int) []Move {
ops := make([]Move, opCount)
for i := range ops {
ops[i] = Move{
Parent: uint64(rand.Intn(benchNodeCount)),
Parent: uint64(r.Intn(benchNodeCount)),
Meta: Meta{
Time: Timestamp(i),
Items: []KeyValue{{Value: []byte{0, 1, 2, 3, 4}}},
},
Child: uint64(rand.Intn(benchNodeCount)),
Child: uint64(r.Intn(benchNodeCount)),
}
}
return ops
@ -930,17 +934,18 @@ func BenchmarkApplyReorderLast(b *testing.B) {
b.Run(providers[i].name, func(b *testing.B) {
for _, bs := range batchSizes {
b.Run("batchsize="+strconv.Itoa(bs), func(b *testing.B) {
r := mrand.New(mrand.NewSource(time.Now().Unix()))
s := providers[i].construct(b, WithMaxBatchSize(bs))
benchmarkApply(b, s, func(opCount int) []Move {
ops := make([]Move, opCount)
for i := range ops {
ops[i] = Move{
Parent: uint64(rand.Intn(benchNodeCount)),
Parent: uint64(r.Intn(benchNodeCount)),
Meta: Meta{
Time: Timestamp(i),
Items: []KeyValue{{Value: []byte{0, 1, 2, 3, 4}}},
},
Child: uint64(rand.Intn(benchNodeCount)),
Child: uint64(r.Intn(benchNodeCount)),
}
if i != 0 && i%blockSize == 0 {
for j := 0; j < blockSize/2; j++ {
@ -957,8 +962,6 @@ func BenchmarkApplyReorderLast(b *testing.B) {
}
func benchmarkApply(b *testing.B, s Forest, genFunc func(int) []Move) {
rand.Seed(42)
ops := genFunc(b.N)
cid := cidtest.ID()
treeID := "version"

View file

@ -1,7 +1,7 @@
package pilorama
import (
"math/rand"
"crypto/rand"
"testing"
"github.com/stretchr/testify/require"

View file

@ -2,7 +2,7 @@ package shard_test
import (
"context"
"math/rand"
"crypto/rand"
"testing"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"

View file

@ -1,7 +1,7 @@
package util_test
import (
"math/rand"
"crypto/rand"
"testing"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util"

View file

@ -9,11 +9,11 @@ import (
)
func TestInitEndpoints(t *testing.T) {
rand.Seed(time.Now().UnixNano())
r := rand.New(rand.NewSource(time.Now().UnixNano()))
ee := make([]Endpoint, 100)
for i := range ee {
ee[i].Priority = rand.Int()
ee[i].Priority = r.Int()
}
var eeInternal endpoints

View file

@ -1,8 +1,8 @@
package netmap
import (
"crypto/rand"
"math/big"
"math/rand"
"strconv"
"testing"