Files
gocryptotrader/exchanges/orderbook/node_test.go
Ryan O'Hara-Reid a2381310da GCT: general updates across codebase (#699)
* orderbook: export orderbook nodes for external strategy inspection

* orderbook: Add in methods for locking and unlocking multiple books at the same time e.g. book1.LockWith(book2); defer book1.UnlockWith(book2)

* include waiting functionality for depth change alert

* backtester: add word.

* log: include logger changes to impl with downstream integration

* engine: reduce params for loading exchange

* assort: rm verbose in tests, change wording in ob, expose sync.waitgroup for ext. sync options

* ticker: reduce map look ups and contention when using RW mutex when there are over 80% writes adds find last function to get the latest rate

* engine/syncmanager: add in waitgroup for step over for external package calls

* cleaup

* engine: linter fix

* currency/fx: include all references to fiat currencies to default

* orderbook: Add in fields to Unsafe type for strategies to detect potential out of sync book operations

* syncmanager: changed config variable to display correct time

* ordermanager: Add time when none provided

* currency/manager: update getasset param to get enabled assets for minor optimizations

* ftx: use get all wallet balances for a better accounts breakdown

* orderbook: unlock in reverse order

* bithumb: fixes bug on market buy and sell orders

* bithumb: fix bug for nonce is also time window sensitive

* bithumb: get orders add required parameter

* bithumb: Add asset type to account struct

* currency: improve log output when checking currency and it fails

* bithumb: Add error return on incomplete pair

* ticker:unexport all service related methods

* ticker/currency: fixes

* orderbook: fix comment

* engine: revert variable name in LoadExchange method

* sync_manager: fix panic when enabling disabling manager

* engine: fix naming convention of exported function and comments

* engine: update comment

* orderbook: fix comment for unsafe type
2021-07-29 14:42:28 +10:00

102 lines
2.1 KiB
Go

package orderbook
import (
"fmt"
"sync/atomic"
"testing"
"time"
)
func TestPushPop(t *testing.T) {
s := newStack()
var nSlice [100]*Node
for i := 0; i < 100; i++ {
nSlice[i] = s.Pop()
}
if s.getCount() != 0 {
t.Fatalf("incorrect stack count expected %v but received %v", 0, s.getCount())
}
for i := 0; i < 100; i++ {
s.Push(nSlice[i], getNow())
}
if s.getCount() != 100 {
t.Fatalf("incorrect stack count expected %v but received %v", 100, s.getCount())
}
}
func TestCleaner(t *testing.T) {
s := newStack()
var nSlice [100]*Node
for i := 0; i < 100; i++ {
nSlice[i] = s.Pop()
}
tn := getNow()
for i := 0; i < 50; i++ {
s.Push(nSlice[i], tn)
}
// Makes all the 50 pushed nodes invalid
time.Sleep(time.Millisecond * 260)
tn = getNow()
for i := 50; i < 100; i++ {
s.Push(nSlice[i], tn)
}
time.Sleep(time.Millisecond * 50)
if s.getCount() != 50 {
t.Fatalf("incorrect stack count expected %v but received %v", 50, s.getCount())
}
time.Sleep(time.Millisecond * 350)
if s.getCount() != 0 {
t.Fatalf("incorrect stack count expected %v but received %v", 0, s.getCount())
}
}
// Display nodes for testing purposes
func (s *stack) Display() {
for i := int32(0); i < s.getCount(); i++ {
fmt.Printf("NODE IN STACK: %+v %p \n", s.nodes[i], s.nodes[i])
}
fmt.Println("TOTAL COUNT:", s.getCount())
}
// 158 9,521,717 ns/op 9600104 B/op 100001 allocs/op
func BenchmarkWithoutStack(b *testing.B) {
var n *Node
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < 100000; j++ {
n = new(Node)
n.Value.Price = 1337
}
}
}
// 316 3,485,211 ns/op 1 B/op 0 allocs/op
func BenchmarkWithStack(b *testing.B) {
var n *Node
stack := newStack()
b.ReportAllocs()
b.ResetTimer()
tn := getNow()
for i := 0; i < b.N; i++ {
for j := 0; j < 100000; j++ {
n = stack.Pop()
n.Value.Price = 1337
stack.Push(n, tn)
}
}
}
// getCount is a test helper function to derive the count that does not race.
func (s *stack) getCount() int32 {
if !atomic.CompareAndSwapUint32(&s.sema, neutral, active) {
return -1
}
defer atomic.StoreUint32(&s.sema, neutral)
return s.count
}