In the common package added JSONDecode error check. Added verbosity in SendHTTPGetRequest. Updated Nonce package function. Fixed issues in ItBit package and expanded test coverage.

This commit is contained in:
Ryan O'Hara-Reid
2017-09-12 08:37:18 +10:00
parent 939ce3ab21
commit e8c7bf9af4
24 changed files with 601 additions and 241 deletions

View File

@@ -8,8 +8,12 @@ import (
// Nonce struct holds the nonce value
type Nonce struct {
// Standard nonce
n int64
mtx sync.Mutex
// Hash table exclusive exchange specific nonce values
boundedCall map[string]int64
boundedMtx sync.Mutex
}
// Inc increments the nonce value
@@ -49,14 +53,32 @@ func (n *Nonce) String() string {
return result
}
// Evaluate returns a nonce while evaluating in a single locked call
func (n *Nonce) Evaluate() string {
n.mtx.Lock()
defer n.mtx.Unlock()
if n.n == 0 {
n.n = time.Now().Unix()
return strconv.FormatInt(n.n, 10)
// Value is a return type for GetValue
type Value int64
// GetValue returns a nonce value and can be set as a higher precision. Values
// stored in an exchange specific hash table using a single locked call.
func (n *Nonce) GetValue(exchName string, nanoPrecision bool) Value {
n.boundedMtx.Lock()
defer n.boundedMtx.Unlock()
if n.boundedCall == nil {
n.boundedCall = make(map[string]int64)
}
n.n = n.n + 1
return strconv.FormatInt(n.n, 10)
if n.boundedCall[exchName] == 0 {
if nanoPrecision {
n.boundedCall[exchName] = time.Now().UnixNano()
return Value(n.boundedCall[exchName])
}
n.boundedCall[exchName] = time.Now().Unix()
return Value(n.boundedCall[exchName])
}
n.boundedCall[exchName]++
return Value(n.boundedCall[exchName])
}
// String is a Value method that changes format to a string
func (v Value) String() string {
return strconv.FormatInt(int64(v), 10)
}

View File

@@ -1,6 +1,7 @@
package nonce
import (
"strconv"
"testing"
"time"
)
@@ -56,6 +57,16 @@ func TestString(t *testing.T) {
}
}
func TestGetValue(t *testing.T) {
var nonce Nonce
timeNowNano := strconv.FormatInt(time.Now().UnixNano(), 10)
nValue := nonce.GetValue("dingdong", true).String()
if timeNowNano == nValue {
t.Error("Test failed - GetValue() error, incorrect values")
}
}
func TestNonceConcurrency(t *testing.T) {
var nonce Nonce
nonce.Set(12312)