Skip to content

Commit

Permalink
refactor(MongoMemoryReplSet): change "getUri" to be sync
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
change "getUri" to be sync (dosnt wait until running anymore)
  • Loading branch information
hasezoey committed Oct 12, 2020
1 parent 18b9a58 commit 13f3f1d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
9 changes: 4 additions & 5 deletions packages/mongodb-memory-server-core/src/MongoMemoryReplSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,21 +243,20 @@ export class MongoMemoryReplSet extends EventEmitter {
}

/**
* Returns a mongodb: URI to connect to a given database.
* Returns an mongodb URI that is setup with all replSet servers
* @param otherDb use a different database than what was set on creation?
*/
async getUri(otherDb?: string | boolean): Promise<string> {
getUri(otherDb?: string | boolean): string {
log('getUri:', this._state);
switch (this._state) {
case MongoMemoryReplSetStateEnum.init:
await this.waitUntilRunning();
break;
case MongoMemoryReplSetStateEnum.running:
break;
case MongoMemoryReplSetStateEnum.init:
case MongoMemoryReplSetStateEnum.stopped:
default:
throw new Error('Replica Set is not running. Use debug for more info.');
}

const dbName: string = isNullOrUndefined(otherDb)
? this._replSetOpts.dbName
: typeof otherDb === 'string'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ describe('multi-member replica set', () => {
it('should enter running state', async () => {
const replSet = await MongoMemoryReplSet.create({ replSet: { count: 3 } });
expect(replSet.servers.length).toEqual(3);
const uri = await replSet.getUri();
const uri = replSet.getUri();
expect(uri.split(',').length).toEqual(3);

await replSet.stop();
}, 40000);

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

const con = await MongoClient.connect(uri, {
useNewUrlParser: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ jasmine.DEFAULT_TIMEOUT_INTERVAL = 600000;
describe('single server replset', () => {
it('should enter running state', async () => {
const replSet = await MongoMemoryReplSet.create();
const uri = await replSet.getUri();
const uri = 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 = await MongoMemoryReplSet.create();
const uri = await replSet.getUri('other');
const uri = replSet.getUri('other');
expect(uri.split(',').length).toEqual(1);
expect(uri.includes('/other')).toBeTruthy();
expect(uri.includes('replicaSet=testset')).toBeTruthy();
Expand All @@ -32,7 +32,7 @@ describe('single server replset', () => {

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

const con = await MongoClient.connect(`${uri}?replicaSet=testset`, {
useNewUrlParser: true,
Expand Down Expand Up @@ -69,37 +69,36 @@ describe('single server replset', () => {
}
});

it('"getUri" should throw an error if _state is not "running"', async () => {
it('"getUri" should throw an error if _state is not "running" (state = "stopped")', async () => {
const replSet = new MongoMemoryReplSet();
const timeout = setTimeout(() => {
fail('Timeout - Expected "getUri" to throw');
}, 100);

try {
await replSet.getUri();
replSet.getUri();
fail('Expected "getUri" to throw');
} catch (err) {
clearTimeout(timeout);
expect(err.message).toEqual('Replica Set is not running. Use debug for more info.');
}
});

it('"getUri" should execute "waitUntilRunning" if state is "init"', async () => {
it('"getUri" should throw an error if _state is not "running" (state = "stopped")', async () => {
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
// @ts-expect-error
replSet._state = MongoMemoryReplSetStateEnum.init; // artificially set this to init
const promise = replSet.getUri();
// @ts-expect-error
replSet._state = MongoMemoryReplSetStateEnum.stopped; // set it back for "start"
await replSet.start();

await promise; // await the promise to make sure nothing got thrown

expect(spy.mock.calls.length).toEqual(1);
replSet._state = MongoMemoryReplSetStateEnum.init;
const timeout = setTimeout(() => {
fail('Timeout - Expected "getUri" to throw');
}, 100);

await replSet.stop();
try {
replSet.getUri();
fail('Expected "getUri" to throw');
} catch (err) {
clearTimeout(timeout);
expect(err.message).toEqual('Replica Set is not running. Use debug for more info.');
}
});

it('"start" should throw an error if _state is not "stopped"', async () => {
Expand Down

0 comments on commit 13f3f1d

Please sign in to comment.