mirror of
https://github.com/nocobase/nocobase
synced 2024-11-16 05:15:11 +00:00
feat: improve code
This commit is contained in:
parent
2a19484e8f
commit
d13b08be29
@ -8,12 +8,12 @@
|
||||
*/
|
||||
|
||||
import { Plugin } from '@nocobase/server';
|
||||
import { createMultiMockServer, sleep } from '@nocobase/test';
|
||||
import { uid } from '@nocobase/utils';
|
||||
import { createMockCluster, sleep } from '@nocobase/test';
|
||||
|
||||
describe('sync-message-manager', () => {
|
||||
test('subscribe + publish', async () => {
|
||||
const [node1, node2] = await createMultiMockServer({ basename: 'base1' });
|
||||
const cluster = await createMockCluster();
|
||||
const [node1, node2] = cluster.instances;
|
||||
const mockListener = vi.fn();
|
||||
await node1.syncMessageManager.subscribe('test1', mockListener);
|
||||
await node2.syncMessageManager.subscribe('test1', mockListener);
|
||||
@ -21,12 +21,12 @@ describe('sync-message-manager', () => {
|
||||
expect(mockListener).toHaveBeenCalled();
|
||||
expect(mockListener).toBeCalledTimes(1);
|
||||
expect(mockListener).toHaveBeenCalledWith('message1');
|
||||
await node1.destroy();
|
||||
await node2.destroy();
|
||||
await cluster.destroy();
|
||||
});
|
||||
|
||||
test('transaction', async () => {
|
||||
const [node1, node2] = await createMultiMockServer({ basename: 'base1' });
|
||||
const cluster = await createMockCluster();
|
||||
const [node1, node2] = cluster.instances;
|
||||
const mockListener = vi.fn();
|
||||
await node1.syncMessageManager.subscribe('test1', mockListener);
|
||||
const transaction = await node2.db.sequelize.transaction();
|
||||
@ -38,8 +38,7 @@ describe('sync-message-manager', () => {
|
||||
expect(mockListener).toHaveBeenCalled();
|
||||
expect(mockListener).toBeCalledTimes(1);
|
||||
expect(mockListener).toHaveBeenCalledWith('message1');
|
||||
await node1.destroy();
|
||||
await node2.destroy();
|
||||
await cluster.destroy();
|
||||
});
|
||||
|
||||
test('plugin.handleSyncMessage', async () => {
|
||||
@ -52,19 +51,17 @@ describe('sync-message-manager', () => {
|
||||
mockListener(message);
|
||||
}
|
||||
}
|
||||
const [app1, app2] = await createMultiMockServer({
|
||||
basename: uid(),
|
||||
number: 2, // 创建几个 app 实例
|
||||
const cluster = await createMockCluster({
|
||||
plugins: [MyPlugin],
|
||||
});
|
||||
const [app1, app2] = cluster.instances;
|
||||
await app1.pm.get(MyPlugin).sendSyncMessage('message1');
|
||||
expect(mockListener).toBeCalledTimes(1);
|
||||
expect(mockListener).toHaveBeenCalledWith('message1');
|
||||
await app2.pm.get(MyPlugin).sendSyncMessage('message2');
|
||||
expect(mockListener).toBeCalledTimes(2);
|
||||
expect(mockListener).toHaveBeenCalledWith('message2');
|
||||
await app1.destroy();
|
||||
await app2.destroy();
|
||||
await cluster.destroy();
|
||||
});
|
||||
|
||||
test('plugin.handleSyncMessage + transaction', async () => {
|
||||
@ -77,11 +74,10 @@ describe('sync-message-manager', () => {
|
||||
mockListener(message);
|
||||
}
|
||||
}
|
||||
const [app1, app2] = await createMultiMockServer({
|
||||
basename: uid(),
|
||||
number: 2, // 创建几个 app 实例
|
||||
const cluster = await createMockCluster({
|
||||
plugins: [MyPlugin],
|
||||
});
|
||||
const [app1, app2] = cluster.instances;
|
||||
const transaction = await app1.db.sequelize.transaction();
|
||||
app1.pm.get(MyPlugin).sendSyncMessage('message1', { transaction });
|
||||
await sleep(1000);
|
||||
|
@ -9,6 +9,7 @@
|
||||
|
||||
import { mockDatabase } from '@nocobase/database';
|
||||
import { Application, ApplicationOptions, AppSupervisor, Gateway, PluginManager } from '@nocobase/server';
|
||||
import { uid } from '@nocobase/utils';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import _ from 'lodash';
|
||||
import qs from 'qs';
|
||||
@ -257,28 +258,39 @@ export async function startMockServer(options: ApplicationOptions = {}) {
|
||||
|
||||
type BeforeInstallFn = (app) => Promise<void>;
|
||||
|
||||
export async function createMultiMockServer(
|
||||
export async function createMockCluster(
|
||||
options: ApplicationOptions & {
|
||||
number?: number;
|
||||
version?: string;
|
||||
basename?: string;
|
||||
name?: string;
|
||||
appName?: string;
|
||||
beforeInstall?: BeforeInstallFn;
|
||||
skipInstall?: boolean;
|
||||
skipStart?: boolean;
|
||||
} = {},
|
||||
) {
|
||||
const instances: MockServer[] = [];
|
||||
const clusterName = options.name || `cluster_${uid()}`;
|
||||
const appName = options.appName || `app_${uid()}`;
|
||||
for (const i of _.range(0, options.number || 2)) {
|
||||
const app: MockServer = await createMockServer({
|
||||
...options,
|
||||
skipSupervisor: true,
|
||||
name: clusterName + '_' + appName,
|
||||
pubSubManager: {
|
||||
channelPrefix: options.basename,
|
||||
channelPrefix: clusterName,
|
||||
},
|
||||
});
|
||||
instances.push(app);
|
||||
}
|
||||
return instances;
|
||||
return {
|
||||
instances,
|
||||
async destroy() {
|
||||
for (const instance of instances) {
|
||||
await instance.destroy();
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export async function createMockServer(
|
||||
|
Loading…
Reference in New Issue
Block a user