-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbinance_test.go
67 lines (56 loc) · 1.7 KB
/
binance_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package binancewebsocket
import (
"testing"
"time"
)
func TestBinanceDepthConnection(t *testing.T) {
ws := NewBinanceWs()
err, close := ws.SubscribeDepth("btcusdt", func (d *Depth) {})
if err != nil {
t.Fatalf("failed to connect to binance @depth websocket")
}
close <- struct{}{}
}
func TestBinanceDepthCloseConnectionDirectly(t *testing.T) {
ws := NewBinanceWs()
err, _ := ws.SubscribeDepth("btcusdt", func (d *Depth) {})
if err != nil {
t.Fatalf("failed to connect to binance @depth websocket")
}
ws.Conn.Close()
time.Sleep(time.Duration(1) * time.Second)
}
func TestBinanceDepthMessage(t *testing.T) {
ws := NewBinanceWs()
messages := make(chan *Depth, 10)
err, close := ws.SubscribeDepth("btcusdt", func (d *Depth) {
messages <- d
})
if err != nil {
t.Fatalf("failed to connect to binance @depth websocket")
}
msg := <- messages
if msg.LastUpdateID == 0 {
t.Errorf("LastUpdateID should not be 0")
}
if len(msg.Bids) == 0 && len(msg.Asks) == 0 {
t.Errorf("Depth update should contain asks or bids")
}
close <- struct{}{}
}
func BenchmarkBinanceMessageHandling(b *testing.B) {
ws := NewBinanceWs()
//messages := make(chan *Depth, 10)
err, close := ws.SubscribeDepth("btcusdt2", func (d *Depth) {
d.DecrementReferenceCount()
})
if err != nil {
b.Fatalf("failed to connect to binance @depth websocket")
}
b.ResetTimer()
msg := []byte("{\"e\":\"depthUpdate\",\"E\":1577485630559,\"s\":\"BTCUSDT\",\"U\":1627259958,\"u\":1627259960,\"b\":[[\"7246.02000000\",\"0.00000000\"],[\"7246.00000000\",\"0.02930400\"],[\"7245.75000000\",\"0.00000000\"],[\"7239.18000000\",\"0.00000000\"]],\"a\":[]}")
for i := 0; i < b.N; i += 1 {
ws.Conn.ReceiveMessage(msg)
}
close <- struct{}{}
}