-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Changes from 3 commits
4d7ef02
501a793
bb5fcf1
c6ee790
0bec30a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() { | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code setting up There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is redundant. If the method was not called then Please remove. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
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 toloopback.getCurrentContext
.There was a problem hiding this comment.
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()
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed