Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RandomHex tests #965

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions cmd/coordctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,16 @@

import (
"context"
"crypto/rand"
"encoding/hex"
"fmt"

"github.com/pg-sharding/spqr/pkg/models/topology"
protos "github.com/pg-sharding/spqr/pkg/protos"
"github.com/pg-sharding/spqr/pkg/spqrlog"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"github.com/pg-sharding/spqr/pkg/coord"

Check failure on line 12 in cmd/coordctl/main.go

View workflow job for this annotation

GitHub Actions / build

found packages coord (adapter.go) and coord_test (utils_tests.go) in /home/runner/work/spqr/spqr/pkg/coord

Check failure on line 12 in cmd/coordctl/main.go

View workflow job for this annotation

GitHub Actions / golangci-lint

found packages coord (adapter.go) and coord_test (utils_tests.go) in pkg/coord (typecheck)

Check failure on line 12 in cmd/coordctl/main.go

View workflow job for this annotation

GitHub Actions / analyze

found packages coord (adapter.go) and coord_test (utils_tests.go) in /home/runner/work/spqr/spqr/pkg/coord
)

// TDB: move to util
// TODO : unit tests
func randomHex(n int) (string, error) {
bytes := make([]byte, n)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
}

var (
coordinatorEndpoint string
Expand Down Expand Up @@ -64,7 +54,7 @@
}

if routerID == "" {
routerID, err = randomHex(6)
routerID, err = coord.RandomHex(6)

if err != nil {
return err
Expand Down Expand Up @@ -144,7 +134,7 @@
}

if shardID == "" {
shardID, err = randomHex(6)
shardID, err = coord.RandomHex(6)

if err != nil {
return err
Expand Down
15 changes: 15 additions & 0 deletions pkg/coord/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package coord

import (
"crypto/rand"
"encoding/hex"
)


func RandomHex(n int) (string, error) {
bytes := make([]byte, n)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
}
21 changes: 21 additions & 0 deletions pkg/coord/utils_tests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package coord_test

import (
"testing"

"github.com/pg-sharding/spqr/pkg/coord"
)

func TestRandomHex(t *testing.T) {
t.Run("Success case", func(t *testing.T) {
output, err := coord.RandomHex(16)

if err != nil {
t.Errorf("Expected no error, got %v", err)
}

if len(output) != 32 {
t.Errorf("Expected string of length 32, got %v", len(output))
}
})
}
Loading