X Tutup
The Wayback Machine - https://web.archive.org/web/20230218071239/https://github.com/nodejs/node/commit/05df701e70
Skip to content
Permalink
Browse files
test: remove common.disableCrashOnUnhandledRejection
Use the --unhandled-rejections=none CLI flag instead.

PR-URL: #38210
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
targos committed Apr 14, 2021
1 parent 3377eb9 commit 05df701
Show file tree
Hide file tree
Showing 17 changed files with 7 additions and 47 deletions.
@@ -250,11 +250,9 @@ countdown.dec(); // The countdown callback will be invoked now.

When writing tests involving promises, it is generally good to wrap the
`onFulfilled` handler, otherwise the test could successfully finish if the
promise never resolves (pending promises do not keep the event loop alive). The
`common` module automatically adds a handler that makes the process crash - and
hence, the test fail - in the case of an `unhandledRejection` event. It is
possible to disable it with `common.disableCrashOnUnhandledRejection()` if
needed.
promise never resolves (pending promises do not keep the event loop alive).
Node.js automatically crashes - and hence, the test fails - in the case of an
`unhandledRejection` event.

```js
const common = require('../common');
@@ -61,14 +61,6 @@ On non-Windows platforms, this always returns `true`.

Creates a 10 MB file of all null characters.

### `disableCrashOnUnhandledRejection()`

Removes the `process.on('unhandledRejection')` handler that crashes the process
after a tick. The handler is useful for tests that use Promises and need to make
sure no unexpected rejections occur, because currently they result in silent
failures. However, it is useful in some rare cases to disable it, for example if
the `unhandledRejection` hook is directly used by the test.

### `enoughTestCpu`

* [&lt;boolean>][]
@@ -628,10 +628,6 @@ function getBufferSources(buf) {
return [...getArrayBufferViews(buf), new Uint8Array(buf).buffer];
}

function disableCrashOnUnhandledRejection() {
process.on('unhandledRejection', () => {});
}

function getTTYfd() {
// Do our best to grab a tty fd.
const tty = require('tty');
@@ -732,7 +728,6 @@ const common = {
canCreateSymLink,
childShouldThrowAndAbort,
createZeroFilledFile,
disableCrashOnUnhandledRejection,
expectsError,
expectWarning,
gcUntil,
@@ -46,7 +46,6 @@ const {
skipIf32Bits,
getArrayBufferViews,
getBufferSources,
disableCrashOnUnhandledRejection,
getTTYfd,
runWithInvalidFD
} = common;
@@ -92,7 +91,6 @@ export {
skipIf32Bits,
getArrayBufferViews,
getBufferSources,
disableCrashOnUnhandledRejection,
getTTYfd,
runWithInvalidFD,
createRequire
@@ -25,7 +25,6 @@ function spawnChildProcess(inspectorFlags, scriptContents, scriptFile) {
const handler = tearDown.bind(null, child);
process.on('exit', handler);
process.on('uncaughtException', handler);
common.disableCrashOnUnhandledRejection();
process.on('unhandledRejection', handler);
process.on('SIGINT', handler);

@@ -1,9 +1,8 @@
'use strict';

const common = require('../common');
require('../common');

if (process.argv[2] === 'async') {
common.disableCrashOnUnhandledRejection();
async function fn() {
fn();
throw new Error();
@@ -16,7 +15,7 @@ const { spawnSync } = require('child_process');

const ret = spawnSync(
process.execPath,
['--stack_size=150', __filename, 'async'],
['--unhandled-rejections=none', '--stack_size=150', __filename, 'async'],
{ maxBuffer: Infinity }
);
assert.strictEqual(ret.status, 0,
@@ -2,10 +2,7 @@

import {
mustCall,
disableCrashOnUnhandledRejection
} from '../common/index.mjs';

disableCrashOnUnhandledRejection();

process.on('unhandledRejection', mustCall());
Promise.reject(new Error('should not be fatal error'));
@@ -2,7 +2,6 @@
const common = require('../common');

// This test verifies that DEP0018 does not occur when rejections are handled.
common.disableCrashOnUnhandledRejection();
process.on('warning', common.mustNotCall());
process.on('unhandledRejection', common.mustCall());
Promise.reject(new Error());
@@ -5,8 +5,6 @@ const common = require('../common');
const Countdown = require('../common/countdown');
const assert = require('assert');

common.disableCrashOnUnhandledRejection();

// Verify that unhandled rejections always trigger uncaught exceptions instead
// of triggering unhandled rejections.

@@ -4,8 +4,6 @@
const common = require('../common');
const assert = require('assert');

common.disableCrashOnUnhandledRejection();

// Verify that ignoring unhandled rejection works fine and that no warning is
// logged even though there is no unhandledRejection hook attached.

@@ -3,8 +3,6 @@

const common = require('../common');

common.disableCrashOnUnhandledRejection();

// Verify that ignoring unhandled rejection works fine and that no warning is
// logged.

@@ -5,8 +5,6 @@ const common = require('../common');
const Countdown = require('../common/countdown');
const assert = require('assert');

common.disableCrashOnUnhandledRejection();

// Verify that the unhandledRejection handler prevents triggering
// uncaught exceptions

@@ -3,8 +3,6 @@

const common = require('../common');

common.disableCrashOnUnhandledRejection();

// Verify that ignoring unhandled rejection works fine and that no warning is
// logged.

@@ -1,8 +1,7 @@
// Flags: --unhandled-rejections=none
'use strict';
const common = require('../common');

common.disableCrashOnUnhandledRejection();

function throwErr() {
throw new Error('Error from proxy');
}
@@ -1,10 +1,9 @@
// Flags: --unhandled-rejections=none
'use strict';
const common = require('../common');
const assert = require('assert');
const { inspect } = require('util');

common.disableCrashOnUnhandledRejection();

const asyncTest = (function() {
let asyncTestsEnabled = false;
let asyncTestLastCheck;
@@ -643,7 +642,6 @@ asyncTest('Throwing an error inside a rejectionHandled handler goes to' +
' unhandledException, and does not cause .catch() to throw an ' +
'exception', function(done) {
clean();
common.disableCrashOnUnhandledRejection();
const e = new Error();
const e2 = new Error();
const tearDownException = setupException(function(err) {
@@ -2,8 +2,6 @@
'use strict';
const common = require('../common');

common.disableCrashOnUnhandledRejection();

const expectedValueWarning = ['Symbol()'];
const expectedPromiseWarning = ['Unhandled promise rejection. ' +
'This error originated either by throwing ' +
@@ -6,8 +6,6 @@ const fs = require('fs');
const path = require('path');
const tmpdir = require('../common/tmpdir');

common.disableCrashOnUnhandledRejection();

if (process.argv[2] === 'child') {
const p = Promise.reject(1); // Handled later
Promise.reject(2); // Unhandled

0 comments on commit 05df701

Please sign in to comment.
X Tutup