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