|
1 | 1 |
|
2 | 2 | var assert = require('assert')
|
| 3 | +var asyncHooks = tryRequire('async_hooks') |
3 | 4 | var http = require('http')
|
4 | 5 | var net = require('net')
|
5 | 6 | var onFinished = require('..')
|
6 | 7 |
|
| 8 | +var describeAsyncHooks = typeof asyncHooks.AsyncLocalStorage === 'function' |
| 9 | + ? describe |
| 10 | + : describe.skip |
| 11 | + |
7 | 12 | describe('onFinished(res, listener)', function () {
|
8 | 13 | it('should invoke listener given an unknown object', function (done) {
|
9 | 14 | onFinished({}, done)
|
@@ -32,15 +37,57 @@ describe('onFinished(res, listener)', function () {
|
32 | 37 | sendGet(server)
|
33 | 38 | })
|
34 | 39 |
|
35 |
| - it('should fire when called after finish', function (done) { |
36 |
| - var server = http.createServer(function (req, res) { |
37 |
| - onFinished(res, function () { |
38 |
| - onFinished(res, done) |
| 40 | + describe('when called after finish', function () { |
| 41 | + it('should fire when called after finish', function (done) { |
| 42 | + var server = http.createServer(function (req, res) { |
| 43 | + onFinished(res, function () { |
| 44 | + onFinished(res, done) |
| 45 | + }) |
| 46 | + setTimeout(res.end.bind(res), 0) |
39 | 47 | })
|
40 |
| - setTimeout(res.end.bind(res), 0) |
| 48 | + |
| 49 | + sendGet(server) |
41 | 50 | })
|
42 | 51 |
|
43 |
| - sendGet(server) |
| 52 | + describeAsyncHooks('when async local storage', function () { |
| 53 | + it('should presist store in callback', function (done) { |
| 54 | + var asyncLocalStorage = new asyncHooks.AsyncLocalStorage() |
| 55 | + var store = { foo: 'bar' } |
| 56 | + |
| 57 | + var server = http.createServer(function (req, res) { |
| 58 | + onFinished(res, function () { |
| 59 | + asyncLocalStorage.run(store, function () { |
| 60 | + onFinished(res, function () { |
| 61 | + assert.strictEqual(asyncLocalStorage.getStore().foo, 'bar') |
| 62 | + done() |
| 63 | + }) |
| 64 | + }) |
| 65 | + }) |
| 66 | + setTimeout(res.end.bind(res), 0) |
| 67 | + }) |
| 68 | + |
| 69 | + sendGet(server) |
| 70 | + }) |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + describeAsyncHooks('when async local storage', function () { |
| 75 | + it('should presist store in callback', function (done) { |
| 76 | + var asyncLocalStorage = new asyncHooks.AsyncLocalStorage() |
| 77 | + var store = { foo: 'bar' } |
| 78 | + |
| 79 | + var server = http.createServer(function (req, res) { |
| 80 | + asyncLocalStorage.run(store, function () { |
| 81 | + onFinished(res, function () { |
| 82 | + assert.strictEqual(asyncLocalStorage.getStore().foo, 'bar') |
| 83 | + done() |
| 84 | + }) |
| 85 | + }) |
| 86 | + setTimeout(res.end.bind(res), 0) |
| 87 | + }) |
| 88 | + |
| 89 | + sendGet(server) |
| 90 | + }) |
44 | 91 | })
|
45 | 92 | })
|
46 | 93 |
|
@@ -385,16 +432,60 @@ describe('onFinished(req, listener)', function () {
|
385 | 432 | sendGet(server)
|
386 | 433 | })
|
387 | 434 |
|
388 |
| - it('should fire when called after finish', function (done) { |
389 |
| - var server = http.createServer(function (req, res) { |
390 |
| - onFinished(req, function () { |
391 |
| - onFinished(req, done) |
| 435 | + describe('when called after finish', function () { |
| 436 | + it('should fire when called after finish', function (done) { |
| 437 | + var server = http.createServer(function (req, res) { |
| 438 | + onFinished(req, function () { |
| 439 | + onFinished(req, done) |
| 440 | + }) |
| 441 | + req.resume() |
| 442 | + setTimeout(res.end.bind(res), 0) |
392 | 443 | })
|
393 |
| - req.resume() |
394 |
| - setTimeout(res.end.bind(res), 0) |
| 444 | + |
| 445 | + sendGet(server) |
395 | 446 | })
|
396 | 447 |
|
397 |
| - sendGet(server) |
| 448 | + describeAsyncHooks('when async local storage', function () { |
| 449 | + it('should presist store in callback', function (done) { |
| 450 | + var asyncLocalStorage = new asyncHooks.AsyncLocalStorage() |
| 451 | + var store = { foo: 'bar' } |
| 452 | + |
| 453 | + var server = http.createServer(function (req, res) { |
| 454 | + onFinished(req, function () { |
| 455 | + asyncLocalStorage.run(store, function () { |
| 456 | + onFinished(req, function () { |
| 457 | + assert.strictEqual(asyncLocalStorage.getStore().foo, 'bar') |
| 458 | + done() |
| 459 | + }) |
| 460 | + }) |
| 461 | + }) |
| 462 | + req.resume() |
| 463 | + setTimeout(res.end.bind(res), 0) |
| 464 | + }) |
| 465 | + |
| 466 | + sendGet(server) |
| 467 | + }) |
| 468 | + }) |
| 469 | + }) |
| 470 | + |
| 471 | + describeAsyncHooks('when async local storage', function () { |
| 472 | + it('should presist store in callback', function (done) { |
| 473 | + var asyncLocalStorage = new asyncHooks.AsyncLocalStorage() |
| 474 | + var store = { foo: 'bar' } |
| 475 | + |
| 476 | + var server = http.createServer(function (req, res) { |
| 477 | + asyncLocalStorage.run(store, function () { |
| 478 | + onFinished(req, function () { |
| 479 | + assert.strictEqual(asyncLocalStorage.getStore().foo, 'bar') |
| 480 | + done() |
| 481 | + }) |
| 482 | + }) |
| 483 | + req.resume() |
| 484 | + setTimeout(res.end.bind(res), 0) |
| 485 | + }) |
| 486 | + |
| 487 | + sendGet(server) |
| 488 | + }) |
398 | 489 | })
|
399 | 490 | })
|
400 | 491 |
|
@@ -1068,6 +1159,14 @@ function sendGet (server) {
|
1068 | 1159 | })
|
1069 | 1160 | }
|
1070 | 1161 |
|
| 1162 | +function tryRequire (name) { |
| 1163 | + try { |
| 1164 | + return require(name) |
| 1165 | + } catch (e) { |
| 1166 | + return {} |
| 1167 | + } |
| 1168 | +} |
| 1169 | + |
1071 | 1170 | function writeRequest (socket, chunked) {
|
1072 | 1171 | socket.write('GET / HTTP/1.1\r\n')
|
1073 | 1172 | socket.write('Host: localhost\r\n')
|
|
0 commit comments