Skip to content

Commit fbd0747

Browse files
committed
fix(MongoInstance::closeHandler): emit "instanceError" on non-0 (or non-12) code
fixes #693
1 parent ce6462e commit fbd0747

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

packages/mongodb-memory-server-core/src/util/MongoInstance.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ import {
1313
import { lt } from 'semver';
1414
import { EventEmitter } from 'events';
1515
import { MongoClient, MongoClientOptions, MongoNetworkError } from 'mongodb';
16-
import { KeyFileMissingError, StartBinaryFailedError, StdoutInstanceError } from './errors';
16+
import {
17+
KeyFileMissingError,
18+
StartBinaryFailedError,
19+
StdoutInstanceError,
20+
UnexpectedCloseError,
21+
} from './errors';
1722

1823
// ignore the nodejs warning for coverage
1924
/* istanbul ignore next */
@@ -508,6 +513,7 @@ export class MongoInstance extends EventEmitter implements ManagerBase {
508513
// https://docs.mongodb.com/manual/reference/exit-codes/#12
509514
if ((process.platform === 'win32' && code != 12 && code != 0) || code != 0) {
510515
this.debug('closeHandler: Mongod instance closed with an non-0 (or non 12 on windows) code!');
516+
this.emit(MongoInstanceEvents.instanceError, new UnexpectedCloseError(code, signal));
511517
}
512518

513519
this.debug(`closeHandler: code: "${code}", signal: "${signal}"`);

packages/mongodb-memory-server-core/src/util/__tests__/MongoInstance.test.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as dbUtil from '../utils';
44
import MongodbInstance, { MongoInstanceEvents } from '../MongoInstance';
55
import resolveConfig, { ResolveConfigVariables } from '../resolveConfig';
66
import getPort from 'get-port';
7-
import { StartBinaryFailedError, StdoutInstanceError } from '../errors';
7+
import { StartBinaryFailedError, StdoutInstanceError, UnexpectedCloseError } from '../errors';
88
import { assertIsError } from '../../__tests__/testUtils/test_utils';
99

1010
jest.setTimeout(100000); // 10s
@@ -331,8 +331,43 @@ describe('MongodbInstance', () => {
331331
events.clear();
332332
mongod.closeHandler(null, 'SIG');
333333

334-
expect(events.size).toEqual(1);
334+
expect(events.size).toEqual(2);
335335
expect(events.get(MongoInstanceEvents.instanceClosed)).toEqual([null, 'SIG']);
336+
337+
const event = events.get(MongoInstanceEvents.instanceError)?.[0];
338+
expect(event).toBeInstanceOf(UnexpectedCloseError);
339+
assertIsError(event); // has to be used, because there is not typeguard from "expect(variable).toBeInstanceOf"
340+
expect(event.message).toMatchSnapshot();
341+
}
342+
});
343+
344+
it('"closeHandler" should emit "instanceError" with non-0 or non-12 code', () => {
345+
// test code non-0
346+
{
347+
events.clear();
348+
mongod.closeHandler(1, null);
349+
350+
expect(events.size).toEqual(2);
351+
expect(events.get(MongoInstanceEvents.instanceClosed)).toEqual([1, null]);
352+
353+
const event = events.get(MongoInstanceEvents.instanceError)?.[0];
354+
expect(event).toBeInstanceOf(UnexpectedCloseError);
355+
assertIsError(event); // has to be used, because there is not typeguard from "expect(variable).toBeInstanceOf"
356+
expect(event.message).toMatchSnapshot();
357+
}
358+
359+
// test signal
360+
{
361+
events.clear();
362+
mongod.closeHandler(null, 'SIG');
363+
364+
expect(events.size).toEqual(2);
365+
expect(events.get(MongoInstanceEvents.instanceClosed)).toEqual([null, 'SIG']);
366+
367+
const event = events.get(MongoInstanceEvents.instanceError)?.[0];
368+
expect(event).toBeInstanceOf(UnexpectedCloseError);
369+
assertIsError(event); // has to be used, because there is not typeguard from "expect(variable).toBeInstanceOf"
370+
expect(event.message).toMatchSnapshot();
336371
}
337372
});
338373

packages/mongodb-memory-server-core/src/util/__tests__/__snapshots__/MongoInstance.test.ts.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ exports[`MongodbInstance prepareCommandArgs should throw an error if no dbpath i
66

77
exports[`MongodbInstance prepareCommandArgs should throw an error if no port is provided 1`] = `"\\"instanceOpts.port\\" is required to be set!"`;
88

9+
exports[`MongodbInstance test events "closeHandler" should emit "instanceClosed" 1`] = `"Instance closed unexpectedly with code \\"null\\" and signal \\"SIG\\""`;
10+
11+
exports[`MongodbInstance test events "closeHandler" should emit "instanceError" with non-0 or non-12 code 1`] = `"Instance closed unexpectedly with code \\"1\\" and signal \\"null\\""`;
12+
13+
exports[`MongodbInstance test events "closeHandler" should emit "instanceError" with non-0 or non-12 code 2`] = `"Instance closed unexpectedly with code \\"null\\" and signal \\"SIG\\""`;
14+
915
exports[`MongodbInstance test events checkErrorInLine() should emit "instanceError" when shared libraries fail to load 1`] = `"Instance failed to start because a library is missing or cannot be opened: \\"libcrypto.so.1.1\\""`;
1016

1117
exports[`MongodbInstance test events stdoutHandler() should emit "instanceError" when "excepetion in initAndListen" is thrown DBException in initAndListen 1`] = `

0 commit comments

Comments
 (0)