3 const async
= require('async')
4 const program
= require('commander')
5 const isEqual
= require('lodash/isEqual')
7 process
.env
.NODE_ENV
= 'test'
8 const constants
= require('../../initializers/constants')
10 const utils
= require('../api/utils')
13 .option('-c, --create [weight]', 'Weight for creating videos')
14 .option('-r, --remove [weight]', 'Weight for removing videos')
15 .option('-p, --pods [n]', 'Number of pods to run (3 or 6)', /^3|6$/, 3)
16 .option('-a, --action [interval]', 'Interval in ms for an action')
17 .option('-i, --integrity [interval]', 'Interval in ms for an integrity check')
18 .option('-f, --flush', 'Flush datas on exit')
21 const createWeight
= parseInt(program
.create
) || 5
22 const removeWeight
= parseInt(program
.remove
) || 4
23 const flushAtExit
= program
.flush
|| false
24 const actionInterval
= parseInt(program
.action
) || 500
25 let integrityInterval
= parseInt(program
.integrity
) || 60000
27 const numberOfPods
= 6
28 // Wait requests between pods
29 const requestsMaxPerInterval
= constants
.INTERVAL
/ actionInterval
30 const intervalsToMakeAllRequests
= Math
.ceil(requestsMaxPerInterval
/ constants
.REQUESTS_LIMIT
)
31 const waitForBeforeIntegrityCheck
= (intervalsToMakeAllRequests
* constants
.INTERVAL
) + 1000
33 integrityInterval
+= waitForBeforeIntegrityCheck
35 console
.log('Create weight: %d, remove weight: %d.', createWeight
, removeWeight
)
37 console
.log('Program will flush data on exit.')
39 console
.log('Program will not flush data on exit.')
41 console
.log('Interval in ms for each action: %d.', actionInterval
)
42 console
.log('Interval in ms for each integrity check: %d.', integrityInterval
)
43 console
.log('Will wait %d ms before an integrity check.', waitForBeforeIntegrityCheck
)
45 console
.log('Run servers...')
46 runServers(numberOfPods
, function (err
, servers
) {
49 process
.on('exit', function () {
50 exitServers(servers
, flushAtExit
)
52 process
.on('SIGINT', goodbye
)
53 process
.on('SIGTERM', goodbye
)
55 console
.log('Servers runned')
59 setInterval(function () {
60 if (checking
=== true) return
62 const rand
= getRandomInt(0, createWeight
+ removeWeight
)
64 if (rand
< createWeight
) {
65 upload(servers
, getRandomNumServer(servers
))
67 remove(servers
, getRandomNumServer(servers
))
71 setInterval(function () {
72 console
.log('Checking integrity...')
75 setTimeout(function () {
76 checkIntegrity(servers
, function () {
79 }, waitForBeforeIntegrityCheck
)
83 // ----------------------------------------------------------------------------
85 function getRandomInt (min
, max
) {
86 return Math
.floor(Math
.random() * (max
- min
)) + min
89 function getRandomNumServer (servers
) {
90 return getRandomInt(0, servers
.length
)
93 function runServers (numberOfPods
, callback
) {
99 utils
.flushAndRunMultipleServers(numberOfPods
, function (serversRun
) {
104 // Get the access tokens
106 async
.each(servers
, function (server
, callbackEach
) {
107 utils
.loginAndGetAccessToken(server
, function (err
, accessToken
) {
108 if (err
) return callbackEach(err
)
110 server
.accessToken
= accessToken
116 const server
= servers
[1]
117 utils
.makeFriends(server
.url
, server
.accessToken
, next
)
120 const server
= servers
[0]
121 utils
.makeFriends(server
.url
, server
.accessToken
, next
)
124 setTimeout(next
, 1000)
127 const server
= servers
[3]
128 utils
.makeFriends(server
.url
, server
.accessToken
, next
)
131 const server
= servers
[5]
132 utils
.makeFriends(server
.url
, server
.accessToken
, next
)
135 const server
= servers
[4]
136 utils
.makeFriends(server
.url
, server
.accessToken
, next
)
139 setTimeout(next
, 1000)
142 return callback(err
, servers
)
146 function exitServers (servers
, callback
) {
147 if (!callback
) callback = function () {}
149 servers
.forEach(function (server
) {
150 if (server
.app
) process
.kill(-server
.app
.pid
)
153 if (flushAtExit
) utils
.flushTests(callback
)
156 function upload (servers
, numServer
, callback
) {
157 if (!callback
) callback = function () {}
159 const name
= 'my super name for pod 1'
160 const description
= 'my super description for pod 1'
161 const tags
= [ 'tag1p1', 'tag2p1' ]
162 const file
= 'video_short1.webm'
164 console
.log('Upload video to server ' + numServer
)
166 utils
.uploadVideo(servers
[numServer
].url
, servers
[numServer
].accessToken
, name
, description
, tags
, file
, callback
)
169 function remove (servers
, numServer
, callback
) {
170 if (!callback
) callback = function () {}
172 utils
.getVideosList(servers
[numServer
].url
, function (err
, res
) {
175 const videos
= res
.body
.data
176 if (videos
.length
=== 0) return callback()
178 const toRemove
= videos
[getRandomInt(0, videos
.length
)].id
180 console
.log('Removing video from server ' + numServer
)
181 utils
.removeVideo(servers
[numServer
].url
, servers
[numServer
].accessToken
, toRemove
, callback
)
185 function checkIntegrity (servers
, callback
) {
187 async
.each(servers
, function (server
, callback
) {
188 utils
.getAllVideosListBy(server
.url
, function (err
, res
) {
190 const serverVideos
= res
.body
.data
191 for (const serverVideo
of serverVideos
) {
192 delete serverVideo
.id
193 delete serverVideo
.isLocal
194 delete serverVideo
.thumbnailPath
197 videos
.push(serverVideos
)
201 for (const video
of videos
) {
202 if (!isEqual(video
, videos
[0])) {
203 console
.error('Integrity not ok!')
208 console
.log('Integrity ok.')
213 function goodbye () {
214 return process
.exit(-1)