Skip to content

Commit

Permalink
refactor(MongoMemoryServer): start: remove first ".catch"
Browse files Browse the repository at this point in the history
- remove first ".catch" function to restart the server if address is already in use
(removed because the error it was checking for didnt exist anymore,
and "getPort" already checks if the address is in use")
- modify test to test this behaviour
  • Loading branch information
hasezoey committed Oct 7, 2020
1 parent 5c80e4e commit fafaa29
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
20 changes: 6 additions & 14 deletions packages/mongodb-memory-server-core/src/MongoMemoryServer.ts
Expand Up @@ -77,20 +77,12 @@ export class MongoMemoryServer {
);
}

this.instanceInfo = await this._startUpInstance()
.catch((err) => {
if (err.message === 'Mongod shutting down' || err === 'Mongod shutting down') {
log(`Mongodb did not start. Trying to start on another port one more time...`);
return this._startUpInstance();
}
throw err; // give error to next handler
})
.catch((err) => {
if (!debug.enabled('MongoMS:MongoMemoryServer')) {
console.warn('Starting the instance failed, enable debug for more infomation');
}
throw err;
});
this.instanceInfo = await this._startUpInstance().catch((err) => {
if (!debug.enabled('MongoMS:MongoMemoryServer')) {
console.warn('Starting the instance failed, enable debug for more infomation');
}
throw err;
});

return true;
}
Expand Down
Expand Up @@ -21,26 +21,25 @@ describe('MongoMemoryServer', () => {
expect(mongoServer._startUpInstance).toHaveBeenCalledTimes(1);
});

it('_startUpInstance should be called a second time if an error is thrown on the first call and assign the current port to nulll', async () => {
const mongoServer = new MongoMemoryServer({
instance: {
port: 123,
},
it('"_startUpInstance" should use an different port if address is already in use (use same port for 2 servers)', async () => {
const mongoServer1 = await MongoMemoryServer.create({
instance: { port: 27444 },
});

jest
.spyOn(mongoServer, '_startUpInstance')
.mockRejectedValueOnce(new Error('Mongod shutting down'))
// @ts-expect-error expect an error here rather than an "as any"
.mockResolvedValueOnce({});
const mongoServer2 = await MongoMemoryServer.create({
instance: { port: mongoServer1.getPort() },
});

await expect(mongoServer.start()).resolves.toEqual(true);
expect(mongoServer1.getInstanceInfo()).toBeDefined();
expect(mongoServer2.getInstanceInfo()).toBeDefined();
expect(mongoServer1.getPort()).not.toEqual(mongoServer2.getPort());

expect(mongoServer._startUpInstance).toHaveBeenCalledTimes(2);
await mongoServer1.stop();
await mongoServer2.stop();
});

it('should throw an error if _startUpInstance throws an unknown error', async () => {
console.warn = jest.fn(); // mock it to prevent writing to console
jest.spyOn(console, 'warn').mockImplementationOnce(() => void 0);

const mongoServer = new MongoMemoryServer({
instance: {
Expand All @@ -53,6 +52,7 @@ describe('MongoMemoryServer', () => {
await expect(mongoServer.start()).rejects.toThrow('unknown error');

expect(mongoServer._startUpInstance).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledTimes(1);
});
});

Expand Down

0 comments on commit fafaa29

Please sign in to comment.