-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
websocket_test.go
186 lines (171 loc) · 4.53 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package whatsonchain
import (
"log"
"testing"
"github.com/centrifugal/centrifuge-go"
"github.com/stretchr/testify/assert"
)
type testHandler struct {
HandlerName string
}
func (h *testHandler) OnPublish(*centrifuge.Subscription, centrifuge.PublishEvent) {
log.Printf("OnPublish")
}
func (h *testHandler) OnJoin(*centrifuge.Subscription, centrifuge.JoinEvent) {
log.Printf("OnJoin")
}
func (h *testHandler) OnLeave(*centrifuge.Subscription, centrifuge.LeaveEvent) {
log.Printf("OnLeave")
}
func (h *testHandler) OnSubscribeSuccess(*centrifuge.Subscription, centrifuge.SubscribeSuccessEvent) {
log.Printf("OnSubscribeSuccess")
}
func (h *testHandler) OnSubscribeError(*centrifuge.Subscription, centrifuge.SubscribeErrorEvent) {
log.Printf("OnSubscribeError")
}
func (h *testHandler) OnUnsubscribe(*centrifuge.Subscription, centrifuge.UnsubscribeEvent) {
log.Printf("OnUnsubscribe")
}
func (h *testHandler) OnConnect(_ *centrifuge.Client, _ centrifuge.ConnectEvent) {
log.Printf("connected to socket")
}
func (h *testHandler) OnDisconnect(_ *centrifuge.Client, _ centrifuge.DisconnectEvent) {
log.Printf("disconnected from socket")
}
func (h *testHandler) OnMessage(_ *centrifuge.Client, _ centrifuge.MessageEvent) {
log.Printf("received message")
}
func (h *testHandler) OnServerPublish(_ *centrifuge.Client, _ centrifuge.ServerPublishEvent) {
log.Printf("received server publish event")
}
func (h *testHandler) OnError(_ *centrifuge.Client, _ centrifuge.ErrorEvent) {
log.Printf("error")
}
func (h *testHandler) OnServerJoin(_ *centrifuge.Client, _ centrifuge.ServerJoinEvent) {
log.Printf("server join event")
}
func (h *testHandler) OnServerLeave(_ *centrifuge.Client, _ centrifuge.ServerLeaveEvent) {
log.Printf("server leave event")
}
func (h *testHandler) OnServerSubscribe(_ *centrifuge.Client, _ centrifuge.ServerSubscribeEvent) {
log.Printf("server subscribe event")
}
func (h *testHandler) OnServerUnsubscribe(_ *centrifuge.Client, _ centrifuge.ServerUnsubscribeEvent) {
log.Printf("server unsubscribe event")
}
func TestClient_NewMempoolWebsocket(t *testing.T) {
type args struct {
handler SocketHandler
}
tests := []struct {
name string
args args
want *centrifuge.Client
}{
{"nil handler should set nil client",
args{
handler: nil,
},
nil,
},
}
client := newMockClient(&mockHTTPAddresses{})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, client.NewMempoolWebsocket(tt.args.handler), "NewMempoolWebsocket(%v)", tt.args.handler)
})
}
}
func TestClient_NewBlockHeadersWebsocket(t *testing.T) {
type args struct {
handler SocketHandler
}
tests := []struct {
name string
args args
want *centrifuge.Client
}{
{"nil handler should set nil client",
args{
handler: nil,
},
nil,
},
}
client := newMockClient(&mockHTTPAddresses{})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, client.NewBlockHeadersWebsocket(tt.args.handler), "NewMempoolWebsocket(%v)", tt.args.handler)
})
}
}
func Test_newWebsocketClient(t *testing.T) {
type args struct {
url string
handler SocketHandler
}
tests := []struct {
name string
args args
nilClient bool
connectError bool
}{
{
name: "empty url should set nil client",
args: args{
url: "",
handler: nil,
},
nilClient: true,
connectError: false,
},
{
name: "nil handler should set nil client",
args: args{
url: "wss://socket.whatsonchain.com/mempool",
handler: nil,
},
nilClient: true,
connectError: false,
},
{
name: "valid handler should successfully connect and disconnect",
args: args{
url: "wss://socket.whatsonchain.com/mempool",
handler: &testHandler{
HandlerName: "test handler",
},
},
connectError: false,
},
{
name: "valid handler with malformed url should not successfully connect",
args: args{
url: "wss//socket.whatsonchain.com/mempool",
handler: &testHandler{
HandlerName: "test handler",
},
},
connectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := newWebsocketClient(tt.args.url, tt.args.handler)
if c == nil && tt.nilClient {
return
}
if c == nil {
t.Fatalf("expected a valid client, got %v", c)
}
err := c.Connect()
if err != nil && !tt.connectError {
t.Fatalf("unexpectedly failed to connect to websocket: %v", err)
}
err = c.Disconnect()
if err != nil {
t.Fatalf("failed to disconnect from websocket: %v", err)
}
})
}
}