11'use strict' ;
22// Tests of multiple domains happening at once.
33
4- require ( '../common' ) ;
5- const assert = require ( 'assert' ) ;
4+ const common = require ( '../common' ) ;
65const domain = require ( 'domain' ) ;
7-
8- let caughtA = false ;
9- let caughtB = false ;
10- let caughtC = false ;
11-
6+ const http = require ( 'http' ) ;
127
138const a = domain . create ( ) ;
149a . 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- const http = require ( 'http' ) ;
23- const server = http . createServer ( function ( req , res ) {
13+ const server = http . createServer ( ( req , res ) => {
2414 // child domain of a.
2515 const b = domain . create ( ) ;
2616 a . add ( b ) ;
@@ -31,47 +21,34 @@ const 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 ( ) {
40+ } ) . listen ( 0 , ( ) => {
5341 const c = domain . create ( ) ;
54- const req = http . get ( { host : 'localhost' , port : this . address ( ) . port } ) ;
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