1
1
'use strict' ;
2
2
// Tests of multiple domains happening at once.
3
3
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' ) ;
12
7
13
8
var a = domain . create ( ) ;
14
9
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
- } ) ;
20
10
11
+ a . on ( 'error' , common . fail ) ;
21
12
22
- var http = require ( 'http' ) ;
23
- var server = http . createServer ( function ( req , res ) {
13
+ const server = http . createServer ( ( req , res ) => {
24
14
// child domain of a.
25
15
var b = domain . create ( ) ;
26
16
a . add ( b ) ;
@@ -31,47 +21,34 @@ var server = http.createServer(function(req, res) {
31
21
b . add ( req ) ;
32
22
b . add ( res ) ;
33
23
34
- b . on ( 'error' , function ( er ) {
35
- caughtB = true ;
36
- console . error ( 'Error encountered' , er ) ;
24
+ b . on ( 'error' , common . mustCall ( ( er ) => {
37
25
if ( res ) {
38
26
res . writeHead ( 500 ) ;
39
27
res . end ( 'An error occurred' ) ;
40
28
}
41
29
// res.writeHead(500), res.destroy, etc.
42
30
server . close ( ) ;
43
- } ) ;
31
+ } ) ) ;
44
32
45
33
// XXX this bind should not be necessary.
46
34
// the write cb behavior in http/net should use an
47
35
// event so that it picks up the domain handling.
48
- res . write ( 'HELLO\n' , b . bind ( function ( ) {
36
+ res . write ( 'HELLO\n' , b . bind ( ( ) => {
49
37
throw new Error ( 'this kills domain B, not A' ) ;
50
38
} ) ) ;
51
39
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 } ) ;
55
43
56
44
// add the request to the C domain
57
45
c . add ( req ) ;
58
46
59
- req . on ( 'response' , function ( res ) {
60
- console . error ( 'got response' ) ;
47
+ req . on ( 'response' , ( res ) => {
61
48
// add the response object to the C domain
62
49
c . add ( res ) ;
63
50
res . pipe ( process . stdout ) ;
64
51
} ) ;
65
52
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 ) => { } ) ) ;
77
54
} ) ;
0 commit comments