-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdisconnect_test.go
101 lines (83 loc) · 4.4 KB
/
disconnect_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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"time"
)
func (s *gatewaySuite) TestDisconnect() {
// assert FIX MD logon
fix, err := s.fixMd.WaitForMessage(s.MarketDataSessionID, 1)
s.Require().Nil(err)
err = s.checkFixTags(fix, "35=A", "49=BFXFIX", "56=EXORG_MD")
s.Require().Nil(err)
// assert FIX order logon
fix, err = s.fixOrd.WaitForMessage(s.OrderSessionID, 1)
s.Require().Nil(err)
err = s.checkFixTags(fix, "35=A", "49=BFXFIX", "56=EXORG_ORD")
s.Require().Nil(err)
// assume both ws clients connected in setup()
s.srvWs.Broadcast(`{"event":"info","version":2}`)
// assert MD ws auth request
msg, err := s.srvWs.WaitForMessage(MarketDataClient, 0)
s.Require().Nil(err)
s.Require().EqualValues(`{"subId":"nonce1","event":"auth","apiKey":"apiKey1","authSig":"2744ec1afc974eadbda7e09efa03da80578628ba90e2aa5fcba8c2c61014b811f3a8be5a041c3ee35c464a59856b3869","authPayload":"AUTHnonce1","authNonce":"nonce1"}`, msg)
// assert order ws auth request
msg, err = s.srvWs.WaitForMessage(OrdersClient, 0)
s.Require().Nil(err)
s.Require().EqualValues(`{"subId":"nonce1","event":"auth","apiKey":"apiKey1","authSig":"2744ec1afc974eadbda7e09efa03da80578628ba90e2aa5fcba8c2c61014b811f3a8be5a041c3ee35c464a59856b3869","authPayload":"AUTHnonce1","authNonce":"nonce1"}`, msg)
// broadcast auth ack to both clients
s.srvWs.Broadcast(`{"event":"auth","status":"OK","chanId":0,"userId":1,"subId":"nonce1","auth_id":"valid-auth-guid","caps":{"orders":{"read":1,"write":0},"account":{"read":1,"write":0},"funding":{"read":1,"write":0},"history":{"read":1,"write":0},"wallets":{"read":1,"write":0},"withdraw":{"read":0,"write":0},"positions":{"read":1,"write":0}}}`)
// disconnect ws
err = s.srvWs.Stop()
s.Require().Nil(err)
err = s.srvWs.KillConnections()
s.Require().Nil(err)
defer func() { s.isWsOnline = false }()
// wait for ws disconnect & reconnect period, assert FIX logoff msgs
_, err = s.fixMd.WaitForMessageWithWait(s.MarketDataSessionID, 2, time.Second*20)
s.Require().Nil(err)
}
func (s *gatewaySuite) TestReconnect() {
// assert FIX MD logon
fix, err := s.fixMd.WaitForMessage(s.MarketDataSessionID, 1)
s.Require().Nil(err)
err = s.checkFixTags(fix, "35=A", "49=BFXFIX", "56=EXORG_MD")
s.Require().Nil(err)
// assert FIX order logon
fix, err = s.fixOrd.WaitForMessage(s.OrderSessionID, 1)
s.Require().Nil(err)
err = s.checkFixTags(fix, "35=A", "49=BFXFIX", "56=EXORG_ORD")
s.Require().Nil(err)
// assume both ws clients connected in setup()
s.srvWs.Broadcast(`{"event":"info","version":2}`)
// assert MD ws auth request
msg, err := s.srvWs.WaitForMessage(MarketDataClient, 0)
s.Require().Nil(err)
s.Require().EqualValues(`{"subId":"nonce1","event":"auth","apiKey":"apiKey1","authSig":"2744ec1afc974eadbda7e09efa03da80578628ba90e2aa5fcba8c2c61014b811f3a8be5a041c3ee35c464a59856b3869","authPayload":"AUTHnonce1","authNonce":"nonce1"}`, msg)
// assert order ws auth request
msg, err = s.srvWs.WaitForMessage(OrdersClient, 0)
s.Require().Nil(err)
s.Require().EqualValues(`{"subId":"nonce1","event":"auth","apiKey":"apiKey1","authSig":"2744ec1afc974eadbda7e09efa03da80578628ba90e2aa5fcba8c2c61014b811f3a8be5a041c3ee35c464a59856b3869","authPayload":"AUTHnonce1","authNonce":"nonce1"}`, msg)
// broadcast auth ack to both clients
s.srvWs.Broadcast(`{"event":"auth","status":"OK","chanId":0,"userId":1,"subId":"nonce1","auth_id":"valid-auth-guid","caps":{"orders":{"read":1,"write":0},"account":{"read":1,"write":0},"funding":{"read":1,"write":0},"history":{"read":1,"write":0},"wallets":{"read":1,"write":0},"withdraw":{"read":0,"write":0},"positions":{"read":1,"write":0}}}`)
// disconnect ws and kill outstanding connections
err = s.srvWs.Stop()
s.Require().Nil(err)
err = s.srvWs.KillConnections()
s.Require().Nil(err)
// wait for ws disconnect & reconnect period, assert FIX logoff msgs
fix, err = s.fixOrd.WaitForMessageWithWait(s.OrderSessionID, 2, time.Second*20)
s.Require().Nil(err)
err = s.checkFixTags(fix, "35=5", "49=BFXFIX", "56=EXORG_ORD")
s.Require().Nil(err)
// start ws again
err = s.srvWs.Start()
s.Require().Nil(err)
// wait for ws to reconnect, get a sequence number reset
fix, err = s.fixOrd.WaitForMessageWithWait(s.OrderSessionID, 3, time.Second*20)
s.Require().Nil(err)
err = s.checkFixTags(fix, "35=4", "49=BFXFIX", "56=EXORG_ORD")
s.Require().Nil(err)
fix, err = s.fixOrd.WaitForMessage(s.OrderSessionID, 4)
s.Require().Nil(err)
err = s.checkFixTags(fix, "35=A", "49=BFXFIX", "56=EXORG_ORD")
s.Require().Nil(err)
}