Skip to content

Commit 79328e3

Browse files
committed
test(NODE-6804): convert unhandled rejections into uncaught exceptions
1 parent 398e361 commit 79328e3

File tree

11 files changed

+105
-78
lines changed

11 files changed

+105
-78
lines changed

.mocharc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = {
77
require: [
88
'source-map-support/register',
99
'ts-node/register',
10+
'test/tools/runner/throw_rejections.cjs',
1011
'test/tools/runner/chai_addons.ts',
1112
'test/tools/runner/ee_checker.ts'
1213
],
@@ -17,5 +18,5 @@ module.exports = {
1718
reporter: 'test/tools/reporter/mongodb_reporter.js',
1819
sort: true,
1920
color: true,
20-
'node-option': Number(major) >= 23 ? ['no-experimental-strip-types'] : undefined
21+
'node-option': Number(major) >= 23 ? ['no-experimental-strip-types'] : undefined
2122
};

package-lock.json

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
"@types/whatwg-url": "^11.0.5",
8282
"@typescript-eslint/eslint-plugin": "8.4.0",
8383
"@typescript-eslint/parser": "8.4.0",
84+
"aws4": "^1.13.2",
8485
"chai": "^4.4.1",
8586
"chai-subset": "^1.6.0",
8687
"chalk": "^4.1.2",

src/sdam/server.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {
4848
maxWireVersion,
4949
type MongoDBNamespace,
5050
noop,
51+
squashError,
5152
supportsRetryableWrites
5253
} from '../utils';
5354
import { throwIfWriteConcernError } from '../write_concern';
@@ -345,10 +346,8 @@ export class Server extends TypedEventEmitter<ServerEvents> {
345346
operationError instanceof MongoError &&
346347
operationError.code === MONGODB_ERROR_CODES.Reauthenticate
347348
) {
348-
reauthPromise = this.pool.reauthenticate(conn).catch(error => {
349-
reauthPromise = null;
350-
throw error;
351-
});
349+
// TODO: what do we do with this error? the signal has aborted so where should it be delivered?
350+
reauthPromise = this.pool.reauthenticate(conn).then(undefined, squashError);
352351

353352
await abortable(reauthPromise, options);
354353
reauthPromise = null; // only reachable if reauth succeeds

test/integration/node-specific/abort_signal.test.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -751,9 +751,10 @@ describe('AbortSignal support', () => {
751751
...args
752752
) {
753753
if (args[1].find != null) {
754+
await sleep(1);
754755
commandStub.restore();
755756
controller.abort();
756-
throw new ReAuthenticationError({});
757+
throw new ReAuthenticationError({ message: 'This is a fake reauthentication error' });
757758
}
758759
return commandStub.wrappedMethod.apply(this, args);
759760
});
@@ -793,7 +794,7 @@ describe('AbortSignal support', () => {
793794
beforeEach(() => {
794795
sinon.stub(ConnectionPool.prototype, 'reauthenticate').callsFake(async function () {
795796
await sleep(1000);
796-
throw new Error();
797+
throw new Error('Rejecting reauthenticate for testing');
797798
});
798799
});
799800

test/manual/mocharc.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ const [major] = process.versions.node.split('.');
44

55
/** @type {import("mocha").MochaOptions} */
66
module.exports = {
7-
require: ['ts-node/register', 'test/tools/runner/chai_addons.ts'],
7+
require: [
8+
'ts-node/register',
9+
'test/tools/runner/throw_rejections.cjs',
10+
'test/tools/runner/chai_addons.ts'
11+
],
812
reporter: 'test/tools/reporter/mongodb_reporter.js',
913
failZero: true,
1014
color: true,

test/mocha_lambda.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ const [major] = process.versions.node.split('.');
44

55
/** @type {import("mocha").MochaOptions} */
66
module.exports = {
7-
require: ['test/integration/node-specific/examples/setup.js'],
7+
require: [
8+
'test/tools/runner/throw_rejections.cjs',
9+
'test/integration/node-specific/examples/setup.js'
10+
],
811
extension: ['js'],
912
ui: 'test/tools/runner/metadata_ui.js',
1013
recursive: true,

test/mocha_mongodb.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = {
77
require: [
88
'source-map-support/register',
99
'ts-node/register',
10+
'test/tools/runner/throw_rejections.cjs',
1011
'test/tools/runner/chai_addons.ts',
1112
'test/tools/runner/ee_checker.ts',
1213
'test/tools/runner/hooks/configuration.ts',
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// eslint-disable-next-line @typescript-eslint/no-require-imports
2+
const process = require('process');
3+
4+
process.on('unhandledRejection', error => {
5+
throw error;
6+
});

test/unit/assorted/optional_require.test.js

-69
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { expect } from 'chai';
2+
import { existsSync } from 'fs';
3+
import { resolve } from 'path';
4+
5+
import {
6+
AuthContext,
7+
compress,
8+
GSSAPI,
9+
HostAddress,
10+
MongoDBAWS,
11+
MongoMissingDependencyError
12+
} from '../../mongodb';
13+
14+
function moduleExistsSync(moduleName) {
15+
return existsSync(resolve(__dirname, `../../../node_modules/${moduleName}`));
16+
}
17+
18+
describe('optionalRequire', function () {
19+
describe('Snappy', function () {
20+
it('should error if not installed', async function () {
21+
const moduleName = 'snappy';
22+
if (moduleExistsSync(moduleName)) {
23+
return this.skip();
24+
}
25+
26+
const error = await compress(
27+
{ zlibCompressionLevel: 0, agreedCompressor: 'snappy' },
28+
Buffer.alloc(1)
29+
).then(
30+
() => null,
31+
e => e
32+
);
33+
34+
expect(error).to.be.instanceOf(MongoMissingDependencyError);
35+
});
36+
});
37+
38+
describe('Kerberos', function () {
39+
it('should error if not installed', async function () {
40+
const moduleName = 'kerberos';
41+
if (moduleExistsSync(moduleName)) {
42+
return this.skip();
43+
}
44+
const gssapi = new GSSAPI();
45+
46+
const error = await gssapi
47+
.auth(new AuthContext(null, true, { hostAddress: new HostAddress('a'), credentials: true }))
48+
.then(
49+
() => null,
50+
e => e
51+
);
52+
53+
expect(error).to.be.instanceOf(MongoMissingDependencyError);
54+
});
55+
});
56+
57+
describe('aws4', function () {
58+
it('should error if not installed', async function () {
59+
const moduleName = 'aws4';
60+
if (moduleExistsSync(moduleName)) {
61+
return this.skip();
62+
}
63+
const mdbAWS = new MongoDBAWS();
64+
65+
const error = await mdbAWS.auth(
66+
new AuthContext({ hello: { maxWireVersion: 9 } }, true, null)
67+
);
68+
69+
expect(error).to.be.instanceOf(MongoMissingDependencyError);
70+
});
71+
});
72+
});

0 commit comments

Comments
 (0)