forked from potato2003/actioncable-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer_test.go
137 lines (110 loc) · 3.13 KB
/
consumer_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
package actioncable
import (
"fmt"
"net/url"
"os"
"strings"
"testing"
"time"
)
func TestCreateConsumer(t *testing.T) {
url := TestWebsocketURL()
if url == nil {
fmt.Println("skip TestCreateConsumer")
return
}
consumer, err := CreateConsumer(url, nil)
if err != nil {
t.Fatalf("bad: %s", err)
}
consumer.Connect()
if consumer.connection == nil {
t.Fatalf("bad: consumer.connection is nil")
}
consumer.Disconnect()
}
// TODO move to subscription_test
func TestCreateConsumer2(t *testing.T) {
if os.Getenv("TEST_WS") == "" {
fmt.Println("skip TestCreateConsumer")
return
}
url, _ := url.Parse("ws://localhost:3000/cable")
if strings.HasSuffix(os.Getenv("TEST_WS"), "ws") {
url, _ = url.Parse(os.Getenv("TEST_WS"))
}
consumer, err := CreateConsumer(url, nil)
consumer.Connect()
channelIdentifier := NewChannelIdentifier("ChatChannel", map[string]interface{}{"room": "BestRoom"})
subscription, err := consumer.Subscriptions.Create(channelIdentifier)
if err != nil {
t.Fatalf("bad: %s", err)
}
if consumer.connection == nil {
t.Fatalf("bad: consumer.connection is nil")
}
handler := NewTestChatSubscriptionEventHandler()
subscription.SetHandler(handler)
select {
case <-handler.ConnectedEvents:
case <-time.After(1 * time.Second):
t.Fatal("bad: connection timeout")
}
// data := map[string]interface{}{}
// se.ReadJSON(&data)
}
type TestChatSubscriptionEventHandler struct {
NumConnected int
NumDisconnected int
NumRejected int
NumReceived int
ConnectedEvents chan *SubscriptionEvent
DisconnectedEvents chan *SubscriptionEvent
RejectedEvents chan *SubscriptionEvent
ReceivedEvents chan *SubscriptionEvent
SubscriptionEventHandler
}
func NewTestChatSubscriptionEventHandler() *TestChatSubscriptionEventHandler {
h := TestChatSubscriptionEventHandler{}
h.NumConnected = 0
h.NumDisconnected = 0
h.NumRejected = 0
h.NumReceived = 0
h.ConnectedEvents = make(chan *SubscriptionEvent, 128)
h.DisconnectedEvents = make(chan *SubscriptionEvent, 128)
h.RejectedEvents = make(chan *SubscriptionEvent, 128)
h.ReceivedEvents = make(chan *SubscriptionEvent, 128)
return &h
}
func (h *TestChatSubscriptionEventHandler) Reset() {
h.NumConnected = 0
h.NumDisconnected = 0
h.NumRejected = 0
h.NumReceived = 0
h.ConnectedEvents = make(chan *SubscriptionEvent, 128)
h.DisconnectedEvents = make(chan *SubscriptionEvent, 128)
h.RejectedEvents = make(chan *SubscriptionEvent, 128)
h.ReceivedEvents = make(chan *SubscriptionEvent, 128)
}
func (h *TestChatSubscriptionEventHandler) Close() {
close(h.ConnectedEvents)
close(h.DisconnectedEvents)
close(h.RejectedEvents)
close(h.ReceivedEvents)
}
func (h *TestChatSubscriptionEventHandler) OnConnected(se *SubscriptionEvent) {
h.NumConnected++
h.ConnectedEvents <- se
}
func (h *TestChatSubscriptionEventHandler) OnDisconnected(se *SubscriptionEvent) {
h.NumDisconnected++
h.DisconnectedEvents <- se
}
func (h *TestChatSubscriptionEventHandler) OnRejected(se *SubscriptionEvent) {
h.NumReceived++
h.RejectedEvents <- se
}
func (h *TestChatSubscriptionEventHandler) OnReceived(se *SubscriptionEvent) {
h.NumReceived++
h.ReceivedEvents <- se
}