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