PeerTube/server/tests/real-world/populate-database.ts

124 lines
2.8 KiB
TypeScript
Raw Normal View History

2018-07-17 18:17:50 +02:00
import { VideoRateType } from '../../../shared'
import {
addVideoChannel,
createUser,
flushTests,
getVideosList,
killallServers,
rateVideo,
runServer,
ServerInfo,
setAccessTokensToServers,
uploadVideo
} from '../../../shared/utils'
2018-07-17 18:17:50 +02:00
import * as Bluebird from 'bluebird'
start()
.catch(err => console.error(err))
// ----------------------------------------------------------------------------
async function start () {
await flushTests()
console.log('Flushed tests.')
const server = await runServer(6)
2018-07-17 18:17:50 +02:00
process.on('exit', async () => {
killallServers([ server ])
return
})
process.on('SIGINT', goodbye)
process.on('SIGTERM', goodbye)
await setAccessTokensToServers([ server ])
console.log('Servers ran.')
// Forever
const fakeTab = Array.from(Array(1000000).keys())
2018-07-18 14:17:54 +02:00
const funs = [
uploadCustom
// uploadCustom,
// uploadCustom,
// uploadCustom,
// likeCustom,
// createUserCustom,
// createCustomChannel
]
const promises = []
for (const fun of funs) {
promises.push(
Bluebird.map(fakeTab, () => {
return fun(server).catch(err => console.error(err))
}, { concurrency: 3 })
)
}
await Promise.all(promises)
2018-07-17 18:17:50 +02:00
}
function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min)) + min
}
function createCustomChannel (server: ServerInfo) {
const videoChannel = {
2018-08-17 15:45:42 +02:00
name: Date.now().toString(),
2018-07-17 18:17:50 +02:00
displayName: Date.now().toString(),
description: Date.now().toString()
}
return addVideoChannel(server.url, server.accessToken, videoChannel)
}
function createUserCustom (server: ServerInfo) {
const username = Date.now().toString() + getRandomInt(0, 100000)
console.log('Creating user %s.', username)
return createUser(server.url, server.accessToken, username, 'coucou')
}
function uploadCustom (server: ServerInfo) {
console.log('Uploading video.')
const videoAttributes = {
name: Date.now() + ' name',
category: 4,
nsfw: false,
licence: 2,
language: 'en',
description: Date.now() + ' description',
tags: [ Date.now().toString().substring(0, 5) + 't1', Date.now().toString().substring(0, 5) + 't2' ],
fixture: 'video_short.mp4'
}
return uploadVideo(server.url, server.accessToken, videoAttributes)
}
function likeCustom (server: ServerInfo) {
return rateCustom(server, 'like')
}
function dislikeCustom (server: ServerInfo) {
return rateCustom(server, 'dislike')
}
async function rateCustom (server: ServerInfo, rating: VideoRateType) {
const res = await getVideosList(server.url)
const videos = res.body.data
if (videos.length === 0) return undefined
const videoToRate = videos[getRandomInt(0, videos.length)]
console.log('Rating (%s) video.', rating)
return rateVideo(server.url, server.accessToken, videoToRate.id, rating)
}
function goodbye () {
return process.exit(-1)
}