Skip to content

Commit

Permalink
test(MongoInstance): change test to be more in-depth and consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
hasezoey committed Nov 13, 2023
1 parent 31e3374 commit 624ad39
Showing 1 changed file with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
UnexpectedCloseError,
} from '../errors';
import { assertIsError } from '../../__tests__/testUtils/test_utils';
import { MongoClient } from 'mongodb';
import { MongoClient, MongoServerSelectionError } from 'mongodb';

jest.setTimeout(100000); // 10s

Expand Down Expand Up @@ -292,6 +292,7 @@ describe('MongodbInstance', () => {
await mongod.start();
jest.spyOn(MongoClient, 'connect');
process.kill(mongod.mongodProcess!.pid, 'SIGKILL');
// loop until the mongod process actually exits
while (dbUtil.isAlive(mongod.mongodProcess!.pid)) {
await new Promise<void>((resolve) => setTimeout(resolve, 5));
}
Expand All @@ -302,6 +303,11 @@ describe('MongodbInstance', () => {

test('"kill" should not wait too much to open a connection to a ReplSet instance', async () => {
const gotPort = await getFreePort();

// mock indicating some kind of server selection problem (undetected shutdown / unresponsive)
// so that the ".stop" function does not exit too early
jest.spyOn(MongodbInstance.prototype, 'closeHandler').mockImplementationOnce(() => void 0);

const mongod = new MongodbInstance({
instance: {
replSet: 'testset',
Expand All @@ -311,11 +317,31 @@ describe('MongodbInstance', () => {
},
});
await mongod.start();
mongod.extraConnectionOptions = {
// the following is set to 1s to speed-up the test, instead of the set default of 5s (or the 30s of mongodb default)
serverSelectionTimeoutMS: 1000, // 1 second
};
jest.spyOn(MongoClient, 'connect');
jest.spyOn(MongoClient.prototype, 'db');
jest.spyOn(console, 'warn').mockImplementationOnce(() => void 0);
process.kill(mongod.mongodProcess!.pid, 'SIGKILL');
// loop until the mongod process actually exits
while (dbUtil.isAlive(mongod.mongodProcess!.pid)) {
await new Promise<void>((resolve) => setTimeout(resolve, 5));
}

// mock indicating some kind of server selection problem (undetected shutdown / unresponsive)
// so that the ".stop" function does not exit too early
jest.spyOn(dbUtil, 'isAlive').mockImplementationOnce(() => true);

await mongod.stop();
// connect should be called, but never beyond that (con.db is called next)
expect(MongoClient.connect).toHaveBeenCalledTimes(1);
}, 8000); // the default serverSelectionTimeoutMS is 30s, so waiting for 8s seems ok to catch it
expect(MongoClient.prototype.db).not.toHaveBeenCalled();
// should print a warning about "ECONREFUSED" or "Server selection timed-out"
expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(expect.any(MongoServerSelectionError));
}, 8000); // the default serverSelectionTimeoutMS is 30s, the overwritten config is 5s, so waiting 8s should be fine

it('"_launchMongod" should throw an error if "mongodProcess.pid" is undefined', () => {
const mongod = new MongodbInstance({ instance: { port: 0, dbPath: '' } }); // dummy values - they shouldnt matter
Expand Down

0 comments on commit 624ad39

Please sign in to comment.