forked from mcollina/mqtt-level-store
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
216 lines (184 loc) · 5.94 KB
/
test.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
'use strict'
/* eslint-env mocha */
var abstractTest = require('mqtt/test/abstract_store')
var level = require('level-test')()
var mqttLevelStore = require('./')
var mqtt = require('mqtt')
var Connection = require('mqtt-connection')
var concat = require('concat-stream')
var net = require('net')
var should = require('should')
describe('mqtt level store', function () {
abstractTest(function (done) {
done(null, mqttLevelStore.single({ level: level() }))
})
})
describe('mqtt level store manager', function () {
var manager
beforeEach(function () {
manager = mqttLevelStore({ level: level() })
})
afterEach(function (done) {
manager.close(done)
})
describe('incoming', function () {
abstractTest(function (done) {
done(null, manager.incoming)
})
})
describe('outgoing', function () {
abstractTest(function (done) {
done(null, manager.outgoing)
})
})
})
describe('mqtt level store manager close', function () {
it('should finish successfully.', function (done) {
var manager = mqttLevelStore({ level: level() })
manager.close(done)
})
it('should return errors when failed to close.', function (done) {
var errorManager = mqttLevelStore({ level: level() })
var incomingCloseSaved = errorManager.incoming.close
var outgoingCloseSaved = errorManager.outgoing.close
var subLevelCloseSaved = errorManager._sublevel.close
var levelCloseSaved = errorManager._level.close
errorManager.incoming.close = function (cb) { cb(new Error('error_i')) }
errorManager.outgoing.close = function (cb) { cb(new Error('error_o')) }
errorManager._sublevel.close = function (cb) { cb(new Error('error_s')) }
errorManager._level.close = function (cb) { cb(new Error('error_l')) }
var expected = { 'incoming': 'error_i', 'outgoing': 'error_o', 'sublevel': 'error_s', 'level': 'error_l' }
errorManager.close(function (err) {
should.deepEqual(JSON.parse(err.message), expected)
errorManager.incoming.close = incomingCloseSaved
errorManager.outgoing.close = outgoingCloseSaved
errorManager._sublevel.close = subLevelCloseSaved
errorManager._level.close = levelCloseSaved
errorManager.close(function (err) {
should.not.exist(err)
done()
})
})
})
})
describe('mqtt.connect flow', function () {
var server
var manager
beforeEach(function (done) {
server = new net.Server()
server.listen(8883, done)
server.on('connection', function (stream) {
var client = Connection(stream)
client.on('connect', function () {
client.connack({ returnCode: 0 })
})
server.emit('client', client)
})
manager = mqttLevelStore({ level: level() })
})
afterEach(function (done) {
server.close(done)
})
it('should resend messages by published order', function (done) {
var serverCount = 0
var client = mqtt.connect({
port: 8883,
incomingStore: manager.incoming,
outgoingStore: manager.outgoing
})
client.nextId = 65535
client.publish('topic', 'payload1', { qos: 1 })
client.publish('topic', 'payload2', { qos: 1 })
client.publish('topic', 'payload3', { qos: 1 })
server.once('client', function (serverClient) {
serverClient.once('publish', function () {
serverClient.stream.destroy()
manager.outgoing.createStream().pipe(concat(function (list) {
list.length.should.equal(3)
}))
})
server.once('client', function (serverClient2) {
serverClient2.on('publish', function (packet) {
serverClient2.puback(packet)
switch (serverCount++) {
case 0:
packet.payload.toString().should.equal('payload1')
break
case 1:
packet.payload.toString().should.equal('payload2')
break
case 2:
packet.payload.toString().should.equal('payload3')
setTimeout(function () {
// make sure additional publish shouldn't be received
serverCount.should.equal(3)
client.end()
done()
}, 200)
break
default:
break
}
})
})
})
})
})
// Topic Alias extract scenario
// Topic Alias mapping
// 'topic1' : 123
// Topic Aliased packet is TopicName: '', Property TopicAlias: 123
// Overwrite
// TopicName: '' to 'topic1', remove Property TopicAlias
describe('overwrite', function () {
var manager
beforeEach(function () {
manager = mqttLevelStore({ level: level() })
})
afterEach(function (done) {
manager.close(done)
})
function testOverwrite (store, done) {
var packet = {
cmd: 'publish',
topic: '',
payload: 'msg',
qos: 1,
retain: false,
messageId: 1,
dup: false,
properties: { topicAlias: 123 }
}
store.put(packet, function (err) {
if (!err) {
store.get(packet, function (err, packet2) {
if (!err) {
packet2.should.have.property('topic', '')
packet2.should.have.property('properties')
packet2.properties.should.have.property('topicAlias', 123)
packet2.topic = 'topic1'
packet2.properties = {}
store.put(packet2, function (err) {
if (!err) {
store.get(packet2, function (err, packet3) {
if (!err) {
packet3.should.have.property('topic', 'topic1')
packet3.should.have.property('properties')
packet2.properties.should.not.have.property('topicAlias')
done()
}
})
}
})
}
})
}
})
}
it('should be able to overwrite incoming', function (done) {
testOverwrite(manager.incoming, done)
})
it('should be able to overwrite outgoing', function (done) {
testOverwrite(manager.outgoing, done)
})
})