-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathSchedulerClient.test.ts
More file actions
168 lines (152 loc) · 5.15 KB
/
SchedulerClient.test.ts
File metadata and controls
168 lines (152 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { expect, describe, test, jest, afterAll } from "@jest/globals";
import {
ExtendedWorkflowDef,
SaveScheduleRequest,
TaskType,
} from "../open-api";
import {
MetadataClient,
orkesConductorClient,
SchedulerClient,
} from "../sdk";
describe("SchedulerClient", () => {
const clientPromise = orkesConductorClient();
jest.setTimeout(15000);
const now = Date.now();
const name = `jsSdkTestSchedule_${now}`;
const cronExpression = "0/5 * * ? * *"; //every 5 second
const workflowName = `jsSdkTestScheduleWf_${now}`;
const workflowVersion = 1;
afterAll(async () => {
const client = await clientPromise;
const schedulerClient = new SchedulerClient(client);
const metadataClient = new MetadataClient(client);
try {
await schedulerClient.deleteSchedule(name);
} catch (e) {
console.debug(`Failed to cleanup schedule ${name}:`, e);
}
try {
await metadataClient.unregisterWorkflow(workflowName, workflowVersion);
} catch (e) {
console.debug(
`Failed to cleanup workflow ${workflowName}:`,
e
);
}
});
test("Should be able to register a workflow and retrieve it", async () => {
const client = await clientPromise;
const executor = new SchedulerClient(client);
const workflowDefinition: ExtendedWorkflowDef = {
name: workflowName,
version: workflowVersion,
description: "Test Workflow for Scheduler",
tasks: [
{
type: TaskType.SET_VARIABLE,
name: "setVariable",
taskReferenceName: "httpTaskRef",
inputParameters: {
hello: "world",
},
},
],
inputParameters: [],
timeoutSeconds: 15,
};
const metadataClient = new MetadataClient(client);
await metadataClient.registerWorkflowDef(workflowDefinition, true);
const workflowDefinitionFromApi = await metadataClient.getWorkflowDef(
workflowName,
workflowVersion
);
expect(workflowDefinitionFromApi).toBeDefined();
if (!workflowDefinitionFromApi) {
throw new Error("Workflow definition is undefined");
}
expect(workflowDefinitionFromApi.name).toEqual(workflowName);
expect(workflowDefinitionFromApi.version).toEqual(workflowVersion);
const schedulerDefinition: SaveScheduleRequest = {
name,
cronExpression,
paused: true,
runCatchupScheduleInstances: false,
startWorkflowRequest: {
name: workflowDefinitionFromApi.name,
version: workflowDefinitionFromApi.version,
input: {},
correlationId: "",
taskToDomain: {},
priority: 0,
},
};
await expect(
executor.saveSchedule(schedulerDefinition)
).resolves.not.toThrow();
const scheduler = await executor.getSchedule(name);
expect(scheduler).toBeDefined();
if (!scheduler) {
throw new Error("Scheduler is undefined");
}
expect(scheduler.name).toEqual(name);
expect(scheduler.cronExpression).toEqual(cronExpression);
});
test("Should be able to resume the schedule", async () => {
const client = await clientPromise;
const executor = new SchedulerClient(client);
await executor.resumeSchedule(name);
const scheduler = await executor.getSchedule(name);
expect(scheduler).toBeDefined();
if (!scheduler) {
throw new Error("Scheduler is undefined");
}
expect(scheduler.paused).toBeFalsy();
});
test("Should be able to see the result in executed schedule", async () => {
const client = await clientPromise;
const executor = new SchedulerClient(client);
await new Promise((resolve) => setTimeout(resolve, 10000));
const result = await executor.search(
0,
15,
"startTime:DESC",
"",
`scheduleName IN (${name})`
);
expect(result?.results?.length).toBeGreaterThan(0);
});
test("Should be able to pause the schedule", async () => {
const client = await clientPromise;
const executor = new SchedulerClient(client);
await executor.pauseSchedule(name);
const scheduler = await executor.getSchedule(name);
if (!scheduler) {
throw new Error("Scheduler is undefined");
}
expect(scheduler.paused).toBeTruthy();
});
test("Should be able to delete the schedule", async () => {
const client = await clientPromise;
const executor = new SchedulerClient(client);
const metadataClient = new MetadataClient(client);
await executor.deleteSchedule(name);
// delete workflowDef too
await metadataClient.unregisterWorkflow(workflowName, workflowVersion);
const schedulerList = await executor.getAllSchedules();
if (!schedulerList) {
throw new Error("Scheduler list is undefined");
}
const testSchedule = schedulerList.some(
(schedule) => schedule.name === name
);
expect(testSchedule).toBeFalsy();
});
test("Should be able to retrieve next (default 3) execution times for a scheduler", async () => {
const cronExpression = "0 0 * ? * *"; //every hour
const client = await clientPromise;
const executor = new SchedulerClient(client);
const result = await executor.getNextFewSchedules(cronExpression);
expect(result?.length).toBeGreaterThan(0);
});
});