3 const each
= require('each')
4 const isEqual
= require('lodash/isEqual')
5 const program
= require('commander')
6 const series
= require('async/series')
8 process
.env
.NODE_ENV
= 'test'
9 const constants
= require('../../initializers/constants')
11 const utils
= require('../api/utils')
14 .option('-c, --create [weight]', 'Weight for creating videos')
15 .option('-r, --remove [weight]', 'Weight for removing videos')
16 .option('-p, --pods [n]', 'Number of pods to run (3 or 6)', /^3|6$/, 3)
17 .option('-a, --action [interval]', 'Interval in ms for an action')
18 .option('-i, --integrity [interval]', 'Interval in ms for an integrity check')
19 .option('-f, --flush', 'Flush datas on exit')
22 const createWeight
= parseInt(program
.create
) || 5
23 const removeWeight
= parseInt(program
.remove
) || 4
24 const flushAtExit
= program
.flush
|| false
25 const actionInterval
= parseInt(program
.action
) || 500
26 let integrityInterval
= parseInt(program
.integrity
) || 60000
28 const numberOfPods
= 6
29 // Wait requests between pods
30 const requestsMaxPerInterval
= constants
.INTERVAL
/ actionInterval
31 const intervalsToMakeAllRequests
= Math
.ceil(requestsMaxPerInterval
/ constants
.REQUESTS_LIMIT
)
32 const waitForBeforeIntegrityCheck
= (intervalsToMakeAllRequests
* constants
.INTERVAL
) + 1000
34 integrityInterval
+= waitForBeforeIntegrityCheck
36 console
.log('Create weight: %d, remove weight: %d.', createWeight
, removeWeight
)
38 console
.log('Program will flush data on exit.')
40 console
.log('Program will not flush data on exit.')
42 console
.log('Interval in ms for each action: %d.', actionInterval
)
43 console
.log('Interval in ms for each integrity check: %d.', integrityInterval
)
44 console
.log('Will wait %d ms before an integrity check.', waitForBeforeIntegrityCheck
)
46 console
.log('Run servers...')
47 runServers(numberOfPods
, function (err
, servers
) {
50 process
.on('exit', function () {
51 exitServers(servers
, flushAtExit
)
53 process
.on('SIGINT', goodbye
)
54 process
.on('SIGTERM', goodbye
)
56 console
.log('Servers runned')
60 setInterval(function () {
61 if (checking
=== true) return
63 const rand
= getRandomInt(0, createWeight
+ removeWeight
)
65 if (rand
< createWeight
) {
66 upload(servers
, getRandomNumServer(servers
))
68 remove(servers
, getRandomNumServer(servers
))
72 setInterval(function () {
73 console
.log('Checking integrity...')
76 setTimeout(function () {
77 checkIntegrity(servers
, function () {
80 }, waitForBeforeIntegrityCheck
)
84 // ----------------------------------------------------------------------------
86 function getRandomInt (min
, max
) {
87 return Math
.floor(Math
.random() * (max
- min
)) + min
90 function getRandomNumServer (servers
) {
91 return getRandomInt(0, servers
.length
)
94 function runServers (numberOfPods
, callback
) {
100 utils
.flushAndRunMultipleServers(numberOfPods
, function (serversRun
) {
105 // Get the access tokens
107 each(servers
, function (server
, callbackEach
) {
108 utils
.loginAndGetAccessToken(server
, function (err
, accessToken
) {
109 if (err
) return callbackEach(err
)
111 server
.accessToken
= accessToken
117 const server
= servers
[1]
118 utils
.makeFriends(server
.url
, server
.accessToken
, next
)
121 const server
= servers
[0]
122 utils
.makeFriends(server
.url
, server
.accessToken
, next
)
125 setTimeout(next
, 1000)
128 const server
= servers
[3]
129 utils
.makeFriends(server
.url
, server
.accessToken
, next
)
132 const server
= servers
[5]
133 utils
.makeFriends(server
.url
, server
.accessToken
, next
)
136 const server
= servers
[4]
137 utils
.makeFriends(server
.url
, server
.accessToken
, next
)
140 setTimeout(next
, 1000)
143 return callback(err
, servers
)
147 function exitServers (servers
, callback
) {
148 if (!callback
) callback = function () {}
150 servers
.forEach(function (server
) {
151 if (server
.app
) process
.kill(-server
.app
.pid
)
154 if (flushAtExit
) utils
.flushTests(callback
)
157 function upload (servers
, numServer
, callback
) {
158 if (!callback
) callback = function () {}
160 const name
= 'my super name for pod 1'
161 const description
= 'my super description for pod 1'
162 const tags
= [ 'tag1p1', 'tag2p1' ]
163 const file
= 'video_short1.webm'
165 console
.log('Upload video to server ' + numServer
)
167 utils
.uploadVideo(servers
[numServer
].url
, servers
[numServer
].accessToken
, name
, description
, tags
, file
, callback
)
170 function remove (servers
, numServer
, callback
) {
171 if (!callback
) callback = function () {}
173 utils
.getVideosList(servers
[numServer
].url
, function (err
, res
) {
176 const videos
= res
.body
.data
177 if (videos
.length
=== 0) return callback()
179 const toRemove
= videos
[getRandomInt(0, videos
.length
)].id
181 console
.log('Removing video from server ' + numServer
)
182 utils
.removeVideo(servers
[numServer
].url
, servers
[numServer
].accessToken
, toRemove
, callback
)
186 function checkIntegrity (servers
, callback
) {
188 each(servers
, function (server
, callback
) {
189 utils
.getAllVideosListBy(server
.url
, function (err
, res
) {
191 const serverVideos
= res
.body
.data
192 for (const serverVideo
of serverVideos
) {
193 delete serverVideo
.id
194 delete serverVideo
.isLocal
195 delete serverVideo
.thumbnailPath
198 videos
.push(serverVideos
)
202 for (const video
of videos
) {
203 if (!isEqual(video
, videos
[0])) {
204 console
.error('Integrity not ok!')
209 console
.log('Integrity ok.')
214 function goodbye () {
215 return process
.exit(-1)