1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { VideoDetails } from '../../../../shared/models/videos'
8 flushAndRunMultipleServers,
10 getFollowingListPaginationAndSort,
14 setAccessTokensToServers,
17 root, viewVideo, immutableAssign
19 import { waitJobs } from '../../utils/server/jobs'
20 import * as magnetUtil from 'magnet-uri'
21 import { updateRedundancy } from '../../utils/server/redundancy'
22 import { ActorFollow } from '../../../../shared/models/actors'
23 import { readdir } from 'fs-extra'
24 import { join } from 'path'
25 import { VideoRedundancyStrategy } from '../../../../shared/models/redundancy'
27 const expect = chai.expect
29 let servers: ServerInfo[] = []
30 let video1Server2UUID: string
31 let video2Server2UUID: string
33 function checkMagnetWebseeds (file: { magnetUri: string, resolution: { id: number } }, baseWebseeds: string[]) {
34 const parsed = magnetUtil.decode(file.magnetUri)
36 for (const ws of baseWebseeds) {
37 const found = parsed.urlList.find(url => url === `${ws}-${file.resolution.id}.mp4`)
38 expect(found, `Webseed ${ws} not found in ${file.magnetUri}`).to.not.be.undefined
42 async function runServers (strategy: VideoRedundancyStrategy, additionalParams: any = {}) {
53 servers = await flushAndRunMultipleServers(3, config)
55 // Get the access tokens
56 await setAccessTokensToServers(servers)
59 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 1 server 2' })
60 video1Server2UUID = res.body.video.uuid
62 await viewVideo(servers[ 1 ].url, video1Server2UUID)
66 const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 2 server 2' })
67 video2Server2UUID = res.body.video.uuid
70 await waitJobs(servers)
72 // Server 1 and server 2 follow each other
73 await doubleFollow(servers[ 0 ], servers[ 1 ])
74 // Server 1 and server 3 follow each other
75 await doubleFollow(servers[ 0 ], servers[ 2 ])
76 // Server 2 and server 3 follow each other
77 await doubleFollow(servers[ 1 ], servers[ 2 ])
79 await waitJobs(servers)
82 async function check1WebSeed () {
84 'http://localhost:9002/static/webseed/' + video1Server2UUID
87 for (const server of servers) {
88 const res = await getVideo(server.url, video1Server2UUID)
90 const video: VideoDetails = res.body
91 video.files.forEach(f => checkMagnetWebseeds(f, webseeds))
95 async function enableRedundancy () {
96 await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, true)
98 const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 5, '-createdAt')
99 const follows: ActorFollow[] = res.body.data
100 const server2 = follows.find(f => f.following.host === 'localhost:9002')
101 const server3 = follows.find(f => f.following.host === 'localhost:9003')
103 expect(server3).to.not.be.undefined
104 expect(server3.following.hostRedundancyAllowed).to.be.false
106 expect(server2).to.not.be.undefined
107 expect(server2.following.hostRedundancyAllowed).to.be.true
110 async function check2Webseeds () {
111 await waitJobs(servers)
113 await waitJobs(servers)
116 'http://localhost:9001/static/webseed/' + video1Server2UUID,
117 'http://localhost:9002/static/webseed/' + video1Server2UUID
120 for (const server of servers) {
121 const res = await getVideo(server.url, video1Server2UUID)
123 const video: VideoDetails = res.body
125 for (const file of video.files) {
126 checkMagnetWebseeds(file, webseeds)
130 const files = await readdir(join(root(), 'test1', 'videos'))
131 expect(files).to.have.lengthOf(4)
133 for (const resolution of [ 240, 360, 480, 720 ]) {
134 expect(files.find(f => f === `${video1Server2UUID}-${resolution}.mp4`)).to.not.be.undefined
138 async function cleanServers () {
139 killallServers(servers)
142 describe('Test videos redundancy', function () {
144 describe('With most-views strategy', function () {
149 return runServers('most-views')
152 it('Should have 1 webseed on the first video', function () {
153 return check1WebSeed()
156 it('Should enable redundancy on server 1', function () {
157 return enableRedundancy()
160 it('Should have 2 webseed on the first video', function () {
163 return check2Webseeds()
167 return cleanServers()
171 describe('With trending strategy', function () {
176 return runServers('trending')
179 it('Should have 1 webseed on the first video', function () {
180 return check1WebSeed()
183 it('Should enable redundancy on server 1', function () {
184 return enableRedundancy()
187 it('Should have 2 webseed on the first video', function () {
190 return check2Webseeds()
194 return cleanServers()
198 describe('With recently added strategy', function () {
203 return runServers('recently-added', { minViews: 3 })
206 it('Should have 1 webseed on the first video', function () {
207 return check1WebSeed()
210 it('Should enable redundancy on server 1', function () {
211 return enableRedundancy()
214 it('Should still have 1 webseed on the first video', async function () {
217 await waitJobs(servers)
219 await waitJobs(servers)
221 return check1WebSeed()
224 it('Should view 2 times the first video', async function () {
227 await viewVideo(servers[ 0 ].url, video1Server2UUID)
228 await viewVideo(servers[ 2 ].url, video1Server2UUID)
231 await waitJobs(servers)
234 it('Should have 2 webseed on the first video', function () {
237 return check2Webseeds()
241 return cleanServers()