-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.js
101 lines (85 loc) · 2.3 KB
/
events.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
'use strict'
var test = require('tape').test
var helper = require('./helper')
var aedes = require('../')
var setup = helper.setup
var connect = helper.connect
var subscribe = helper.subscribe
test('publishes an hearbeat', function (t) {
t.plan(3)
var broker = aedes({
heartbeatInterval: 10 // ms
})
broker.subscribe('$SYS/+/heartbeat', function (message, cb) {
var id = message.topic.match(/\$SYS\/([^/]+)\/heartbeat/)[1]
t.equal(id, broker.id, 'broker id matches')
t.deepEqual(message.payload.toString(), id, 'message has id as the payload')
broker.close(t.pass.bind(t, 'broker closes'))
})
})
test('does not forward $SYS topics to # subscription', function (t) {
t.plan(4)
var s = connect(setup())
subscribe(t, s, '#', 0, function () {
s.outStream.once('data', function (packet) {
t.fail('no packet should be received')
})
s.broker.mq.emit({
cmd: 'publish',
topic: '$SYS/hello',
payload: 'world'
}, function () {
t.pass('nothing happened')
})
})
})
test('does not store $SYS topics to QoS 1 # subscription', function (t) {
t.plan(3)
var broker = aedes()
var opts = { clean: false, clientId: 'abcde' }
var s = connect(setup(broker), opts)
subscribe(t, s, '#', 1, function () {
s.inStream.end()
s.broker.publish({
cmd: 'publish',
topic: '$SYS/hello',
payload: 'world',
qos: 1
}, function () {
s = connect(setup(broker), { clean: false, clientId: 'abcde' }, function () {
t.end()
})
s.outStream.once('data', function (packet) {
t.fail('no packet should be received')
})
})
})
})
test('Emit event when receives a ping', function (t) {
t.plan(6)
t.timeoutAfter(2000)
var broker = aedes()
broker.on('ping', function (packet, client) {
if (client && client) {
t.equal(client.id, 'abcde')
t.equal(packet.cmd, 'pingreq')
t.equal(packet.payload, null)
t.equal(packet.topic, null)
t.equal(packet.length, 0)
broker.close()
t.pass('ended')
}
})
var s = connect(setup(broker), { clientId: 'abcde' })
s.inStream.write({
cmd: 'pingreq'
})
})
test('Emit event when broker closed', function (t) {
t.plan(1)
var broker = aedes()
broker.once('closed', function () {
t.ok(true)
})
broker.close()
})