-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from daniloisr/handle-async-context
Fix context between two async requests
- Loading branch information
Showing
13 changed files
with
185 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
language: node_js | ||
node_js: | ||
- "4" | ||
- "5" | ||
- "8" | ||
- "stable" | ||
|
||
sudo: required | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
|
||
const asyncHooks = require('async_hooks'); | ||
|
||
class AsyncContext { | ||
constructor() { | ||
this.map = new Map(); | ||
asyncHooks.createHook({ | ||
init: (id, _type, triggerId) => { | ||
if (this.map.has(triggerId)) | ||
this.map.set(id, this.map.get(triggerId)); | ||
}, | ||
destroy: (id) => this.map.delete(id) | ||
}).enable(); | ||
} | ||
|
||
get() { | ||
const id = asyncHooks.executionAsyncId(); | ||
if (this.map.has(id)) | ||
return this.map.get(id); | ||
} | ||
|
||
set(val) { | ||
this.map.set(asyncHooks.executionAsyncId(), val); | ||
} | ||
} | ||
|
||
module.exports = new AsyncContext(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
|
||
var expect = require('chai').expect; | ||
|
||
module.exports = function(server) { | ||
describe('Concurrent Async Requests', function() { | ||
before(server.setUp.bind(null, 'async')); | ||
after(server.tearDown); | ||
|
||
it('Each profile runs on its own context', function(done) { | ||
let countDone = 0; | ||
const partialDone = () => { if (++countDone === 2) done(); }; | ||
|
||
server.get('/', (err, response) => { | ||
var ids = JSON.parse(response.headers['x-miniprofiler-ids']); | ||
expect(ids).to.have.lengthOf(1); | ||
|
||
server.post('/mini-profiler-resources/results/', { id: ids[0], popup: 1 }, (err, response, body) => { | ||
var result = JSON.parse(body); | ||
expect(result.Root.CustomTimings.async).to.have.lengthOf(2); | ||
partialDone(); | ||
}); | ||
}); | ||
|
||
server.get('/?once=true', (err, response) => { | ||
var ids = JSON.parse(response.headers['x-miniprofiler-ids']); | ||
expect(ids).to.have.lengthOf(1); | ||
|
||
server.post('/mini-profiler-resources/results/', { id: ids[0], popup: 1 }, (err, response, body) => { | ||
var result = JSON.parse(body); | ||
expect(result.Root.CustomTimings.async).to.have.lengthOf(1); | ||
partialDone(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
module.exports = function(obj) { | ||
return { | ||
name: 'dummy-async', | ||
handler: function(req, res, next) { | ||
obj.asyncFn = function() { | ||
const timing = req.miniprofiler.startTimeQuery('async', 'dummy call'); | ||
|
||
return new Promise(resolve => { | ||
setTimeout(() => { | ||
req.miniprofiler.stopTimeQuery(timing); | ||
resolve(); | ||
}, 25); | ||
}); | ||
}; | ||
|
||
next(); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
asyncFn: () => Promise.resolve() | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict'; | ||
|
||
var miniprofiler = require('../../../lib/miniprofiler.js'); | ||
var dummyModule = require('../dummy-module'); | ||
var express = require('express'); | ||
|
||
var app = express(); | ||
|
||
app.use(miniprofiler.express()); | ||
app.use(miniprofiler.express.for(require('../async-provider.js')(dummyModule))); | ||
|
||
app.get('/', (req, res) => { | ||
dummyModule.asyncFn().then(() => { | ||
Promise.resolve(req.query.once ? undefined : dummyModule.asyncFn()) | ||
.then(() => res.send(res.locals.miniprofiler.include())); | ||
}); | ||
}); | ||
|
||
module.exports = app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict'; | ||
|
||
var miniprofiler = require('../../../lib/miniprofiler.js'); | ||
var dummyModule = require('../dummy-module'); | ||
const Hapi = require('hapi'); | ||
|
||
const server = new Hapi.Server(); | ||
server.connection({ port: 8083 }); | ||
|
||
server.register(miniprofiler.hapi(), (err) => { | ||
if (err) throw err; | ||
}); | ||
|
||
server.register(miniprofiler.hapi.for(require('../async-provider.js')(dummyModule)), (err) => { | ||
if (err) throw err; | ||
}); | ||
|
||
server.route({ | ||
method: 'GET', | ||
path:'/', | ||
handler: function(request, reply) { | ||
dummyModule.asyncFn().then(() => { | ||
Promise.resolve(request.query.once ? undefined : dummyModule.asyncFn()) | ||
.then(() => reply(request.app.miniprofiler.include())); | ||
}); | ||
} | ||
}); | ||
|
||
module.exports = server; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict'; | ||
|
||
var miniprofiler = require('../../../lib/miniprofiler.js'); | ||
var dummyModule = require('../dummy-module'); | ||
var koa = require('koa'); | ||
var route = require('koa-route'); | ||
var app = koa(); | ||
|
||
app.use(miniprofiler.koa()); | ||
app.use(miniprofiler.koa.for(require('../async-provider.js')(dummyModule))); | ||
|
||
app.use(route.get('/', function *(){ | ||
yield dummyModule.asyncFn().then(() => { | ||
return Promise.resolve(this.query.once ? undefined : dummyModule.asyncFn()) | ||
.then(() => { this.body = this.state.miniprofiler.include(); }); | ||
}); | ||
})); | ||
|
||
module.exports = app; |