From d641cba2fc9d33d895da95387a8cf8ad3affe9bb Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 9 Aug 2023 16:01:44 +0300 Subject: [PATCH] [#587] Do not use math/rand.Read Fix staticcheck warnings after go1.20 update. Signed-off-by: Evgenii Stratonikov --- .../blobovnicza/blobovnicza_test.go | 4 +-- .../blobstor/internal/blobstortest/common.go | 6 ++-- .../metabase/index_test.go | 7 +++-- .../pilorama/forest_test.go | 31 ++++++++++--------- .../pilorama/meta_test.go | 2 +- .../shard/shutdown_test.go | 2 +- .../util/splitinfo_test.go | 2 +- pkg/morph/client/multy_test.go | 4 +-- pkg/morph/client/netmap/netmap_test.go | 2 +- 9 files changed, 33 insertions(+), 27 deletions(-) diff --git a/pkg/local_object_storage/blobovnicza/blobovnicza_test.go b/pkg/local_object_storage/blobovnicza/blobovnicza_test.go index c7b7bb8a..f8f63856 100644 --- a/pkg/local_object_storage/blobovnicza/blobovnicza_test.go +++ b/pkg/local_object_storage/blobovnicza/blobovnicza_test.go @@ -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 diff --git a/pkg/local_object_storage/blobstor/internal/blobstortest/common.go b/pkg/local_object_storage/blobstor/internal/blobstortest/common.go index e31f3280..1f234198 100644 --- a/pkg/local_object_storage/blobstor/internal/blobstortest/common.go +++ b/pkg/local_object_storage/blobstor/internal/blobstortest/common.go @@ -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() diff --git a/pkg/local_object_storage/metabase/index_test.go b/pkg/local_object_storage/metabase/index_test.go index eb7238a5..45b9bc75 100644 --- a/pkg/local_object_storage/metabase/index_test.go +++ b/pkg/local_object_storage/metabase/index_test.go @@ -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]) } diff --git a/pkg/local_object_storage/pilorama/forest_test.go b/pkg/local_object_storage/pilorama/forest_test.go index 5c143d3e..0e2dd5cc 100644 --- a/pkg/local_object_storage/pilorama/forest_test.go +++ b/pkg/local_object_storage/pilorama/forest_test.go @@ -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" diff --git a/pkg/local_object_storage/pilorama/meta_test.go b/pkg/local_object_storage/pilorama/meta_test.go index 7adb97ab..9df4c7e9 100644 --- a/pkg/local_object_storage/pilorama/meta_test.go +++ b/pkg/local_object_storage/pilorama/meta_test.go @@ -1,7 +1,7 @@ package pilorama import ( - "math/rand" + "crypto/rand" "testing" "github.com/stretchr/testify/require" diff --git a/pkg/local_object_storage/shard/shutdown_test.go b/pkg/local_object_storage/shard/shutdown_test.go index 15bff7f1..68ef9096 100644 --- a/pkg/local_object_storage/shard/shutdown_test.go +++ b/pkg/local_object_storage/shard/shutdown_test.go @@ -2,7 +2,7 @@ package shard_test import ( "context" - "math/rand" + "crypto/rand" "testing" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object" diff --git a/pkg/local_object_storage/util/splitinfo_test.go b/pkg/local_object_storage/util/splitinfo_test.go index 642fef3b..0b7be5af 100644 --- a/pkg/local_object_storage/util/splitinfo_test.go +++ b/pkg/local_object_storage/util/splitinfo_test.go @@ -1,7 +1,7 @@ package util_test import ( - "math/rand" + "crypto/rand" "testing" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util" diff --git a/pkg/morph/client/multy_test.go b/pkg/morph/client/multy_test.go index 4bc38c70..84a07b0a 100644 --- a/pkg/morph/client/multy_test.go +++ b/pkg/morph/client/multy_test.go @@ -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 diff --git a/pkg/morph/client/netmap/netmap_test.go b/pkg/morph/client/netmap/netmap_test.go index d6172a68..a8a30619 100644 --- a/pkg/morph/client/netmap/netmap_test.go +++ b/pkg/morph/client/netmap/netmap_test.go @@ -1,8 +1,8 @@ package netmap import ( + "crypto/rand" "math/big" - "math/rand" "strconv" "testing"