Skip to content

Commit

Permalink
feat: still run "stop" even if state is "stopped"
Browse files Browse the repository at this point in the history
  • Loading branch information
hasezoey committed Jan 15, 2022
1 parent 1c2e09c commit ca38110
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ export class MongoMemoryReplSet extends EventEmitter implements ManagerAdvanced
log(`stop: called by ${isNullOrUndefined(process.exitCode) ? 'manual' : 'process exit'}`);

if (this._state === MongoMemoryReplSetStates.stopped) {
return false;
log('stop: state is "stopped", trying to stop / kill anyway');
}

const bool = await Promise.all(this.servers.map((s) => s.stop(false)))
Expand Down
6 changes: 2 additions & 4 deletions packages/mongodb-memory-server-core/src/MongoMemoryServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,13 +477,11 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
}

if (this._state === MongoMemoryServerStates.stopped) {
this.debug(`stop: state is "stopped", so already stopped`);

return false;
this.debug('stop: state is "stopped", trying to stop / kill anyway');
}

this.debug(
`stop: Stopping MongoDB server on port ${this._instanceInfo.port} with pid ${this._instanceInfo.instance.mongodProcess?.pid}` // "undefined" would say more than ""
`stop: Stopping MongoDB server on port ${this._instanceInfo.port} with pid ${this._instanceInfo.instance?.mongodProcess?.pid}` // "undefined" would say more than ""
);
await this._instanceInfo.instance.stop();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,16 +476,21 @@ describe('MongoMemoryServer', () => {
expect(utils.isNullOrUndefined).toHaveBeenCalledTimes(1);
});

it('should return "true" if instance is already stopped', async () => {
it('should still run "stop" even if state is already "stopped"', async () => {
const mongoServer = new MongoMemoryServer();
const instance = new MongoInstance({});
const instanceStopSpy = jest
.spyOn(instance, 'stop')
.mockImplementation(() => Promise.resolve(true));
// @ts-expect-error because "_instanceInfo" is protected
mongoServer._instanceInfo = {};
mongoServer._instanceInfo = { instance: instance };
// @ts-expect-error because "_state" is protected
mongoServer._state = MongoMemoryServerStates.stopped;
jest.spyOn(utils, 'isNullOrUndefined');
jest.spyOn(utils, 'assertion');

expect(await mongoServer.stop()).toEqual(false);
expect(await mongoServer.stop(false)).toEqual(true);
expect(instanceStopSpy).toHaveBeenCalledTimes(1);
expect(utils.isNullOrUndefined).toHaveBeenCalledTimes(1);
expect(utils.assertion).not.toHaveBeenCalled();
});
Expand Down

0 comments on commit ca38110

Please sign in to comment.