]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/real-world/real-world.js
Server: refractoring upload/update video test utils
[github/Chocobozzz/PeerTube.git] / server / tests / real-world / real-world.js
index 9a63860adca7c47c869ff2a9c64712014dfdef3b..32afeec68d0b8f4f437307a3472d44072fe60bd7 100644 (file)
@@ -13,10 +13,15 @@ const loginUtils = require('../utils/login')
 const podsUtils = require('../utils/pods')
 const serversUtils = require('../utils/servers')
 const videosUtils = require('../utils/videos')
+const requestStatsUtils = require('../utils/requests-stats')
 
 program
   .option('-c, --create [weight]', 'Weight for creating videos')
   .option('-r, --remove [weight]', 'Weight for removing videos')
+  .option('-u, --update [weight]', 'Weight for updating videos')
+  .option('-v, --view [weight]', 'Weight for viewing videos')
+  .option('-l, --like [weight]', 'Weight for liking videos')
+  .option('-s --dislike [weight]', 'Weight for disliking videos')
   .option('-p, --pods [n]', 'Number of pods to run (3 or 6)', /^3|6$/, 3)
   .option('-a, --action [interval]', 'Interval in ms for an action')
   .option('-i, --integrity [interval]', 'Interval in ms for an integrity check')
@@ -26,20 +31,18 @@ program
 
 const createWeight = program.create !== undefined ? parseInt(program.create) : 5
 const removeWeight = program.remove !== undefined ? parseInt(program.remove) : 4
+const updateWeight = program.update !== undefined ? parseInt(program.update) : 4
+const viewWeight = program.view !== undefined ? parseInt(program.view) : 4
+const likeWeight = program.like !== undefined ? parseInt(program.like) : 4
+const dislikeWeight = program.dislike !== undefined ? parseInt(program.dislike) : 4
 const flushAtExit = program.flush || false
 const actionInterval = program.action !== undefined ? parseInt(program.action) : 500
-let integrityInterval = program.integrity !== undefined ? parseInt(program.integrity) : 60000
+const integrityInterval = program.integrity !== undefined ? parseInt(program.integrity) : 60000
 const displayDiffOnFail = program.integrity || false
 
 const numberOfPods = 6
 
-// Wait requests between pods
-const baseRequestInterval = integrityInterval < constants.REQUESTS_INTERVAL ? integrityInterval : constants.REQUESTS_INTERVAL
-const requestsMaxPerInterval = baseRequestInterval / actionInterval
-const intervalsToMakeAllRequests = Math.ceil(requestsMaxPerInterval / (constants.REQUESTS_LIMIT_PER_POD * numberOfPods))
-const waitForBeforeIntegrityCheck = (intervalsToMakeAllRequests * constants.REQUESTS_INTERVAL) + 1000
-
-console.log('Create weight: %d, remove weight: %d.', createWeight, removeWeight)
+console.log('Create weight: %d, update weight: %d, remove weight: %d, view weight: %d, like weight: %d, dislike weight: %d.', createWeight, updateWeight, removeWeight, viewWeight, likeWeight, dislikeWeight)
 if (flushAtExit) {
   console.log('Program will flush data on exit.')
 } else {
@@ -52,7 +55,6 @@ if (displayDiffOnFail) {
 }
 console.log('Interval in ms for each action: %d.', actionInterval)
 console.log('Interval in ms for each integrity check: %d.', integrityInterval)
-console.log('Will wait %d ms before an integrity check.', waitForBeforeIntegrityCheck)
 
 console.log('Run servers...')
 runServers(numberOfPods, function (err, servers) {
@@ -65,37 +67,65 @@ runServers(numberOfPods, function (err, servers) {
   process.on('SIGTERM', goodbye)
 
   console.log('Servers runned')
+  initializeRequestsPerServer(servers)
 
   let checking = false
 
   setInterval(function () {
     if (checking === true) return
 
-    const rand = getRandomInt(0, createWeight + removeWeight)
+    const rand = getRandomInt(0, createWeight + updateWeight + removeWeight + viewWeight + likeWeight + dislikeWeight)
+
+    const numServer = getRandomNumServer(servers)
+    servers[numServer].requestsNumber++
 
     if (rand < createWeight) {
-      upload(servers, getRandomNumServer(servers))
+      upload(servers, numServer)
+    } else if (rand < createWeight + updateWeight) {
+      update(servers, numServer)
+    } else if (rand < createWeight + updateWeight + removeWeight) {
+      remove(servers, numServer)
+    } else if (rand < createWeight + updateWeight + removeWeight + viewWeight) {
+      view(servers, numServer)
+    } else if (rand < createWeight + updateWeight + removeWeight + viewWeight + likeWeight) {
+      like(servers, numServer)
     } else {
-      remove(servers, getRandomNumServer(servers))
+      dislike(servers, numServer)
     }
   }, actionInterval)
 
+  // The function will check the consistency between servers (should have the same videos with same attributes...)
   setInterval(function () {
     if (checking === true) return
 
     console.log('Checking integrity...')
     checking = true
 
-    setTimeout(function () {
-      checkIntegrity(servers, function () {
-        checking = false
+    const waitingInterval = setInterval(function () {
+      isThereAwaitingRequests(servers, function (res) {
+        if (res === true) {
+          console.log('A server has awaiting requests, waiting...')
+          return
+        }
+
+        checkIntegrity(servers, function () {
+          initializeRequestsPerServer(servers)
+          checking = false
+          clearInterval(waitingInterval)
+        })
       })
-    }, waitForBeforeIntegrityCheck)
+    }, constants.REQUESTS_INTERVAL)
   }, integrityInterval)
 })
 
 // ----------------------------------------------------------------------------
 
+function initializeRequestsPerServer (servers) {
+  servers.forEach(function (server) {
+    server.requestsNumber = 0
+  })
+}
+
 function getRandomInt (min, max) {
   return Math.floor(Math.random() * (max - min)) + min
 }
@@ -170,14 +200,38 @@ function exitServers (servers, callback) {
 function upload (servers, numServer, callback) {
   if (!callback) callback = function () {}
 
-  const name = Date.now() + ' name'
-  const description = Date.now() + ' description'
-  const tags = [ Date.now().toString().substring(0, 5) + 't1', Date.now().toString().substring(0, 5) + 't2' ]
-  const file = 'video_short1.webm'
+  console.log('Uploading video to server ' + numServer)
+
+  const videoAttributes = {
+    name: Date.now() + ' name',
+    category: 4,
+    description: Date.now() + ' description',
+    tags: [ Date.now().toString().substring(0, 5) + 't1', Date.now().toString().substring(0, 5) + 't2' ],
+    fixture: 'video_short1.webm'
+  }
+  videosUtils.uploadVideo(servers[numServer].url, servers[numServer].accessToken, videoAttributes, callback)
+}
+
+function update (servers, numServer, callback) {
+  if (!callback) callback = function () {}
+
+  videosUtils.getVideosList(servers[numServer].url, function (err, res) {
+    if (err) throw err
+
+    const videos = res.body.data.filter(function (video) { return video.isLocal })
+    if (videos.length === 0) return callback()
+
+    const toUpdate = videos[getRandomInt(0, videos.length)].id
+    const attributes = {
+      name: Date.now() + ' name',
+      description: Date.now() + ' description',
+      tags: [ Date.now().toString().substring(0, 5) + 't1', Date.now().toString().substring(0, 5) + 't2' ]
+    }
 
-  console.log('Upload video to server ' + numServer)
+    console.log('Updating video of server ' + numServer)
 
-  videosUtils.uploadVideo(servers[numServer].url, servers[numServer].accessToken, name, description, tags, file, callback)
+    videosUtils.updateVideo(servers[numServer].url, servers[numServer].accessToken, toUpdate, attributes, callback)
+  })
 }
 
 function remove (servers, numServer, callback) {
@@ -196,6 +250,46 @@ function remove (servers, numServer, callback) {
   })
 }
 
+function view (servers, numServer, callback) {
+  if (!callback) callback = function () {}
+
+  videosUtils.getVideosList(servers[numServer].url, function (err, res) {
+    if (err) throw err
+
+    const videos = res.body.data
+    if (videos.length === 0) return callback()
+
+    const toView = videos[getRandomInt(0, videos.length)].id
+
+    console.log('Viewing video from server ' + numServer)
+    videosUtils.getVideo(servers[numServer].url, toView, callback)
+  })
+}
+
+function like (servers, numServer, callback) {
+  rate(servers, numServer, 'like', callback)
+}
+
+function dislike (servers, numServer, callback) {
+  rate(servers, numServer, 'dislike', callback)
+}
+
+function rate (servers, numServer, rating, callback) {
+  if (!callback) callback = function () {}
+
+  videosUtils.getVideosList(servers[numServer].url, function (err, res) {
+    if (err) throw err
+
+    const videos = res.body.data
+    if (videos.length === 0) return callback()
+
+    const toRate = videos[getRandomInt(0, videos.length)].id
+
+    console.log('Rating (%s) video from server %d', rating, numServer)
+    videosUtils.getVideo(servers[numServer].url, toRate, callback)
+  })
+}
+
 function checkIntegrity (servers, callback) {
   const videos = []
   each(servers, function (server, callback) {
@@ -207,22 +301,28 @@ function checkIntegrity (servers, callback) {
         delete serverVideo.isLocal
         delete serverVideo.thumbnailPath
         delete serverVideo.updatedAt
+        delete serverVideo.views
       }
 
       videos.push(serverVideos)
       callback()
     })
   }, function () {
+    let i = 0
+
     for (const video of videos) {
       if (!isEqual(video, videos[0])) {
-        console.error('Integrity not ok!')
+        console.error('Integrity not ok with server %d!', i + 1)
 
         if (displayDiffOnFail) {
           console.log(differenceWith(videos[0], video, isEqual))
+          console.log(differenceWith(video, videos[0], isEqual))
         }
 
         process.exit(-1)
       }
+
+      i++
     }
 
     console.log('Integrity ok.')
@@ -233,3 +333,30 @@ function checkIntegrity (servers, callback) {
 function goodbye () {
   return process.exit(-1)
 }
+
+function isThereAwaitingRequests (servers, callback) {
+  let noRequests = true
+
+  // Check is each server has awaiting requestq
+  each(servers, function (server, callbackEach) {
+    requestStatsUtils.getRequestsStats(server, server.accessToken, function (err, res) {
+      if (err) throw err
+
+      const stats = res.body
+
+      if (
+        stats.requestScheduler.totalRequests !== 0 ||
+        stats.requestVideoEventScheduler.totalRequests !== 0 ||
+        stats.requestVideoQaduScheduler.totalRequests !== 0
+      ) {
+        noRequests = false
+      }
+
+      callbackEach()
+    })
+  }, function (err) {
+    if (err) throw err
+
+    return callback(noRequests === false)
+  })
+}