Skip to content

Commit fc14ef6

Browse files
committed
chore: replset for test servers and better cleanup
1 parent c8e9dbd commit fc14ef6

File tree

9 files changed

+169
-137
lines changed

9 files changed

+169
-137
lines changed

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
"package-compass": "npm run package-compass --workspace=mongodb-compass --",
3333
"package-compass-debug": "npm run package-compass-debug --workspace=mongodb-compass --",
3434
"package-compass-nocompile": "npm run package-compass-nocompile --workspace=mongodb-compass --",
35-
"prestart": "npm run compile --workspace=@mongodb-js/webpack-config-compass",
36-
"prestart-web": "npm run prestart",
3735
"start": "npm run start --workspace=mongodb-compass",
3836
"start-web": "npm run start --workspace=@mongodb-js/compass-web",
3937
"test": "lerna run test --concurrency 1 --stream",

packages/compass-e2e-tests/helpers/compass.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { execFile } from 'child_process';
88
import type { ExecFileOptions, ExecFileException } from 'child_process';
99
import { promisify } from 'util';
1010
import zlib from 'zlib';
11-
import { remote } from 'webdriverio';
11+
import { remote, RemoteOptions } from 'webdriverio';
1212
import { rebuild } from '@electron/rebuild';
1313
import type { RebuildOptions } from '@electron/rebuild';
1414
import type { ConsoleMessageType } from 'puppeteer';
@@ -721,7 +721,7 @@ export async function startBrowser(
721721
runCounter++;
722722
const { webdriverOptions, wdioOptions } = await processCommonOpts();
723723

724-
const browser: CompassBrowser = (await remote({
724+
const options: RemoteOptions = {
725725
capabilities: {
726726
browserName: BROWSER_NAME, // 'chrome' or 'firefox'
727727
// https://webdriver.io/docs/driverbinaries/
@@ -738,8 +738,14 @@ export async function startBrowser(
738738
},
739739
...webdriverOptions,
740740
...wdioOptions,
741-
})) as CompassBrowser;
741+
};
742+
743+
debug('Starting compass via webdriverio with the following configuration:');
744+
debug(JSON.stringify(options, null, 2));
745+
746+
const browser: CompassBrowser = (await remote(options)) as CompassBrowser;
742747
await browser.navigateTo('http://localhost:7777/');
748+
743749
const compass = new Compass(name, browser, {
744750
mode: 'web',
745751
writeCoverage: false,

packages/compass-e2e-tests/helpers/gunzip.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
import Debug from 'debug';
22
import fastGlob from 'fast-glob';
33
import { createReadStream, createWriteStream } from 'fs';
4-
import { pipeline } from 'stream';
5-
import { promisify } from 'util';
4+
import { pipeline } from 'stream/promises';
65
import { createGunzip } from 'zlib';
76

87
const debug = Debug('compass-e2e-tests:gunzip');
98

10-
const pipe = promisify(pipeline);
11-
12-
async function gunzip(input: string, output: string) {
9+
async function gunzip(input: string, output: string, signal: AbortSignal) {
1310
const readStream = createReadStream(input);
1411
const gunzip = createGunzip();
1512
const writeStream = createWriteStream(output);
16-
17-
await pipe(readStream, gunzip, writeStream);
13+
try {
14+
await pipeline(readStream, gunzip, writeStream, { signal, end: true });
15+
} catch (err) {
16+
if (signal.aborted) {
17+
return;
18+
}
19+
throw err;
20+
}
1821
}
1922

20-
async function run(glob: string) {
23+
async function run(glob: string, signal: AbortSignal) {
2124
const filenames = await fastGlob(glob);
2225
for (const input of filenames) {
26+
if (signal.aborted) {
27+
return;
28+
}
2329
const output = input.replace(/\.gz$/, '');
2430
debug(input, '=>', output);
25-
await gunzip(input, output);
31+
await gunzip(input, output, signal);
2632
}
2733
}
2834

packages/compass-e2e-tests/helpers/test-runner-context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export const DEFAULT_CONNECTION_NAMES = DEFAULT_CONNECTIONS.map((info) => {
7171

7272
export const DEFAULT_CONNECTION_PORTS = DEFAULT_CONNECTIONS.map((info) => {
7373
const str = new ConnectionString(info.connectionOptions.connectionString);
74-
return str.hosts[0].split(':')[1];
74+
return Number(str.hosts[0].split(':')[1]);
7575
});
7676

7777
export const DEFAULT_CONNECTIONS_SERVER_INFO: {

packages/compass-e2e-tests/helpers/test-runner-global-fixtures.ts

Lines changed: 108 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
LOG_PATH,
1010
SKIP_COMPASS_DESKTOP_COMPILE,
1111
SKIP_NATIVE_MODULE_REBUILD,
12+
TEST_COMPASS_DESKTOP,
1213
TEST_COMPASS_DESKTOP_PACKAGED_APP,
1314
TEST_COMPASS_WEB,
1415
} from './test-runner-context';
@@ -26,6 +27,16 @@ import {
2627
removeUserDataDir,
2728
} from './compass';
2829

30+
export const globalFixturesAbortController = new AbortController();
31+
32+
function throwIfAborted() {
33+
if (globalFixturesAbortController.signal.aborted) {
34+
throw new Error('Mocha run was aborted while global setup was in progress');
35+
}
36+
}
37+
38+
export let abortRunner: (() => void) | undefined;
39+
2940
const debug = Debug('compass-e2e-tests:mocha-global-fixtures');
3041

3142
const wait = (ms: number) =>
@@ -47,74 +58,100 @@ const servers: MongoCluster[] = [];
4758
* - Compiles the desktop app
4859
*/
4960
export async function mochaGlobalSetup(this: Mocha.Runner) {
50-
debug('Unzipping fixtures...');
51-
await gunzip(path.join(E2E_WORKSPACE_PATH, 'fixtures', '*.gz'));
52-
53-
debug('X DISPLAY', process.env.DISPLAY);
54-
55-
if (!DISABLE_START_STOP) {
56-
debug('Starting MongoDB servers');
57-
58-
for (const port of DEFAULT_CONNECTION_PORTS) {
59-
const server = await startTestServer({
60-
args: ['--port', port],
61-
version:
62-
process.env.MONGODB_VERSION ?? process.env.MONGODB_RUNNER_VERSION,
63-
});
64-
servers.push(server);
65-
cleanupFns.push(() => {
66-
debug('Stopping server at port %s', port);
67-
return server.close();
68-
});
69-
}
61+
abortRunner = () => {
62+
globalFixturesAbortController.abort();
63+
this.abort();
64+
};
7065

71-
if (TEST_COMPASS_WEB) {
72-
debug('Starting Compass Web');
73-
const compassWeb = spawnCompassWeb();
74-
cleanupFns.push(() => {
75-
if (compassWeb.pid) {
76-
debug(`Killing compass-web [${compassWeb.pid}]`);
77-
kill(compassWeb.pid, 'SIGINT');
78-
} else {
79-
debug('No pid for compass-web');
80-
}
81-
});
82-
await waitForCompassWebToBeReady();
66+
try {
67+
debug('Unzipping fixtures...');
68+
await gunzip(
69+
path.join(E2E_WORKSPACE_PATH, 'fixtures', '*.gz'),
70+
globalFixturesAbortController.signal
71+
);
72+
73+
throwIfAborted();
74+
75+
debug('X DISPLAY', process.env.DISPLAY);
76+
77+
if (!DISABLE_START_STOP) {
78+
debug('Starting MongoDB servers');
79+
80+
for (const port of DEFAULT_CONNECTION_PORTS) {
81+
const server = await startTestServer({
82+
topology: 'replset',
83+
secondaries: 0,
84+
args: ['--port', String(port)],
85+
version:
86+
process.env.MONGODB_VERSION ?? process.env.MONGODB_RUNNER_VERSION,
87+
});
88+
servers.push(server);
89+
cleanupFns.push(() => {
90+
debug('Stopping server at port %s', port);
91+
return server.close();
92+
});
93+
throwIfAborted();
94+
}
95+
96+
if (TEST_COMPASS_WEB) {
97+
debug('Starting Compass Web');
98+
const compassWeb = spawnCompassWeb();
99+
cleanupFns.push(() => {
100+
if (compassWeb.pid) {
101+
debug(`Killing compass-web [${compassWeb.pid}]`);
102+
kill(compassWeb.pid, 'SIGINT');
103+
} else {
104+
debug('No pid for compass-web');
105+
}
106+
});
107+
await waitForCompassWebToBeReady();
108+
}
83109
}
84-
}
85110

86-
debug('Getting mongodb server info');
87-
await updateMongoDBServerInfo();
111+
debug('Getting mongodb server info');
112+
await updateMongoDBServerInfo();
88113

89-
try {
90-
debug('Clearing out past logs');
91-
fs.rmdirSync(LOG_PATH, { recursive: true });
92-
} catch (e) {
93-
debug('.log dir already removed');
94-
}
114+
throwIfAborted();
95115

96-
fs.mkdirSync(LOG_PATH, { recursive: true });
116+
try {
117+
debug('Clearing out past logs');
118+
fs.rmdirSync(LOG_PATH, { recursive: true });
119+
} catch (e) {
120+
debug('.log dir already removed');
121+
}
122+
123+
fs.mkdirSync(LOG_PATH, { recursive: true });
97124

98-
if (TEST_COMPASS_DESKTOP_PACKAGED_APP) {
99-
debug('Building Compass before running the tests ...');
100-
await buildCompass();
101-
} else {
102-
debug('Preparing Compass before running the tests');
125+
if (TEST_COMPASS_DESKTOP) {
126+
if (TEST_COMPASS_DESKTOP_PACKAGED_APP) {
127+
debug('Building Compass before running the tests ...');
128+
await buildCompass();
129+
} else {
130+
debug('Preparing Compass before running the tests');
103131

104-
if (!SKIP_NATIVE_MODULE_REBUILD) {
105-
debug('Rebuilding native modules ...');
106-
await rebuildNativeModules();
132+
if (!SKIP_NATIVE_MODULE_REBUILD) {
133+
debug('Rebuilding native modules ...');
134+
await rebuildNativeModules();
135+
}
136+
137+
if (!SKIP_COMPASS_DESKTOP_COMPILE) {
138+
debug('Compiling Compass assets ...');
139+
await compileCompassAssets();
140+
}
141+
}
107142
}
108143

109-
if (!SKIP_COMPASS_DESKTOP_COMPILE) {
110-
debug('Compiling Compass assets ...');
111-
await compileCompassAssets();
144+
throwIfAborted();
145+
146+
cleanupFns.push(() => {
147+
removeUserDataDir();
148+
});
149+
} catch (err) {
150+
if (globalFixturesAbortController.signal.aborted) {
151+
return;
112152
}
153+
throw err;
113154
}
114-
115-
cleanupFns.push(() => {
116-
removeUserDataDir();
117-
});
118155
}
119156

120157
export async function mochaGlobalTeardown() {
@@ -127,13 +164,18 @@ export async function mochaGlobalTeardown() {
127164
}
128165

129166
function spawnCompassWeb() {
130-
const proc = crossSpawn.spawn('npm', [
131-
'run',
132-
'--unsafe-perm',
133-
'start-web',
134-
'--workspace',
135-
'@mongodb-js/compass-web',
136-
]);
167+
const proc = crossSpawn.spawn(
168+
'npm',
169+
['run', '--unsafe-perm', 'start', '--workspace', '@mongodb-js/compass-web'],
170+
{
171+
env: {
172+
...process.env,
173+
OPEN_BROWSER: 'false', // tell webpack dev server not to open the default browser
174+
DISABLE_DEVSERVER_OVERLAY: 'true',
175+
APP_ENV: 'webdriverio',
176+
},
177+
}
178+
);
137179
proc.stdout.pipe(process.stdout);
138180
proc.stderr.pipe(process.stderr);
139181
return proc;
@@ -143,6 +185,7 @@ async function waitForCompassWebToBeReady() {
143185
let serverReady = false;
144186
const start = Date.now();
145187
while (!serverReady) {
188+
throwIfAborted();
146189
if (Date.now() - start >= 120_000) {
147190
throw new Error(
148191
'The compass-web sandbox is still not running after 120000ms'
@@ -153,7 +196,7 @@ async function waitForCompassWebToBeReady() {
153196
serverReady = res.ok;
154197
debug('Web server ready:', serverReady);
155198
} catch (err) {
156-
debug('Failed to connect to dev server', err);
199+
debug('Failed to connect to dev server:', (err as any).message);
157200
}
158201
await wait(1000);
159202
}

0 commit comments

Comments
 (0)