This repository has been archived by the owner on Aug 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
websocket_test.go
141 lines (125 loc) · 2.68 KB
/
websocket_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package kumex
import (
"testing"
)
func TestApiService_WebSocketPublicToken(t *testing.T) {
s := NewApiServiceFromEnv()
rsp, err := s.WebSocketPublicToken()
if err != nil {
t.Fatal(err)
}
pt := &WebSocketTokenModel{}
if err := rsp.ReadData(pt); err != nil {
t.Fatal(err)
}
t.Log(pt.Token)
switch {
case pt.Token == "":
t.Error("Empty key 'token'")
case len(pt.Servers) == 0:
t.Fatal("Empty key 'instanceServers'")
}
for _, s := range pt.Servers {
t.Log(ToJsonString(s))
switch {
case s.Endpoint == "":
t.Error("Empty key 'endpoint'")
case s.Protocol == "":
t.Fatal("Empty key 'protocol'")
}
}
}
func TestApiService_WebSocketPrivateToken(t *testing.T) {
s := NewApiServiceFromEnv()
rsp, err := s.WebSocketPrivateToken()
if err != nil {
t.Fatal(err)
}
pt := &WebSocketTokenModel{}
if err := rsp.ReadData(pt); err != nil {
t.Fatal(err)
}
t.Log(pt.Token)
switch {
case pt.Token == "":
t.Error("Empty key 'token'")
case len(pt.Servers) == 0:
t.Fatal("Empty key 'instanceServers'")
}
for _, s := range pt.Servers {
t.Log(ToJsonString(s))
switch {
case s.Endpoint == "":
t.Error("Empty key 'endpoint'")
case s.Protocol == "":
t.Fatal("Empty key 'protocol'")
}
}
}
func TestWebSocketClient_Connect(t *testing.T) {
s := NewApiServiceFromEnv()
rsp, err := s.WebSocketPublicToken()
if err != nil {
t.Fatal(err)
}
tk := &WebSocketTokenModel{}
if err := rsp.ReadData(tk); err != nil {
t.Fatal(err)
}
c := s.NewWebSocketClient(tk)
_, _, err = c.Connect()
if err != nil {
t.Fatal(err)
}
}
func TestWebSocketClient_Subscribe(t *testing.T) {
t.SkipNow()
s := NewApiServiceFromEnv()
rsp, err := s.WebSocketPublicToken()
if err != nil {
t.Fatal(err)
}
tk := &WebSocketTokenModel{}
if err := rsp.ReadData(tk); err != nil {
t.Fatal(err)
}
c := s.NewWebSocketClient(tk)
mc, ec, err := c.Connect()
if err != nil {
t.Fatal(err)
}
ch1 := NewSubscribeMessage("/contractMarket/ticker:XBTUSDM", false)
ch2 := NewSubscribeMessage("/contractMarket/ticker:XBTUSDM", false)
uch := NewUnsubscribeMessage("/contractMarket/ticker:XBTUSDM", false)
if err := c.Subscribe(ch1, ch2); err != nil {
t.Fatal(err)
}
var i = 0
for {
select {
case err := <-ec:
c.Stop() // Stop subscribing the WebSocket feed
t.Fatal(err)
case msg := <-mc:
t.Log(ToJsonString(msg))
i++
if i == 5 {
t.Log("Unsubscribe XBTUSDM")
if err = c.Unsubscribe(uch); err != nil {
t.Fatal(err)
}
}
if i == 10 {
t.Log("Subscribe XBTUSDM")
if err = c.Subscribe(ch2); err != nil {
t.Fatal(err)
}
}
if i == 15 {
t.Log("Exit subscribing")
c.Stop() // Stop subscribing the WebSocket feed
return
}
}
}
}