Skip to content

Instantly share code, notes, and snippets.

@othiym23
Forked from dazld/_notes.md
Last active December 27, 2015 08:18
Show Gist options
  • Save othiym23/7294773 to your computer and use it in GitHub Desktop.
Save othiym23/7294773 to your computer and use it in GitHub Desktop.

The script populates a few keys in redis (assuming it is running), and once that is complete, calls a series of tests trying to get them.

Each test stores a key in cls, and compares the key it is given when calling the get function with the one in CLS, before and after invoking GET on the redis client, failing if they are not the same.

Ther are two versions of the get function

  • Straight up callback
  • Promise based with Q

Regardless of whether hiredis is present in the project, am getting errors as follows.

3 Nov 20:43:30 - [nodemon] starting `node index.js`
redis populated, running tests

/Users/dan/projects/cls-bughunt2/node_modules/redis/index.js:587
                throw err;
                      ^
AssertionError: checking value in cls inner (cb version)
    at /Users/dan/projects/cls-bughunt2/index.js:28:9
    at try_callback (/Users/dan/projects/cls-bughunt2/node_modules/redis/index.js:580:9)
    at RedisClient.return_reply (/Users/dan/projects/cls-bughunt2/node_modules/redis/index.js:670:13)
    at ReplyParser.<anonymous> (/Users/dan/projects/cls-bughunt2/node_modules/redis/index.js:312:14)
    at ReplyParser.EventEmitter.emit (events.js:95:17)
    at ReplyParser.send_reply (/Users/dan/projects/cls-bughunt2/node_modules/redis/lib/parser/javascript.js:300:10)
    at ReplyParser.execute (/Users/dan/projects/cls-bughunt2/node_modules/redis/lib/parser/javascript.js:203:22)
    at RedisClient.on_data (/Users/dan/projects/cls-bughunt2/node_modules/redis/index.js:535:27)
    at Socket.<anonymous> (/Users/dan/projects/cls-bughunt2/node_modules/redis/index.js:91:14)
    at Socket.EventEmitter.emit (events.js:95:17)
3 Nov 20:43:31 - [nodemon] app crashed - waiting for file changes before starting...


// deps
var Q = require('q'),
redis = require('redis'),
assert = require('assert'),
cls = require('continuation-local-storage');
var ns = cls.createNamespace('__BUGHUNT');
var client = redis.createClient();
var counter = function(){
var inc = 0;
return function(){
return inc++;
};
}();
// get stuff from redis, and call done when complete
function getCallback (key, done){
assert(ns.get('id') === key, 'checking value in cls outer (cb version)');
client.get(key, ns.bind(function(error, reply){
assert(ns.get('id') === key, 'checking value in cls inner (cb version)');
done(reply);
}));
}
// returns promise to the stuff we're getting from redis
function gettingFromRedis(key) {
// the key passed to the function should be equal to the one in CLS
assert(ns.get('id') === key, 'checking value in cls outer (promise version)');
return Q.ninvoke(client, 'get', key).then(ns.bind(function(data) {
data = JSON.parse(data);
assert(ns.get('id') === data.id, 'checking value in cls after (promise version)');
if (data) {
return Q.resolve(data);
} else {
return Q.reject();
}
}),function(error){
console.log('error with redis')
return Q.reject(error);
});
}
var test = function test() {
ns.run(function(){
var count = counter();
ns.set('id', count);
// straight callback
// is this binding correct? was thinking that the CB should be bound, cf. next in express middleware..?
getCallback(count, ns.bind(function(data){
console.log(data);
}));
// using proimises
gettingFromRedis(count).then(function(response){
console.log('reply is: ',response);
}).fail(function(error){
console.log('prob: ', error);
});
});
};
// setup data
var numTests = 5;
// array of promises
var savingData = [];
for(var i = 0; i < numTests; i++){
var saving = Q.defer();
var toSave = JSON.stringify({
id: i
});
savingData.push(saving);
client.set(i, toSave, function(){
saving.resolve();
});
}
// do tests once have populated all our data in redis
Q.allSettled(savingData).then(function(){
console.log('redis populated, running tests');
// run the tests
while(numTests--){
test();
}
});
{
"dependencies": {
"redis": "~0.9.0",
"hiredis": "~0.1.15",
"q": "~0.9.7",
"continuation-local-storage": "~2.5.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment