refactor(test): change mock cluster constructor arguments (#4917)

* refactor(test): change mock cluster constructor arguments

* refactor(test): change mock cluster constructor arguments
This commit is contained in:
Junyi 2024-07-21 15:16:55 +08:00 committed by GitHub
parent 39f43cb9f7
commit 3144ce3eed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -14,22 +14,23 @@ import { getPortPromise } from 'portfinder';
import { uid } from '@nocobase/utils'; import { uid } from '@nocobase/utils';
import { createMockServer } from './mock-server'; import { createMockServer } from './mock-server';
type ProcessConfig = {
env: Record<string, any>;
};
type ClusterOptions = { type ClusterOptions = {
script: string; script?: string;
config: ProcessConfig; env?: Record<string, any>;
instances: number;
plugins?: string[]; plugins?: string[];
instances?: number;
}; };
export class MockCluster { export class MockCluster {
private script = `${process.env.APP_PACKAGE_ROOT}/src/index.ts`;
private processes = []; private processes = [];
private mockApp; private mockApp;
constructor(private options: ClusterOptions) {} constructor(private options: ClusterOptions = {}) {
if (options.script) {
this.script = options.script;
}
}
async start(): Promise<number[]> { async start(): Promise<number[]> {
// NOTE: use this for install app first // NOTE: use this for install app first
@ -40,12 +41,12 @@ export class MockCluster {
this.processes = []; this.processes = [];
const ports = []; const ports = [];
for (let i = 0; i < this.options.instances; i++) { for (let i = 0; i < (this.options.instances ?? 2); i++) {
const port = await getPortPromise(); const port = await getPortPromise();
const childProcess = spawn('node', ['./node_modules/tsx/dist/cli.mjs', this.options.script, 'start'], { const childProcess = spawn('node', ['./node_modules/tsx/dist/cli.mjs', this.script, 'start'], {
env: { env: {
...process.env, ...process.env,
...this.options.config.env, ...this.options.env,
APP_PORT: `${port}`, APP_PORT: `${port}`,
APPEND_PRESET_BUILT_IN_PLUGINS: (this.options.plugins ?? []).join(','), APPEND_PRESET_BUILT_IN_PLUGINS: (this.options.plugins ?? []).join(','),
SOCKET_PATH: `storage/tests/gateway-cluster-${uid()}.sock`, SOCKET_PATH: `storage/tests/gateway-cluster-${uid()}.sock`,