Skip to content

Commit

Permalink
test: split up and refactor test-domain
Browse files Browse the repository at this point in the history
Split up test-domain into multiple, more focused test files
and use more modern JS inside of them.

PR-URL: #13614
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
  • Loading branch information
addaleax authored and MylesBorins committed Nov 28, 2017
1 parent ba62b0e commit 806f03e
Show file tree
Hide file tree
Showing 12 changed files with 263 additions and 254 deletions.
17 changes: 17 additions & 0 deletions test/parallel/test-domain-bind-timeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const domain = require('domain');

const d = new domain.Domain();

d.on('error', common.mustCall((err) => {
assert.strictEqual(err.message, 'foobar');
assert.strictEqual(err.domain, d);
assert.strictEqual(err.domainEmitter, undefined);
assert.strictEqual(err.domainBound, undefined);
assert.strictEqual(err.domainThrown, true);
}));

setTimeout(d.bind(() => { throw new Error('foobar'); }), 1);
28 changes: 28 additions & 0 deletions test/parallel/test-domain-ee-implicit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const domain = require('domain');
const EventEmitter = require('events');

const d = new domain.Domain();
let implicit;

d.on('error', common.mustCall((err) => {
assert.strictEqual(err.message, 'foobar');
assert.strictEqual(err.domain, d);
assert.strictEqual(err.domainEmitter, implicit);
assert.strictEqual(err.domainBound, undefined);
assert.strictEqual(err.domainThrown, false);
}));

// Implicit addition of the EventEmitter by being created within a domain-bound
// context.
d.run(common.mustCall(() => {
implicit = new EventEmitter();
}));

setTimeout(common.mustCall(() => {
// escape from the domain, but implicit is still bound to it.
implicit.emit('error', new Error('foobar'));
}), 1);
20 changes: 20 additions & 0 deletions test/parallel/test-domain-ee.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const domain = require('domain');
const EventEmitter = require('events');

const d = new domain.Domain();
const e = new EventEmitter();

d.on('error', common.mustCall((err) => {
assert.strictEqual(err.message, 'foobar');
assert.strictEqual(err.domain, d);
assert.strictEqual(err.domainEmitter, e);
assert.strictEqual(err.domainBound, undefined);
assert.strictEqual(err.domainThrown, false);
}));

d.add(e);
e.emit('error', new Error('foobar'));
23 changes: 23 additions & 0 deletions test/parallel/test-domain-error-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const domain = require('domain');

// This test is similar to test-domain-multiple-errors, but uses a new domain
// for each errors.

for (const something of [
42, null, undefined, false, () => {}, 'string', Symbol('foo')
]) {
const d = new domain.Domain();
d.run(common.mustCall(() => {
process.nextTick(common.mustCall(() => {
throw something;
}));
}));

d.on('error', common.mustCall((err) => {
assert.strictEqual(something, err);
}));
}
20 changes: 20 additions & 0 deletions test/parallel/test-domain-fs-enoent-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const domain = require('domain');
const fs = require('fs');

const d = new domain.Domain();

const fst = fs.createReadStream('stream for nonexistent file');

d.on('error', common.mustCall((err) => {
assert.ok(err.message.match(/^ENOENT: no such file or directory, open '/));
assert.strictEqual(err.domain, d);
assert.strictEqual(err.domainEmitter, fst);
assert.strictEqual(err.domainBound, undefined);
assert.strictEqual(err.domainThrown, false);
}));

d.add(fst);
31 changes: 31 additions & 0 deletions test/parallel/test-domain-implicit-binding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const domain = require('domain');
const fs = require('fs');

{
const d = new domain.Domain();

d.on('error', common.mustCall((err) => {
assert.strictEqual(err.message, 'foobar');
assert.strictEqual(err.domain, d);
assert.strictEqual(err.domainEmitter, undefined);
assert.strictEqual(err.domainBound, undefined);
assert.strictEqual(err.domainThrown, true);
}));

d.run(common.mustCall(() => {
process.nextTick(common.mustCall(() => {
const i = setInterval(common.mustCall(() => {
clearInterval(i);
setTimeout(common.mustCall(() => {
fs.stat('this file does not exist', common.mustCall((er, stat) => {
throw new Error('foobar');
}));
}), 1);
}), 1);
}));
}));
}
43 changes: 43 additions & 0 deletions test/parallel/test-domain-intercept.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const domain = require('domain');

{
const d = new domain.Domain();

const mustNotCall = common.mustNotCall();

d.on('error', common.mustCall((err) => {
assert.strictEqual(err.message, 'foobar');
assert.strictEqual(err.domain, d);
assert.strictEqual(err.domainEmitter, undefined);
assert.strictEqual(err.domainBound, mustNotCall);
assert.strictEqual(err.domainThrown, false);
}));

const bound = d.intercept(mustNotCall);
bound(new Error('foobar'));
}

{
const d = new domain.Domain();

const bound = d.intercept(common.mustCall((data) => {
assert.strictEqual(data, 'data');
}));

bound(null, 'data');
}

{
const d = new domain.Domain();

const bound = d.intercept(common.mustCall((data, data2) => {
assert.strictEqual(data, 'data');
assert.strictEqual(data2, 'data2');
}));

bound(null, 'data', 'data2');
}
26 changes: 26 additions & 0 deletions test/parallel/test-domain-multiple-errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const domain = require('domain');

// This test is similar to test-domain-error-types, but uses a single domain
// to emit all errors.

const d = new domain.Domain();

const values = [
42, null, undefined, false, () => {}, 'string', Symbol('foo')
];

d.on('error', common.mustCall((err) => {
assert(values.includes(err));
}, values.length));

for (const something of values) {
d.run(common.mustCall(() => {
process.nextTick(common.mustCall(() => {
throw something;
}));
}));
}
21 changes: 21 additions & 0 deletions test/parallel/test-domain-nexttick.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const domain = require('domain');

const d = new domain.Domain();

d.on('error', common.mustCall((err) => {
assert.strictEqual(err.message, 'foobar');
assert.strictEqual(err.domain, d);
assert.strictEqual(err.domainEmitter, undefined);
assert.strictEqual(err.domainBound, undefined);
assert.strictEqual(err.domainThrown, true);
}));

d.run(common.mustCall(() => {
process.nextTick(common.mustCall(() => {
throw new Error('foobar');
}));
}));
13 changes: 13 additions & 0 deletions test/parallel/test-domain-run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

require('../common');
const assert = require('assert');
const domain = require('domain');

const d = new domain.Domain();

assert.strictEqual(d.run(() => 'return value'),
'return value');

assert.strictEqual(d.run((a, b) => `${a} ${b}`, 'return', 'value'),
'return value');
21 changes: 21 additions & 0 deletions test/parallel/test-domain-timer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const domain = require('domain');

const d = new domain.Domain();

d.on('error', common.mustCall((err) => {
assert.strictEqual(err.message, 'foobar');
assert.strictEqual(err.domain, d);
assert.strictEqual(err.domainEmitter, undefined);
assert.strictEqual(err.domainBound, undefined);
assert.strictEqual(err.domainThrown, true);
}));

d.run(common.mustCall(() => {
setTimeout(common.mustCall(() => {
throw new Error('foobar');
}), 1);
}));
Loading

0 comments on commit 806f03e

Please sign in to comment.