1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-01 16:46:54 +03:00
Files
node-redis/test/connect.slave.spec.js
Ruben Bridgewater 2b4ab10305 chore - remove standard and use individual config
Standard is not as up to date and still uses a old eslint version.
Instead, use the airbnb default with a couple of modifications.

All required changes are included.
2017-11-28 21:38:21 -02:00

102 lines
2.6 KiB
JavaScript

'use strict'
const assert = require('assert')
const config = require('./lib/config')
const helper = require('./helper')
const RedisProcess = require('./lib/redis-process')
let rp
const path = require('path')
const { redis } = config
// TODO: Fix redis process spawn on windows
if (process.platform !== 'win32') {
describe('master slave sync', () => {
let master = null
let slave = null
before((done) => {
helper.stopRedis(() => {
helper.startRedis('./conf/password.conf', done)
})
})
before(() => {
if (helper.redisProcess().spawnFailed()) return
master = redis.createClient({
password: 'porkchopsandwiches'
})
const multi = master.multi()
let i = 0
while (i < 1000) {
i++
// Write some data in the redis instance, so there's something to sync
multi.set(`foo${i}`, `bar${new Array(500).join(Math.random())}`)
}
return multi.exec()
})
it('sync process and no master should delay ready being emitted for slaves', function (done) {
if (helper.redisProcess().spawnFailed()) this.skip()
const port = 6381
let firstInfo
slave = redis.createClient({
port,
retryStrategy(options) {
// Try to reconnect in very small intervals to catch the
// master_link_status down before the sync completes
return 10
}
})
const tmp = slave.info.bind(slave)
let i = 0
slave.info = function () {
i++
const promise = tmp()
if (!firstInfo || Object.keys(firstInfo).length === 0) {
firstInfo = slave.serverInfo
}
return promise
}
slave.on('connect', () => {
assert.strictEqual(i, 0)
})
const end = helper.callFuncAfter(done, 2)
slave.on('ready', function () {
assert.strictEqual(this.serverInfo.replication.master_link_status, 'up')
assert.strictEqual(firstInfo.replication.master_link_status, 'down')
assert(i > 1)
this.get('foo300').then((res) => {
assert.strictEqual(res.substr(0, 3), 'bar')
end()
})
})
RedisProcess.start((err, _rp) => {
rp = _rp
end(err)
}, path.resolve(__dirname, './conf/slave.conf'), port)
})
after((done) => {
if (helper.redisProcess().spawnFailed()) return done()
const end = helper.callFuncAfter(done, 3)
rp.stop(end)
slave.end(true)
master.flushdb().then(() => {
end()
master.end(true)
}).catch(done)
helper.stopRedis(() => {
helper.startRedis('./conf/redis.conf', end)
})
})
})
}