[#587] Do not use math/rand.Read
Some checks failed
Build / Build Components (1.20) (pull_request) Failing after 3s
Build / Build Components (1.21) (pull_request) Failing after 3s
Tests and linters / Lint (pull_request) Failing after 3s
Tests and linters / Tests (1.20) (pull_request) Failing after 3s
Tests and linters / Tests (1.21) (pull_request) Failing after 3s
Tests and linters / Tests with -race (pull_request) Failing after 5s
Tests and linters / Staticcheck (pull_request) Failing after 8s
Vulncheck / Vulncheck (pull_request) Failing after 2s

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

View file

@ -2,7 +2,8 @@ package blobstortest
import ( import (
"context" "context"
"math/rand" "crypto/rand"
mrand "math/rand"
"testing" "testing"
objectCore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object" 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 { func prepare(t *testing.T, count int, s common.Storage, min, max uint64) []objectDesc {
objects := make([]objectDesc, count) objects := make([]objectDesc, count)
r := mrand.New(mrand.NewSource(0))
for i := range objects { 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) objects[i].addr = objectCore.AddressOf(objects[i].obj)
raw, err := objects[i].obj.Marshal() raw, err := objects[i].obj.Marshal()

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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