forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: cls variant of test-http-abort-stream-end
Refs: nodejs#31978 fixup: address review comment
- Loading branch information
1 parent
757e203
commit f732e6d
Showing
2 changed files
with
106 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const http = require('http'); | ||
const { AsyncLocalStorage } = require('async_hooks'); | ||
const asyncLocalStorage = new AsyncLocalStorage(); | ||
|
||
const maxSize = 1024; | ||
|
||
const server = http.createServer(common.mustCall((req, res) => { | ||
server.close(); | ||
|
||
res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
for (let i = 0; i < maxSize; i++) { | ||
res.write(`x${i}`); | ||
} | ||
res.end(); | ||
})); | ||
|
||
server.listen(0, () => { | ||
asyncLocalStorage.run(new Map(), common.mustCall(() => { | ||
const options = { port: server.address().port }; | ||
const req = http.get(options, common.mustCall((res) => { | ||
const store = asyncLocalStorage.getStore(); | ||
store.set('req', req); | ||
store.set('size', 0); | ||
res.on('data', ondata); | ||
req.on('abort', common.mustCall(onabort)); | ||
assert.strictEqual(req.aborted, false); | ||
})); | ||
})); | ||
}); | ||
|
||
function ondata(d) { | ||
const store = asyncLocalStorage.getStore(); | ||
const req = store.get('req'); | ||
let size = store.get('size'); | ||
size += d.length; | ||
assert(!req.aborted, 'got data after abort'); | ||
if (size > maxSize) { | ||
req.abort(); | ||
assert.strictEqual(req.aborted, true); | ||
size = maxSize; | ||
} | ||
store.set('size', size); | ||
} | ||
|
||
function onabort() { | ||
const store = asyncLocalStorage.getStore(); | ||
const size = store.get('size'); | ||
assert.strictEqual(size, maxSize); | ||
} |
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,53 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const http = require('http'); | ||
const { AsyncLocalStorage } = require('async_hooks'); | ||
const als = new AsyncLocalStorage(); | ||
|
||
const maxSize = 1024; | ||
|
||
const server = http.createServer(common.mustCall((req, res) => { | ||
server.close(); | ||
|
||
res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
for (let i = 0; i < maxSize; i++) { | ||
res.write(`x${i}`); | ||
} | ||
res.end(); | ||
})); | ||
|
||
server.listen(0, () => { | ||
als.run(new Map(), common.mustCall(() => { | ||
const options = { port: server.address().port }; | ||
const req = http.get(options, common.mustCall((res) => { | ||
const store = als.getStore(); | ||
store.set('req', req); | ||
store.set('size', 0); | ||
res.on('data', ondata); | ||
req.on('abort', common.mustCall(onabort)); | ||
assert.strictEqual(req.aborted, false); | ||
})); | ||
})); | ||
}); | ||
|
||
function ondata(d) { | ||
const store = als.getStore(); | ||
const req = store.get('req'); | ||
let size = store.get('size'); | ||
size += d.length; | ||
assert(!req.aborted, 'got data after abort'); | ||
if (size > maxSize) { | ||
req.abort(); | ||
assert.strictEqual(req.aborted, true); | ||
size = maxSize; | ||
} | ||
store.set('size', size); | ||
} | ||
|
||
function onabort() { | ||
const store = als.getStore(); | ||
const size = store.get('size'); | ||
assert.strictEqual(size, maxSize); | ||
} |