forked from moscajs/aedes-persistence-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-clusters.js
69 lines (57 loc) · 1.43 KB
/
test-clusters.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
const test = require('node:test')
const persistence = require('./persistence')
const Redis = require('ioredis')
const mqemitterRedis = require('mqemitter-redis')
const abs = require('aedes-cached-persistence/abstract')
function unref () {
this.connector.stream.unref()
}
function sleep (sec) {
return new Promise(resolve => setTimeout(resolve, sec * 1000))
}
const nodes = [
{ host: 'localhost', port: 6378 },
{ host: 'localhost', port: 6380 },
{ host: 'localhost', port: 6381 },
{ host: 'localhost', port: 6382 },
{ host: 'localhost', port: 6383 },
{ host: 'localhost', port: 6384 }
]
const db = new Redis.Cluster(nodes)
db.on('error', e => {
console.trace(e)
})
function buildEmitter () {
const emitter = mqemitterRedis()
emitter.subConn.on('connect', unref)
emitter.pubConn.on('connect', unref)
return emitter
}
function clusterPersistence (cb) {
const slaves = db.nodes('master')
Promise.all(slaves.map((node) => {
return node.flushdb().catch(err => {
console.error('flushRedisKeys-error:', err)
})
})).then(() => {
const conn = new Redis.Cluster(nodes)
conn.on('error', e => {
console.trace(e)
})
conn.on('ready', () => {
cb(null, persistence({
conn,
cluster: true
}))
})
})
}
db.on('ready', () => {
abs({
test,
buildEmitter,
persistence: clusterPersistence,
waitForReady: true
})
})
sleep(10).then(() => process.exit(0))