Last active
August 29, 2015 14:07
-
-
Save danieltdt/2d2cc51d58d606e5bc97 to your computer and use it in GitHub Desktop.
test helper for redis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var util = require('util'); | |
var assert = require('assert'); | |
var bluebird = require('bluebird'); | |
var EventEmitter = require('events').EventEmitter; | |
module.exports = RedisMonitor; | |
function RedisMonitor(url) { | |
if (!(this instanceof RedisMonitor)) | |
return new RedisMonitor(url); | |
var redis = require('../../redis')(url); | |
this.timeout = 300; //ms | |
this.expect = function () { | |
var commands = Array.prototype.slice.apply(arguments); | |
var id = 'monitor.' + Math.random(); | |
var timeout = this.timeout; | |
var expectations = []; | |
for (var i = 0; i < commands.length; i++) | |
expectations.push([].concat(commands[i])); | |
var watcher = new MonitorWatcher(id); | |
var watchedCommands = new bluebird(function (resolve, reject) { | |
watcher.once('done', resolve); | |
}); | |
var watching = watcher.watch.bind(watcher); | |
return redis | |
.tap(function (client) { | |
return client.monitorAsync(); | |
}) | |
.tap(function (client) { | |
client.on('monitor', watching); | |
}) | |
.then(function (client) { | |
return function expectationVerifier() { | |
watcher.capture(client); | |
var results = watchedCommands | |
.then(function (commandsReceived) { | |
if (commandsReceived.length < expectations.length) { | |
assert.fail( | |
'Not all commands were received\n' + | |
util.inspect(commandsReceived) | |
); | |
} | |
var expectedMatches = expectations.length; | |
for (var i = 0; i < expectations.length; i++) { | |
var expected = expectations[i]; | |
var received = commandsReceived[i]; | |
if (expected.length !== received.length) | |
continue; | |
for (var j = 0; j < expected.length; j++) { | |
var expectedArg = expected[j].toLowerCase(); | |
var receivedArg = received[j].toLowerCase(); | |
if (expectedArg !== receivedArg) | |
continue; | |
expectedMatches--; | |
} | |
} | |
assert( | |
expectedMatches === 0, | |
'commands not match' + | |
'\nexpected: ' + util.inspect(expectations) + | |
'\nreceived: ' + util.inspect(commandsReceived) | |
); | |
}) | |
return (timeout ? results.timeout(timeout) : results) | |
.finally(function () { | |
client.removeListener('monitor', watching); | |
}); | |
}; | |
}); | |
}; | |
} | |
util.inherits(MonitorWatcher, EventEmitter); | |
function MonitorWatcher(stopAtEcho) { | |
if (!(this instanceof MonitorWatcher)) | |
return new MonitorWatcher(stopAtEcho); | |
EventEmitter.call(this); | |
var done = false; | |
this.commands = []; | |
this.capture = function (client) { | |
return client.echo(stopAtEcho); | |
}; | |
this.watch = function (time, args) { | |
if (done) return; | |
if (String(args) === String(['echo', stopAtEcho])) { | |
done = true; | |
this.emit('done', this.commands); | |
this.removeAllListeners('done'); | |
} else { | |
this.commands.push(args); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment