Skip to content

Commit

Permalink
Added experimental support for continuation local storage (see issue #64
Browse files Browse the repository at this point in the history
)
  • Loading branch information
analog-nico committed Oct 10, 2015
1 parent d346752 commit 3941033
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/rp.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,9 @@ request.Request.prototype.pipe = RP$noStreaming;
request.Request.prototype.pipeDest = RP$noStreaming;


request.bindCLS = function RP$bindCLS(ns) {
require('cls-bluebird')(ns);
};


module.exports = request;
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"dependencies": {
"bluebird": "^2.3",
"cls-bluebird": "^1.0.1",
"lodash": "^3.10.0",
"request": "^2.34"
},
Expand All @@ -38,6 +39,7 @@
"chai": "2.2.x",
"chai-as-promised": "4.3.x",
"chalk": "^1.1.1",
"continuation-local-storage": "^3.1.4",
"event-stream": "3.3.x",
"gulp": "3.8.x",
"gulp-coveralls": "0.1.x",
Expand Down
34 changes: 34 additions & 0 deletions test/fixtures/cls/single-namespace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

var rp = require('../../../lib/rp.js');
var cls = require('continuation-local-storage');


var ns = cls.createNamespace('testNS');
rp.bindCLS(ns);


function print() {
ns.run(function () {

ns.set('value', ns.get('value') + 1);

console.log(ns.get('value'));

});
}

ns.run(function () {

ns.set('value', 0);

rp('http://localhost:4000/200')
.then(function () {
ns.run(function () {
ns.set('value', ns.get('value') + 1);

setTimeout(print);
});
});

});
17 changes: 17 additions & 0 deletions test/spec/request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,23 @@ describe('Request-Promise', function () {

});

describe('should support continuation local storage', function () {

it('with an untouched Bluebird prototype bound to a single namespace', function (done) {

childProcess.exec('node ' + path.join(__dirname, '../fixtures/cls/single-namespace.js'), function (err, stdout, stderr) {
try {
expect(stdout).to.contain('2\n');
done();
} catch (e) {
done(e);
}
});

});

});

describe('should run the examples', function () {

it('in the README', function () {
Expand Down

0 comments on commit 3941033

Please # to comment.