From 0718efdcbf53e3c7496d09351c9f8da7fb2e1979 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Thu, 3 Jan 2019 18:09:23 -0800 Subject: [PATCH] add test for utils --- internal/utils/metrics_test.go | 19 ++++++++++++++ internal/utils/utils.go | 23 ----------------- internal/utils/utils_test.go | 47 +++++++++++++++++++++++++++++++++- 3 files changed, 65 insertions(+), 24 deletions(-) create mode 100644 internal/utils/metrics_test.go diff --git a/internal/utils/metrics_test.go b/internal/utils/metrics_test.go new file mode 100644 index 000000000..7c5a2fa20 --- /dev/null +++ b/internal/utils/metrics_test.go @@ -0,0 +1,19 @@ +package utils + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +// Test for BToMb. +func TestBToMb(t *testing.T) { + a := uint64(1024*1024 + 1) + assert.Equal(t, BToMb(a), uint64(1), "should be equal to 1") + + a = uint64(1024*1024 - 1) + assert.Equal(t, BToMb(a), uint64(0), "should be equal to 0") + + a = uint64(1024 * 1024) + assert.Equal(t, BToMb(a), uint64(1), "should be equal to 0") +} diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 12c38b9c7..c15201633 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/binary" "log" - "os/exec" "regexp" "strconv" @@ -41,28 +40,6 @@ func GetUniqueIDFromIPPort(ip, port string) uint32 { return uint32(value) } -// RunCmd runs command `name` with arguments `args` -func RunCmd(name string, args ...string) error { - cmd := exec.Command(name, args...) - stderrBytes := &bytes.Buffer{} - cmd.Stderr = stderrBytes - if err := cmd.Start(); err != nil { - log.Fatal(err) - return err - } - - log.Println("Command running", name, args) - go func() { - if err := cmd.Wait(); err != nil { - log.Printf("Command finished with error: %v", err) - log.Printf("Stderr: %v", string(stderrBytes.Bytes())) - } else { - log.Printf("Command finished successfully") - } - }() - return nil -} - // GenKey generates a key given ip and port. func GenKey(ip, port string) (kyber.Scalar, kyber.Point) { priKey := crypto.Ed25519Curve.Scalar().SetInt64(int64(GetUniqueIDFromIPPort(ip, port))) // TODO: figure out why using a random hash value doesn't work for private key (schnorr) diff --git a/internal/utils/utils_test.go b/internal/utils/utils_test.go index 566b06b0c..275b33c56 100644 --- a/internal/utils/utils_test.go +++ b/internal/utils/utils_test.go @@ -1,7 +1,13 @@ package utils -import "testing" +import ( + "testing" + "github.com/harmony-one/harmony/p2p" + "github.com/stretchr/testify/assert" +) + +// Tests for TestConvertFixedDataIntoByteArray. func TestConvertFixedDataIntoByteArray(t *testing.T) { res := ConvertFixedDataIntoByteArray(int16(3)) if len(res) != 2 { @@ -12,3 +18,42 @@ func TestConvertFixedDataIntoByteArray(t *testing.T) { t.Errorf("Conversion incorrect.") } } + +// Tests for TestAllocateShard. +func TestAllocateShard(t *testing.T) { + num, success := AllocateShard(1, 1) + assert.Equal(t, num, 1, "error") + assert.True(t, success, "error") + + num, success = AllocateShard(2, 1) + assert.False(t, success, "error") + assert.Equal(t, num, 1, "error") + + num, success = AllocateShard(1, 2) + assert.True(t, success, "error") + assert.Equal(t, num, 1, "error") + + num, success = AllocateShard(5, 3) + assert.False(t, success, "error") + assert.Equal(t, num, 2, "error") + + num, success = AllocateShard(6, 3) + assert.False(t, success, "error") + assert.Equal(t, num, 3, "error") +} + +// Test for GenKey +func TestGenKey(t *testing.T) { + GenKey("3.3.3.3", "3456") +} + +// Test for GetUniqueIDFromPeer +func TestGetUniqueIDFromPeer(t *testing.T) { + peer := p2p.Peer{IP: "1.1.1.1", Port: "123"} + assert.Equal(t, GetUniqueIDFromPeer(peer), uint32(1111123), "should be equal to 1111123") +} + +// Test for GetUniqueIDFromIPPort +func TestGetUniqueIDFromIPPort(t *testing.T) { + assert.Equal(t, GetUniqueIDFromIPPort("1.1.1.1", "123"), uint32(1111123), "should be equal to 1111123") +}