add test for utils

pull/212/head
Minh Doan 6 years ago committed by Minh Doan
parent 99a9baf8d4
commit 0718efdcbf
  1. 19
      internal/utils/metrics_test.go
  2. 23
      internal/utils/utils.go
  3. 47
      internal/utils/utils_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")
}

@ -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)

@ -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")
}

Loading…
Cancel
Save