mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-24 23:16:52 +00:00
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:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user