Skip to content

Commit 33cd89c

Browse files
authored
fix: standby tests (#76)
- make the stanby task's name shorter (to be dns friendly) - annotate standby runs right after we get response - put task deletion in finally block
1 parent bea6997 commit 33cd89c

1 file changed

Lines changed: 39 additions & 30 deletions

File tree

lib/lib.ts

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -92,29 +92,24 @@ export const testStandbyActor = <I = any, O = any>(
9292

9393
vitestTest.runIf(shouldRun)(name, options, async <T extends TestContext>(context: T) => {
9494
const standbyTask = await createStandbyTask(actorName, config.get(actorName)?.buildNumber);
95-
const { annotate } = context;
9695
const { expect, ...rest } = context;
9796

98-
// NOTE: we need to wrap `fn` in try-catch so that we can always clean up (delete the task) afterwards
97+
// NOTE: we wrap `fn` in try/finally so cleanup (deleting the task) always runs afterwards
9998
try {
10099
await fn({
101100
expect: extendExpect(expect),
102-
callStandby: createStartStandbyFn(standbyTask),
101+
callStandby: createStartStandbyFn(standbyTask, context, name),
103102
...rest,
104103
});
105-
} catch {
106-
/* */
107-
}
108-
109-
const { taskId } = standbyTask;
110-
const runs = (await apifyClient.task(taskId).runs().list()).items;
111-
for (const run of runs) {
112-
const runLink = generateRunLink(run);
113-
await annotate(`${name} - ${runLink}`, 'run_link');
114-
}
115-
116-
if (taskId) {
117-
await apifyClient.task(taskId).delete();
104+
} finally {
105+
const { taskId } = standbyTask;
106+
// we want to delete the task at the end of the test
107+
await apifyClient
108+
.task(taskId)
109+
.delete()
110+
.catch((error) => {
111+
console.error(`Failed to delete standby task "${taskId}": ${error}`);
112+
});
118113
}
119114
});
120115
};
@@ -129,7 +124,7 @@ export const testTestActor = <T>(
129124
expect: extendExpect(expect),
130125
// @ts-expect-error: this just to test custom matchers
131126
// eslint-disable-next-line @typescript-eslint/no-empty-function
132-
run: () => {},
127+
run: () => { },
133128
...rest,
134129
});
135130
});
@@ -138,11 +133,28 @@ export const testTestActor = <T>(
138133
export const it = testActor;
139134

140135
/**
141-
* Creates a function the accepts input for a standby actor and sends request containing input
136+
* Creates a function that accepts input for a standby actor and sends a request containing the input
142137
* to the task's standby url.
143138
*/
144-
const createStartStandbyFn = <I, O>(standbyTask: StandbyTask) => {
145-
const { standbyUrl } = standbyTask;
139+
const createStartStandbyFn = <I, O>(standbyTask: StandbyTask, { annotate }: TestContext, testName: string) => {
140+
const { standbyUrl, taskId } = standbyTask;
141+
const annotatedRuns = new Set<string>();
142+
143+
// We annotate all the runs of the task, though it will be only one run
144+
// for most of the tests. To avoid annotating the same run multiple times
145+
// (in case the test calls the standby more than once), we use `annotatedRuns`
146+
// set to keep track of which runs have already been annotated.
147+
const annotateStandbyRuns = async () => {
148+
const runs = (await apifyClient.task(taskId).runs().list()).items;
149+
for (const run of runs) {
150+
if (!annotatedRuns.has(run.id)) {
151+
const runLink = generateRunLink(run);
152+
await annotate(`${testName} - ${runLink}`, 'run_link');
153+
}
154+
annotatedRuns.add(run.id);
155+
}
156+
};
157+
146158
return async ({
147159
input,
148160
path = '',
@@ -157,6 +169,8 @@ const createStartStandbyFn = <I, O>(standbyTask: StandbyTask) => {
157169
body: JSON.stringify(input),
158170
});
159171

172+
await annotateStandbyRuns();
173+
160174
const data = (await response.json()) as O;
161175
return {
162176
data,
@@ -171,10 +185,6 @@ interface StandbyTask {
171185
taskId: string;
172186
}
173187

174-
const randomInt = (min: number, max: number) => {
175-
return Math.floor(Math.random() * (max - min + 1)) + min;
176-
};
177-
178188
/**
179189
* Creates a task with specific `build` - either `buildNumber` or default.
180190
*
@@ -206,16 +216,15 @@ const createStandbyTask = async (actorNameOrId: string, buildNumber?: string): P
206216
};
207217

208218
try {
209-
const title = `Test task - ${build}:${actorNameOrId}`.slice(0, 62);
219+
const title = `Test task - ${build}`.slice(0, 62);
220+
const randomValueLength = 15;
210221
// we try to create unique task name containing only `a-z0-9-` characters and at most 63 characters long
211-
const name = `${randomInt(1, 1_000_000)}${title
212-
.toLowerCase()
213-
.replaceAll(/\s+/g, '')
214-
.replaceAll(/[^a-z0-9-]+/g, '-')}`.slice(0, 62);
222+
const randomValue = Math.random().toString(10).slice(2).padEnd(randomValueLength, '0');
223+
const name = `test-${randomValue.slice(0, randomValueLength)}`;
215224
const newTask = (await apifyClient.tasks().create({
216225
actId: actorNameOrId,
217226
actorStandby: actorStandbyOptions,
218-
description: `Task for testing standby version ${build}`,
227+
description: `Task for testing standby version ${build} of actor "${actorNameOrId}"`,
219228
title,
220229
name,
221230
})) as Task & { standbyUrl?: string };

0 commit comments

Comments
 (0)