Skip to content

Instantly share code, notes, and snippets.

@dazld
Last active December 22, 2015 15:39
Show Gist options
  • Save dazld/6493549 to your computer and use it in GitHub Desktop.
Save dazld/6493549 to your computer and use it in GitHub Desktop.
cls bughunt
var Router = require('./router');
var ns = require('./namespace-module');
var router = new Router();
router.initialize();
var NUM_TESTS = 10;
var spawnAsync = function (i) {
var req = {
url: '/page/'+i,
method: 'get',
someId: Math.floor(Math.random() * Date.now()),
body: '',
headers: {}
};
ns.set('id', req.someId);
req.headers['content-type'] = 'text/html';
var dispatchBound = router.dispatch.bind(router, req);
process.nextTick(dispatchBound);
};
router.on('routeMatched', function () {
var id = ns.get('id');
var req = ns.get('req') || {};
console.log('routeMatched listener: ',req.someId);
});
for (var i = 0; i < NUM_TESTS; i++) {
var saBound = spawnAsync.bind(null,i);
ns.run(saBound);
// saBound();
};
var cls = require('continuation-local-storage');
var ns = cls.createNamespace('testing');
module.exports = ns;
{
"name": "clsbughunt",
"dependencies": {
"continuation-local-storage": "~2.1.1",
"director": "~1.2.0"
}
}
var director = require('director');
var events = require('events');
var ns = require('./namespace-module');
var Router = function(){
var emitter = new events.EventEmitter();
var router = new director.http.Router();
var createHandler = function (){
return function(){
console.log('handler: ',ns.get('id')); // this is ok
emitter.emit('routeMatched', ns.get('id')); // but the listener for this event is not
}
}
var ret = {
_createHandler: createHandler,
initialize: function(routes){
router.on('get','/page/:id', ret._createHandler(/* route, routeFunction */));
},
on: emitter.on.bind(emitter),
once: emitter.once.bind(emitter),
dispatch: function(req,res,next){
console.log('in dispatch: ', ns.get('id'));
router.dispatch(req,res,function(err){
console.log(err);
});
}
}
return ret;
}
module.exports = Router;
@othiym23
Copy link

Line 20 of index.js sets id on a context. Line 30 of index.js fetches and prints req.someId. That's the problem in this example. See https://gist.github.com/othiym23/6505028 for a reduced and fixed version of this. Is there a different problem in the example you reduced this from?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment