Skip to content

Commit

Permalink
chore: replace math/rand with math/rand/v2
Browse files Browse the repository at this point in the history
New package arrived in Go 1.22 which provides better rand primitives and functions.
Use it instead of the old one.

Signed-off-by: Dmitriy Matrenichev <[email protected]>
  • Loading branch information
DmitriyMV committed Apr 18, 2024
1 parent 89040ce commit ec69d7a
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 20 deletions.
4 changes: 2 additions & 2 deletions internal/app/machined/pkg/controllers/config/acquire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"context"
stderrors "errors"
"fmt"
"math/rand"
"math/rand/v2"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestAcquireSuite(t *testing.T) {
}
s.eventPublisher = &eventPublisherMock{}

s.clusterName = fmt.Sprintf("cluster-%d", rand.Int31())
s.clusterName = fmt.Sprintf("cluster-%d", rand.Int32())
input, err := generate.NewInput(s.clusterName, "https://localhost:6443", "")
s.Require().NoError(err)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"context"
"fmt"
"log"
"math/rand"
"math/rand/v2"
"net"
"net/netip"
"os"
Expand Down Expand Up @@ -61,7 +61,7 @@ func (suite *AddressSpecSuite) SetupTest() {
}

func (suite *AddressSpecSuite) uniqueDummyInterface() string {
return fmt.Sprintf("dummy%02x%02x%02x", rand.Int31()&0xff, rand.Int31()&0xff, rand.Int31()&0xff)
return fmt.Sprintf("dummy%02x%02x%02x", rand.Int32()&0xff, rand.Int32()&0xff, rand.Int32()&0xff)
}

func (suite *AddressSpecSuite) startRuntime() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"context"
"fmt"
"log"
"math/rand"
"math/rand/v2"
"net/netip"
"os"
"sync"
Expand Down Expand Up @@ -139,7 +139,7 @@ func (suite *LinkSpecSuite) assertNoInterface(id string) error {
}

func (suite *LinkSpecSuite) uniqueDummyInterface() string {
return fmt.Sprintf("dummy%02x%02x%02x", rand.Int31()&0xff, rand.Int31()&0xff, rand.Int31()&0xff)
return fmt.Sprintf("dummy%02x%02x%02x", rand.Int32()&0xff, rand.Int32()&0xff, rand.Int32()&0xff)
}

func (suite *LinkSpecSuite) TestLoopback() {
Expand Down Expand Up @@ -945,7 +945,8 @@ func TestSortBonds(t *testing.T) {
}

seed := time.Now().Unix()
rnd := rand.New(rand.NewSource(seed))

rnd := rand.New(rand.NewPCG(uint64(time.Now().Unix()), uint64(time.Now().Unix())))

for i := range 100 {
res := toResources(expectedSlice)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"errors"
"fmt"
"log"
"math/rand"
"math/rand/v2"
"net"
"os"
"strings"
Expand Down Expand Up @@ -79,7 +79,7 @@ func (suite *LinkStatusSuite) startRuntime() {
}

func (suite *LinkStatusSuite) uniqueDummyInterface() string {
return fmt.Sprintf("dummy%02x%02x%02x", rand.Int31()&0xff, rand.Int31()&0xff, rand.Int31()&0xff)
return fmt.Sprintf("dummy%02x%02x%02x", rand.Int32()&0xff, rand.Int32()&0xff, rand.Int32()&0xff)
}

func (suite *LinkStatusSuite) assertInterfaces(requiredIDs []string, check func(*network.LinkStatus) error) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"context"
"fmt"
"log"
"math/rand"
"math/rand/v2"
"net"
"net/netip"
"os"
Expand Down Expand Up @@ -67,7 +67,7 @@ func (suite *RouteSpecSuite) SetupTest() {
}

func (suite *RouteSpecSuite) uniqueDummyInterface() string {
return fmt.Sprintf("dummy%02x%02x%02x", rand.Int31()&0xff, rand.Int31()&0xff, rand.Int31()&0xff)
return fmt.Sprintf("dummy%02x%02x%02x", rand.Int32()&0xff, rand.Int32()&0xff, rand.Int32()&0xff)
}

func (suite *RouteSpecSuite) startRuntime() {
Expand Down
4 changes: 2 additions & 2 deletions internal/integration/base/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"encoding/hex"
"fmt"
"io"
"math/rand"
"math/rand/v2"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -143,7 +143,7 @@ func (apiSuite *APISuite) RandomDiscoveredNodeInternalIP(types ...machine.Type)

apiSuite.Require().NotEmpty(nodes)

return nodes[rand.Intn(len(nodes))].InternalIP.String()
return nodes[rand.IntN(len(nodes))].InternalIP.String()
}

// Capabilities describes current cluster allowed actions.
Expand Down
4 changes: 2 additions & 2 deletions internal/integration/base/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package base
import (
"context"
"fmt"
"math/rand"
"math/rand/v2"
"os/exec"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -73,7 +73,7 @@ func (cliSuite *CLISuite) RandomDiscoveredNodeInternalIP(types ...machine.Type)

cliSuite.Require().NotEmpty(nodes)

return nodes[rand.Intn(len(nodes))].InternalIP.String()
return nodes[rand.IntN(len(nodes))].InternalIP.String()
}

func (cliSuite *CLISuite) discoverKubectl() cluster.Info {
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"errors"
"fmt"
"io"
"math/rand"
"math/rand/v2"
"net"
"slices"
"strings"
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"errors"
"fmt"
"log"
"math/rand"
"math/rand/v2"
"os"
"time"

Expand Down
4 changes: 2 additions & 2 deletions pkg/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"encoding/base64"
"fmt"
"io"
"math/rand"
"math/rand/v2"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -217,7 +217,7 @@ func download(req *http.Request, options *downloadOptions) (data []byte, err err
transport.RegisterProtocol("tftp", NewTFTPTransport())

if options.LowSrcPort {
port := 100 + rand.Intn(512)
port := 100 + rand.IntN(512)

localTCPAddr, tcperr := net.ResolveTCPAddr("tcp", ":"+strconv.Itoa(port))
if tcperr != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/machinery/client/resolver/roundrobin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package resolver

import (
"math/rand"
"math/rand/v2"
"net"
"strconv"
"strings"
Expand Down

0 comments on commit ec69d7a

Please sign in to comment.