-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.js
193 lines (173 loc) · 4.32 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
const test = require('tape').test
const persistence = require('./')
const abs = require('aedes-persistence/abstract')
const { Level } = require('level') // Level >= 8.0.0
const { randomUUID } = require('crypto')
const { tmpdir } = require('os')
const { join } = require('path')
const { mkdirSync } = require('fs')
function tempDir () {
const dir = join(tmpdir(), 'aedes-persistence-level-test', randomUUID())
mkdirSync(dir, { recursive: true })
return dir
}
function leveldb () {
return new Level(tempDir())
}
abs({
test,
persistence () {
return persistence(leveldb())
}
})
test('restore', t => {
const db = leveldb()
const instance = persistence(db)
const client = {
id: 'abcde'
}
const subs = [{
topic: 'hello',
qos: 1
}, {
topic: 'hello/#',
qos: 1
}, {
topic: 'matteo',
qos: 1
}]
instance.addSubscriptions(client, subs, err => {
t.notOk(err, 'no error')
const instance2 = persistence(db)
instance2.subscriptionsByTopic('hello', (err, resubs) => {
t.notOk(err, 'no error')
t.deepEqual(resubs, [{
clientId: client.id,
topic: 'hello/#',
qos: 1,
rh: undefined,
rap: undefined,
nl: undefined
}, {
clientId: client.id,
topic: 'hello',
qos: 1,
rh: undefined,
rap: undefined,
nl: undefined
}])
instance.destroy(t.end.bind(t))
})
})
})
test('outgoing update after enqueuing a possible offline message', t => {
const db = leveldb()
const instance = persistence(db)
const client = {
clientId: 'abcde'
}
const client1 = {
id: 'abcde'
}
const packet = {
cmd: 'publish',
brokerId: 'adasdasd',
brokerCounter: 0,
topic: 'test',
payload: 'Return of the Jedi',
messageId: 7
}
const updatePacket = {
cmd: 'pubrel',
messageId: 7
}
// Enqueue an offline packet
instance.outgoingEnqueue(client, packet, (err, packet1) => {
t.error(err)
// When the client comes back online, aedes calls emptyQueue which calls outgoingUpdate
instance.outgoingUpdate(client1, packet, (err, client, packet) => {
t.notOk(err, 'no error')
// When pubrel is published, outgoingUpdate is called again without the broker Id
instance.outgoingUpdate(client, updatePacket, (err, client, packet) => {
t.notOk(err, 'no error')
instance.destroy(t.end.bind(t))
})
})
})
})
test('Dont replace subscriptions with different QoS if client id is different', t => {
const db = leveldb()
const instance = persistence(db)
const client = {
id: 'test'
}
const client1 = {
id: 'test.1'
}
const sub1 = [{
topic: 'test/+/dev/#',
qos: 2
}]
const sub2 = [{
topic: 'test/television/dev/about',
qos: 1
}]
instance.addSubscriptions(client, sub1, err => {
t.notOk(err, 'no error')
instance.addSubscriptions(client1, sub2, err => {
t.notOk(err, 'no error')
instance.subscriptionsByTopic('test/television/dev/about', (err, resubs) => {
t.notOk(err, 'no error')
t.deepEqual(resubs, [{
topic: 'test/television/dev/about',
clientId: 'test.1',
qos: 1,
rh: undefined,
rap: undefined,
nl: undefined
}, {
topic: 'test/+/dev/#',
clientId: 'test',
qos: 2,
rh: undefined,
rap: undefined,
nl: undefined
}])
instance.destroy(t.end.bind(t))
})
})
})
})
test('Replace subscriptions with different QoS if client id is same', t => {
const db = leveldb()
const instance = persistence(db)
const client = {
id: 'test'
}
const sub1 = [{
topic: 'test/+/dev/#',
qos: 2
}]
const sub2 = [{
topic: 'test/television/dev/about',
qos: 1
}]
instance.addSubscriptions(client, sub1, err => {
t.notOk(err, 'no error')
instance.addSubscriptions(client, sub2, err => {
t.notOk(err, 'no error')
instance.subscriptionsByTopic('test/television/dev/about', (err, resubs) => {
t.notOk(err, 'no error')
t.deepEqual(resubs, [{
topic: 'test/television/dev/about',
clientId: 'test',
qos: 1,
rh: undefined,
rap: undefined,
nl: undefined
}])
instance.destroy(t.end.bind(t))
})
})
})
})