1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-10 11:43:01 +03:00
Files
node-redis/test/pubsub.spec.js
Ruben Bridgewater f1a7bcd735 chore: use standard
2017-05-06 07:06:52 +02:00

651 lines
23 KiB
JavaScript

'use strict'
var Buffer = require('safe-buffer').Buffer
var assert = require('assert')
var config = require('./lib/config')
var helper = require('./helper')
var redis = config.redis
describe('publish/subscribe', function () {
helper.allTests(function (ip, args) {
describe('using ' + ip, function () {
var pub = null
var sub = null
var channel = 'test channel'
var channel2 = 'test channel 2'
var message = 'test message'
beforeEach(function (done) {
var end = helper.callFuncAfter(done, 2)
pub = redis.createClient.apply(null, args)
sub = redis.createClient.apply(null, args)
pub.once('connect', function () {
pub.flushdb(function () {
end()
})
})
sub.once('connect', function () {
end()
})
})
describe('disable resubscribe', function () {
beforeEach(function (done) {
sub.end(false)
sub = redis.createClient({
disableResubscribing: true
})
sub.once('connect', function () {
done()
})
})
it('does not fire subscribe events after reconnecting', function (done) {
var a = false
sub.on('subscribe', function (chnl, count) {
if (chnl === channel2) {
if (a) {
return done(new Error('Test failed'))
}
assert.strictEqual(2, count)
sub.stream.destroy()
}
})
sub.on('reconnecting', function () {
a = true
sub.on('ready', function () {
assert.strictEqual(sub.commandQueue.length, 0)
done()
})
})
sub.subscribe(channel, channel2)
})
})
describe('stringNumbers and pub sub', function () {
beforeEach(function (done) {
sub.end(false)
sub = redis.createClient({
stringNumbers: true
})
sub.once('connect', function () {
done()
})
})
it('does not fire subscribe events after reconnecting', function (done) {
var i = 0
var end = helper.callFuncAfter(done, 2)
sub.on('subscribe', function (chnl, count) {
assert.strictEqual(typeof count, 'number')
assert.strictEqual(++i, count)
})
sub.on('unsubscribe', function (chnl, count) {
assert.strictEqual(typeof count, 'number')
assert.strictEqual(--i, count)
})
sub.subscribe(channel, channel2)
sub.unsubscribe(function (err, res) { // Do not pass a channel here!
if (err) throw err
assert.strictEqual(sub.pubSubMode, 2)
assert.deepEqual(sub.subscriptionSet, {})
end()
})
sub.set('foo', 'bar', helper.isString('OK'))
sub.subscribe(channel2, end)
})
})
describe('subscribe', function () {
it('fires a subscribe event for each channel subscribed to even after reconnecting', function (done) {
var a = false
sub.on('subscribe', function (chnl, count) {
if (chnl === channel2) {
assert.strictEqual(2, count)
if (a) return done()
sub.stream.destroy()
}
})
sub.on('reconnecting', function () {
a = true
})
sub.subscribe(channel, channel2)
})
it('fires a subscribe event for each channel as buffer subscribed to even after reconnecting', function (done) {
var a = false
sub.end(true)
sub = redis.createClient({
detectBuffers: true
})
sub.on('subscribe', function (chnl, count) {
if (chnl.inspect() === Buffer.from([0xAA, 0xBB, 0x00, 0xF0]).inspect()) {
assert.strictEqual(1, count)
if (a) {
return done()
}
sub.stream.destroy()
}
})
sub.on('reconnecting', function () {
a = true
})
sub.subscribe(Buffer.from([0xAA, 0xBB, 0x00, 0xF0]), channel2)
})
it('receives messages on subscribed channel', function (done) {
var end = helper.callFuncAfter(done, 2)
sub.on('subscribe', function (chnl, count) {
pub.publish(channel, message, function (err, res) {
helper.isNumber(1)(err, res)
end()
})
})
sub.on('message', function (chnl, msg) {
assert.strictEqual(chnl, channel)
assert.strictEqual(msg, message)
end()
})
sub.subscribe(channel)
})
it('receives messages if subscribe is called after unsubscribe', function (done) {
var end = helper.callFuncAfter(done, 2)
sub.once('subscribe', function (chnl, count) {
pub.publish(channel, message, function (err, res) {
helper.isNumber(1)(err, res)
end()
})
})
sub.on('message', function (chnl, msg) {
assert.strictEqual(chnl, channel)
assert.strictEqual(msg, message)
end()
})
sub.subscribe(channel)
sub.unsubscribe(channel)
sub.subscribe(channel)
})
it('handles SUB UNSUB MSG SUB', function (done) {
sub.subscribe('chan8')
sub.subscribe('chan9')
sub.unsubscribe('chan9')
pub.publish('chan8', 'something')
sub.subscribe('chan9', done)
})
it('handles SUB UNSUB MSG SUB 2', function (done) {
sub.psubscribe('abc*', helper.isDeepEqual([1, ['abc*']]))
sub.subscribe('xyz')
sub.unsubscribe('xyz')
pub.publish('abcd', 'something')
sub.subscribe('xyz', done)
})
it('emits end event if quit is called from within subscribe', function (done) {
sub.on('end', done)
sub.on('subscribe', function (chnl, count) {
sub.quit()
})
sub.subscribe(channel)
})
it('subscribe; close; resubscribe with prototype inherited property names', function (done) {
var count = 0
var channels = ['channel 1', 'channel 2']
var msg = ['hi from channel 1', 'hi from channel 2']
sub.on('message', function (channel, message) {
var n = Math.max(count - 1, 0)
assert.strictEqual(channel, channels[n])
assert.strictEqual(message, msg[n])
if (count === 2) return done()
sub.stream.end()
})
sub.select(3)
sub.subscribe(channels)
sub.on('ready', function (err, results) {
if (err) throw err
pub.publish(channels[count], msg[count])
count++
})
pub.publish(channels[count], msg[count])
})
})
describe('multiple subscribe / unsubscribe commands', function () {
it('reconnects properly with pub sub and select command', function (done) {
var end = helper.callFuncAfter(done, 2)
sub.select(3)
sub.set('foo', 'bar')
sub.set('failure', helper.isError()) // Triggering a warning while subscribing should work
sub.mget('foo', 'bar', 'baz', 'hello', 'world', helper.isDeepEqual(['bar', null, null, null, null]))
sub.subscribe('somechannel', 'another channel', function (err, res) {
if (err) throw err
end()
sub.stream.destroy()
})
assert(sub.ready)
sub.on('ready', function () {
sub.unsubscribe()
sub.del('foo')
sub.info(end)
})
})
it('should not go into pubsub mode with unsubscribe commands', function (done) {
sub.on('unsubscribe', function (msg) {
// The unsubscribe should not be triggered, as there was no corresponding channel
throw new Error('Test failed')
})
sub.set('foo', 'bar')
sub.unsubscribe(helper.isDeepEqual([0, []]))
sub.del('foo', done)
})
it('handles multiple channels with the same channel name properly, even with buffers', function (done) {
var channels = ['a', 'b', 'a', Buffer.from('a'), 'c', 'b']
var subscribedChannels = [1, 2, 2, 2, 3, 3]
var i = 0
sub.subscribe(channels)
sub.on('subscribe', function (channel, count) {
if (Buffer.isBuffer(channel)) {
assert.strictEqual(channel.inspect(), Buffer.from(channels[i]).inspect())
} else {
assert.strictEqual(channel, channels[i].toString())
}
assert.strictEqual(count, subscribedChannels[i])
i++
})
sub.unsubscribe('a', 'c', 'b')
sub.get('foo', done)
})
it('should only resubscribe to channels not unsubscribed earlier on a reconnect', function (done) {
sub.subscribe('/foo', '/bar')
sub.batch().unsubscribe(['/bar'], function () {
pub.pubsub('channels', helper.isDeepEqual(['/foo'], function () {
sub.stream.destroy()
sub.once('ready', function () {
pub.pubsub('channels', helper.isDeepEqual(['/foo'], function () {
sub.unsubscribe('/foo', done)
}))
})
}))
}).exec()
})
it('unsubscribes, subscribes, unsubscribes... single and multiple entries mixed. Withouth callbacks', function (done) {
function subscribe (channels) {
sub.unsubscribe(helper.isNull)
sub.subscribe(channels, helper.isNull)
}
var all = false
var subscribeMsg = ['1', '3', '2', '5', 'test', 'bla']
sub.on('subscribe', function (msg, count) {
subscribeMsg.splice(subscribeMsg.indexOf(msg), 1)
if (subscribeMsg.length === 0 && all) {
assert.strictEqual(count, 3)
done()
}
})
var unsubscribeMsg = ['1', '3', '2']
sub.on('unsubscribe', function (msg, count) {
unsubscribeMsg.splice(unsubscribeMsg.indexOf(msg), 1)
if (unsubscribeMsg.length === 0) {
assert.strictEqual(count, 0)
all = true
}
})
subscribe(['1', '3'])
subscribe(['2'])
subscribe(['5', 'test', 'bla'])
})
it('unsubscribes, subscribes, unsubscribes... single and multiple entries mixed. Without callbacks', function (done) {
function subscribe (channels) {
sub.unsubscribe()
sub.subscribe(channels)
}
var all = false
var subscribeMsg = ['1', '3', '2', '5', 'test', 'bla']
sub.on('subscribe', function (msg, count) {
subscribeMsg.splice(subscribeMsg.indexOf(msg), 1)
if (subscribeMsg.length === 0 && all) {
assert.strictEqual(count, 3)
done()
}
})
var unsubscribeMsg = ['1', '3', '2']
sub.on('unsubscribe', function (msg, count) {
unsubscribeMsg.splice(unsubscribeMsg.indexOf(msg), 1)
if (unsubscribeMsg.length === 0) {
assert.strictEqual(count, 0)
all = true
}
})
subscribe(['1', '3'])
subscribe(['2'])
subscribe(['5', 'test', 'bla'])
})
it('unsubscribes, subscribes, unsubscribes... single and multiple entries mixed. Without callback and concret channels', function (done) {
function subscribe (channels) {
sub.unsubscribe(channels)
sub.unsubscribe(channels)
sub.subscribe(channels)
}
var all = false
var subscribeMsg = ['1', '3', '2', '5', 'test', 'bla']
sub.on('subscribe', function (msg, count) {
subscribeMsg.splice(subscribeMsg.indexOf(msg), 1)
if (subscribeMsg.length === 0 && all) {
assert.strictEqual(count, 6)
done()
}
})
var unsubscribeMsg = ['1', '3', '2', '5', 'test', 'bla']
sub.on('unsubscribe', function (msg, count) {
var pos = unsubscribeMsg.indexOf(msg)
if (pos !== -1) { unsubscribeMsg.splice(pos, 1) }
if (unsubscribeMsg.length === 0) {
all = true
}
})
subscribe(['1', '3'])
subscribe(['2'])
subscribe(['5', 'test', 'bla'])
})
it('unsubscribes, subscribes, unsubscribes... with pattern matching', function (done) {
function subscribe (channels, callback) {
sub.punsubscribe('prefix:*', helper.isNull)
sub.psubscribe(channels, function (err, res) {
helper.isNull(err)
if (callback) callback(err, res)
})
}
var all = false
var end = helper.callFuncAfter(done, 8)
var subscribeMsg = ['prefix:*', 'prefix:3', 'prefix:2', '5', 'test:a', 'bla']
sub.on('psubscribe', function (msg, count) {
subscribeMsg.splice(subscribeMsg.indexOf(msg), 1)
if (subscribeMsg.length === 0) {
assert.strictEqual(count, 5)
all = true
}
})
var rest = 1
var unsubscribeMsg = ['prefix:*', 'prefix:*', 'prefix:*', '*']
sub.on('punsubscribe', function (msg, count) {
unsubscribeMsg.splice(unsubscribeMsg.indexOf(msg), 1)
if (all) {
assert.strictEqual(unsubscribeMsg.length, 0)
assert.strictEqual(count, rest--) // Print the remaining channels
end()
} else {
assert.strictEqual(msg, 'prefix:*')
assert.strictEqual(count, rest++ - 1)
}
})
sub.on('pmessage', function (pattern, channel, msg) {
assert.strictEqual(msg, 'test')
assert.strictEqual(pattern, 'prefix:*')
assert.strictEqual(channel, 'prefix:1')
end()
})
subscribe(['prefix:*', 'prefix:3'], function () {
pub.publish('prefix:1', Buffer.from('test'), function () {
subscribe(['prefix:2'])
subscribe(['5', 'test:a', 'bla'], function () {
assert(all)
})
sub.punsubscribe(function (err, res) {
assert(!err)
assert.deepStrictEqual(res, [0, ['prefix:3', 'prefix:2', '5', 'test:a', 'bla']])
assert(all)
all = false // Make sure the callback is actually after the emit
end()
})
sub.pubsub('channels', helper.isDeepEqual([], end))
})
})
})
})
describe('unsubscribe', function () {
it('fires an unsubscribe event', function (done) {
sub.on('subscribe', function (chnl, count) {
sub.unsubscribe(channel)
})
sub.subscribe(channel)
sub.on('unsubscribe', function (chnl, count) {
assert.strictEqual(chnl, channel)
assert.strictEqual(count, 0)
return done()
})
})
it('puts client back into write mode', function (done) {
sub.on('subscribe', function (chnl, count) {
sub.unsubscribe(channel)
})
sub.subscribe(channel)
sub.on('unsubscribe', function (chnl, count) {
pub.incr('foo', helper.isNumber(1, done))
})
})
it('sub executes callback when unsubscribe is called and there are no subscriptions', function (done) {
sub.unsubscribe(helper.isDeepEqual([0, []], done))
})
it('pub executes callback when unsubscribe is called and there are no subscriptions', function (done) {
pub.unsubscribe(helper.isDeepEqual([0, []]))
pub.get('foo', done)
})
})
describe('psubscribe', function () {
it('allows all channels to be subscribed to using a * pattern', function (done) {
var sub2 = redis.createClient({
returnBuffers: true
})
sub.subscribe('/foo', function () {
sub2.on('ready', function () {
sub2.batch().psubscribe('*', helper.isDeepEqual([1, ['*']])).exec()
sub2.subscribe('/foo', function () {
pub.pubsub('numsub', '/foo', helper.isDeepEqual(['/foo', 2]))
// sub2 is counted twice as it subscribed with psubscribe and subscribe
pub.publish('/foo', 'hello world', helper.isNumber(3))
})
sub2.on('pmessage', function (pattern, channel, message) {
assert.strictEqual(pattern.inspect(), Buffer.from('*').inspect())
assert.strictEqual(channel.inspect(), Buffer.from('/foo').inspect())
assert.strictEqual(message.inspect(), Buffer.from('hello world').inspect())
sub2.quit(done)
})
})
})
})
it('allows to listen to pmessageBuffer and pmessage', function (done) {
var end = helper.callFuncAfter(done, 6)
var data = Array(10000).join('äüs^öéÉÉ`e')
sub.set('foo', data, function () {
sub.get('foo')
sub.stream.once('data', function () {
assert.strictEqual(sub.messageBuffers, false)
assert.strictEqual(sub.shouldBuffer, false)
sub.on('pmessageBuffer', function (pattern, channel, message) {
assert.strictEqual(pattern.inspect(), Buffer.from('*').inspect())
assert.strictEqual(channel.inspect(), Buffer.from('/foo').inspect())
sub.quit(end)
})
assert.notStrictEqual(sub.messageBuffers, sub.buffers)
})
var batch = sub.batch()
batch.psubscribe('*')
batch.subscribe('/foo')
batch.unsubscribe('/foo')
batch.unsubscribe(helper.isDeepEqual([1, []]))
batch.subscribe(['/foo'], helper.isDeepEqual([2, ['/foo']]))
batch.exec(function () {
// There's one subscriber to this channel
pub.pubsub('numsub', '/foo', helper.isDeepEqual(['/foo', 1], end))
// There's exactly one channel that is listened too
pub.pubsub('channels', helper.isDeepEqual(['/foo'], end))
// One pattern is active
pub.pubsub('numpat', helper.isNumber(1, end))
pub.publish('/foo', 'hello world', helper.isNumber(2))
})
// Either messageBuffers or buffers has to be true, but not both at the same time
sub.on('pmessage', function (pattern, channel, message) {
assert.strictEqual(pattern, '*')
assert.strictEqual(channel, '/foo')
assert.strictEqual(message, 'hello world')
end()
})
sub.on('message', function (channel, message) {
assert.strictEqual(channel, '/foo')
assert.strictEqual(message, 'hello world')
end()
})
})
})
})
describe('punsubscribe', function () {
it('does not complain when punsubscribe is called and there are no subscriptions', function () {
sub.punsubscribe()
})
it('executes callback when punsubscribe is called and there are no subscriptions', function (done) {
pub.batch().punsubscribe(helper.isDeepEqual([0, []])).exec(done)
})
})
describe('fail for other commands while in pub sub mode', function () {
it('return error if only pub sub commands are allowed', function (done) {
sub.subscribe('channel')
// Ping is allowed even if not listed as such!
sub.ping(function (err, res) {
assert.strictEqual(err, null)
assert.strictEqual(res[0], 'pong')
})
// Get is forbidden
sub.get('foo', function (err, res) {
assert(/^ERR only \(P\)SUBSCRIBE \/ \(P\)UNSUBSCRIBE/.test(err.message))
assert.strictEqual(err.command, 'GET')
})
// Quit is allowed
sub.quit(done)
})
it('emit error if only pub sub commands are allowed without callback', function (done) {
sub.subscribe('channel')
sub.on('error', function (err) {
assert(/^ERR only \(P\)SUBSCRIBE \/ \(P\)UNSUBSCRIBE/.test(err.message))
assert.strictEqual(err.command, 'GET')
done()
})
sub.get('foo')
})
})
it('should not publish a message multiple times per command', function (done) {
var published = {}
function subscribe (message) {
sub.removeAllListeners('subscribe')
sub.removeAllListeners('message')
sub.removeAllListeners('unsubscribe')
sub.on('subscribe', function () {
pub.publish('/foo', message)
})
sub.on('message', function (channel, message) {
if (published[message]) {
done(new Error('Message published more than once.'))
}
published[message] = true
})
sub.on('unsubscribe', function (channel, count) {
assert.strictEqual(count, 0)
})
sub.subscribe('/foo')
}
subscribe('hello')
setTimeout(function () {
sub.unsubscribe()
setTimeout(function () {
subscribe('world')
setTimeout(done, 50)
}, 40)
}, 40)
})
it('should not publish a message without any publish command', function (done) {
pub.set('foo', 'message')
pub.set('bar', 'hello')
pub.mget('foo', 'bar')
pub.subscribe('channel', function () {
setTimeout(done, 50)
})
pub.on('message', function (msg) {
done(new Error('This message should not have been published: ' + msg))
})
})
it('arguments variants', function (done) {
sub.batch()
.info(['stats'])
.info()
.client('KILL', ['type', 'pubsub'])
.client('KILL', ['type', 'pubsub'], function () {})
.unsubscribe()
.psubscribe(['pattern:*'])
.punsubscribe('unknown*')
.punsubscribe(['pattern:*'])
.exec(function (err, res) {
if (err) throw err
sub.client('kill', ['type', 'pubsub'])
sub.psubscribe('*')
sub.punsubscribe('pa*')
sub.punsubscribe(['a', '*'], done)
})
})
afterEach(function () {
// Explicitly ignore still running commands
pub.end(false)
sub.end(false)
})
})
})
})