This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathws_test.go
129 lines (109 loc) · 2.93 KB
/
ws_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
package subscriber
import (
"testing"
"github.com/gorilla/websocket"
"github.com/smartcontractkit/external-initiator/store"
)
var upgrader = websocket.Upgrader{} // use default options
func TestWebsocketSubscriber_SubscribeToEvents(t *testing.T) {
t.Run("subscribes and ignores confirmation message", func(t *testing.T) {
wss := WebsocketSubscriber{Endpoint: wsMockUrl.String(), Manager: TestsMockManager{true}}
events := make(chan Event)
sub, err := wss.SubscribeToEvents(events, store.RuntimeConfig{})
if err != nil {
t.Errorf("SubscribeToEvents() error = %v", err)
return
}
defer sub.Unsubscribe()
event := <-events
mockevent := string(event)
if mockevent == "confirmation" {
t.Error("SubscribeToEvents() got unexpected confirmation")
return
}
if mockevent != "event" {
t.Errorf("SubscribeToEvents() got unexpected message = %v", mockevent)
return
}
})
t.Run("fails subscribe to invalid URL", func(t *testing.T) {
wss := WebsocketSubscriber{Endpoint: "", Manager: TestsMockManager{false}}
events := make(chan Event)
sub, err := wss.SubscribeToEvents(events, store.RuntimeConfig{})
if err == nil {
sub.Unsubscribe()
t.Error("SubscribeToEvents() expected error, but got nil")
return
}
})
t.Run("subscribes and attempts reconnect", func(t *testing.T) {
wss := WebsocketSubscriber{Endpoint: wsMockUrl.String(), Manager: &TestsReconnectManager{}}
events := make(chan Event)
sub, err := wss.SubscribeToEvents(events, store.RuntimeConfig{})
if err != nil {
t.Errorf("SubscribeToEvents() error = %v", err)
return
}
defer sub.Unsubscribe()
event := <-events
mockevent := string(event)
if mockevent != "event" {
t.Errorf("SubscribeToEvents() got unexpected message = %v", mockevent)
return
}
})
}
type TestsReconnectManager struct {
connections int
}
func (m TestsReconnectManager) ParseResponse(data []byte) ([]Event, bool) {
return []Event{data}, true
}
func (m *TestsReconnectManager) GetTriggerJson() []byte {
count := m.connections
m.connections++
switch count {
case 0:
return []byte(`close`)
default:
return []byte(`true`)
}
}
func (m TestsReconnectManager) GetTestJson() []byte {
return nil
}
func (m TestsReconnectManager) ParseTestResponse(data []byte) error {
return nil
}
func TestWebsocketSubscriber_Test(t *testing.T) {
type fields struct {
Endpoint string
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{
"succeeds connecting to valid endpoint",
fields{Endpoint: wsMockUrl.String()},
false,
},
{
"fails connecting to invalid endpoint",
fields{Endpoint: "ws://localhost:9999/invalid"},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
wss := WebsocketSubscriber{
Endpoint: tt.fields.Endpoint,
Manager: TestsMockManager{},
}
if err := wss.Test(); (err != nil) != tt.wantErr {
t.Errorf("Test() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}