node-red-contrib-aedes 0.15.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,7 @@ const aedesNode = require('../aedes.js');
5
5
  const mqttNode = require('../node_modules/node-red/node_modules/@node-red/nodes/core/network/10-mqtt.js');
6
6
  const mqtt = require('mqtt');
7
7
  const should = require('should');
8
+ const { logError } = require('./test-utils');
8
9
 
9
10
  helper.init(require.resolve('node-red'));
10
11
 
@@ -52,50 +53,49 @@ describe('Aedes Broker QOS tests', function () {
52
53
  }
53
54
  ];
54
55
  helper.load([aedesNode, mqttNode], flow, function () {
55
- const client1 = mqtt.connect('mqtt://localhost:1883', { clientId: 'client1' });
56
- client1.on('error', function (err) {
57
- console.error('Error: ', err.toString());
58
- });
59
- client1.on('connect', function () {
60
- // console.log('External client1 connected');
61
- });
62
- const client2 = mqtt.connect('mqtt://localhost:1883', { clientId: 'client2', clean: false });
63
- client2.on('error', function (err) {
64
- console.error('Error: ', err.toString());
65
- });
66
- client2.on('connect', function () {
67
- // console.log('External client2 connected');
68
- client2.subscribe('test1883', { qos: 1 }, function (err, granted) {
69
- // console.log('Subscription successful ' + JSON.stringify(granted));
70
- if (err) {
71
- console.error('Error subscribing');
72
- done();
56
+ const n1 = helper.getNode('n1');
57
+ n1._initPromise.then(function () {
58
+ const client1 = mqtt.connect('mqtt://localhost:1883', { clientId: 'client1' });
59
+ client1.on('error', logError);
60
+ client1.on('connect', function () {
61
+ // console.log('External client1 connected');
62
+ });
63
+ const client2 = mqtt.connect('mqtt://localhost:1883', { clientId: 'client2', clean: false });
64
+ client2.on('error', logError);
65
+ client2.on('connect', function () {
66
+ // console.log('External client2 connected');
67
+ client2.subscribe('test1883', { qos: 1 }, function (err, granted) {
68
+ // console.log('Subscription successful ' + JSON.stringify(granted));
69
+ if (err) {
70
+ console.error('Error subscribing');
71
+ done();
72
+ }
73
+ });
74
+ });
75
+ const n2 = helper.getNode('n2');
76
+ n2.on('input', function (msg) {
77
+ // console.log('Broker received message topic: ' + msg.topic + ', clientid: ' + msg.payload.client.id);
78
+ if (msg.topic === 'subscribe') {
79
+ // console.log('Client ' + msg.payload.client.id + ' subscribed ' + JSON.stringify(msg.payload.client.subscriptions));
80
+ client2.end(function () {
81
+ client1.publish('test1883', 'test', { qos: 1 }, function () {
82
+ // console.log('Published');
83
+ client2.reconnect();
84
+ });
85
+ });
86
+ } else if (msg.topic === 'clientReady') {
87
+ // console.log('Client ' + msg.payload.client.id + ' connected with clean ' + msg.payload.client.clean);
73
88
  }
74
89
  });
75
- });
76
- const n2 = helper.getNode('n2');
77
- n2.on('input', function (msg) {
78
- // console.log('Broker received message topic: ' + msg.topic + ', clientid: ' + msg.payload.client.id);
79
- if (msg.topic === 'subscribe') {
80
- // console.log('Client ' + msg.payload.client.id + ' subscribed ' + JSON.stringify(msg.payload.client.subscriptions));
90
+ client2.on('message', function (topic, message) {
91
+ // console.log(message.toString());
92
+ should(topic.toString()).equal('test1883');
93
+ should(message.toString()).equal('test');
81
94
  client2.end(function () {
82
- client1.publish('test1883', 'test', { qos: 1 }, function () {
83
- // console.log('Published');
84
- client2.reconnect();
95
+ client1.end(function () {
96
+ done();
85
97
  });
86
98
  });
87
- } else if (msg.topic === 'clientReady') {
88
- // console.log('Client ' + msg.payload.client.id + ' connected with clean ' + msg.payload.client.clean);
89
- }
90
- });
91
- client2.on('message', function (topic, message) {
92
- // console.log(message.toString());
93
- should(topic.toString()).equal('test1883');
94
- should(message.toString()).equal('test');
95
- client2.end(function () {
96
- client1.end(function () {
97
- done();
98
- });
99
99
  });
100
100
  });
101
101
  });
@@ -118,49 +118,48 @@ describe('Aedes Broker QOS tests', function () {
118
118
  }
119
119
  ];
120
120
  helper.load([aedesNode, mqttNode], flow, function () {
121
- const client1 = mqtt.connect('mqtt://localhost:1883', { clientId: 'client1' });
122
- client1.on('error', function (err) {
123
- console.error('Error: ', err.toString());
124
- });
125
- client1.on('connect', function () {
126
- // console.log('External client1 connected');
127
- });
128
- const client2 = mqtt.connect('mqtt://localhost:1883', { clientId: 'client2', clean: false });
129
- client2.on('error', function (err) {
130
- console.error('Error: ', err.toString());
131
- });
132
- client2.on('connect', function () {
133
- // console.log('External client2 connected');
134
- client2.subscribe('test1883', { qos: 2 }, function (err, granted) {
135
- // console.log('Subscription successful ' + JSON.stringify(granted));
136
- if (err) {
137
- console.error('Error subscribing');
138
- done();
121
+ const n1 = helper.getNode('n1');
122
+ n1._initPromise.then(function () {
123
+ const client1 = mqtt.connect('mqtt://localhost:1883', { clientId: 'client1' });
124
+ client1.on('error', logError);
125
+ client1.on('connect', function () {
126
+ // console.log('External client1 connected');
127
+ });
128
+ const client2 = mqtt.connect('mqtt://localhost:1883', { clientId: 'client2', clean: false });
129
+ client2.on('error', logError);
130
+ client2.on('connect', function () {
131
+ // console.log('External client2 connected');
132
+ client2.subscribe('test1883', { qos: 2 }, function (err, granted) {
133
+ // console.log('Subscription successful ' + JSON.stringify(granted));
134
+ if (err) {
135
+ console.error('Error subscribing');
136
+ done();
137
+ }
138
+ });
139
+ }); const n2 = helper.getNode('n2');
140
+ n2.on('input', function (msg) {
141
+ // console.log('Broker received message topic: ' + msg.topic + ', clientid: ' + msg.payload.client.id);
142
+ if (msg.topic === 'subscribe') {
143
+ // console.log('Client ' + msg.payload.client.id + ' subscribed ' + JSON.stringify(msg.payload.client.subscriptions));
144
+ client2.end(function () {
145
+ client1.publish('test1883', 'test', { qos: 2 }, function () {
146
+ // console.log('Published');
147
+ client2.reconnect();
148
+ });
149
+ });
150
+ } else if (msg.topic === 'clientReady') {
151
+ // console.log('Client ' + msg.payload.client.id + ' connected with clean ' + msg.payload.client.clean);
139
152
  }
140
153
  });
141
- }); const n2 = helper.getNode('n2');
142
- n2.on('input', function (msg) {
143
- // console.log('Broker received message topic: ' + msg.topic + ', clientid: ' + msg.payload.client.id);
144
- if (msg.topic === 'subscribe') {
145
- // console.log('Client ' + msg.payload.client.id + ' subscribed ' + JSON.stringify(msg.payload.client.subscriptions));
154
+ client2.on('message', function (topic, message) {
155
+ // console.log(message.toString());
156
+ should(topic.toString()).equal('test1883');
157
+ should(message.toString()).equal('test');
146
158
  client2.end(function () {
147
- client1.publish('test1883', 'test', { qos: 2 }, function () {
148
- // console.log('Published');
149
- client2.reconnect();
159
+ client1.end(function () {
160
+ done();
150
161
  });
151
162
  });
152
- } else if (msg.topic === 'clientReady') {
153
- // console.log('Client ' + msg.payload.client.id + ' connected with clean ' + msg.payload.client.clean);
154
- }
155
- });
156
- client2.on('message', function (topic, message) {
157
- // console.log(message.toString());
158
- should(topic.toString()).equal('test1883');
159
- should(message.toString()).equal('test');
160
- client2.end(function () {
161
- client1.end(function () {
162
- done();
163
- });
164
163
  });
165
164
  });
166
165
  });
@@ -5,6 +5,7 @@ const aedesNode = require('../aedes.js');
5
5
  const mqttNode = require('../node_modules/node-red/node_modules/@node-red/nodes/core/network/10-mqtt.js');
6
6
  const mqtt = require('mqtt');
7
7
  const should = require('should');
8
+ const { logError } = require('./test-utils');
8
9
 
9
10
  helper.init(require.resolve('node-red'));
10
11
 
@@ -52,87 +53,16 @@ describe('Aedes Broker retain tests', function () {
52
53
  }
53
54
  ];
54
55
  helper.load([aedesNode, mqttNode], flow, function () {
55
- const client1 = mqtt.connect('mqtt://localhost:1883', { clientId: 'client1' });
56
- const client2 = mqtt.connect('mqtt://localhost:1883', { clientId: 'client2' });
57
- client1.on('error', function (err) {
58
- console.error('Error: ', err.toString());
59
- });
60
- client2.on('error', function (err) {
61
- console.error('Error: ', err.toString());
62
- });
63
- client1.on('connect', function () {
64
- // console.log('External client1 connected');
65
- client1.publish('test1883', 'test', { retain: true }, function () {
66
- // console.log('Published test');
67
- client2.subscribe('test1883', function (err, granted) {
68
- // console.log('Subscription successful ' + JSON.stringify(granted));
69
- if (err) {
70
- console.error('Error subscribing');
71
- done();
72
- }
73
- });
74
- });
75
- });
76
- client2.on('connect', function () {
77
- // console.log('External client2 connected');
78
- });
79
-
80
- const n2 = helper.getNode('n2');
81
- n2.on('input', function (msg) {
82
- // console.log('Broker received message topic: ' + msg.topic + ', clientid: ' + msg.payload.client.id);
83
- if (msg.topic === 'subscribe') {
84
- // console.log('Client ' + msg.payload.client.id + ' subscribed ' + JSON.stringify(msg.payload.client.subscriptions));
85
- } else if (msg.topic === 'clientReady') {
86
- // console.log('Client ' + msg.payload.client.id + ' connected with clean ' + msg.payload.client.clean);
87
- }
88
- });
89
- client2.on('message', function (topic, message) {
90
- // console.log(message.toString());
91
- should(topic.toString()).equal('test1883');
92
- should(message.toString()).equal('test');
93
- client2.end(function () {
94
- client1.end(function () {
95
- done();
96
- });
97
- });
98
- });
99
- });
100
- });
101
- it('a subscriber (retain = true) should only receive the last message on first subscribe', function (done) {
102
- this.timeout(10000); // have to wait for the inject with delay of 10 seconds
103
- const flow = [
104
- {
105
- id: 'n1',
106
- type: 'aedes broker',
107
- mqtt_port: '1883',
108
- name: 'Aedes 1883',
109
- wires: [
110
- ['n2'], []
111
- ]
112
- },
113
- {
114
- id: 'n2',
115
- type: 'helper'
116
- }
117
- ];
118
- helper.load([aedesNode, mqttNode], flow, function () {
119
- const client1 = mqtt.connect('mqtt://localhost:1883', { clientId: 'client1' });
120
- const client2 = mqtt.connect('mqtt://localhost:1883', { clientId: 'client2' });
121
- client1.on('error', function (err) {
122
- console.error('Error: ', err.toString());
123
- });
124
- client2.on('error', function (err) {
125
- console.error('Error: ', err.toString());
126
- });
127
- client2.on('connect', function () {
128
- // console.log('External client2 connected');
129
- });
130
- client1.on('connect', function () {
131
- // console.log('External client1 connected');
132
- client1.publish('test1883', 'test1', { retain: true }, function () {
133
- // console.log('Published test1');
134
- client1.publish('test1883', 'test2', { retain: true }, function () {
135
- // console.log('Published test2');
56
+ const n1 = helper.getNode('n1');
57
+ n1._initPromise.then(function () {
58
+ const client1 = mqtt.connect('mqtt://localhost:1883', { clientId: 'client1' });
59
+ const client2 = mqtt.connect('mqtt://localhost:1883', { clientId: 'client2' });
60
+ client1.on('error', logError);
61
+ client2.on('error', logError);
62
+ client1.on('connect', function () {
63
+ // console.log('External client1 connected');
64
+ client1.publish('test1883', 'test', { retain: true }, function () {
65
+ // console.log('Published test');
136
66
  client2.subscribe('test1883', function (err, granted) {
137
67
  // console.log('Subscription successful ' + JSON.stringify(granted));
138
68
  if (err) {
@@ -142,70 +72,11 @@ describe('Aedes Broker retain tests', function () {
142
72
  });
143
73
  });
144
74
  });
145
- });
146
- const n2 = helper.getNode('n2');
147
- n2.on('input', function (msg) {
148
- // console.log('Broker received message topic: ' + msg.topic + ', clientid: ' + msg.payload.client.id);
149
- if (msg.topic === 'subscribe') {
150
- // console.log('Client ' + msg.payload.client.id + ' subscribed ' + JSON.stringify(msg.payload.client.subscriptions));
151
- } else if (msg.topic === 'clientReady') {
152
- // console.log('Client ' + msg.payload.client.id + ' connected with clean ' + msg.payload.client.clean);
153
- }
154
- });
155
- client2.on('message', function (topic, message) {
156
- // console.log(message.toString());
157
- should(topic.toString()).equal('test1883');
158
- should(message.toString()).equal('test2');
159
- client2.end(function () {
160
- client1.end(function () {
161
- done();
162
- });
75
+ client2.on('connect', function () {
76
+ // console.log('External client2 connected');
163
77
  });
164
- });
165
- });
166
- });
167
78
 
168
- /*
169
-
170
- it('a publisher (retain = true) should send a message', function (done) {
171
- this.timeout(10000); // have to wait for the inject with delay of 10 seconds
172
- const flow = [
173
- {
174
- id: 'n1',
175
- type: 'aedes broker',
176
- mqtt_port: '1883',
177
- persistence_bind: 'level',
178
- name: 'Aedes 1883',
179
- wires: [
180
- ['n2'], []
181
- ]
182
- },
183
- {
184
- id: 'n2',
185
- type: 'helper'
186
- }
187
- ];
188
-
189
- helper.load([aedesNode, mqttNode], flow, function () {
190
- const client1 = mqtt.connect('mqtt://localhost:1883', { clientId: 'client1' });
191
- client1.on('error', function (err) {
192
- console.error('Error: ', err.toString());
193
- });
194
-
195
- client1.on('connect', function () {
196
- // console.log('External client1 connected');
197
- client1.subscribe('test1883', function (err, granted) {
198
- // console.log('Subscription successful ' + JSON.stringify(granted));
199
- if (err) {
200
- console.error('Error subscribing');
201
- done();
202
- }
203
- client1.publish('test1883', 'Retained Message', { retain: true }, function () {
204
- // console.log('Published Retained Message');
205
- });
206
- });
207
79
  const n2 = helper.getNode('n2');
208
- let counter = 0;
209
80
  n2.on('input', function (msg) {
210
81
  // console.log('Broker received message topic: ' + msg.topic + ', clientid: ' + msg.payload.client.id);
211
82
  if (msg.topic === 'subscribe') {
@@ -214,38 +85,29 @@ describe('Aedes Broker retain tests', function () {
214
85
  // console.log('Client ' + msg.payload.client.id + ' connected with clean ' + msg.payload.client.clean);
215
86
  }
216
87
  });
217
- client1.on('message', function (topic, message) {
218
- if (counter === 0) {
219
- counter++;
220
- } else {
221
- return;
222
- }
223
- console.log(message.toString());
88
+ client2.on('message', function (topic, message) {
89
+ // console.log(message.toString());
224
90
  should(topic.toString()).equal('test1883');
225
- should(message.toString()).equal('Retained Message');
226
- try {
227
- client1.end(true, {}, function () {
91
+ should(message.toString()).equal('test');
92
+ client2.end(function () {
93
+ client1.end(function () {
228
94
  done();
229
95
  });
230
- } catch (err) {
231
- done(err);
232
- }
96
+ });
233
97
  });
234
98
  });
235
99
  });
236
100
  });
237
-
238
- it('a subscriber (retain = true) should receive the last message on first subscribe after restart', function (done) {
101
+ it('a subscriber (retain = true) should only receive the last message on first subscribe', function (done) {
239
102
  this.timeout(10000); // have to wait for the inject with delay of 10 seconds
240
103
  const flow = [
241
104
  {
242
105
  id: 'n1',
243
106
  type: 'aedes broker',
244
107
  mqtt_port: '1883',
245
- persistence_bind: 'level',
246
108
  name: 'Aedes 1883',
247
109
  wires: [
248
- ['n2']
110
+ ['n2'], []
249
111
  ]
250
112
  },
251
113
  {
@@ -253,41 +115,52 @@ describe('Aedes Broker retain tests', function () {
253
115
  type: 'helper'
254
116
  }
255
117
  ];
256
-
257
118
  helper.load([aedesNode, mqttNode], flow, function () {
258
- const client1 = mqtt.connect('mqtt://localhost:1883', { clientId: 'client1' });
259
- client1.on('error', function (err) {
260
- console.error('Error: ', err.toString());
261
- });
262
-
263
- client1.on('connect', function () {
264
- // console.log('External client1 connected');
265
- client1.subscribe('test1883', function (err, granted) {
266
- // console.log('Subscription successful ' + JSON.stringify(granted));
267
- if (err) {
268
- console.error('Error subscribing');
269
- done();
119
+ const n1 = helper.getNode('n1');
120
+ n1._initPromise.then(function () {
121
+ const client1 = mqtt.connect('mqtt://localhost:1883', { clientId: 'client1' });
122
+ const client2 = mqtt.connect('mqtt://localhost:1883', { clientId: 'client2' });
123
+ client1.on('error', logError);
124
+ client2.on('error', logError);
125
+ client2.on('connect', function () {
126
+ // console.log('External client2 connected');
127
+ });
128
+ client1.on('connect', function () {
129
+ // console.log('External client1 connected');
130
+ client1.publish('test1883', 'test1', { retain: true }, function () {
131
+ // console.log('Published test1');
132
+ client1.publish('test1883', 'test2', { retain: true }, function () {
133
+ // console.log('Published test2');
134
+ client2.subscribe('test1883', function (err, granted) {
135
+ // console.log('Subscription successful ' + JSON.stringify(granted));
136
+ if (err) {
137
+ console.error('Error subscribing');
138
+ done();
139
+ }
140
+ });
141
+ });
142
+ });
143
+ });
144
+ const n2 = helper.getNode('n2');
145
+ n2.on('input', function (msg) {
146
+ // console.log('Broker received message topic: ' + msg.topic + ', clientid: ' + msg.payload.client.id);
147
+ if (msg.topic === 'subscribe') {
148
+ // console.log('Client ' + msg.payload.client.id + ' subscribed ' + JSON.stringify(msg.payload.client.subscriptions));
149
+ } else if (msg.topic === 'clientReady') {
150
+ // console.log('Client ' + msg.payload.client.id + ' connected with clean ' + msg.payload.client.clean);
270
151
  }
271
152
  });
272
- });
273
- const n2 = helper.getNode('n2');
274
- n2.on('input', function (msg) {
275
- // console.log('Broker received message topic: ' + msg.topic + ', clientid: ' + msg.payload.client.id);
276
- if (msg.topic === 'subscribe') {
277
- // console.log('Client ' + msg.payload.client.id + ' subscribed ' + JSON.stringify(msg.payload.client.subscriptions));
278
- } else if (msg.topic === 'clientReady') {
279
- // console.log('Client ' + msg.payload.client.id + ' connected with clean ' + msg.payload.client.clean);
280
- }
281
- });
282
- client1.on('message', function (topic, message) {
283
- // console.log(message.toString());
284
- should(topic.toString()).equal('test1883');
285
- should(message.toString()).equal('Retained Message');
286
- client1.end(true, {}, function () {
287
- done();
153
+ client2.on('message', function (topic, message) {
154
+ // console.log(message.toString());
155
+ should(topic.toString()).equal('test1883');
156
+ should(message.toString()).equal('test2');
157
+ client2.end(function () {
158
+ client1.end(function () {
159
+ done();
160
+ });
161
+ });
288
162
  });
289
163
  });
290
164
  });
291
165
  });
292
- */
293
166
  });