test(plugin-workflow): add test case for duplicated triggering schedule workflow (#3817)

This commit is contained in:
Junyi 2024-04-18 17:15:14 +08:00 committed by GitHub
parent ca29515bcd
commit 52d00aef58
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 121 additions and 70 deletions

View File

@ -3,6 +3,8 @@ import { MockServer } from '@nocobase/test';
import Database from '@nocobase/database'; import Database from '@nocobase/database';
import { getApp, sleep } from '@nocobase/plugin-workflow-test'; import { getApp, sleep } from '@nocobase/plugin-workflow-test';
import Plugin from '../../..';
async function sleepToEvenSecond() { async function sleepToEvenSecond() {
const now = new Date(); const now = new Date();
// NOTE: align to even(0, 2, ...) + 0.5 seconds to start // NOTE: align to even(0, 2, ...) + 0.5 seconds to start
@ -23,7 +25,6 @@ describe('workflow > triggers > schedule > static mode', () => {
let db: Database; let db: Database;
let PostRepo; let PostRepo;
let CategoryRepo; let CategoryRepo;
let WorkflowModel;
let WorkflowRepo; let WorkflowRepo;
beforeEach(async () => { beforeEach(async () => {
@ -31,7 +32,6 @@ describe('workflow > triggers > schedule > static mode', () => {
db = app.db; db = app.db;
const workflow = db.getCollection('workflows'); const workflow = db.getCollection('workflows');
WorkflowModel = workflow.model;
WorkflowRepo = workflow.repository; WorkflowRepo = workflow.repository;
PostRepo = db.getCollection('posts').repository; PostRepo = db.getCollection('posts').repository;
CategoryRepo = db.getCollection('categories').repository; CategoryRepo = db.getCollection('categories').repository;
@ -41,11 +41,13 @@ describe('workflow > triggers > schedule > static mode', () => {
describe('configuration', () => { describe('configuration', () => {
it('neither startsOn nor repeat configurated', async () => { it('neither startsOn nor repeat configurated', async () => {
const workflow = await WorkflowModel.create({ const workflow = await WorkflowRepo.create({
enabled: true, values: {
type: 'schedule', enabled: true,
config: { type: 'schedule',
mode: 0, config: {
mode: 0,
},
}, },
}); });
@ -62,12 +64,14 @@ describe('workflow > triggers > schedule > static mode', () => {
start.setMilliseconds(0); start.setMilliseconds(0);
start.setSeconds(start.getSeconds() + 2); start.setSeconds(start.getSeconds() + 2);
const workflow = await WorkflowModel.create({ const workflow = await WorkflowRepo.create({
enabled: true, values: {
type: 'schedule', enabled: true,
config: { type: 'schedule',
mode: 0, config: {
startsOn: start.toISOString(), mode: 0,
startsOn: start.toISOString(),
},
}, },
}); });
@ -80,13 +84,15 @@ describe('workflow > triggers > schedule > static mode', () => {
it('on every 2 seconds', async () => { it('on every 2 seconds', async () => {
const start = await sleepToEvenSecond(); const start = await sleepToEvenSecond();
const workflow = await WorkflowModel.create({ const workflow = await WorkflowRepo.create({
enabled: true, values: {
type: 'schedule', enabled: true,
config: { type: 'schedule',
mode: 0, config: {
startsOn: start.toISOString(), mode: 0,
repeat: '*/2 * * * * *', startsOn: start.toISOString(),
repeat: '*/2 * * * * *',
},
}, },
}); });
@ -101,14 +107,16 @@ describe('workflow > triggers > schedule > static mode', () => {
it('on every even seconds and limit 1', async () => { it('on every even seconds and limit 1', async () => {
const start = await sleepToEvenSecond(); const start = await sleepToEvenSecond();
const workflow = await WorkflowModel.create({ const workflow = await WorkflowRepo.create({
enabled: true, values: {
type: 'schedule', enabled: true,
config: { type: 'schedule',
mode: 0, config: {
startsOn: start.toISOString(), mode: 0,
repeat: '*/2 * * * * *', startsOn: start.toISOString(),
limit: 1, repeat: '*/2 * * * * *',
limit: 1,
},
}, },
}); });
@ -123,14 +131,16 @@ describe('workflow > triggers > schedule > static mode', () => {
const start = new Date(); const start = new Date();
start.setMilliseconds(0); start.setMilliseconds(0);
const workflow = await WorkflowModel.create({ const workflow = await WorkflowRepo.create({
enabled: true, values: {
type: 'schedule', enabled: true,
config: { type: 'schedule',
mode: 0, config: {
startsOn: start.toISOString(), mode: 0,
repeat: 2000, startsOn: start.toISOString(),
limit: 1, repeat: 2000,
limit: 1,
},
}, },
}); });
@ -149,13 +159,15 @@ describe('workflow > triggers > schedule > static mode', () => {
await sleep(1500); await sleep(1500);
const workflow = await WorkflowModel.create({ const workflow = await WorkflowRepo.create({
enabled: true, values: {
type: 'schedule', enabled: true,
config: { type: 'schedule',
mode: 0, config: {
startsOn, mode: 0,
repeat: `${now.getSeconds()} * * * * *`, startsOn,
repeat: `${now.getSeconds()} * * * * *`,
},
}, },
}); });
@ -170,12 +182,14 @@ describe('workflow > triggers > schedule > static mode', () => {
it('no repeat triggered then update to repeat', async () => { it('no repeat triggered then update to repeat', async () => {
const start = await sleepToEvenSecond(); const start = await sleepToEvenSecond();
const workflow = await WorkflowModel.create({ const workflow = await WorkflowRepo.create({
enabled: true, values: {
type: 'schedule', enabled: true,
config: { type: 'schedule',
mode: 0, config: {
startsOn: start.toISOString(), mode: 0,
startsOn: start.toISOString(),
},
}, },
}); });
@ -207,13 +221,15 @@ describe('workflow > triggers > schedule > static mode', () => {
const future = new Date(); const future = new Date();
future.setSeconds(future.getSeconds() + 2); future.setSeconds(future.getSeconds() + 2);
const workflow = await WorkflowModel.create({ const workflow = await WorkflowRepo.create({
enabled: true, values: {
type: 'schedule', enabled: true,
config: { type: 'schedule',
mode: 0, config: {
startsOn: future.toISOString(), mode: 0,
repeat: 1000, startsOn: future.toISOString(),
repeat: 1000,
},
}, },
}); });
@ -265,7 +281,10 @@ describe('workflow > triggers > schedule > static mode', () => {
}); });
await sleep(3000); await sleep(3000);
await WorkflowModel.update({ enabled: false }, { where: { enabled: true } }); await WorkflowRepo.update({
values: { enabled: false },
filter: { enabled: true },
});
const [e1] = await w1.getExecutions(); const [e1] = await w1.getExecutions();
expect(e1).toBeDefined(); expect(e1).toBeDefined();
@ -287,12 +306,14 @@ describe('workflow > triggers > schedule > static mode', () => {
start.setMilliseconds(0); start.setMilliseconds(0);
start.setSeconds(start.getSeconds() + 2); start.setSeconds(start.getSeconds() + 2);
const workflow = await WorkflowModel.create({ const workflow = await WorkflowRepo.create({
enabled: true, values: {
type: 'schedule', enabled: true,
config: { type: 'schedule',
mode: 0, config: {
startsOn: start.toISOString(), mode: 0,
startsOn: start.toISOString(),
},
}, },
}); });
@ -315,12 +336,14 @@ describe('workflow > triggers > schedule > static mode', () => {
start.setMilliseconds(0); start.setMilliseconds(0);
start.setSeconds(start.getSeconds() + 2); start.setSeconds(start.getSeconds() + 2);
const workflow = await WorkflowModel.create({ const workflow = await WorkflowRepo.create({
enabled: true, values: {
type: 'schedule', enabled: true,
config: { type: 'schedule',
mode: 0, config: {
startsOn: start.toISOString(), mode: 0,
startsOn: start.toISOString(),
},
}, },
}); });
@ -337,4 +360,32 @@ describe('workflow > triggers > schedule > static mode', () => {
expect(c2).toBe(1); expect(c2).toBe(1);
}); });
}); });
describe('duplications', () => {
it('same workflow should not be triggered in same time more than once', async () => {
await sleepToEvenSecond();
const start = new Date();
start.setMilliseconds(0);
start.setSeconds(start.getSeconds() + 2);
const workflow = await WorkflowRepo.create({
values: {
enabled: true,
type: 'schedule',
config: {
mode: 0,
startsOn: start.toISOString(),
},
},
});
(app.pm.get('workflow') as Plugin).trigger(workflow, { date: start });
await sleep(3000);
const e1s = await workflow.getExecutions();
expect(e1s.length).toBe(1);
});
});
}); });

View File

@ -44,7 +44,7 @@ export default class ScheduleTrigger extends Trigger {
} }
const existed = await workflow.countExecutions({ const existed = await workflow.countExecutions({
where: { where: {
'context.date': context.date, 'context.date': context.date instanceof Date ? context.date.toISOString() : context.date,
}, },
transaction: options.transaction, transaction: options.transaction,
}); });