forked from mcollina/mqemitter-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
116 lines (90 loc) · 2.28 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
'use strict'
const redis = require('./')
const test = require('tape').test
const abstractTests = require('mqemitter/abstractTest.js')
abstractTests({
builder: redis,
test
})
abstractTests({
builder: function (opts) { return new redis.MQEmitterRedisPrefix('some_prefix/', opts) },
test
})
function noop () {}
test('actual unsubscribe from Redis', function (t) {
const e = redis()
e.subConn.on('message', function (topic, message) {
t.fail('the message should not be emitted')
})
e.on('hello', noop)
e.removeListener('hello', noop)
e.emit({ topic: 'hello' }, function (err) {
t.notOk(err)
e.close(function () {
t.end()
})
})
})
test('ioredis connect event', function (t) {
const e = redis()
let subConnectEventReceived = false
let pubConnectEventReceived = false
e.state.on('pubConnect', function () {
pubConnectEventReceived = true
newConnectionEvent()
})
e.state.on('subConnect', function () {
subConnectEventReceived = true
newConnectionEvent()
})
function newConnectionEvent () {
if (subConnectEventReceived && pubConnectEventReceived) {
e.close(function () {
t.end()
})
}
}
})
test('ioredis error event', function (t) {
const e = redis({ host: '127' })
t.plan(1)
e.state.once('error', function (err) {
t.deepEqual(err.message.substr(0, 7), 'connect')
e.close(function () {
t.end()
})
})
})
test('topic pattern adapter', function (t) {
const e = redis()
const mqttTopic = 'rooms/+/devices/+/status'
const expectedRedisPattern = 'rooms/*/devices/*/status'
const subTopic = e._subTopic(mqttTopic)
t.plan(1)
t.deepEqual(subTopic, expectedRedisPattern)
e.close(function () {
t.end()
})
})
test('ioredis connection string', function (t) {
const e = redis({
connectionString: 'redis://localhost:6379/0'
})
let subConnectEventReceived = false
let pubConnectEventReceived = false
e.state.on('pubConnect', function () {
pubConnectEventReceived = true
newConnectionEvent()
})
e.state.on('subConnect', function () {
subConnectEventReceived = true
newConnectionEvent()
})
function newConnectionEvent () {
if (subConnectEventReceived && pubConnectEventReceived) {
e.close(function () {
t.end()
})
}
}
})