/
interops-spec.js
374 lines (334 loc) · 10.3 KB
/
interops-spec.js
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
var settings = require('./client-harness');
var testServer = settings.interopServer;
var testPort = settings.interopPort;
var testPath = settings.interopPath;
var topicPrefix = settings.topicPrefix;
var testMqttVersion = 4;
var genStr = function(str){
var time = new Date();
return str + '.' + time.getTime();
};
var topics = ["TopicA", "TopicA/B", "Topic/C", "TopicA/C", "/TopicA"];
var wildtopics = ["TopicA/+", "+/C", "#", "/#", "/+", "+/+", "TopicA/#"];
var nosubscribetopics = ["test/nosubscribe",];
describe('InteropsTests', function() {
var clientId = this.description;
var client = null;
var failure = false;
var subscribed = false;
var disconnectError = null;
var disconnectErrorMsg = null;
var subscribed = false;
var messageReceivedCount = 0;
var messagePublishedCount = 0;
var sendingComplete = false;
var receivingComplete = false;
beforeEach(function(){
failure = false;
subscribed = false;
disconnectError = null;
disconnectErrorMsg = null;
messageReceivedCount = 0
messagePublishedCount = 0;
sendingComplete = false;
receivingComplete = false;
});
afterEach(function(){
if (client !== null && client.isConnected()) {
client.disconnect();
}
client = null;
});
var callbacks = {
onConnectionLost: function(err) {
console.log("connectionLost " + err.errorMessage);
},
onMessageArrived: function(message) {
console.log("messageArrived %s %s %s %s", message.destinationName, message.payloadString, message.qos, message.retained);
messageReceivedCount++;
if (messageReceivedCount == 3) {
receivingComplete = true;
}
},
onConnectSuccess: function(response) {
connected = true;
},
onConnectFailure: function(err) {
console.log('Connect failed %s %s',err.errCode,err.errorMessage);
},
onDisconnectSuccess: function(response) {
connected = false;
console.log("Disconnected from server")
},
onDisconnectFailure: function(err) {
console.log('Disconnect failed %s %s',err.errCode,err.errorMessage);
},
onMessageDelivered: function(reponse) {
messagePublishedCount++;
if (messagePublishedCount == 3) {
sendingComplete = true;
}
},
onSubscribeSuccess: function() {
subscribed = true;
},
};
it('should connect, disconnect, subscribe, publish and receive messages', function() {
client = new Paho.Client(testServer, testPort, testPath, "testclientid-js");
client.onMessageArrived = callbacks.onMessageArrived;
client.onMessageDelivered = callbacks.onMessageDelivered;
expect(client).not.toBe(null);
runs(function() {
client.connect({onSuccess: callbacks.onConnectSuccess, mqttVersion:testMqttVersion, useSSL: true});
});
waitsFor(function() {
return client.isConnected();
}, "the client should connect", 5000);
runs(function() {
expect(client.isConnected()).toBe(true);
});
runs(function() {
client.disconnect();
});
waitsFor(function() {
return true;
}, "the client should disconnect", 5000);
runs(function() {
expect(client.isConnected()).toBe(false);
});
runs(function() {
client.connect({onSuccess: callbacks.onConnectSuccess, mqttVersion:testMqttVersion, useSSL: true});
});
waitsFor(function() {
return client.isConnected();
}, "the client should connect again", 5000);
runs(function() {
expect(client.isConnected()).toBe(true);
});
runs(function() {
client.subscribe(topicPrefix + topics[0], {qos:2, onSuccess: callbacks.onSubscribeSuccess});
});
waitsFor(function() {
return subscribed;
}, "the client should subscribe", 2000);
runs(function() {
expect(subscribed).toBe(true);
});
runs(function (){
for (var i = 0; i < 3; i++) {
var message = new Paho.Message("qos " + i);
message.destinationName = topicPrefix + topics[0];
message.qos=i;
client.send(message);
}
});
waitsFor(function() {
return sendingComplete;
}, "the client should send 3 messages", 5000);
waitsFor(function() {
return receivingComplete;
}, "the client should receive 3 messages", 5000);
runs(function() {
expect(messagePublishedCount).toBe(3);
expect(messageReceivedCount).toBe(3)
});
runs(function() {
client.disconnect({onSuccess: callbacks.onDisconnectSuccess});
});
waitsFor(function() {
return connected;
}, "the client should disconnect", 5000);
runs(function() {
expect(client.isConnected()).toBe(false);
});
});
it('should connect, attempt to connect again and fail', function() {
var exception = false;
client = new Paho.Client(testServer, testPort, testPath, "testclientid-js");
expect(client).not.toBe(null);
runs(function() {
client.connect({onSuccess: callbacks.onConnectSuccess, mqttVersion:testMqttVersion, useSSL: true});
});
waitsFor(function() {
return client.isConnected();
}, "the client should connect", 5000);
runs(function() {
expect(client.isConnected()).toBe(true);
});
runs(function() {
try {
client.connect({onSuccess: callbacks.onConnectSuccess, mqttVersion:testMqttVersion, useSSL: true});
} catch (e) {
console.log(e.message)
if (e.message == "AMQJS0011E Invalid state already connected.") {
exception = true
}
}
});
runs(function() {
expect(exception).toBe(true);
});
});
it('should connect successfully with a 0 length clientid with cleansession true', function() {
client = new Paho.Client(testServer, testPort, testPath, "");
expect(client).not.toBe(null);
runs(function() {
client.connect({cleanSession:true, onSuccess: callbacks.onConnectSuccess, mqttVersion:testMqttVersion, useSSL: true});
});
waitsFor(function() {
return client.isConnected();
}, "the client should connect", 5000);
runs(function() {
expect(client.isConnected()).toBe(true);
});
runs(function() {
client.disconnect();
});
waitsFor(function() {
return true;
}, "the client should disconnect", 5000);
runs(function() {
expect(client.isConnected()).toBe(false);
});
});
it('should fail to connect successfully with a 0 length clientid with cleansession false', function() {
var connectFail = false;
var failCallback = function(err) {
connectFail = true;
}
client = new Paho.Client(testServer, testPort, testPath, "");
expect(client).not.toBe(null);
runs(function() {
client.connect({cleanSession:false, onFailure:failCallback, mqttVersion:testMqttVersion, useSSL: true});
});
waitsFor(function() {
return connectFail
}, "the client should fail to connect", 5000);
runs(function() {
expect(client.isConnected()).toBe(false);
});
});
/*
it('should queue up messages on the server for offline clients', function() {
client = new Paho.Client(testServer, testPort, testPath, "testclientid-js");
client.onMessageArrived = callbacks.onMessageArrived;
expect(client).not.toBe(null);
runs(function() {
client.connect({onSuccess: callbacks.onConnectSuccess, mqttVersion:testMqttVersion, cleanSession:false});
});
waitsFor(function() {
return client.isConnected();
}, "the client should connect", 5000);
runs(function() {
expect(client.isConnected()).toBe(true);
});
runs(function() {
client.subscribe(wildtopics[5], {qos:2, onSuccess: callbacks.onSubscribeSuccess});
});
waitsFor(function() {
return subscribed;
}, "the client should subscribe", 2000);
runs(function() {
expect(subscribed).toBe(true);
});
runs(function() {
client.disconnect();
});
waitsFor(function() {
return true;
}, "the client should disconnect", 5000);
runs(function() {
expect(client.isConnected()).toBe(false);
});
bClient = new Paho.Client(testServer, testPort, testPath, "testclientid-js-b");
bClient.onMessageDelivered = callbacks.onMessageDelivered;
runs(function() {
bClient.connect({onSuccess: callbacks.onConnectSuccess, mqttVersion:testMqttVersion, cleanSession:true});
});
waitsFor(function() {
return bClient.isConnected();
}, "the client should connect again", 5000);
runs(function() {
expect(bClient.isConnected()).toBe(true);
});
runs(function (){
for (var i = 0; i < 3; i++) {
var message = new Paho.Message("qos " + i);
message.destinationName = topics[i+1];
message.qos=i;
bClient.send(message);
}
});
waitsFor(function() {
return sendingComplete;
}, "the client should send 3 messages", 5000);
runs(function() {
expect(messagePublishedCount).toBe(3);
});
runs(function() {
bClient.disconnect({onSuccess: callbacks.onDisconnectSuccess});
});
waitsFor(function() {
return connected;
}, "the client should disconnect", 5000);
runs(function() {
expect(bClient.isConnected()).toBe(false);
});
runs(function() {
client.connect({onSuccess: callbacks.onConnectSuccess, mqttVersion:testMqttVersion, cleanSession:false});
});
waitsFor(function() {
return client.isConnected();
}, "the client should connect", 5000);
runs(function() {
expect(client.isConnected()).toBe(true);
});
waitsFor(function() {
return (messageReceivedCount > 1);
}, "the client should receive 2/3 messages", 5000);
runs(function() {
expect(messageReceivedCount).toBeGreaterThan(1);
});
runs(function() {
client.disconnect();
});
waitsFor(function() {
return true;
}, "the client should disconnect", 5000);
runs(function() {
expect(client.isConnected()).toBe(false);
});
});
// This test has been commented out as it is only valid for a messagesight
// server and behaviour differs between mqtt server implementations.
it('should get a return code for failure to subscribe', function() {
client = new Paho.Client(testServer, testPort, testPath, "testclientid-js");
client.onMessageArrived = callbacks.onMessageArrived;
var subFailed = false;
var failSubscribe = function(response) {
if (response.errorCode.get(0) == 0x80) {
subFailed = true;
}
}
expect(client).not.toBe(null);
runs(function() {
client.connect({onSuccess: callbacks.onConnectSuccess, mqttVersion:testMqttVersion, cleanSession:true});
});
waitsFor(function() {
return client.isConnected();
}, "the client should connect", 5000);
runs(function() {
expect(client.isConnected()).toBe(true);
});
runs(function() {
client.subscribe(nosubscribetopics[0], {qos:2, onFailure: failSubscribe});
});
waitsFor(function() {
return subFailed;
}, "the client should fail to subscribe", 2000);
runs(function() {
expect(subFailed).toBe(true);
});
});
*/
})