Skip to content

Commit

Permalink
test: cls variant of test-http-abort-stream-end
Browse files Browse the repository at this point in the history
Refs: nodejs#31978

fixup: address review comment
  • Loading branch information
HarshithaKP committed Mar 11, 2020
1 parent 757e203 commit f732e6d
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
53 changes: 53 additions & 0 deletions test/experimental/test-http-abort-stream-end-als.js
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);
}
53 changes: 53 additions & 0 deletions test/experimental/test-http-abort-stream-end-cls.js
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);
}

0 comments on commit f732e6d

Please sign in to comment.