11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// This test makes sure that when using --abort-on-uncaught-exception and
41cb0ef41Sopenharmony_ci// when throwing an error from within a domain that has an error handler
51cb0ef41Sopenharmony_ci// setup, the process _does not_ abort.
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst common = require('../common');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst assert = require('assert');
101cb0ef41Sopenharmony_ciconst domain = require('domain');
111cb0ef41Sopenharmony_ciconst child_process = require('child_process');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst tests = [
141cb0ef41Sopenharmony_ci  function nextTick() {
151cb0ef41Sopenharmony_ci    const d = domain.create();
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci    d.once('error', common.mustCall());
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci    d.run(function() {
201cb0ef41Sopenharmony_ci      process.nextTick(function() {
211cb0ef41Sopenharmony_ci        throw new Error('exceptional!');
221cb0ef41Sopenharmony_ci      });
231cb0ef41Sopenharmony_ci    });
241cb0ef41Sopenharmony_ci  },
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  function timer() {
271cb0ef41Sopenharmony_ci    const d = domain.create();
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci    d.on('error', common.mustCall());
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci    d.run(function() {
321cb0ef41Sopenharmony_ci      setTimeout(function() {
331cb0ef41Sopenharmony_ci        throw new Error('exceptional!');
341cb0ef41Sopenharmony_ci      }, 33);
351cb0ef41Sopenharmony_ci    });
361cb0ef41Sopenharmony_ci  },
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  function immediate() {
391cb0ef41Sopenharmony_ci    const d = domain.create();
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci    d.on('error', common.mustCall());
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci    d.run(function() {
441cb0ef41Sopenharmony_ci      setImmediate(function() {
451cb0ef41Sopenharmony_ci        throw new Error('boom!');
461cb0ef41Sopenharmony_ci      });
471cb0ef41Sopenharmony_ci    });
481cb0ef41Sopenharmony_ci  },
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  function timerPlusNextTick() {
511cb0ef41Sopenharmony_ci    const d = domain.create();
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci    d.on('error', common.mustCall());
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci    d.run(function() {
561cb0ef41Sopenharmony_ci      setTimeout(function() {
571cb0ef41Sopenharmony_ci        process.nextTick(function() {
581cb0ef41Sopenharmony_ci          throw new Error('exceptional!');
591cb0ef41Sopenharmony_ci        });
601cb0ef41Sopenharmony_ci      }, 33);
611cb0ef41Sopenharmony_ci    });
621cb0ef41Sopenharmony_ci  },
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  function firstRun() {
651cb0ef41Sopenharmony_ci    const d = domain.create();
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci    d.on('error', common.mustCall());
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci    d.run(function() {
701cb0ef41Sopenharmony_ci      throw new Error('exceptional!');
711cb0ef41Sopenharmony_ci    });
721cb0ef41Sopenharmony_ci  },
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci  function fsAsync() {
751cb0ef41Sopenharmony_ci    const d = domain.create();
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci    d.on('error', common.mustCall());
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci    d.run(function() {
801cb0ef41Sopenharmony_ci      const fs = require('fs');
811cb0ef41Sopenharmony_ci      fs.exists('/non/existing/file', function onExists(exists) {
821cb0ef41Sopenharmony_ci        throw new Error('boom!');
831cb0ef41Sopenharmony_ci      });
841cb0ef41Sopenharmony_ci    });
851cb0ef41Sopenharmony_ci  },
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci  function netServer() {
881cb0ef41Sopenharmony_ci    const net = require('net');
891cb0ef41Sopenharmony_ci    const d = domain.create();
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci    d.on('error', common.mustCall());
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci    d.run(function() {
941cb0ef41Sopenharmony_ci      const server = net.createServer(function(conn) {
951cb0ef41Sopenharmony_ci        conn.pipe(conn);
961cb0ef41Sopenharmony_ci      });
971cb0ef41Sopenharmony_ci      server.listen(0, common.localhostIPv4, function() {
981cb0ef41Sopenharmony_ci        const conn = net.connect(this.address().port, common.localhostIPv4);
991cb0ef41Sopenharmony_ci        conn.once('data', function() {
1001cb0ef41Sopenharmony_ci          throw new Error('ok');
1011cb0ef41Sopenharmony_ci        });
1021cb0ef41Sopenharmony_ci        conn.end('ok');
1031cb0ef41Sopenharmony_ci        server.close();
1041cb0ef41Sopenharmony_ci      });
1051cb0ef41Sopenharmony_ci    });
1061cb0ef41Sopenharmony_ci  },
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci  function firstRunOnlyTopLevelErrorHandler() {
1091cb0ef41Sopenharmony_ci    const d = domain.create();
1101cb0ef41Sopenharmony_ci    const d2 = domain.create();
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci    d.on('error', common.mustCall());
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci    d.run(function() {
1151cb0ef41Sopenharmony_ci      d2.run(function() {
1161cb0ef41Sopenharmony_ci        throw new Error('boom!');
1171cb0ef41Sopenharmony_ci      });
1181cb0ef41Sopenharmony_ci    });
1191cb0ef41Sopenharmony_ci  },
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci  function firstRunNestedWithErrorHandler() {
1221cb0ef41Sopenharmony_ci    const d = domain.create();
1231cb0ef41Sopenharmony_ci    const d2 = domain.create();
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci    d2.on('error', common.mustCall());
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci    d.run(function() {
1281cb0ef41Sopenharmony_ci      d2.run(function() {
1291cb0ef41Sopenharmony_ci        throw new Error('boom!');
1301cb0ef41Sopenharmony_ci      });
1311cb0ef41Sopenharmony_ci    });
1321cb0ef41Sopenharmony_ci  },
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ci  function timeoutNestedWithErrorHandler() {
1351cb0ef41Sopenharmony_ci    const d = domain.create();
1361cb0ef41Sopenharmony_ci    const d2 = domain.create();
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci    d2.on('error', common.mustCall());
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci    d.run(function() {
1411cb0ef41Sopenharmony_ci      d2.run(function() {
1421cb0ef41Sopenharmony_ci        setTimeout(function() {
1431cb0ef41Sopenharmony_ci          console.log('foo');
1441cb0ef41Sopenharmony_ci          throw new Error('boom!');
1451cb0ef41Sopenharmony_ci        }, 33);
1461cb0ef41Sopenharmony_ci      });
1471cb0ef41Sopenharmony_ci    });
1481cb0ef41Sopenharmony_ci  },
1491cb0ef41Sopenharmony_ci
1501cb0ef41Sopenharmony_ci  function setImmediateNestedWithErrorHandler() {
1511cb0ef41Sopenharmony_ci    const d = domain.create();
1521cb0ef41Sopenharmony_ci    const d2 = domain.create();
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ci    d2.on('error', common.mustCall());
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ci    d.run(function() {
1571cb0ef41Sopenharmony_ci      d2.run(function() {
1581cb0ef41Sopenharmony_ci        setImmediate(function() {
1591cb0ef41Sopenharmony_ci          throw new Error('boom!');
1601cb0ef41Sopenharmony_ci        });
1611cb0ef41Sopenharmony_ci      });
1621cb0ef41Sopenharmony_ci    });
1631cb0ef41Sopenharmony_ci  },
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci  function nextTickNestedWithErrorHandler() {
1661cb0ef41Sopenharmony_ci    const d = domain.create();
1671cb0ef41Sopenharmony_ci    const d2 = domain.create();
1681cb0ef41Sopenharmony_ci
1691cb0ef41Sopenharmony_ci    d2.on('error', common.mustCall());
1701cb0ef41Sopenharmony_ci
1711cb0ef41Sopenharmony_ci    d.run(function() {
1721cb0ef41Sopenharmony_ci      d2.run(function() {
1731cb0ef41Sopenharmony_ci        process.nextTick(function() {
1741cb0ef41Sopenharmony_ci          throw new Error('boom!');
1751cb0ef41Sopenharmony_ci        });
1761cb0ef41Sopenharmony_ci      });
1771cb0ef41Sopenharmony_ci    });
1781cb0ef41Sopenharmony_ci  },
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_ci  function fsAsyncNestedWithErrorHandler() {
1811cb0ef41Sopenharmony_ci    const d = domain.create();
1821cb0ef41Sopenharmony_ci    const d2 = domain.create();
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_ci    d2.on('error', common.mustCall());
1851cb0ef41Sopenharmony_ci
1861cb0ef41Sopenharmony_ci    d.run(function() {
1871cb0ef41Sopenharmony_ci      d2.run(function() {
1881cb0ef41Sopenharmony_ci        const fs = require('fs');
1891cb0ef41Sopenharmony_ci        fs.exists('/non/existing/file', function onExists(exists) {
1901cb0ef41Sopenharmony_ci          throw new Error('boom!');
1911cb0ef41Sopenharmony_ci        });
1921cb0ef41Sopenharmony_ci      });
1931cb0ef41Sopenharmony_ci    });
1941cb0ef41Sopenharmony_ci  },
1951cb0ef41Sopenharmony_ci];
1961cb0ef41Sopenharmony_ci
1971cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') {
1981cb0ef41Sopenharmony_ci  const testIndex = +process.argv[3];
1991cb0ef41Sopenharmony_ci
2001cb0ef41Sopenharmony_ci  tests[testIndex]();
2011cb0ef41Sopenharmony_ci
2021cb0ef41Sopenharmony_ci} else {
2031cb0ef41Sopenharmony_ci
2041cb0ef41Sopenharmony_ci  tests.forEach(function(test, testIndex) {
2051cb0ef41Sopenharmony_ci    let testCmd = '';
2061cb0ef41Sopenharmony_ci    if (!common.isWindows) {
2071cb0ef41Sopenharmony_ci      // Do not create core files, as it can take a lot of disk space on
2081cb0ef41Sopenharmony_ci      // continuous testing and developers' machines
2091cb0ef41Sopenharmony_ci      testCmd += 'ulimit -c 0 && ';
2101cb0ef41Sopenharmony_ci    }
2111cb0ef41Sopenharmony_ci
2121cb0ef41Sopenharmony_ci    testCmd += `"${process.argv[0]}" --abort-on-uncaught-exception ` +
2131cb0ef41Sopenharmony_ci               `"${process.argv[1]}" child ${testIndex}`;
2141cb0ef41Sopenharmony_ci
2151cb0ef41Sopenharmony_ci    try {
2161cb0ef41Sopenharmony_ci      child_process.execSync(testCmd);
2171cb0ef41Sopenharmony_ci    } catch (e) {
2181cb0ef41Sopenharmony_ci      assert.fail(`Test index ${testIndex} failed: ${e}`);
2191cb0ef41Sopenharmony_ci    }
2201cb0ef41Sopenharmony_ci  });
2211cb0ef41Sopenharmony_ci}
222