Skip to content

Commit

Permalink
Merge pull request #1270 from towersxu/master
Browse files Browse the repository at this point in the history
refactor(engine): improve readablity
  • Loading branch information
wumail authored Aug 8, 2023
2 parents 9b644e4 + 69f058c commit 01a9aba
Show file tree
Hide file tree
Showing 15 changed files with 503 additions and 224 deletions.
9 changes: 7 additions & 2 deletions packages/engine/__test__/01_index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ import Engine from '../src/index';

describe('@logicflow/engine', () => {
test('Execution Process Completed, Returning Data Containing executionId', async () => {
const engine = new Engine();
// TODO: context在初始化engine时传入
const engine = new Engine({
context: {},
});
const flowData = {
/**
* node1 |--> node2
*/
graphData: {
nodes: [
{
Expand All @@ -23,7 +29,6 @@ describe('@logicflow/engine', () => {
}
]
},
context: {},
globalData: {},
}
const flowModel = engine.load(flowData);
Expand Down
49 changes: 45 additions & 4 deletions packages/engine/__test__/02_recorder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ describe('@logicflow/engine Recorder', () => {
test('Using the getExecutionRecord API, receive the complete execution record of the process.', async () => {
const engine = new Engine();
const flowData = {
/**
* node1 |--> node2
*/
graphData: {
nodes: [
{
Expand Down Expand Up @@ -33,18 +36,19 @@ describe('@logicflow/engine Recorder', () => {
/**
* [
* {
* taskId: '',
* nodeId: '',
* instanceId: '',
* actionId: '', // 某一个节点在某一次执行时生成的Id
* nodeId: '', // 流程图节点Id
* executionId: '', // 某一次执行的Id
* nodeType: '',
* timestamp: '',
* properties: {},
* }
* ]
*/
// TODO: 给个例子自定义执行记录
const execution = await engine.getExecutionRecord(executionId);
expect(execution.length).toBe(2);
expect(execution[1]).toHaveProperty('taskId');
expect(execution[1]).toHaveProperty('actionId');
expect(execution[1]).toHaveProperty('nodeId');
expect(execution[1]).toHaveProperty('executionId');
expect(execution[1]).toHaveProperty('nodeType');
Expand All @@ -53,4 +57,41 @@ describe('@logicflow/engine Recorder', () => {
expect(execution[1].nodeId).toBe('node2');
expect(execution[1].nodeType).toBe('TaskNode');
});
test('The execution record cannot be obtained when the number of executions exceeds the maximum number of executions.', async () => {
const engine = new Engine();
const flowData = {
/**
* node1 |--> node2
*/
graphData: {
nodes: [
{
id: 'node1',
type: 'StartNode',
properties: {}
},
{
id: 'node2',
type: 'TaskNode',
properties: {}
}
],
edges: [
{
id: 'edge1',
sourceNodeId: 'node1',
targetNodeId: 'node2',
}
]
},
global: {},
}
engine.load(flowData);
engine.recorder.setMaxRecorderNumber(2);
const result = await engine.execute();
await engine.execute();
await engine.execute();
const execution = await engine.getExecutionRecord(result.executionId);
expect(execution).toBe(null);
})
});
4 changes: 4 additions & 0 deletions packages/engine/__test__/03_condition.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ describe('@logicflow/engine condition', () => {
test('The process will not continue its execution if the condition expression evaluates to false.', async () => {
const engine = new Engine();
const flowData = {
/**
* node1 |--> node2
* |--> node3
*/
graphData: {
nodes: [
{
Expand Down
16 changes: 16 additions & 0 deletions packages/engine/__test__/04_execute.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ describe('@logicflow/engine Execute', () => {
test('When there are multiple start nodes in a process, all of them are executed by default.', async () => {
const engine = new Engine();
const flowData = {
/**
* node1 |--> node2
* node3 |--> node4
*/
graphData: {
nodes: [
{
Expand Down Expand Up @@ -59,6 +63,10 @@ describe('@logicflow/engine Execute', () => {
test('When there are multiple start nodes in a process, you can specify which start node to execute.', async () => {
const engine = new Engine();
const flowData = {
/**
* node1 |--> node2
* node3 |--> node4
*/
graphData: {
nodes: [
{
Expand Down Expand Up @@ -118,6 +126,10 @@ describe('@logicflow/engine Execute', () => {
test('When attempting to execute a non-existent start node in a process, an execution exception is raised.', async () => {
const engine = new Engine();
const flowData = {
/**
* node1 |--> node2
* node3 |--> node4
*/
graphData: {
nodes: [
{
Expand Down Expand Up @@ -177,6 +189,10 @@ describe('@logicflow/engine Execute', () => {
test('When there are multiple start nodes in a process, parallel execution is supported, and each start node generates a unique execution ID', async () => {
const engine = new Engine();
const flowData = {
/**
* node1 |--> node2
* node3 |--> node4
*/
graphData: {
nodes: [
{
Expand Down
88 changes: 58 additions & 30 deletions packages/engine/__test__/05_customNode.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,51 @@
import Engine, { TaskNode } from '../src/index';

describe('@logicflow/engine Customize Node', () => {
class UserTask extends TaskNode {
class DataNode extends TaskNode {
async action() {
return {
status: 'interrupted',
detail: {
formId: 'form_1'
}
};
this.globalData['dataSource'] = {
time: this.context.getTime(),
}
}
}
class Mod2Node extends TaskNode {
async action() {
const dataSource = this.globalData['dataSource'];
if (dataSource && dataSource.time) {
dataSource.time % 2 === 0 ? this.globalData['output'] = 'even' : this.globalData['output'] = 'odd';
}
}
}
class OutputNode extends TaskNode {
async action() {
const output = this.globalData['output'];
this.properties['output'] = output;
}
}
const engine = new Engine();
const engine = new Engine({
context: {
getTime() {
return new Date().getTime();
}
}
});
engine.register({
type: 'DataNode',
model: DataNode,
})
engine.register({
type: 'Mod2Node',
model: Mod2Node,
})
engine.register({
type: 'UserTask',
model: UserTask,
type: 'OutputNode',
model: OutputNode,
})

const flowData = {
/**
* node1 |--> node2(DataNode) |--> node3(Mod2Node) |--> node4(OutputNode)
*/
graphData: {
nodes: [
{
Expand All @@ -27,14 +56,19 @@ describe('@logicflow/engine Customize Node', () => {
},
{
id: 'node2',
type: 'UserTask',
type: 'DataNode',
properties: {}
},
{
id: 'node3',
type: 'TaskNode',
type: 'Mod2Node',
properties: {}
}
},
{
id: 'node4',
type: 'OutputNode',
properties: {}
},
],
edges: [
{
Expand All @@ -50,29 +84,23 @@ describe('@logicflow/engine Customize Node', () => {
targetNodeId: 'node3',
properties: {
}
}
},
{
id: 'edge3',
sourceNodeId: 'node3',
targetNodeId: 'node4',
properties: {
}
},
]
},
globalData: {
},
}
engine.load(flowData);
test('After executing the process, receive the flow status as "interrupted" and include detailed information returned by the custom node.', async () => {
test('When the process is completed, the output field in the properties attribute of the last node is odd or even.', async () => {
const result = await engine.execute();
expect(result.status).toBe('interrupted');
expect(result.detail.formId).toEqual('form_1');
});
test('After a process is interrupted, you can resume its execution using the API.', async () => {
const result = await engine.execute();
const result2 = await engine.resume({
executionId: result.executionId,
nodeId: result.nodeId,
taskId: result.taskId,
data: {
formId: 'form_2'
}
})
expect(result2.status).toBe('completed')
expect(result2.nodeId).toEqual('node3')
const execution = await engine.getExecutionRecord(result.executionId);
expect(['odd', 'even'].indexOf(execution[execution.length - 1].properties.output) !== -1).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Engine, { TaskNode } from '../src/index';

describe('@logicflow/engine parallel and serial', () => {
describe('@logicflow/engine parallel execution', () => {
class FetchNode extends TaskNode {
async action() {
await this.fetch()
Expand All @@ -19,6 +19,10 @@ describe('@logicflow/engine parallel and serial', () => {
model: FetchNode,
})
const flowData = {
/**
* node1 |--> node2(FetchTask)
* |--> node3 |--> node4
*/
graphData: {
nodes: [
{
Expand Down Expand Up @@ -77,12 +81,4 @@ describe('@logicflow/engine parallel and serial', () => {
expect(execution.length).toBe(4);
expect(execution[3].nodeId).toEqual('node2')
});
test('When the process is executed twice, the second execution will start only after the first execution is completed.', async () => {
const r = engine.execute();
const r2 = engine.execute();
const result = await Promise.all([r, r2]);
const execution1 = await engine.getExecutionRecord(result[0].executionId);
const execution2 = await engine.getExecutionRecord(result[1].executionId);
expect(execution2[0].timestamp >= execution1[3].timestamp).toBe(true)
});
})
})
Loading

0 comments on commit 01a9aba

Please sign in to comment.