Unverified 提交 71d8c11b authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo 提交者: GitHub

Make Node tests independent (part 1) (#1295)

- Make each `test()` independent of other tests in same file. The goal is that a failing test doesn't make other tests also fail.
- For now just done in `test-Worker.ts` as a POC.
- Rename `node/src/tests/` to `node/src/test/` for consistency and convention.
- Make it possible to pass Jest command line args to `npm run test:node` script.
上级 f8bcb033
......@@ -8,11 +8,11 @@ The `package.json` file in the main folder includes the following scripts:
### `npm run typescript:build`
Compiles mediasoup TypeScript code (`lib` folder) JavaScript and places it into the `lib` directory.
Compiles mediasoup TypeScript code (`node/src` folder) JavaScript and places it into the `node/lib` directory.
### `npm run typescript:watch`
Compiles mediasoup TypeScript code (`lib` folder) JavaScript, places it into the `lib` directory an watches for changes in the TypeScript files.
Compiles mediasoup TypeScript code (`node/src` folder) JavaScript, places it into the `node/lib` directory an watches for changes in the TypeScript files.
### `npm run worker:build`
......@@ -56,11 +56,17 @@ Runs both `npm run test:node` and `npm run test:worker`.
### `npm run test:node`
Runs [Jest](https://jestjs.io) test units located at `test/` folder.
Runs [Jest](https://jestjs.io) test units located at `node/test` folder.
Jest command arguments can be given using `--` as follows:
```bash
npm run test:node -- --testPathPattern "test-Worker.ts" --testNamePattern "createWorker"
```
### `npm run test:worker`
Runs [Catch2](https://github.com/catchorg/Catch2) test units located at `worker/test/` folder. It invokes `invoke test` below.
Runs [Catch2](https://github.com/catchorg/Catch2) test units located at `worker/test` folder. It invokes `invoke test` below.
### `npm run coverage:node`
......@@ -78,7 +84,7 @@ It is not necessary for normal usage of mediasoup as a dependency.
## Python Invoke and `tasks.py` file
mediasoup uses Python [Invoke](https://www.pyinvoke.org/) library for managing and organizing tasks in the `worker` folder (mediasoup worker C++ subproject). `Invoke` is basically a replacemente of `make` + `Makefile` written in Python. mediasoup automatically installs `Invoke` in a local custom path during the installation process (in both Node and Rust) so the user doesn't need to worry about it.
mediasoup uses Python [Invoke](https://www.pyinvoke.org) library for managing and organizing tasks in the `worker` folder (mediasoup worker C++ subproject). `Invoke` is basically a replacemente of `make` + `Makefile` written in Python. mediasoup automatically installs `Invoke` in a local custom path during the installation process (in both Node and Rust) so the user doesn't need to worry about it.
Tasks are defined in `worker/tasks.py`. For development purposes, developers or contributors can install `Invoke` using `pip3 install invoke` and run tasks below within the `worker` folder.
......@@ -129,7 +135,7 @@ invoke update-wrap-file openssl
### `invoke mediasoup-worker`
Builds the `mediasoup-worker` binary at `worker/out/Release/`.
Builds the `mediasoup-worker` binary at `worker/out/Release`.
If the "MEDIASOUP_MAX_CORES" environment variable is set, the build process will use that number of CPU cores. Otherwise it will auto-detect the number of cores in the machine.
......@@ -153,7 +159,7 @@ MEDIASOUP_WORKER_BIN="/home/xxx/src/foo/mediasoup-worker" node myapp.js
### `invoke libmediasoup-worker`
Builds the `libmediasoup-worker` static library at `worker/out/Release/`.
Builds the `libmediasoup-worker` static library at `worker/out/Release`.
"MEDIASOUP_MAX_CORES"` and "MEDIASOUP_BUILDTYPE" environment variables from above still apply for static library build.
......@@ -171,7 +177,7 @@ Rewrites mediasoup worker C++ files using [clang-format](https://clang.llvm.org/
### `invoke test`
Builds and runs the `mediasoup-worker-test` binary at `worker/out/Release/` (or at `worker/out/Debug/` if the "MEDIASOUP_BUILDTYPE" environment variable is set to "Debug"), which uses [Catch2](https://github.com/catchorg/Catch2) to run test units located at `worker/test/` folder.
Builds and runs the `mediasoup-worker-test` binary at `worker/out/Release` (or at `worker/out/Debug` if the "MEDIASOUP_BUILDTYPE" environment variable is set to "Debug"), which uses [Catch2](https://github.com/catchorg/Catch2) to run test units located at `worker/test` folder.
### `invoke test-asan`
......@@ -179,19 +185,19 @@ Run test with Address Sanitizer.
### `invoke tidy`
Runs [clang-tidy](http://clang.llvm.org/extra/clang-tidy/) and performs C++ code checks following `worker/.clang-tidy` rules.
Runs [clang-tidy](http://clang.llvm.org/extra/clang-tidy) and performs C++ code checks following `worker/.clang-tidy` rules.
**Requirements:**
* `invoke clean` and `invoke mediasoup-worker` must have been called first.
* [PyYAML](https://pyyaml.org/) is required.
* [PyYAML](https://pyyaml.org) is required.
- In OSX install it with `brew install libyaml` and `sudo easy_install-X.Y pyyaml`.
"MEDIASOUP_TIDY_CHECKS" environment variable with a comma separated list of checks overrides the checks defined in `.clang-tidy` file.
### `invoke fuzzer`
Builds the `mediasoup-worker-fuzzer` binary (which uses [libFuzzer](http://llvm.org/docs/LibFuzzer.html)) at `worker/out/Release/` (or at `worker/out/Debug/` if the "MEDIASOUP_BUILDTYPE" environment variable is set to "Debug").
Builds the `mediasoup-worker-fuzzer` binary (which uses [libFuzzer](http://llvm.org/docs/LibFuzzer.html)) at `worker/out/Release` (or at `worker/out/Debug/` if the "MEDIASOUP_BUILDTYPE" environment variable is set to "Debug").
**Requirements:**
......@@ -218,7 +224,7 @@ scripts/get-dep.sh clang-fuzzer
### `invoke docker-run`
Runs a container of the Ubuntu Docker image created with `invoke docker`. It automatically executes a `bash` session in the `/mediasoup` directory, which is a Docker volume that points to the real `mediasoup` directory.
Runs a container of the Ubuntu Docker image created with `invoke docker`. It automatically executes a `bash` session in the `/mediasoup` directory, which is a Docker volume that points to the mediasoup root folder.
**NOTE:** To install and run mediasoup in the container, previous installation (if any) must be properly cleaned by entering the `worker` directory and running `invoke clean-all`.
......@@ -228,7 +234,7 @@ Builds a Linux Alpine Docker image with all dependencies to run mediasoup.
### `invoke docker-alpine-run`
Runs a container of the Alpine Docker image created with `invoke docker-alpine`. It automatically executes an `ash` session in the `/mediasoup` directory, which is a Docker volume that points to the real `mediasoup` directory.
Runs a container of the Alpine Docker image created with `invoke docker-alpine`. It automatically executes an `ash` session in the `/mediasoup` directory, which is a Docker volume that points to the mediasoup root folder.
**NOTE:** To install and run mediasoup in the container, previous installation (if any) must be properly cleaned by entering the `worker` directory and running `invoke clean-all`.
......
......@@ -4,12 +4,7 @@ import * as path from 'node:path';
import * as mediasoup from '../';
import { InvalidStateError } from '../errors';
let worker: mediasoup.types.Worker;
beforeEach(() => worker && !worker.closed && worker.close());
afterEach(() => worker && !worker.closed && worker.close());
test('Worker.workerBin matches mediasoup-worker absolute path', async () =>
test('Worker.workerBin matches mediasoup-worker absolute path', () =>
{
const workerBin = process.env.MEDIASOUP_WORKER_BIN
? process.env.MEDIASOUP_WORKER_BIN
......@@ -26,22 +21,21 @@ test('createWorker() succeeds', async () =>
mediasoup.observer.once('newworker', onObserverNewWorker);
worker = await mediasoup.createWorker();
const worker1 = await mediasoup.createWorker();
expect(onObserverNewWorker).toHaveBeenCalledTimes(1);
expect(onObserverNewWorker).toHaveBeenCalledWith(worker);
expect(worker.constructor.name).toBe('Worker');
expect(typeof worker.pid).toBe('number');
expect(worker.closed).toBe(false);
expect(worker.died).toBe(false);
expect(onObserverNewWorker).toHaveBeenCalledWith(worker1);
expect(worker1.constructor.name).toBe('Worker');
expect(typeof worker1.pid).toBe('number');
expect(worker1.closed).toBe(false);
expect(worker1.died).toBe(false);
worker.close();
worker1.close();
expect(worker.closed).toBe(true);
expect(worker.died).toBe(false);
expect(worker1.closed).toBe(true);
expect(worker1.died).toBe(false);
// eslint-disable-next-line require-atomic-updates
worker = await mediasoup.createWorker<{ foo: number; bar?: string }>(
const worker2 = await mediasoup.createWorker<{ foo: number; bar?: string }>(
{
logLevel : 'debug',
logTags : [ 'info' ],
......@@ -52,16 +46,17 @@ test('createWorker() succeeds', async () =>
libwebrtcFieldTrials : 'WebRTC-Bwe-AlrLimitedBackoff/Disabled/',
appData : { foo: 456 }
});
expect(worker.constructor.name).toBe('Worker');
expect(typeof worker.pid).toBe('number');
expect(worker.closed).toBe(false);
expect(worker.died).toBe(false);
expect(worker.appData).toEqual({ foo: 456 });
worker.close();
expect(worker2.constructor.name).toBe('Worker');
expect(typeof worker2.pid).toBe('number');
expect(worker2.closed).toBe(false);
expect(worker2.died).toBe(false);
expect(worker2.appData).toEqual({ foo: 456 });
expect(worker.closed).toBe(true);
expect(worker.died).toBe(false);
worker2.close();
expect(worker2.closed).toBe(true);
expect(worker2.died).toBe(false);
}, 2000);
test('createWorker() with wrong settings rejects with TypeError', async () =>
......@@ -96,7 +91,7 @@ test('createWorker() with wrong settings rejects with TypeError', async () =>
test('worker.updateSettings() succeeds', async () =>
{
worker = await mediasoup.createWorker();
const worker = await mediasoup.createWorker();
await expect(worker.updateSettings({ logLevel: 'debug', logTags: [ 'ice' ] }))
.resolves
......@@ -107,7 +102,7 @@ test('worker.updateSettings() succeeds', async () =>
test('worker.updateSettings() with wrong settings rejects with TypeError', async () =>
{
worker = await mediasoup.createWorker();
const worker = await mediasoup.createWorker();
// @ts-ignore
await expect(worker.updateSettings({ logLevel: 'chicken' }))
......@@ -119,19 +114,18 @@ test('worker.updateSettings() with wrong settings rejects with TypeError', async
test('worker.updateSettings() rejects with InvalidStateError if closed', async () =>
{
worker = await mediasoup.createWorker();
const worker = await mediasoup.createWorker();
worker.close();
await expect(worker.updateSettings({ logLevel: 'error' }))
.rejects
.toThrow(InvalidStateError);
worker.close();
}, 2000);
test('worker.dump() succeeds', async () =>
{
worker = await mediasoup.createWorker();
const worker = await mediasoup.createWorker();
await expect(worker.dump())
.resolves
......@@ -152,19 +146,18 @@ test('worker.dump() succeeds', async () =>
test('worker.dump() rejects with InvalidStateError if closed', async () =>
{
worker = await mediasoup.createWorker();
const worker = await mediasoup.createWorker();
worker.close();
await expect(worker.dump())
.rejects
.toThrow(InvalidStateError);
worker.close();
}, 2000);
test('worker.getResourceUsage() succeeds', async () =>
{
worker = await mediasoup.createWorker();
const worker = await mediasoup.createWorker();
await expect(worker.getResourceUsage())
.resolves
......@@ -175,8 +168,7 @@ test('worker.getResourceUsage() succeeds', async () =>
test('worker.close() succeeds', async () =>
{
worker = await mediasoup.createWorker({ logLevel: 'warn' });
const worker = await mediasoup.createWorker({ logLevel: 'warn' });
const onObserverClose = jest.fn();
worker.observer.once('close', onObserverClose);
......@@ -192,24 +184,26 @@ test('Worker emits "died" if worker process died unexpectedly', async () =>
let onDied: ReturnType<typeof jest.fn>;
let onObserverClose: ReturnType<typeof jest.fn>;
worker = await mediasoup.createWorker({ logLevel: 'warn' });
const worker1 = await mediasoup.createWorker({ logLevel: 'warn' });
onDied = jest.fn();
onObserverClose = jest.fn();
worker.observer.once('close', onObserverClose);
worker1.observer.once('close', onObserverClose);
await new Promise<void>((resolve, reject) =>
{
worker.on('died', () =>
worker1.on('died', () =>
{
onDied();
if (onObserverClose.mock.calls.length > 0)
{
reject(
new Error('observer "close" event emitted before worker "died" event'));
new Error('observer "close" event emitted before worker "died" event')
);
}
else if (worker.closed)
else if (worker1.closed)
{
resolve();
}
......@@ -219,33 +213,34 @@ test('Worker emits "died" if worker process died unexpectedly', async () =>
}
});
process.kill(worker.pid, 'SIGINT');
process.kill(worker1.pid, 'SIGINT');
});
expect(onDied).toHaveBeenCalledTimes(1);
expect(onObserverClose).toHaveBeenCalledTimes(1);
expect(worker.closed).toBe(true);
expect(worker.died).toBe(true);
expect(worker1.closed).toBe(true);
expect(worker1.died).toBe(true);
const worker2 = await mediasoup.createWorker({ logLevel: 'warn' });
// eslint-disable-next-line require-atomic-updates
worker = await mediasoup.createWorker({ logLevel: 'warn' });
onDied = jest.fn();
onObserverClose = jest.fn();
worker.observer.once('close', onObserverClose);
worker2.observer.once('close', onObserverClose);
await new Promise<void>((resolve, reject) =>
{
worker.on('died', () =>
worker2.on('died', () =>
{
onDied();
if (onObserverClose.mock.calls.length > 0)
{
reject(
new Error('observer "close" event emitted before worker "died" event'));
new Error('observer "close" event emitted before worker "died" event')
);
}
else if (worker.closed)
else if (worker2.closed)
{
resolve();
}
......@@ -255,33 +250,34 @@ test('Worker emits "died" if worker process died unexpectedly', async () =>
}
});
process.kill(worker.pid, 'SIGTERM');
process.kill(worker2.pid, 'SIGTERM');
});
expect(onDied).toHaveBeenCalledTimes(1);
expect(onObserverClose).toHaveBeenCalledTimes(1);
expect(worker.closed).toBe(true);
expect(worker.died).toBe(true);
expect(worker2.closed).toBe(true);
expect(worker2.died).toBe(true);
const worker3 = await mediasoup.createWorker({ logLevel: 'warn' });
// eslint-disable-next-line require-atomic-updates
worker = await mediasoup.createWorker({ logLevel: 'warn' });
onDied = jest.fn();
onObserverClose = jest.fn();
worker.observer.once('close', onObserverClose);
worker3.observer.once('close', onObserverClose);
await new Promise<void>((resolve, reject) =>
{
worker.on('died', () =>
worker3.on('died', () =>
{
onDied();
if (onObserverClose.mock.calls.length > 0)
{
reject(
new Error('observer "close" event emitted before worker "died" event'));
new Error('observer "close" event emitted before worker "died" event')
);
}
else if (worker.closed)
else if (worker3.closed)
{
resolve();
}
......@@ -291,42 +287,40 @@ test('Worker emits "died" if worker process died unexpectedly', async () =>
}
});
process.kill(worker.pid, 'SIGKILL');
process.kill(worker3.pid, 'SIGKILL');
});
expect(onDied).toHaveBeenCalledTimes(1);
expect(onObserverClose).toHaveBeenCalledTimes(1);
expect(worker.closed).toBe(true);
expect(worker.died).toBe(true);
expect(worker3.closed).toBe(true);
expect(worker3.died).toBe(true);
}, 5000);
test('worker process ignores PIPE, HUP, ALRM, USR1 and USR2 signals', async () =>
// Windows doesn't have some signals such as SIGPIPE, SIGALRM, SIGUSR1, SIGUSR2
// so we just skip this test in Windows.
if (os.platform() !== 'win32')
{
// Windows doesn't have some signals such as SIGPIPE, SIGALRM, SIGUSR1, SIGUSR2
// so we just skip this test in Windows.
if (os.platform() === 'win32')
test('worker process ignores PIPE, HUP, ALRM, USR1 and USR2 signals', async () =>
{
return;
}
const worker = await mediasoup.createWorker({ logLevel: 'warn' });
worker = await mediasoup.createWorker({ logLevel: 'warn' });
await new Promise<void>((resolve, reject) =>
{
worker.on('died', reject);
await new Promise<void>((resolve, reject) =>
{
worker.on('died', reject);
process.kill(worker.pid, 'SIGPIPE');
process.kill(worker.pid, 'SIGHUP');
process.kill(worker.pid, 'SIGALRM');
process.kill(worker.pid, 'SIGUSR1');
process.kill(worker.pid, 'SIGUSR2');
process.kill(worker.pid, 'SIGPIPE');
process.kill(worker.pid, 'SIGHUP');
process.kill(worker.pid, 'SIGALRM');
process.kill(worker.pid, 'SIGUSR1');
process.kill(worker.pid, 'SIGUSR2');
setTimeout(() =>
{
expect(worker.closed).toBe(false);
setTimeout(() =>
{
expect(worker.closed).toBe(false);
worker.close();
resolve();
}, 2000);
});
}, 3000);
worker.close();
resolve();
}, 2000);
});
}, 3000);
}
......@@ -21,7 +21,8 @@ const WORKER_PREBUILD_TAR_PATH = `${WORKER_PREBUILD_DIR}/${WORKER_PREBUILD_TAR}`
const GH_OWNER = 'versatica';
const GH_REPO = 'mediasoup';
const task = process.argv.slice(2).join(' ');
const task = process.argv[2];
const args = process.argv.slice(3).join(' ');
// PYTHONPATH env must be updated now so all invoke calls below will find the
// pip invoke module.
......@@ -45,6 +46,8 @@ run();
async function run()
{
logInfo(args ? `[args:"${args}"]` : '');
switch (task)
{
// As per NPM documentation (https://docs.npmjs.com/cli/v9/using-npm/scripts)
......@@ -125,7 +128,7 @@ async function run()
case 'typescript:watch':
{
deleteNodeLib();
executeCmd('tsc --project node --watch');
executeCmd(`tsc --project node --watch ${args}`);
break;
}
......@@ -199,7 +202,7 @@ async function run()
case 'coverage:node':
{
buildTypescript({ force: false });
executeCmd('jest --coverage');
executeCmd(`jest --coverage ${args}`);
executeCmd('open-cli coverage/lcov-report/index.html');
break;
......@@ -438,14 +441,7 @@ function testNode()
{
logInfo('testNode()');
if (!process.env.TEST_FILE)
{
executeCmd('jest');
}
else
{
executeCmd(`jest --testPathPattern "${process.env.TEST_FILE}"`);
}
executeCmd(`jest ${args}`);
}
function testWorker()
......
......@@ -73,7 +73,7 @@
"jest": {
"verbose": true,
"testEnvironment": "node",
"testRegex": "node/src/tests/test-.*\\.ts",
"testRegex": "node/src/test/test-.*\\.ts",
"transform": {
"^.*\\.ts$": [
"ts-jest",
......@@ -88,7 +88,7 @@
},
"coveragePathIgnorePatterns": [
"node/src/fbs",
"node/src/tests"
"node/src/test"
],
"cacheDirectory": ".cache/jest",
"modulePathIgnorePatterns": [
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论