Skip to content

Commit c98bdb2

Browse files
addaleaxMylesBorins
authored andcommitted
test: split up and refactor test-domain
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 <refack@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 5de6dfc commit c98bdb2

12 files changed

+263
-254
lines changed
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const domain = require('domain');
6+
7+
const d = new domain.Domain();
8+
9+
d.on('error', common.mustCall((err) => {
10+
assert.strictEqual(err.message, 'foobar');
11+
assert.strictEqual(err.domain, d);
12+
assert.strictEqual(err.domainEmitter, undefined);
13+
assert.strictEqual(err.domainBound, undefined);
14+
assert.strictEqual(err.domainThrown, true);
15+
}));
16+
17+
setTimeout(d.bind(() => { throw new Error('foobar'); }), 1);
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const domain = require('domain');
6+
const EventEmitter = require('events');
7+
8+
const d = new domain.Domain();
9+
let implicit;
10+
11+
d.on('error', common.mustCall((err) => {
12+
assert.strictEqual(err.message, 'foobar');
13+
assert.strictEqual(err.domain, d);
14+
assert.strictEqual(err.domainEmitter, implicit);
15+
assert.strictEqual(err.domainBound, undefined);
16+
assert.strictEqual(err.domainThrown, false);
17+
}));
18+
19+
// Implicit addition of the EventEmitter by being created within a domain-bound
20+
// context.
21+
d.run(common.mustCall(() => {
22+
implicit = new EventEmitter();
23+
}));
24+
25+
setTimeout(common.mustCall(() => {
26+
// escape from the domain, but implicit is still bound to it.
27+
implicit.emit('error', new Error('foobar'));
28+
}), 1);

test/parallel/test-domain-ee.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const domain = require('domain');
6+
const EventEmitter = require('events');
7+
8+
const d = new domain.Domain();
9+
const e = new EventEmitter();
10+
11+
d.on('error', common.mustCall((err) => {
12+
assert.strictEqual(err.message, 'foobar');
13+
assert.strictEqual(err.domain, d);
14+
assert.strictEqual(err.domainEmitter, e);
15+
assert.strictEqual(err.domainBound, undefined);
16+
assert.strictEqual(err.domainThrown, false);
17+
}));
18+
19+
d.add(e);
20+
e.emit('error', new Error('foobar'));
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const domain = require('domain');
6+
7+
// This test is similar to test-domain-multiple-errors, but uses a new domain
8+
// for each errors.
9+
10+
for (const something of [
11+
42, null, undefined, false, () => {}, 'string', Symbol('foo')
12+
]) {
13+
const d = new domain.Domain();
14+
d.run(common.mustCall(() => {
15+
process.nextTick(common.mustCall(() => {
16+
throw something;
17+
}));
18+
}));
19+
20+
d.on('error', common.mustCall((err) => {
21+
assert.strictEqual(something, err);
22+
}));
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const domain = require('domain');
6+
const fs = require('fs');
7+
8+
const d = new domain.Domain();
9+
10+
const fst = fs.createReadStream('stream for nonexistent file');
11+
12+
d.on('error', common.mustCall((err) => {
13+
assert.ok(err.message.match(/^ENOENT: no such file or directory, open '/));
14+
assert.strictEqual(err.domain, d);
15+
assert.strictEqual(err.domainEmitter, fst);
16+
assert.strictEqual(err.domainBound, undefined);
17+
assert.strictEqual(err.domainThrown, false);
18+
}));
19+
20+
d.add(fst);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const domain = require('domain');
6+
const fs = require('fs');
7+
8+
{
9+
const d = new domain.Domain();
10+
11+
d.on('error', common.mustCall((err) => {
12+
assert.strictEqual(err.message, 'foobar');
13+
assert.strictEqual(err.domain, d);
14+
assert.strictEqual(err.domainEmitter, undefined);
15+
assert.strictEqual(err.domainBound, undefined);
16+
assert.strictEqual(err.domainThrown, true);
17+
}));
18+
19+
d.run(common.mustCall(() => {
20+
process.nextTick(common.mustCall(() => {
21+
const i = setInterval(common.mustCall(() => {
22+
clearInterval(i);
23+
setTimeout(common.mustCall(() => {
24+
fs.stat('this file does not exist', common.mustCall((er, stat) => {
25+
throw new Error('foobar');
26+
}));
27+
}), 1);
28+
}), 1);
29+
}));
30+
}));
31+
}
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const domain = require('domain');
6+
7+
{
8+
const d = new domain.Domain();
9+
10+
const mustNotCall = common.mustNotCall();
11+
12+
d.on('error', common.mustCall((err) => {
13+
assert.strictEqual(err.message, 'foobar');
14+
assert.strictEqual(err.domain, d);
15+
assert.strictEqual(err.domainEmitter, undefined);
16+
assert.strictEqual(err.domainBound, mustNotCall);
17+
assert.strictEqual(err.domainThrown, false);
18+
}));
19+
20+
const bound = d.intercept(mustNotCall);
21+
bound(new Error('foobar'));
22+
}
23+
24+
{
25+
const d = new domain.Domain();
26+
27+
const bound = d.intercept(common.mustCall((data) => {
28+
assert.strictEqual(data, 'data');
29+
}));
30+
31+
bound(null, 'data');
32+
}
33+
34+
{
35+
const d = new domain.Domain();
36+
37+
const bound = d.intercept(common.mustCall((data, data2) => {
38+
assert.strictEqual(data, 'data');
39+
assert.strictEqual(data2, 'data2');
40+
}));
41+
42+
bound(null, 'data', 'data2');
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const domain = require('domain');
6+
7+
// This test is similar to test-domain-error-types, but uses a single domain
8+
// to emit all errors.
9+
10+
const d = new domain.Domain();
11+
12+
const values = [
13+
42, null, undefined, false, () => {}, 'string', Symbol('foo')
14+
];
15+
16+
d.on('error', common.mustCall((err) => {
17+
assert(values.includes(err));
18+
}, values.length));
19+
20+
for (const something of values) {
21+
d.run(common.mustCall(() => {
22+
process.nextTick(common.mustCall(() => {
23+
throw something;
24+
}));
25+
}));
26+
}

test/parallel/test-domain-nexttick.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const domain = require('domain');
6+
7+
const d = new domain.Domain();
8+
9+
d.on('error', common.mustCall((err) => {
10+
assert.strictEqual(err.message, 'foobar');
11+
assert.strictEqual(err.domain, d);
12+
assert.strictEqual(err.domainEmitter, undefined);
13+
assert.strictEqual(err.domainBound, undefined);
14+
assert.strictEqual(err.domainThrown, true);
15+
}));
16+
17+
d.run(common.mustCall(() => {
18+
process.nextTick(common.mustCall(() => {
19+
throw new Error('foobar');
20+
}));
21+
}));

test/parallel/test-domain-run.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const domain = require('domain');
6+
7+
const d = new domain.Domain();
8+
9+
assert.strictEqual(d.run(() => 'return value'),
10+
'return value');
11+
12+
assert.strictEqual(d.run((a, b) => `${a} ${b}`, 'return', 'value'),
13+
'return value');

test/parallel/test-domain-timer.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const domain = require('domain');
6+
7+
const d = new domain.Domain();
8+
9+
d.on('error', common.mustCall((err) => {
10+
assert.strictEqual(err.message, 'foobar');
11+
assert.strictEqual(err.domain, d);
12+
assert.strictEqual(err.domainEmitter, undefined);
13+
assert.strictEqual(err.domainBound, undefined);
14+
assert.strictEqual(err.domainThrown, true);
15+
}));
16+
17+
d.run(common.mustCall(() => {
18+
setTimeout(common.mustCall(() => {
19+
throw new Error('foobar');
20+
}), 1);
21+
}));

0 commit comments

Comments
 (0)