aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/real-world/populate-database.ts
blob: f0f82f7f8cf2fd587906a52c89cb8a5373eaae68 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { VideoRateType } from '../../../shared'
import {
  addVideoChannel,
  createUser,
  flushTests,
  getVideosList,
  killallServers,
  rateVideo,
  runServer,
  ServerInfo,
  setAccessTokensToServers,
  uploadVideo
} from '../utils'
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)

  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())
  await Bluebird.map(fakeTab, () => {
    return Promise.all([
      uploadCustom(server),
      likeCustom(server),
      dislikeCustom(server),
      createUserCustom(server),
      createCustomChannel(server)
    ]).catch(err => console.error(err))
  }, { concurrency: 5 })
}

function getRandomInt (min, max) {
  return Math.floor(Math.random() * (max - min)) + min
}

function createCustomChannel (server: ServerInfo) {
  const videoChannel = {
    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)
}