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