Skip to content

Commit d2873ea

Browse files
committed
test: fix brroken test if 27017 port is already busy
1 parent e19a738 commit d2873ea

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

packages/mongodb-memory-server-core/src/__tests__/replset-single-restart-test.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import MongoMemoryReplSet from '../MongoMemoryReplSet';
1+
import MongoMemoryReplSet, { MongoMemoryReplSetOptsT } from '../MongoMemoryReplSet';
22
import * as tmp from 'tmp';
3+
import getPort from 'get-port';
34

45
let tmpDir: tmp.DirResult;
56
beforeEach(() => {
@@ -16,7 +17,7 @@ const sleep = (ms: number) => {
1617

1718
describe('single-member replica set', () => {
1819
it('should start multiple times', async () => {
19-
const opts: any = {
20+
const opts = {
2021
replSet: {
2122
storageEngine: 'wiredTiger',
2223
},
@@ -26,11 +27,17 @@ describe('single-member replica set', () => {
2627
dbPath: tmpDir.name,
2728
},
2829
],
29-
};
30+
} as MongoMemoryReplSetOptsT;
3031

3132
const replSetBefore = new MongoMemoryReplSet(opts);
3233
await replSetBefore.waitUntilRunning();
34+
35+
// Write real port to config (because 27017 may be busy, we need to get real port)
36+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
37+
opts.instanceOpts![0].port = await replSetBefore.servers[0].getPort();
38+
3339
await replSetBefore.stop();
40+
3441
/**
3542
* get-port has a portlocking-feature that keeps ports locked for
3643
* "a minimum of 15 seconds and a maximum of 30 seconds before being released again"

0 commit comments

Comments
 (0)