forked from googlearchive/go-gcm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_test.go
324 lines (281 loc) · 7.79 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gcm
import (
"errors"
"testing"
"time"
"github.com/stretchr/testify/mock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
)
func TestClient(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "GCM Client")
}
var _ = Describe("GCM Client", func() {
Describe("initializing", func() {
DescribeTable("wrong initialization parameters",
func(config *Config, h MessageHandler, errStr string) {
c, err := NewClient(config, h)
Expect(c).To(BeNil())
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(errStr))
},
Entry("it should fail on nil config", nil, nil, "config is nil"),
Entry("it should fail on nil message handler",
&Config{}, nil, "message handler is nil"),
Entry("it should fail on empty api key",
&Config{SenderID: "123"}, func(cm CCSMessage) error { return nil }, "empty api key"),
Entry("it should fail on wrong credentials",
&Config{SenderID: "123", APIKey: "key"}, func(cm CCSMessage) error { return nil },
"error connecting gcm xmpp client: auth failure: not-authorized"),
)
Context("good config", func() {
var (
xm *xmppCMock
hm *httpCMock
)
BeforeEach(func() {
xm = new(xmppCMock)
hm = new(httpCMock)
})
AfterEach(func() {
gt := GinkgoT()
xm.AssertExpectations(gt)
hm.AssertExpectations(gt)
})
It("should fail on xmpp connection error", func() {
xm.On("ID").Return("id")
xm.On("Listen", mock.AnythingOfType("gcm.MessageHandler")).
Return(errors.New("Connect"))
c, err := newGCMClient(xm, hm, &Config{}, nil)
Expect(err).To(HaveOccurred())
Expect(c).To(BeNil())
Expect(err).To(MatchError("Connect"))
})
It("should succeed", func() {
xm.On("ID").Return("id")
xm.On("Listen", mock.AnythingOfType("gcm.MessageHandler")).
Return(nil)
c, err := newGCMClient(xm, hm, &Config{}, nil)
Expect(err).NotTo(HaveOccurred())
Expect(c).To(BeAssignableToTypeOf(&gcmClient{}))
Expect(c.pingInterval).To(Equal(DefaultPingInterval))
Expect(c.pingTimeout).To(Equal(DefaultPingTimeout))
c.Lock()
Expect(c.xmppClient).To(Equal(xm))
Expect(c.cerr).NotTo(BeClosed())
c.Unlock()
})
})
})
Describe("listening", func() {
var (
xm *xmppCMock
c *gcmClient
)
BeforeEach(func() {
xm = new(xmppCMock)
c = &gcmClient{
xmppClient: xm,
cerr: make(chan error),
senderID: "sender id",
apiKey: "api key",
mh: nil,
killMonitor: make(chan bool, 1),
xmppChan: make(chan xmppPacket),
}
})
AfterEach(func() {
xm.AssertExpectations(GinkgoT())
})
It("should fail on listen error", func() {
xm.On("ID").Return("id")
xm.On("Listen", mock.AnythingOfType("gcm.MessageHandler")).
Return(errors.New("Listen"))
go c.monitorXMPP(false, make(chan bool, 1))
err := <-c.cerr
Expect(err).To(MatchError("Listen"))
})
})
Describe("sending", func() {
var (
xm XMPPMessage
hm HTTPMessage
h *httpCMock
x *xmppCMock
c Client
)
BeforeEach(func() {
xm = XMPPMessage{To: "me", MessageID: "id1"}
hm = HTTPMessage{To: "me"}
h = new(httpCMock)
x = new(xmppCMock)
c = &gcmClient{
httpClient: h,
xmppClient: x,
cerr: make(chan error),
senderID: "sender id",
apiKey: "api key",
mh: nil,
killMonitor: make(chan bool, 1),
xmppChan: make(chan xmppPacket),
}
go c.(*gcmClient).loop(false)
})
AfterEach(func() {
gt := GinkgoT()
h.AssertExpectations(gt)
x.AssertExpectations(gt)
})
It("should send http message", func() {
r := &HTTPResponse{MulticastID: 100}
h.On("Send", hm).Return(r, nil)
resp, err := c.SendHTTP(hm)
Expect(err).NotTo(HaveOccurred())
Expect(resp).To(Equal(r))
})
It("should fail sending http message", func() {
h.On("Send", hm).Return(nil, errors.New("send error"))
resp, err := c.SendHTTP(hm)
Expect(err).To(HaveOccurred())
Expect(resp).To(BeNil())
Expect(err).To(MatchError("send error"))
})
It("should send xmpp message", func() {
x.On("Send", xm).Return("id1", 100, nil)
id, bytes, err := c.SendXMPP(xm)
Expect(err).NotTo(HaveOccurred())
Expect(id).To(Equal("id1"))
Expect(bytes).To(Equal(100))
})
It("should fail on send error", func() {
x.On("Send", xm).Return("", 0, errors.New("send error"))
id, bytes, err := c.SendXMPP(xm)
Expect(err).To(HaveOccurred())
Expect(id).To(BeEmpty())
Expect(bytes).To(Equal(0))
Expect(err).To(MatchError("send error"))
})
})
Describe("closing", func() {
var (
x *xmppCMock
c *gcmClient
)
BeforeEach(func() {
x = new(xmppCMock)
c = &gcmClient{xmppClient: x, killMonitor: make(chan bool, 1)}
})
AfterEach(func() {
x.AssertExpectations(GinkgoT())
})
It("should close successfully", func() {
x.On("Close", true).Return(nil)
x.On("IsClosed").Return(true)
err := c.Close()
Expect(err).NotTo(HaveOccurred())
Expect(x.IsClosed()).To(BeTrue())
})
It("should return close error from xmpp", func() {
x.On("Close", true).Return(errors.New("close error"))
x.On("IsClosed").Return(true)
err := c.Close()
Expect(err).To(MatchError("close error"))
Expect(x.IsClosed()).To(BeTrue())
})
})
Describe("periodic ping", func() {
var (
x *xmppCMock
)
BeforeEach(func() {
x = new(xmppCMock)
})
AfterEach(func() {
x.AssertExpectations(GinkgoT())
})
It("should exit when xmpp client is closed", func() {
x.On("IsClosed").Return(true)
err := pingPeriodically(x, time.Millisecond, time.Millisecond)
Expect(err).NotTo(HaveOccurred())
})
It("should exit when xmpp ping errors", func() {
x.On("IsClosed").Return(false)
x.On("Ping", time.Millisecond).Return(errors.New("Ping"))
err := pingPeriodically(x, time.Millisecond, time.Millisecond)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("Ping"))
})
})
Describe("handling upstream messages", func() {
var (
x *xmppCMock
c *gcmClient
)
BeforeEach(func() {
x = new(xmppCMock)
c = &gcmClient{xmppClient: x, cerr: make(chan error, 1)}
})
AfterEach(func() {
x.AssertExpectations(GinkgoT())
})
It("should handle connection drainging request", func() {
cm := CCSMessage{
MessageType: CCSControl,
ControlType: "CONNECTION_DRAINING",
}
x.On("ID").Return("id")
err := c.onCCSMessage(cm)
Expect(err).NotTo(HaveOccurred())
Expect(c.cerr).To(HaveLen(1))
err = <-c.cerr
Expect(err).To(MatchError("connection draining"))
})
It("should bubble up everything else", func() {
c.mh = func(cm CCSMessage) error {
return errors.New("Bubble")
}
cm := CCSMessage{
MessageType: CCSReceipt,
}
err := c.onCCSMessage(cm)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("Bubble"))
})
})
Describe("misc", func() {
var (
x *xmppCMock
c *gcmClient
)
BeforeEach(func() {
x = new(xmppCMock)
c = &gcmClient{xmppClient: x}
})
AfterEach(func() {
x.AssertExpectations(GinkgoT())
})
It("should return client id", func() {
x.On("ID").Return("id1")
Expect(c.ID()).To(Equal("id1"))
})
It("should return client jid", func() {
x.On("JID").Return("jid")
Expect(c.JID()).To(Equal("jid"))
})
})
})