Skip to content

Commit 1b1ba74

Browse files
edsadrMylesBorins
authored andcommitted
test: improve code in test-domain-multi
* use common.mustCall to validate functions executions * use common.fail to control error * remove unnecessary variables * remove unnecessary assertions * remove console.log and console.error * use arrow functions PR-URL: #10798 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent ee27917 commit 1b1ba74

File tree

1 file changed

+13
-36
lines changed

1 file changed

+13
-36
lines changed

test/parallel/test-domain-multi.js

+13-36
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
'use strict';
22
// Tests of multiple domains happening at once.
33

4-
require('../common');
5-
var assert = require('assert');
6-
var domain = require('domain');
7-
8-
var caughtA = false;
9-
var caughtB = false;
10-
var caughtC = false;
11-
4+
const common = require('../common');
5+
const domain = require('domain');
6+
const http = require('http');
127

138
var a = domain.create();
149
a.enter(); // this will be our "root" domain
15-
a.on('error', function(er) {
16-
caughtA = true;
17-
console.log('This should not happen');
18-
throw er;
19-
});
2010

11+
a.on('error', common.fail);
2112

22-
var http = require('http');
23-
var server = http.createServer(function(req, res) {
13+
const server = http.createServer((req, res) => {
2414
// child domain of a.
2515
var b = domain.create();
2616
a.add(b);
@@ -31,47 +21,34 @@ var server = http.createServer(function(req, res) {
3121
b.add(req);
3222
b.add(res);
3323

34-
b.on('error', function(er) {
35-
caughtB = true;
36-
console.error('Error encountered', er);
24+
b.on('error', common.mustCall((er) => {
3725
if (res) {
3826
res.writeHead(500);
3927
res.end('An error occurred');
4028
}
4129
// res.writeHead(500), res.destroy, etc.
4230
server.close();
43-
});
31+
}));
4432

4533
// XXX this bind should not be necessary.
4634
// the write cb behavior in http/net should use an
4735
// event so that it picks up the domain handling.
48-
res.write('HELLO\n', b.bind(function() {
36+
res.write('HELLO\n', b.bind(() => {
4937
throw new Error('this kills domain B, not A');
5038
}));
5139

52-
}).listen(0, function() {
53-
var c = domain.create();
54-
var req = http.get({ host: 'localhost', port: this.address().port });
40+
}).listen(0, () => {
41+
const c = domain.create();
42+
const req = http.get({ host: 'localhost', port: server.address().port });
5543

5644
// add the request to the C domain
5745
c.add(req);
5846

59-
req.on('response', function(res) {
60-
console.error('got response');
47+
req.on('response', (res) => {
6148
// add the response object to the C domain
6249
c.add(res);
6350
res.pipe(process.stdout);
6451
});
6552

66-
c.on('error', function(er) {
67-
caughtC = true;
68-
console.error('Error on c', er.message);
69-
});
70-
});
71-
72-
process.on('exit', function() {
73-
assert.strictEqual(caughtA, false);
74-
assert.strictEqual(caughtB, true);
75-
assert.strictEqual(caughtC, true);
76-
console.log('ok - Errors went where they were supposed to go');
53+
c.on('error', common.mustCall((er) => { }));
7754
});

0 commit comments

Comments
 (0)