2
1

populate-database.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { VideoRateType } from '../../../shared'
  2. import {
  3. addVideoChannel,
  4. createUser,
  5. flushTests,
  6. getVideosList,
  7. killallServers,
  8. rateVideo,
  9. flushAndRunServer,
  10. ServerInfo,
  11. setAccessTokensToServers,
  12. uploadVideo
  13. } from '../../../shared/extra-utils'
  14. import * as Bluebird from 'bluebird'
  15. start()
  16. .catch(err => console.error(err))
  17. // ----------------------------------------------------------------------------
  18. async function start () {
  19. console.log('Flushed tests.')
  20. const server = await flushAndRunServer(6)
  21. process.on('exit', async () => {
  22. killallServers([ server ])
  23. return
  24. })
  25. process.on('SIGINT', goodbye)
  26. process.on('SIGTERM', goodbye)
  27. await setAccessTokensToServers([ server ])
  28. console.log('Servers ran.')
  29. // Forever
  30. const fakeTab = Array.from(Array(1000000).keys())
  31. const funs = [
  32. uploadCustom
  33. // uploadCustom,
  34. // uploadCustom,
  35. // uploadCustom,
  36. // likeCustom,
  37. // createUserCustom,
  38. // createCustomChannel
  39. ]
  40. const promises = []
  41. for (const fun of funs) {
  42. promises.push(
  43. Bluebird.map(fakeTab, () => {
  44. return fun(server).catch(err => console.error(err))
  45. }, { concurrency: 3 })
  46. )
  47. }
  48. await Promise.all(promises)
  49. }
  50. function getRandomInt (min, max) {
  51. return Math.floor(Math.random() * (max - min)) + min
  52. }
  53. function createCustomChannel (server: ServerInfo) {
  54. const videoChannel = {
  55. name: Date.now().toString(),
  56. displayName: Date.now().toString(),
  57. description: Date.now().toString()
  58. }
  59. return addVideoChannel(server.url, server.accessToken, videoChannel)
  60. }
  61. function createUserCustom (server: ServerInfo) {
  62. const username = Date.now().toString() + getRandomInt(0, 100000)
  63. console.log('Creating user %s.', username)
  64. return createUser({ url: server.url, accessToken: server.accessToken, username: username, password: 'coucou' })
  65. }
  66. function uploadCustom (server: ServerInfo) {
  67. console.log('Uploading video.')
  68. const videoAttributes = {
  69. name: Date.now() + ' name',
  70. category: 4,
  71. nsfw: false,
  72. licence: 2,
  73. language: 'en',
  74. description: Date.now() + ' description',
  75. tags: [ Date.now().toString().substring(0, 5) + 't1', Date.now().toString().substring(0, 5) + 't2' ],
  76. fixture: 'video_short.mp4'
  77. }
  78. return uploadVideo(server.url, server.accessToken, videoAttributes)
  79. }
  80. function likeCustom (server: ServerInfo) {
  81. return rateCustom(server, 'like')
  82. }
  83. function dislikeCustom (server: ServerInfo) {
  84. return rateCustom(server, 'dislike')
  85. }
  86. async function rateCustom (server: ServerInfo, rating: VideoRateType) {
  87. const res = await getVideosList(server.url)
  88. const videos = res.body.data
  89. if (videos.length === 0) return undefined
  90. const videoToRate = videos[getRandomInt(0, videos.length)]
  91. console.log('Rating (%s) video.', rating)
  92. return rateVideo(server.url, server.accessToken, videoToRate.id, rating)
  93. }
  94. function goodbye () {
  95. return process.exit(-1)
  96. }