Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB - loopback.getCurrentContext() return null #885

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions server/middleware/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var loopback = require('../../lib/loopback');
var juggler = require('loopback-datasource-juggler');
var remoting = require('strong-remoting');
var cls = require('continuation-local-storage');
var domain = require('domain');

module.exports = context;

Expand Down Expand Up @@ -44,6 +45,13 @@ function context(options) {
var scope = options.name || name;
var enableHttpContext = options.enableHttpContext || false;
var ns = createContext(scope);

var currentDomain = process.domain = domain.create();
currentDomain.oldBind = currentDomain.bind;
currentDomain.bind = function(callback, context) {
return currentDomain.oldBind(ns.bind(callback, context), context);
};

// Return the middleware
return function contextHandler(req, res, next) {
if (req.loopbackContext) {
Expand All @@ -53,13 +61,19 @@ function context(options) {
// Bind req/res event emitters to the given namespace
ns.bindEmitter(req);
ns.bindEmitter(res);

currentDomain.add(req);
currentDomain.add(res);

// Create namespace for the request context
ns.run(function processRequestInContext(context) {
// Run the code in the context of the namespace
if (enableHttpContext) {
ns.set('http', {req: req, res: res}); // Set up the transport context
}
next();
currentDomain.run(function() {
ns.run(function processRequestInContext(context) {
// Run the code in the context of the namespace
if (enableHttpContext) {
ns.set('http', {req: req, res: res}); // Set up the transport context
}
next();
});
});
};
}
Expand Down Expand Up @@ -115,4 +129,4 @@ function chain(child) {
} else {
child.getCurrentContext = loopback.getCurrentContext;
}
}
}
72 changes: 72 additions & 0 deletions test/context.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
var loopback = require('loopback');
var request = require('supertest');
var Domain = require('domain');
var assert = require('assert');
var EventEmitter = require("events").EventEmitter;

describe('check loopback.getCurrentContext', function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move the test to test/loopback.test.js and shorten the describe label to loopback.getCurrentContext.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also note that this is a server-side only test that should not be run in the browser. You need to use describe.onServer().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

var app = loopback();
var runInOtherDomain;
var runnerInterval;

before(function(){
var called = false;
app.use(loopback.rest());
app.dataSource('db', { connector: 'memory' });

var TestModel = loopback.createModel({name: 'TestModel', properties: {name: String}, options: { base: 'Model'}});
app.model(TestModel, {dataSource: "db", public: true});

var emitterInOtherDomain = new EventEmitter();
Domain.create().add(emitterInOtherDomain);

runInOtherDomain = function(fn) {
emitterInOtherDomain.once('run', fn);
}

runnerInterval = setInterval(function() { emitterInOtherDomain.emit('run'); }, 10);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code setting up runInOtherDomain and cleaning up after the test should go to standalone before/after hooks, to make it clear it is only loosely related to the custom app & model built for the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't get what you described about "before/after hooks" here, could you give me more details ?


// function for remote method
TestModel.test = function(inst, cb) {
tmpCtx = loopback.getCurrentContext();
if (tmpCtx) tmpCtx.set('data', 'test');
called = true;
if (process.domain) cb = process.domain.bind(cb); // IMPORTANT
runInOtherDomain(cb);
};

// remote method
TestModel.remoteMethod('test', {
accepts: {arg: 'inst', type: 'TestModel'},
returns: {root: true},
http: {path: '/test', verb: 'get'}
});

// after remote hook
TestModel.afterRemote('**', function(ctxx, inst, next){
ctxx.result.called = called;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is redundant. If the method was not called then tmpCtx.get('data') returns undefined.

Please remove.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

tmpCtx = loopback.getCurrentContext();
if (tmpCtx) {
ctxx.result.data = tmpCtx.get('data');
}else {
ctxx.result.data = "";
}
next();
});
});

after(function tearDownRunInOtherDomain() {
clearInterval(runnerInterval);
});

it('should fail without the patch and it should pass once the patch is applied', function(done) {
request(app)
.get('/TestModels/test')
.end(function(err, res) {
if (err) return done(err);
assert.equal(res.body.called, true);
assert.equal(res.body.data, 'test');
done();
});
});
});