| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9817e40 commit bea1ee8
117 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -225,34 +225,24 @@ countdown.dec(); // The countdown callback will be invoked now. | |||
| 225 | 225 | ||
| 226 | 226 | #### Testing promises | |
| 227 | 227 | ||
| 228 | - When writing tests involving promises, either make sure that the | ||
| 229 | - `onFulfilled` or the `onRejected` handler is wrapped in | ||
| 230 | - `common.mustCall()` or `common.mustNotCall()` accordingly, or | ||
| 231 | - call `common.crashOnUnhandledRejection()` in the top level of the | ||
| 232 | - test to make sure that unhandled rejections would result in a test | ||
| 233 | - failure. For example: | ||
| 228 | + When writing tests involving promises, it is generally good to wrap the | ||
| 229 | + `onFulfilled` handler, otherwise the test could successfully finish if the | ||
| 230 | + promise never resolves (pending promises do not keep the event loop alive). The | ||
| 231 | + `common` module automatically adds a handler that makes the process crash - and | ||
| 232 | + hence, the test fail - in the case of an `unhandledRejection` event. It is | ||
| 233 | + possible to disable it with `common.disableCrashOnUnhandledRejection()` if | ||
| 234 | + needed. | ||
| 234 | 235 | ||
| 235 | 236 | ```javascript | |
| 236 | 237 | const common = require('../common'); | |
| 237 | 238 | const assert = require('assert'); | |
| 238 | 239 | const fs = require('fs').promises; | |
| 239 | 240 | ||
| 240 | - // Use `common.crashOnUnhandledRejection()` to make sure unhandled rejections | ||
| 241 | - // will fail the test. | ||
| 242 | - common.crashOnUnhandledRejection(); | ||
| 243 | - | ||
| 244 | - // Or, wrap the `onRejected` handler in `common.mustNotCall()`. | ||
| 245 | - fs.writeFile('test-file', 'test').catch(common.mustNotCall()); | ||
| 246 | - | ||
| 247 | - // Or, wrap the `onFulfilled` handler in `common.mustCall()`. | ||
| 248 | - // If there are assertions in the `onFulfilled` handler, wrap | ||
| 249 | - // the next `onRejected` handler in `common.mustNotCall()` | ||
| 250 | - // to handle potential failures. | ||
| 241 | + // Wrap the `onFulfilled` handler in `common.mustCall()`. | ||
| 251 | 242 | fs.readFile('test-file').then( | |
| 252 | 243 | common.mustCall( | |
| 253 | 244 | (content) => assert.strictEqual(content.toString(), 'test2') | |
| 254 | - )) | ||
| 255 | - .catch(common.mustNotCall()); | ||
| 245 | + )); | ||
| 256 | 246 | ``` | |
| 257 | 247 | ||
| 258 | 248 | ### Flags | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,8 +7,6 @@ const common = require('../../common'); | |||
| 7 | 7 | const assert = require('assert'); | |
| 8 | 8 | const test_promise = require(`./build/${common.buildType}/test_promise`); | |
| 9 | 9 | ||
| 10 | - common.crashOnUnhandledRejection(); | ||
| 11 | - | ||
| 12 | 10 | // A resolution | |
| 13 | 11 | { | |
| 14 | 12 | const expected_result = 42; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,8 +12,6 @@ const expectedArray = (function(arrayLength) { | |||
| 12 | 12 | return result; | |
| 13 | 13 | })(binding.ARRAY_LENGTH); | |
| 14 | 14 | ||
| 15 | - common.crashOnUnhandledRejection(); | ||
| 16 | - | ||
| 17 | 15 | // Handle the rapid teardown test case as the child process. We unref the | |
| 18 | 16 | // thread-safe function after we have received two values. This causes the | |
| 19 | 17 | // process to exit and the environment cleanup handler to be invoked. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,8 +4,6 @@ const common = require('../../common'); | |||
| 4 | 4 | const assert = require('assert'); | |
| 5 | 5 | const { testResolveAsync } = require(`./build/${common.buildType}/binding`); | |
| 6 | 6 | ||
| 7 | - common.crashOnUnhandledRejection(); | ||
| 8 | - | ||
| 9 | 7 | let called = false; | |
| 10 | 8 | testResolveAsync().then(() => { called = true; }); | |
| 11 | 9 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,8 +9,6 @@ const makeCallback = binding.makeCallback; | |||
| 9 | 9 | // Make sure this is run in the future. | |
| 10 | 10 | const mustCallCheckDomains = common.mustCall(checkDomains); | |
| 11 | 11 | ||
| 12 | - common.crashOnUnhandledRejection(); | ||
| 13 | - | ||
| 14 | 12 | // Make sure that using MakeCallback allows the error to propagate. | |
| 15 | 13 | assert.throws(function() { | |
| 16 | 14 | makeCallback({}, function() { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,8 +8,6 @@ const { checkInvocations } = require('./hook-checks'); | |||
| 8 | 8 | if (!common.isMainThread) | |
| 9 | 9 | common.skip('Worker bootstrapping works differently -> different async IDs'); | |
| 10 | 10 | ||
| 11 | - common.crashOnUnhandledRejection(); | ||
| 12 | - | ||
| 13 | 11 | const p = new Promise(common.mustCall(function executor(resolve, reject) { | |
| 14 | 12 | resolve(5); | |
| 15 | 13 | })); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,8 +9,6 @@ const { checkInvocations } = require('./hook-checks'); | |||
| 9 | 9 | if (!common.isMainThread) | |
| 10 | 10 | common.skip('Worker bootstrapping works differently -> different async IDs'); | |
| 11 | 11 | ||
| 12 | - common.crashOnUnhandledRejection(); | ||
| 13 | - | ||
| 14 | 12 | const hooks = initHooks(); | |
| 15 | 13 | ||
| 16 | 14 | hooks.enable(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -55,18 +55,19 @@ symlinks | |||
| 55 | 55 | ([SeCreateSymbolicLinkPrivilege](https://msdn.microsoft.com/en-us/library/windows/desktop/bb530716(v=vs.85).aspx)). | |
| 56 | 56 | On non-Windows platforms, this always returns `true`. | |
| 57 | 57 | ||
| 58 | - ### crashOnUnhandledRejection() | ||
| 59 | - | ||
| 60 | - Installs a `process.on('unhandledRejection')` handler that crashes the process | ||
| 61 | - after a tick. This is useful for tests that use Promises and need to make sure | ||
| 62 | - no unexpected rejections occur, because currently they result in silent | ||
| 63 | - failures. | ||
| 64 | - | ||
| 65 | 58 | ### ddCommand(filename, kilobytes) | |
| 66 | 59 | * return [<Object>] | |
| 67 | 60 | ||
| 68 | 61 | Platform normalizes the `dd` command | |
| 69 | 62 | ||
| 63 | + ### disableCrashOnUnhandledRejection() | ||
| 64 | + | ||
| 65 | + Removes the `process.on('unhandledRejection')` handler that crashes the process | ||
| 66 | + after a tick. The handler is useful for tests that use Promises and need to make | ||
| 67 | + sure no unexpected rejections occur, because currently they result in silent | ||
| 68 | + failures. However, it is useful in some rare cases to disable it, for example if | ||
| 69 | + the `unhandledRejection` hook is directly used by the test. | ||
| 70 | + | ||
| 70 | 71 | ### enoughTestMem | |
| 71 | 72 | * [<boolean>] | |
| 72 | 73 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -815,9 +815,10 @@ exports.getBufferSources = function getBufferSources(buf) { | |||
| 815 | 815 | }; | |
| 816 | 816 | ||
| 817 | 817 | // Crash the process on unhandled rejections. | |
| 818 | - exports.crashOnUnhandledRejection = function() { | ||
| 819 | - process.on('unhandledRejection', | ||
| 820 | - (err) => process.nextTick(() => { throw err; })); | ||
| 818 | + const crashOnUnhandledRejection = (err) => { throw err; }; | ||
| 819 | + process.on('unhandledRejection', crashOnUnhandledRejection); | ||
| 820 | + exports.disableCrashOnUnhandledRejection = function() { | ||
| 821 | + process.removeListener('unhandledRejection', crashOnUnhandledRejection); | ||
| 821 | 822 | }; | |
| 822 | 823 | ||
| 823 | 824 | exports.getTTYfd = function getTTYfd() { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -52,7 +52,7 @@ const { | |||
| 52 | 52 | skipIf32Bits, | |
| 53 | 53 | getArrayBufferViews, | |
| 54 | 54 | getBufferSources, | |
| 55 | - crashOnUnhandledRejection, | ||
| 55 | + disableCrashOnUnhandledRejection, | ||
| 56 | 56 | getTTYfd, | |
| 57 | 57 | runWithInvalidFD, | |
| 58 | 58 | hijackStdout, | |
@@ -112,7 +112,7 @@ export { | |||
| 112 | 112 | skipIf32Bits, | |
| 113 | 113 | getArrayBufferViews, | |
| 114 | 114 | getBufferSources, | |
| 115 | - crashOnUnhandledRejection, | ||
| 115 | + disableCrashOnUnhandledRejection, | ||
| 116 | 116 | getTTYfd, | |
| 117 | 117 | runWithInvalidFD, | |
| 118 | 118 | hijackStdout, | |
| Back | FazBrowse Home | New Git URL |
0 commit comments