Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix lint CRLF => LF
  • Loading branch information
Flarna committed Dec 16, 2019
commit a80e811469bf3c3e12ddd174c24ce402f3fa5791
346 changes: 173 additions & 173 deletions test/async-hooks/test-async-local.js
Original file line number Diff line number Diff line change
@@ -1,173 +1,173 @@
'use strict';
const common = require('../common');
// This test verifys the AsyncLocal functionality.
const assert = require('assert');
const { AsyncLocal, AsyncResource } = require('async_hooks');
{
common.expectsError(
() => new AsyncLocal(15),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options" argument must be of type Object. ' +
'Received type number'
}
);
common.expectsError(
() => new AsyncLocal({ onChangedCb: {} }),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options.onChangedCb" property must be of type ' +
'function. Received type object'
}
);
}
{
const asyncLocal1 = new AsyncLocal();
const asyncLocal2 = new AsyncLocal();
const asyncLocal3 = new AsyncLocal();
assert.strictEqual(asyncLocal1.value, undefined);
asyncLocal1.value = 'one';
asyncLocal2.value = 'two';
asyncLocal3.value = 'three';
setImmediate(common.mustCall(() => {
assert.strictEqual(asyncLocal1.value, 'one');
assert.strictEqual(asyncLocal2.value, 'two');
assert.strictEqual(asyncLocal3.value, 'three');
setImmediate(common.mustCall(() => {
assert.strictEqual(asyncLocal1.value, 'one');
assert.strictEqual(asyncLocal2.value, 'two');
assert.strictEqual(asyncLocal3.value, 'three');
}));
asyncLocal1.value = null;
asyncLocal3.value = 'four';
assert.strictEqual(asyncLocal1.value, undefined);
assert.strictEqual(asyncLocal2.value, 'two');
assert.strictEqual(asyncLocal3.value, 'four');
setImmediate(common.mustCall(() => {
assert.strictEqual(asyncLocal1.value, undefined);
assert.strictEqual(asyncLocal2.value, 'two');
assert.strictEqual(asyncLocal3.value, 'four');
}));
}));
}
{
async function asyncFunc() {}
const asyncLocal = new AsyncLocal();
async function testAwait() {
asyncLocal.value = 42;
await asyncFunc();
assert.strictEqual(asyncLocal.value, 42);
}
testAwait().then(common.mustCall(() =>
assert.strictEqual(asyncLocal.value, 42)
));
}
{
const asyncLocal = new AsyncLocal();
const mutableObj = { a: 'b' };
asyncLocal.value = mutableObj;
process.nextTick(common.mustCall(() => {
assert.deepStrictEqual(mutableObj, { a: 'b', b: 'a' });
}));
mutableObj.b = 'a';
}
{
const exp = [
[ undefined, 'foo', false ],
[ 'foo', undefined, false ],
[ undefined, 'bar', false ]
];
const act = [];
const asyncLocal = new AsyncLocal({
onChangedCb: (p, c, t) => act.push([p, c, t])
});
asyncLocal.value = 'foo';
assert.strictEqual(act.length, 1);
asyncLocal.value = null;
assert.strictEqual(act.length, 2);
asyncLocal.value = 'bar';
assert.strictEqual(act.length, 3);
assert.deepStrictEqual(act, exp);
}
{
const asyncLocal = new AsyncLocal();
const asyncRes1 = new AsyncResource('Resource1');
asyncLocal.value = 'R';
const asyncRes2 = new AsyncResource('Resource2');
asyncRes1.runInAsyncScope(common.mustCall(() => {
assert.strictEqual(asyncLocal.value, undefined);
asyncRes2.runInAsyncScope(common.mustCall(() =>
assert.strictEqual(asyncLocal.value, 'R')
));
assert.strictEqual(asyncLocal.value, undefined);
}));
assert.strictEqual(asyncLocal.value, 'R');
}
{
const exp = [
[ undefined, 'foo', false ],
[ 'foo', 'bar', false ],
[ 'bar', 'foo', true ],
[ 'foo', 'bar', true ],
[ 'bar', 'foo', true ],
[ 'foo', undefined, true ],
[ undefined, 'foo', true ],
[ 'foo', undefined, true ],
[ undefined, 'bar', true ],
];
const act = [];
const asyncLocal = new AsyncLocal({
onChangedCb: (p, c, t) => act.push([p, c, t])
});
process.nextTick(common.mustCall(() => {
asyncLocal.value = 'foo';
assert.strictEqual(act.length, 1);
const r1 = new AsyncResource('R1');
const r2 = new AsyncResource('R2');
r1.runInAsyncScope(common.mustCall(() => {
asyncLocal.value = 'bar';
assert.strictEqual(act.length, 2);
r2.runInAsyncScope(common.mustCall(() => {
assert.strictEqual(act.length, 3);
setImmediate(common.mustCall(() => {
assert.strictEqual(act.length, 7);
}));
}));
setImmediate(common.mustCall(() => {
assert.strictEqual(act.length, 9);
assert.deepStrictEqual(act, exp);
}));
assert.strictEqual(act.length, 4);
}));
}));
}
'use strict';
const common = require('../common');

// This test verifys the AsyncLocal functionality.

const assert = require('assert');
const { AsyncLocal, AsyncResource } = require('async_hooks');

{
common.expectsError(
() => new AsyncLocal(15),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options" argument must be of type Object. ' +
'Received type number'
}
);

common.expectsError(
() => new AsyncLocal({ onChangedCb: {} }),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options.onChangedCb" property must be of type ' +
'function. Received type object'
}
);
}

{
const asyncLocal1 = new AsyncLocal();
const asyncLocal2 = new AsyncLocal();
const asyncLocal3 = new AsyncLocal();

assert.strictEqual(asyncLocal1.value, undefined);
asyncLocal1.value = 'one';
asyncLocal2.value = 'two';
asyncLocal3.value = 'three';

setImmediate(common.mustCall(() => {
assert.strictEqual(asyncLocal1.value, 'one');
assert.strictEqual(asyncLocal2.value, 'two');
assert.strictEqual(asyncLocal3.value, 'three');
setImmediate(common.mustCall(() => {
assert.strictEqual(asyncLocal1.value, 'one');
assert.strictEqual(asyncLocal2.value, 'two');
assert.strictEqual(asyncLocal3.value, 'three');
}));
asyncLocal1.value = null;
asyncLocal3.value = 'four';
assert.strictEqual(asyncLocal1.value, undefined);
assert.strictEqual(asyncLocal2.value, 'two');
assert.strictEqual(asyncLocal3.value, 'four');
setImmediate(common.mustCall(() => {
assert.strictEqual(asyncLocal1.value, undefined);
assert.strictEqual(asyncLocal2.value, 'two');
assert.strictEqual(asyncLocal3.value, 'four');
}));
}));
}

{
async function asyncFunc() {}

const asyncLocal = new AsyncLocal();

async function testAwait() {
asyncLocal.value = 42;
await asyncFunc();
assert.strictEqual(asyncLocal.value, 42);
}
testAwait().then(common.mustCall(() =>
assert.strictEqual(asyncLocal.value, 42)
));
}

{
const asyncLocal = new AsyncLocal();
const mutableObj = { a: 'b' };

asyncLocal.value = mutableObj;
process.nextTick(common.mustCall(() => {
assert.deepStrictEqual(mutableObj, { a: 'b', b: 'a' });
}));
mutableObj.b = 'a';
}

{
const exp = [
[ undefined, 'foo', false ],
[ 'foo', undefined, false ],
[ undefined, 'bar', false ]
];

const act = [];
const asyncLocal = new AsyncLocal({
onChangedCb: (p, c, t) => act.push([p, c, t])
});

asyncLocal.value = 'foo';
assert.strictEqual(act.length, 1);

asyncLocal.value = null;
assert.strictEqual(act.length, 2);

asyncLocal.value = 'bar';
assert.strictEqual(act.length, 3);

assert.deepStrictEqual(act, exp);
}

{
const asyncLocal = new AsyncLocal();
const asyncRes1 = new AsyncResource('Resource1');
asyncLocal.value = 'R';
const asyncRes2 = new AsyncResource('Resource2');

asyncRes1.runInAsyncScope(common.mustCall(() => {
assert.strictEqual(asyncLocal.value, undefined);
asyncRes2.runInAsyncScope(common.mustCall(() =>
assert.strictEqual(asyncLocal.value, 'R')
));
assert.strictEqual(asyncLocal.value, undefined);
}));
assert.strictEqual(asyncLocal.value, 'R');
}

{
const exp = [
[ undefined, 'foo', false ],
[ 'foo', 'bar', false ],
[ 'bar', 'foo', true ],
[ 'foo', 'bar', true ],
[ 'bar', 'foo', true ],
[ 'foo', undefined, true ],
[ undefined, 'foo', true ],
[ 'foo', undefined, true ],
[ undefined, 'bar', true ],
];

const act = [];
const asyncLocal = new AsyncLocal({
onChangedCb: (p, c, t) => act.push([p, c, t])
});

process.nextTick(common.mustCall(() => {
asyncLocal.value = 'foo';
assert.strictEqual(act.length, 1);

const r1 = new AsyncResource('R1');
Comment thread
Flarna marked this conversation as resolved.
Outdated
const r2 = new AsyncResource('R2');

r1.runInAsyncScope(common.mustCall(() => {
asyncLocal.value = 'bar';
assert.strictEqual(act.length, 2);

r2.runInAsyncScope(common.mustCall(() => {
assert.strictEqual(act.length, 3);

setImmediate(common.mustCall(() => {
assert.strictEqual(act.length, 7);
}));
}));

setImmediate(common.mustCall(() => {
assert.strictEqual(act.length, 9);
assert.deepStrictEqual(act, exp);
}));
assert.strictEqual(act.length, 4);
}));
}));
}