-
Notifications
You must be signed in to change notification settings - Fork 3
/
client_test.go
228 lines (187 loc) · 5.11 KB
/
client_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package pubnub
import (
"os"
"sync"
"testing"
"time"
)
func createClient(id string) *PubNubClient {
cs := newClientSettings(id)
return NewPubNubClient(cs)
}
func newClientSettings(id string) *ClientSettings {
subscribeKey := os.Getenv("PUBNUB_SUBSCRIBE_KEY")
publishKey := os.Getenv("PUBNUB_PUBLISH_KEY")
secretKey := os.Getenv("PUBNUB_SECRET_KEY")
cs := new(ClientSettings)
if id == "" {
uuid := os.Getenv("PUBNUB_UUID")
cs.ID = uuid
}
cs.SubscribeKey = subscribeKey
cs.PublishKey = publishKey
cs.SecretKey = secretKey
return cs
}
func createMessage(body string) map[string]string {
return map[string]string{
"eventName": "messageAdded",
"body": body,
}
}
func TestSubscribe(t *testing.T) {
pc := createClient("tester")
defer pc.Close()
_, err := pc.Subscribe("")
if err != ErrChannelNotSet {
t.Errorf("Expected channel is not set error but got %s", err)
}
channels := "testme"
_, err = pc.Subscribe(channels)
if err != nil {
t.Errorf("Expected nil but got error while subscribing: %s", err)
}
}
func TestPublish(t *testing.T) {
pc := createClient("tester")
defer pc.Close()
message := createMessage("testing all together")
err := pc.Push("tester1", message)
if err != nil {
t.Errorf("Expected nil but got error while publishing: %s", err)
}
}
func TestMessageReception(t *testing.T) {
sender := createClient("tester")
receiver := createClient("receiver")
defer func() {
sender.Close()
receiver.Close()
}()
channel, err := receiver.Subscribe("testme")
if err != nil {
t.Errorf("Expected nil but got error while subscribing: %s", err)
t.FailNow()
}
var wg sync.WaitGroup
go func() {
wg.Add(1)
defer wg.Done()
select {
case msg := <-channel.Consume():
body, ok := msg.Body.(map[string]interface{})
if !ok {
t.Errorf("Wrong message body type")
t.FailNow()
}
val, ok := body["eventName"]
if !ok {
t.Error("'eventName' field is expected in message but not found")
t.Fail()
} else {
if val != "messageAdded" {
t.Errorf("Expected messageAdded event in message but got %s", val)
}
}
val, ok = body["body"]
if !ok {
t.Error("'body' field is expected in message but not found")
t.Fail()
} else {
if val != "testing all together" {
t.Errorf("Expected 'testing all together' as message body but got %s", val)
}
}
case <-time.After(5 * time.Second):
t.Errorf("Expected message but it is timedout")
t.FailNow()
}
}()
err = sender.Push("testme", createMessage("testing all together"))
if err != nil {
t.Errorf("Expected nil but got error while publishing: %s", err)
}
wg.Wait()
}
func TestMultipleMessageReception(t *testing.T) {
sender := createClient("tester")
receiver := createClient("receiver")
defer func() {
sender.Close()
receiver.Close()
}()
// these subscriptions are added for checking if messages are received correctly
// when we are subscribed to more than one channels
channel1, err := receiver.Subscribe("channel1")
if err != nil {
t.Errorf("Expected nil but got got error while subscribing: %s", err)
t.FailNow()
}
channel2, err := receiver.Subscribe("channel2")
if err != nil {
t.Errorf("Expected nil but got error while subscribing: %s", err)
t.FailNow()
}
err = sender.Push("channel1", createMessage("message1"))
if err != nil {
t.Errorf("Expected nil but got error while publishing: %s", err)
t.FailNow()
}
err = sender.Push("channel2", createMessage("message2"))
if err != nil {
t.Errorf("Expected nil but got error while publishing: %s", err)
t.FailNow()
}
// TODO when we concurrently push message and subscribe to a channel, pushed message
// is sometimes lost, due to reconnection process of current pubnub go-client
// receiver.Subscribe("padme")
testConsume := func(channel *Channel, messageBody string) {
var wg sync.WaitGroup
wg.Add(1)
defer wg.Done()
select {
case msg := <-channel.Consume():
body, ok := msg.Body.(map[string]interface{})
if !ok {
t.Errorf("Wrong message body type")
t.FailNow()
}
val, ok := body["eventName"]
if !ok {
t.Error("'eventName' field is expected in message but not found")
t.Fail()
} else {
if val != "messageAdded" {
t.Errorf("Expected messageAdded event in message but got %s", val)
}
}
val, ok = body["body"]
if !ok {
t.Error("'body' field is expected in message but not found")
t.Fail()
} else {
if val != messageBody {
t.Errorf("Expected '%s' as message body but got %s", messageBody, val)
}
}
case <-time.After(5 * time.Second):
t.Errorf("Expected message but it is timedout")
t.FailNow()
}
}
testConsume(channel1, "message1")
testConsume(channel2, "message2")
}
func TestClosedChannel(t *testing.T) {
client := createClient("tester")
client.Close()
if _, err := client.Subscribe("t"); err == nil {
t.Errorf("Expected %s error but got nil", ErrConnectionClosed)
}
if err := client.Push("t", "e"); err == nil {
t.Errorf("Expected %s error but got nil", ErrConnectionClosed)
}
if err := client.Grant(&AuthSettings{}); err == nil {
t.Errorf("Expected %s error but got nil", ErrConnectionClosed)
}
}