Skip to content

Commit

Permalink
feat(MongoMemoryReplSet): remove option "autoStart"
Browse files Browse the repository at this point in the history
- remove option "autoStart"
- remove test to test option "autoStart"
  • Loading branch information
hasezoey committed Oct 12, 2020
1 parent 6dcb12a commit 90ed578
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 47 deletions.
12 changes: 1 addition & 11 deletions packages/mongodb-memory-server-core/src/MongoMemoryReplSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@ export interface MongoMemoryReplSetOptsT {
instanceOpts?: MongoMemoryInstancePropBaseT[];
binary?: MongoBinaryOpts;
replSet?: ReplSetOpts;
/**
* Auto-Start the replSet?
* @default true
*/
autoStart?: boolean;
}

/**
Expand Down Expand Up @@ -121,7 +116,6 @@ export class MongoMemoryReplSet extends EventEmitter {
instanceOpts: MongoMemoryInstancePropBaseT[];
binary: MongoBinaryOpts;
replSet: Required<ReplSetOpts>;
autoStart?: boolean;
};

_state: MongoMemoryReplSetStateEnum = MongoMemoryReplSetStateEnum.stopped;
Expand Down Expand Up @@ -154,10 +148,6 @@ export class MongoMemoryReplSet extends EventEmitter {
if (this.opts.replSet.count <= 0) {
throw new Error('ReplSet Count needs to be 1 or higher!');
}
if (!(opts?.autoStart === false)) {
log('Autostarting MongoMemoryReplSet.');
setImmediate(() => this.start());
}

process.once('beforeExit', this.stop);
}
Expand All @@ -176,7 +166,7 @@ export class MongoMemoryReplSet extends EventEmitter {
* @param opts Options for the ReplSet
*/
static async create(opts: MongoMemoryReplSetOptsT = {}): Promise<MongoMemoryReplSet> {
const replSet = new this({ ...opts, autoStart: false });
const replSet = new this({ ...opts });
await replSet.start();
return replSet;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ jasmine.DEFAULT_TIMEOUT_INTERVAL = 600000;

describe('multi-member replica set', () => {
it('should enter running state', async () => {
const replSet = new MongoMemoryReplSet({ replSet: { count: 3 } });
await replSet.waitUntilRunning();
const replSet = await MongoMemoryReplSet.create({ replSet: { count: 3 } });
expect(replSet.servers.length).toEqual(3);
const uri = await replSet.getUri();
expect(uri.split(',').length).toEqual(3);
Expand All @@ -15,8 +14,7 @@ describe('multi-member replica set', () => {
}, 40000);

it('should be possible to connect replicaset after waitUntilRunning resolveds', async () => {
const replSet = new MongoMemoryReplSet({ replSet: { count: 3 } });
await replSet.waitUntilRunning();
const replSet = await MongoMemoryReplSet.create({ replSet: { count: 3 } });
const uri = await replSet.getUri();

const con = await MongoClient.connect(uri, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ describe('single-member replica set', () => {
],
} as MongoMemoryReplSetOptsT;

const replSetBefore = new MongoMemoryReplSet(opts);
await replSetBefore.waitUntilRunning();
const replSetBefore = await MongoMemoryReplSet.create(opts);

// Write real port to config (because 27017 may be busy, we need to get real port)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
Expand All @@ -45,8 +44,7 @@ describe('single-member replica set', () => {
// this test needs to use the *exact same port* again, otherwise Mongod will throw an error "No host described in new configuration ${newPort} for replica set testset maps to this node"
await sleep(30000);

const replSetAfter = new MongoMemoryReplSet(opts);
await replSetAfter.waitUntilRunning();
const replSetAfter = await MongoMemoryReplSet.create(opts);
await replSetAfter.stop();
}, 600000);
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ jasmine.DEFAULT_TIMEOUT_INTERVAL = 600000;

describe('single server replset', () => {
it('should enter running state', async () => {
const replSet = new MongoMemoryReplSet();
await replSet.waitUntilRunning();
const replSet = await MongoMemoryReplSet.create();
const uri = await replSet.getUri();
expect(uri.split(',').length).toEqual(1);

await replSet.stop();
});

it('should be able to get connection string to specific db', async () => {
const replSet = new MongoMemoryReplSet({});
await replSet.waitUntilRunning();
const replSet = await MongoMemoryReplSet.create();
const uri = await replSet.getUri('other');
expect(uri.split(',').length).toEqual(1);
expect(uri.includes('/other')).toBeTruthy();
Expand All @@ -25,26 +23,15 @@ describe('single server replset', () => {
});

it('should be able to get dbName', async () => {
const opts: any = { autoStart: false, replSet: { dbName: 'static' } };
const replSet = new MongoMemoryReplSet(opts);
const replSet = new MongoMemoryReplSet({ replSet: { dbName: 'static' } });
const dbName = replSet.getDbName();
expect(dbName).toEqual('static');

await replSet.stop();
});

it('should not autostart if autostart: false', async () => {
const replSet = new MongoMemoryReplSet({ autoStart: false });
await new Promise((resolve) => {
setTimeout(resolve, 500);
});

await replSet.stop();
});

it('should be possible to connect replicaset after waitUntilRunning resolves', async () => {
const replSet = new MongoMemoryReplSet();
await replSet.waitUntilRunning();
const replSet = await MongoMemoryReplSet.create();
const uri = await replSet.getUri();

const con = await MongoClient.connect(`${uri}?replicaSet=testset`, {
Expand All @@ -66,7 +53,7 @@ describe('single server replset', () => {
});

it('"waitUntilRunning" should throw an error if _state is not "init"', async () => {
const replSet = new MongoMemoryReplSet({ autoStart: false });
const replSet = new MongoMemoryReplSet();
const timeout = setTimeout(() => {
fail('Timeout - Expected "waitUntilRunning" to throw');
}, 100);
Expand All @@ -83,7 +70,7 @@ describe('single server replset', () => {
});

it('"getUri" should throw an error if _state is not "running"', async () => {
const replSet = new MongoMemoryReplSet({ autoStart: false });
const replSet = new MongoMemoryReplSet();
const timeout = setTimeout(() => {
fail('Timeout - Expected "getUri" to throw');
}, 100);
Expand All @@ -98,7 +85,7 @@ describe('single server replset', () => {
});

it('"getUri" should execute "waitUntilRunning" if state is "init"', async () => {
const replSet = new MongoMemoryReplSet({ autoStart: false });
const replSet = new MongoMemoryReplSet();
const spy = jest.spyOn(replSet, 'waitUntilRunning');
// this case can normally happen if "start" is called without await, and "getUri" directly after and that is awaited
replSet._state = MongoMemoryReplSetStateEnum.init; // artificially set this to init
Expand All @@ -114,7 +101,7 @@ describe('single server replset', () => {
});

it('"start" should throw an error if _state is not "stopped"', async () => {
const replSet = new MongoMemoryReplSet({ autoStart: false });
const replSet = new MongoMemoryReplSet();
const timeout = setTimeout(() => {
fail('Timeout - Expected "start" to throw');
}, 100);
Expand All @@ -132,10 +119,7 @@ describe('single server replset', () => {
});

it('start an replset with instanceOpts', async () => {
const replSet = new MongoMemoryReplSet({
instanceOpts: [{ args: ['--quiet'] }],
autoStart: false,
});
const replSet = new MongoMemoryReplSet({ instanceOpts: [{ args: ['--quiet'] }] });
await replSet.start();

expect(
Expand All @@ -147,7 +131,7 @@ describe('single server replset', () => {
});

it('"waitUntilRunning" should return if state is "running"', async () => {
const replSet = new MongoMemoryReplSet({ autoStart: false });
const replSet = new MongoMemoryReplSet();
const spy = jest.spyOn(replSet, 'once');
replSet._state = MongoMemoryReplSetStateEnum.running; // artificially set this to running to not actually have to start an server (test-speedup)

Expand All @@ -157,7 +141,7 @@ describe('single server replset', () => {
});

it('"_initReplSet" should throw an error if _state is not "init"', async () => {
const replSet = new MongoMemoryReplSet({ autoStart: false });
const replSet = new MongoMemoryReplSet();
const timeout = setTimeout(() => {
fail('Timeout - Expected "_initReplSet" to throw');
}, 100);
Expand All @@ -175,7 +159,7 @@ describe('single server replset', () => {
});

it('"_initReplSet" should throw if server count is 0 or less', async () => {
const replSet = new MongoMemoryReplSet({ autoStart: false });
const replSet = new MongoMemoryReplSet();
const timeout = setTimeout(() => {
fail('Timeout - Expected "_initReplSet" to throw');
}, 100);
Expand Down

0 comments on commit 90ed578

Please sign in to comment.