]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/server/jobs.ts
Fix job queue tests
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / jobs.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
5cd80545 2
5cd80545 3import 'mocha'
c3d29f69 4import * as chai from 'chai'
c55e3d72 5import { dateIsValid } from '@server/tests/shared'
db1ccd05 6import { wait } from '@shared/core-utils'
9c6327f8
C
7import {
8 cleanupTests,
4c7e60bc 9 createMultipleServers,
9c6327f8 10 doubleFollow,
254d3579 11 PeerTubeServer,
9c6327f8 12 setAccessTokensToServers,
9c6327f8 13 waitJobs
bf54587a 14} from '@shared/server-commands'
5cd80545
C
15
16const expect = chai.expect
17
18describe('Test jobs', function () {
254d3579 19 let servers: PeerTubeServer[]
5cd80545
C
20
21 before(async function () {
22 this.timeout(30000)
23
254d3579 24 servers = await createMultipleServers(2)
5cd80545
C
25
26 await setAccessTokensToServers(servers)
27
28 // Server 1 and server 2 follow each other
29 await doubleFollow(servers[0], servers[1])
30 })
31
32 it('Should create some jobs', async function () {
7926c5f9 33 this.timeout(120000)
5cd80545 34
89d241a7
C
35 await servers[1].videos.upload({ attributes: { name: 'video1' } })
36 await servers[1].videos.upload({ attributes: { name: 'video2' } })
5cd80545 37
3cd0734f 38 await waitJobs(servers)
5cd80545
C
39 })
40
41 it('Should list jobs', async function () {
851675c5 42 const body = await servers[1].jobs.list({ state: 'completed' })
9c6327f8
C
43 expect(body.total).to.be.above(2)
44 expect(body.data).to.have.length.above(2)
5cd80545
C
45 })
46
1061c73f
C
47 it('Should list jobs with sort, pagination and job type', async function () {
48 {
851675c5 49 const body = await servers[1].jobs.list({
1061c73f
C
50 state: 'completed',
51 start: 1,
52 count: 2,
53 sort: 'createdAt'
54 })
9c6327f8
C
55 expect(body.total).to.be.above(2)
56 expect(body.data).to.have.lengthOf(2)
1061c73f 57
9c6327f8 58 let job = body.data[0]
1061c73f 59 // Skip repeat jobs
51353d9a 60 if (job.type === 'videos-views-stats') job = body.data[1]
1061c73f
C
61
62 expect(job.state).to.equal('completed')
1061c73f
C
63 expect(dateIsValid(job.createdAt as string)).to.be.true
64 expect(dateIsValid(job.processedOn as string)).to.be.true
65 expect(dateIsValid(job.finishedOn as string)).to.be.true
66 }
5cd80545 67
1061c73f 68 {
851675c5 69 const body = await servers[1].jobs.list({
1061c73f
C
70 state: 'completed',
71 start: 0,
72 count: 100,
73 sort: 'createdAt',
74 jobType: 'activitypub-http-broadcast'
75 })
9c6327f8 76 expect(body.total).to.be.above(2)
94a5ff8a 77
9c6327f8 78 for (const j of body.data) {
1061c73f
C
79 expect(j.type).to.equal('activitypub-http-broadcast')
80 }
81 }
5cd80545
C
82 })
83
402145b8 84 it('Should list all jobs', async function () {
851675c5 85 const body = await servers[1].jobs.list()
9c6327f8 86 expect(body.total).to.be.above(2)
402145b8 87
9c6327f8 88 const jobs = body.data
402145b8
C
89 expect(jobs).to.have.length.above(2)
90
402145b8
C
91 expect(jobs.find(j => j.state === 'completed')).to.not.be.undefined
92 })
93
419b520c
C
94 it('Should pause the job queue', async function () {
95 this.timeout(120000)
96
52fe4b67
C
97 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video2' } })
98 await waitJobs(servers)
419b520c 99
52fe4b67
C
100 await servers[1].jobs.pauseJobQueue()
101 await servers[1].videos.runTranscoding({ videoId: uuid, transcodingType: 'hls' })
419b520c
C
102
103 await wait(5000)
104
e2b2c726
C
105 {
106 const body = await servers[1].jobs.list({ state: 'waiting', jobType: 'video-transcoding' })
107 // waiting includes waiting-children
108 expect(body.data).to.have.lengthOf(4)
109 }
110
111 {
112 const body = await servers[1].jobs.list({ state: 'waiting-children', jobType: 'video-transcoding' })
113 expect(body.data).to.have.lengthOf(1)
114 }
419b520c
C
115 })
116
117 it('Should resume the job queue', async function () {
118 this.timeout(120000)
119
120 await servers[1].jobs.resumeJobQueue()
121
122 await waitJobs(servers)
123
124 const body = await servers[1].jobs.list({ state: 'waiting', jobType: 'video-transcoding' })
125 expect(body.data).to.have.lengthOf(0)
126 })
127
7c3b7976
C
128 after(async function () {
129 await cleanupTests(servers)
5cd80545
C
130 })
131})