Skip to content

Commit c6ab312

Browse files
authored
chore: cleanup queues test suite (#11410)
This PR extracts each workflow of our queues test suite into its own file
1 parent 526e535 commit c6ab312

16 files changed

+744
-673
lines changed

test/queues/config.ts

Lines changed: 31 additions & 673 deletions
Large diffs are not rendered by default.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { WorkflowConfig } from 'payload'
2+
3+
import path from 'path'
4+
import { fileURLToPath } from 'url'
5+
6+
const filename = fileURLToPath(import.meta.url)
7+
const dirname = path.dirname(filename)
8+
9+
export const externalWorkflow: WorkflowConfig<'externalWorkflow'> = {
10+
slug: 'externalWorkflow',
11+
inputSchema: [
12+
{
13+
name: 'message',
14+
type: 'text',
15+
required: true,
16+
},
17+
],
18+
handler: path.resolve(dirname, '../runners/externalWorkflow.ts') + '#externalWorkflowHandler',
19+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { WorkflowConfig } from 'payload'
2+
3+
export const inlineTaskTestWorkflow: WorkflowConfig<'inlineTaskTest'> = {
4+
slug: 'inlineTaskTest',
5+
inputSchema: [
6+
{
7+
name: 'message',
8+
type: 'text',
9+
required: true,
10+
},
11+
],
12+
handler: async ({ job, inlineTask }) => {
13+
await inlineTask('1', {
14+
task: async ({ input, req }) => {
15+
const newSimple = await req.payload.create({
16+
collection: 'simple',
17+
req,
18+
data: {
19+
title: input.message,
20+
},
21+
})
22+
return {
23+
output: {
24+
simpleID: newSimple.id,
25+
},
26+
}
27+
},
28+
input: {
29+
message: job.input.message,
30+
},
31+
})
32+
},
33+
}

test/queues/workflows/noRetriesSet.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type { WorkflowConfig } from 'payload'
2+
3+
export const noRetriesSetWorkflow: WorkflowConfig<'workflowNoRetriesSet'> = {
4+
slug: 'workflowNoRetriesSet',
5+
inputSchema: [
6+
{
7+
name: 'message',
8+
type: 'text',
9+
required: true,
10+
},
11+
],
12+
handler: async ({ job, tasks, req }) => {
13+
await req.payload.update({
14+
collection: 'payload-jobs',
15+
data: {
16+
input: {
17+
...job.input,
18+
amountRetried:
19+
// @ts-expect-error amountRetried is new arbitrary data and not in the type
20+
job.input.amountRetried !== undefined ? job.input.amountRetried + 1 : 0,
21+
},
22+
},
23+
id: job.id,
24+
})
25+
26+
await tasks.CreateSimple('1', {
27+
input: {
28+
message: job.input.message,
29+
},
30+
})
31+
32+
// At this point there should always be one post created.
33+
// job.input.amountRetried will go up to 2 as CreatePost has 2 retries
34+
await tasks.CreateSimple('2', {
35+
input: {
36+
message: job.input.message,
37+
shouldFail: true,
38+
},
39+
})
40+
// This will never be reached
41+
},
42+
}

test/queues/workflows/retries0.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import type { WorkflowConfig } from 'payload'
2+
3+
export const retries0Workflow: WorkflowConfig<'workflowRetries0'> = {
4+
slug: 'workflowRetries0',
5+
inputSchema: [
6+
{
7+
name: 'message',
8+
type: 'text',
9+
required: true,
10+
},
11+
],
12+
retries: 0,
13+
handler: async ({ job, tasks, req }) => {
14+
await req.payload.update({
15+
collection: 'payload-jobs',
16+
data: {
17+
input: {
18+
...job.input,
19+
amountRetried:
20+
// @ts-expect-error amountRetried is new arbitrary data and not in the type
21+
job.input.amountRetried !== undefined ? job.input.amountRetried + 1 : 0,
22+
},
23+
},
24+
id: job.id,
25+
})
26+
27+
await tasks.CreateSimple('1', {
28+
input: {
29+
message: job.input.message,
30+
},
31+
})
32+
33+
// At this point there should always be one post created.
34+
// job.input.amountRetried will go up to 2 as CreatePost has 2 retries
35+
await tasks.CreateSimple('2', {
36+
input: {
37+
message: job.input.message,
38+
shouldFail: true,
39+
},
40+
})
41+
// This will never be reached
42+
},
43+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import type { WorkflowConfig } from 'payload'
2+
3+
export const retriesBackoffTestWorkflow: WorkflowConfig<'retriesBackoffTest'> = {
4+
slug: 'retriesBackoffTest',
5+
inputSchema: [
6+
{
7+
name: 'message',
8+
type: 'text',
9+
required: true,
10+
},
11+
],
12+
handler: async ({ job, inlineTask, req }) => {
13+
const newJob = await req.payload.update({
14+
collection: 'payload-jobs',
15+
data: {
16+
input: {
17+
...job.input,
18+
amountRetried:
19+
// @ts-expect-error amountRetried is new arbitrary data and not in the type
20+
job.input.amountRetried !== undefined ? job.input.amountRetried + 1 : 0,
21+
},
22+
},
23+
id: job.id,
24+
})
25+
job.input = newJob.input as any
26+
27+
await inlineTask('1', {
28+
task: async ({ req }) => {
29+
const totalTried = job?.taskStatus?.inline?.['1']?.totalTried || 0
30+
31+
const { id } = await req.payload.create({
32+
collection: 'simple',
33+
req,
34+
data: {
35+
title: 'should not exist',
36+
},
37+
})
38+
39+
// @ts-expect-error timeTried is new arbitrary data and not in the type
40+
if (!job.input.timeTried) {
41+
// @ts-expect-error timeTried is new arbitrary data and not in the type
42+
job.input.timeTried = {}
43+
}
44+
45+
// @ts-expect-error timeTried is new arbitrary data and not in the type
46+
job.input.timeTried[totalTried] = new Date().toISOString()
47+
48+
await req.payload.update({
49+
collection: 'payload-jobs',
50+
data: {
51+
input: job.input,
52+
},
53+
id: job.id,
54+
})
55+
56+
if (totalTried < 4) {
57+
// Cleanup the post
58+
await req.payload.delete({
59+
collection: 'simple',
60+
id,
61+
req,
62+
})
63+
64+
// Last try it should succeed
65+
throw new Error('Failed on purpose')
66+
}
67+
return {
68+
output: {},
69+
}
70+
},
71+
retries: {
72+
attempts: 4,
73+
backoff: {
74+
type: 'exponential',
75+
// Should retry in 300ms, then 600, then 1200, then 2400, then succeed
76+
delay: 300,
77+
},
78+
},
79+
})
80+
},
81+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import type { WorkflowConfig } from 'payload'
2+
3+
export const retriesRollbackTestWorkflow: WorkflowConfig<'retriesRollbackTest'> = {
4+
slug: 'retriesRollbackTest',
5+
inputSchema: [
6+
{
7+
name: 'message',
8+
type: 'text',
9+
required: true,
10+
},
11+
],
12+
handler: async ({ job, inlineTask, req }) => {
13+
await req.payload.update({
14+
collection: 'payload-jobs',
15+
data: {
16+
input: {
17+
...job.input,
18+
amountRetried:
19+
// @ts-expect-error amountRetried is new arbitrary data and not in the type
20+
job.input.amountRetried !== undefined ? job.input.amountRetried + 1 : 0,
21+
},
22+
},
23+
id: job.id,
24+
})
25+
26+
await inlineTask('1', {
27+
task: async ({ req }) => {
28+
const newSimple = await req.payload.create({
29+
collection: 'simple',
30+
req,
31+
data: {
32+
title: job.input.message,
33+
},
34+
})
35+
return {
36+
output: {
37+
simpleID: newSimple.id,
38+
},
39+
}
40+
},
41+
})
42+
43+
await inlineTask('2', {
44+
task: async ({ req }) => {
45+
await req.payload.create({
46+
collection: 'simple',
47+
req,
48+
data: {
49+
title: 'should not exist',
50+
},
51+
})
52+
// Fail afterwards, so that we can also test that transactions work (i.e. the job is rolled back)
53+
54+
throw new Error('Failed on purpose')
55+
},
56+
retries: {
57+
attempts: 4,
58+
},
59+
})
60+
},
61+
}

test/queues/workflows/retriesTest.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type { WorkflowConfig } from 'payload'
2+
3+
export const retriesTestWorkflow: WorkflowConfig<'retriesTest'> = {
4+
slug: 'retriesTest',
5+
inputSchema: [
6+
{
7+
name: 'message',
8+
type: 'text',
9+
required: true,
10+
},
11+
],
12+
handler: async ({ job, tasks, req }) => {
13+
await req.payload.update({
14+
collection: 'payload-jobs',
15+
data: {
16+
input: {
17+
...job.input,
18+
amountRetried:
19+
// @ts-expect-error amountRetried is new arbitrary data and not in the type
20+
job.input.amountRetried !== undefined ? job.input.amountRetried + 1 : 0,
21+
},
22+
},
23+
id: job.id,
24+
})
25+
26+
await tasks.CreateSimple('1', {
27+
input: {
28+
message: job.input.message,
29+
},
30+
})
31+
32+
// At this point there should always be one post created.
33+
// job.input.amountRetried will go up to 2 as CreatePost has 2 retries
34+
await tasks.CreateSimple('2', {
35+
input: {
36+
message: job.input.message,
37+
shouldFail: true,
38+
},
39+
})
40+
// This will never be reached
41+
},
42+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import type { WorkflowConfig } from 'payload'
2+
3+
export const retriesWorkflowLevelTestWorkflow: WorkflowConfig<'retriesWorkflowLevelTest'> = {
4+
slug: 'retriesWorkflowLevelTest',
5+
inputSchema: [
6+
{
7+
name: 'message',
8+
type: 'text',
9+
required: true,
10+
},
11+
],
12+
retries: 2, // Even though CreateSimple has 3 retries, this workflow only has 2. Thus, it will only retry once
13+
handler: async ({ job, tasks, req }) => {
14+
await req.payload.update({
15+
collection: 'payload-jobs',
16+
data: {
17+
input: {
18+
...job.input,
19+
amountRetried:
20+
// @ts-expect-error amountRetried is new arbitrary data and not in the type
21+
job.input.amountRetried !== undefined ? job.input.amountRetried + 1 : 0,
22+
},
23+
},
24+
id: job.id,
25+
})
26+
27+
await tasks.CreateSimple('1', {
28+
input: {
29+
message: job.input.message,
30+
},
31+
})
32+
33+
// At this point there should always be one post created.
34+
// job.input.amountRetried will go up to 2 as CreatePost has 2 retries
35+
await tasks.CreateSimple('2', {
36+
input: {
37+
message: job.input.message,
38+
shouldFail: true,
39+
},
40+
})
41+
// This will never be reached
42+
},
43+
}

0 commit comments

Comments
 (0)