Skip to content

Commit

Permalink
Workaround issue strongloop#17 with cujojs/when
Browse files Browse the repository at this point in the history
  • Loading branch information
josieusa committed Jan 3, 2017
1 parent db9387e commit a335e2b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"eslint-config-loopback": "^4.0.0",
"loopback": "^3.0.0",
"mocha": "^2.5.3",
"supertest": "^1.2.0"
"supertest": "^1.2.0",
"when": "3.7.7"
}
}
9 changes: 9 additions & 0 deletions server/middleware/per-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ function perRequestContextFactory(options) {
var scope = options.name || name;
var enableHttpContext = options.enableHttpContext || false;
var ns = LoopBackContext.createContext(scope);
// Monkey-patch LoopBackContext.getCurrentContext in order to fix issue #17
var oldGetCurrentContext = LoopBackContext.getCurrentContext;
LoopBackContext.getCurrentContext = function() {
var context = oldGetCurrentContext();
return context ? {
get: context.bind(context.get).bind(context),
set: context.bind(context.set).bind(context),
} : null;
};

// Return the middleware
return function perRequestContext(req, res, next) {
Expand Down
47 changes: 47 additions & 0 deletions test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var EventEmitter = require('events').EventEmitter;
var expect = require('./helpers/expect');
var loopback = require('loopback');
var request = require('supertest');
var when = require('when');

describe('LoopBack Context', function() {
var runInOtherDomain, runnerInterval;
Expand Down Expand Up @@ -129,4 +130,50 @@ describe('LoopBack Context', function() {
], done);
});
});
it('doesn\'t mix up contexts if using concurrently then() from when 3.7.7',
function() {
expect(require('when/package.json').version).to.equal('3.7.7');
var timeout = 50;
// Concurrent execution number 1 of 2
var execution1 = new Promise(function execution1(outerResolve, reject) {
LoopBackContext.runInContext(function pushToContext1() {
var ctx = LoopBackContext.getCurrentContext();
expect(ctx).is.an('object');
ctx.set('test-key', 'test-value-1');
var whenPromise = when.promise(function(resolve) {
setTimeout(resolve, timeout);
});
whenPromise.then(function pullFromContext1() {
var testValue = ctx && ctx.get('test-key', 'test-value-1');
return testValue;
}).then(function verify1(testValue) {
expect(testValue).to.equal('test-value-1');
outerResolve();
}).catch(function(error) {
reject(error);
});
});
});
// Concurrent execution number 2 of 2
var execution2 = new Promise(function execution1(outerResolve, reject) {
LoopBackContext.runInContext(function pushToContext2() {
var ctx = LoopBackContext.getCurrentContext();
expect(ctx).is.an('object');
ctx.set('test-key', 'test-value-2');
var whenPromise = when.promise(function(resolve) {
setTimeout(resolve, timeout);
});
whenPromise.then(function pullFromContext2() {
var testValue = ctx && ctx.get('test-key', 'test-value-2');
return testValue;
}).then(function verify2(testValue) {
expect(testValue).to.equal('test-value-2');
outerResolve();
}).catch(function(error) {
reject(error);
});
});
});
return Promise.all([execution1, execution2]);
});
});

0 comments on commit a335e2b

Please sign in to comment.