diff options
Diffstat (limited to 'server/tests/real-world/real-world.js')
-rw-r--r-- | server/tests/real-world/real-world.js | 367 |
1 files changed, 0 insertions, 367 deletions
diff --git a/server/tests/real-world/real-world.js b/server/tests/real-world/real-world.js deleted file mode 100644 index ea189c5f2..000000000 --- a/server/tests/real-world/real-world.js +++ /dev/null | |||
@@ -1,367 +0,0 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const each = require('async/each') | ||
4 | const isEqual = require('lodash/isEqual') | ||
5 | const differenceWith = require('lodash/differenceWith') | ||
6 | const program = require('commander') | ||
7 | const series = require('async/series') | ||
8 | |||
9 | process.env.NODE_ENV = 'test' | ||
10 | const constants = require('../../initializers/constants') | ||
11 | |||
12 | const loginUtils = require('../utils/login') | ||
13 | const podsUtils = require('../utils/pods') | ||
14 | const serversUtils = require('../utils/servers') | ||
15 | const videosUtils = require('../utils/videos') | ||
16 | const requestSchedulersUtils = require('../utils/request-schedulers') | ||
17 | |||
18 | program | ||
19 | .option('-c, --create [weight]', 'Weight for creating videos') | ||
20 | .option('-r, --remove [weight]', 'Weight for removing videos') | ||
21 | .option('-u, --update [weight]', 'Weight for updating videos') | ||
22 | .option('-v, --view [weight]', 'Weight for viewing videos') | ||
23 | .option('-l, --like [weight]', 'Weight for liking videos') | ||
24 | .option('-s, --dislike [weight]', 'Weight for disliking videos') | ||
25 | .option('-p, --pods [n]', 'Number of pods to run (3 or 6)', /^3|6$/, 3) | ||
26 | .option('-a, --action [interval]', 'Interval in ms for an action') | ||
27 | .option('-i, --integrity [interval]', 'Interval in ms for an integrity check') | ||
28 | .option('-f, --flush', 'Flush datas on exit') | ||
29 | .option('-d, --difference', 'Display difference if integrity is not okay') | ||
30 | .parse(process.argv) | ||
31 | |||
32 | const createWeight = program.create !== undefined ? parseInt(program.create) : 5 | ||
33 | const removeWeight = program.remove !== undefined ? parseInt(program.remove) : 4 | ||
34 | const updateWeight = program.update !== undefined ? parseInt(program.update) : 4 | ||
35 | const viewWeight = program.view !== undefined ? parseInt(program.view) : 4 | ||
36 | const likeWeight = program.like !== undefined ? parseInt(program.like) : 4 | ||
37 | const dislikeWeight = program.dislike !== undefined ? parseInt(program.dislike) : 4 | ||
38 | const flushAtExit = program.flush || false | ||
39 | const actionInterval = program.action !== undefined ? parseInt(program.action) : 500 | ||
40 | const integrityInterval = program.integrity !== undefined ? parseInt(program.integrity) : 60000 | ||
41 | const displayDiffOnFail = program.integrity || false | ||
42 | |||
43 | const numberOfPods = 6 | ||
44 | |||
45 | 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) | ||
46 | if (flushAtExit) { | ||
47 | console.log('Program will flush data on exit.') | ||
48 | } else { | ||
49 | console.log('Program will not flush data on exit.') | ||
50 | } | ||
51 | if (displayDiffOnFail) { | ||
52 | console.log('Program will display diff on failure.') | ||
53 | } else { | ||
54 | console.log('Program will not display diff on failure') | ||
55 | } | ||
56 | console.log('Interval in ms for each action: %d.', actionInterval) | ||
57 | console.log('Interval in ms for each integrity check: %d.', integrityInterval) | ||
58 | |||
59 | console.log('Run servers...') | ||
60 | runServers(numberOfPods, function (err, servers) { | ||
61 | if (err) throw err | ||
62 | |||
63 | process.on('exit', function () { | ||
64 | exitServers(servers, flushAtExit) | ||
65 | }) | ||
66 | process.on('SIGINT', goodbye) | ||
67 | process.on('SIGTERM', goodbye) | ||
68 | |||
69 | console.log('Servers runned') | ||
70 | initializeRequestsPerServer(servers) | ||
71 | |||
72 | let checking = false | ||
73 | |||
74 | setInterval(function () { | ||
75 | if (checking === true) return | ||
76 | |||
77 | const rand = getRandomInt(0, createWeight + updateWeight + removeWeight + viewWeight + likeWeight + dislikeWeight) | ||
78 | |||
79 | const numServer = getRandomNumServer(servers) | ||
80 | servers[numServer].requestsNumber++ | ||
81 | |||
82 | if (rand < createWeight) { | ||
83 | upload(servers, numServer) | ||
84 | } else if (rand < createWeight + updateWeight) { | ||
85 | update(servers, numServer) | ||
86 | } else if (rand < createWeight + updateWeight + removeWeight) { | ||
87 | remove(servers, numServer) | ||
88 | } else if (rand < createWeight + updateWeight + removeWeight + viewWeight) { | ||
89 | view(servers, numServer) | ||
90 | } else if (rand < createWeight + updateWeight + removeWeight + viewWeight + likeWeight) { | ||
91 | like(servers, numServer) | ||
92 | } else { | ||
93 | dislike(servers, numServer) | ||
94 | } | ||
95 | }, actionInterval) | ||
96 | |||
97 | // The function will check the consistency between servers (should have the same videos with same attributes...) | ||
98 | setInterval(function () { | ||
99 | if (checking === true) return | ||
100 | |||
101 | console.log('Checking integrity...') | ||
102 | checking = true | ||
103 | |||
104 | const waitingInterval = setInterval(function () { | ||
105 | isThereAwaitingRequests(servers, function (err, res) { | ||
106 | if (err) throw err | ||
107 | |||
108 | if (res === true) { | ||
109 | console.log('A server has awaiting requests, waiting...') | ||
110 | return | ||
111 | } | ||
112 | |||
113 | checkIntegrity(servers, function () { | ||
114 | initializeRequestsPerServer(servers) | ||
115 | checking = false | ||
116 | clearInterval(waitingInterval) | ||
117 | }) | ||
118 | }) | ||
119 | }, constants.REQUESTS_INTERVAL) | ||
120 | }, integrityInterval) | ||
121 | }) | ||
122 | |||
123 | // ---------------------------------------------------------------------------- | ||
124 | |||
125 | function initializeRequestsPerServer (servers) { | ||
126 | servers.forEach(function (server) { | ||
127 | server.requestsNumber = 0 | ||
128 | }) | ||
129 | } | ||
130 | |||
131 | function getRandomInt (min, max) { | ||
132 | return Math.floor(Math.random() * (max - min)) + min | ||
133 | } | ||
134 | |||
135 | function getRandomNumServer (servers) { | ||
136 | return getRandomInt(0, servers.length) | ||
137 | } | ||
138 | |||
139 | function runServers (numberOfPods, callback) { | ||
140 | let servers = null | ||
141 | |||
142 | series([ | ||
143 | // Run servers | ||
144 | function (next) { | ||
145 | serversUtils.flushAndRunMultipleServers(numberOfPods, function (serversRun) { | ||
146 | servers = serversRun | ||
147 | next() | ||
148 | }) | ||
149 | }, | ||
150 | // Get the access tokens | ||
151 | function (next) { | ||
152 | each(servers, function (server, callbackEach) { | ||
153 | loginUtils.loginAndGetAccessToken(server, function (err, accessToken) { | ||
154 | if (err) return callbackEach(err) | ||
155 | |||
156 | server.accessToken = accessToken | ||
157 | callbackEach() | ||
158 | }) | ||
159 | }, next) | ||
160 | }, | ||
161 | function (next) { | ||
162 | const server = servers[1] | ||
163 | podsUtils.makeFriends(server.url, server.accessToken, next) | ||
164 | }, | ||
165 | function (next) { | ||
166 | const server = servers[0] | ||
167 | podsUtils.makeFriends(server.url, server.accessToken, next) | ||
168 | }, | ||
169 | function (next) { | ||
170 | setTimeout(next, 1000) | ||
171 | }, | ||
172 | function (next) { | ||
173 | const server = servers[3] | ||
174 | podsUtils.makeFriends(server.url, server.accessToken, next) | ||
175 | }, | ||
176 | function (next) { | ||
177 | const server = servers[5] | ||
178 | podsUtils.makeFriends(server.url, server.accessToken, next) | ||
179 | }, | ||
180 | function (next) { | ||
181 | const server = servers[4] | ||
182 | podsUtils.makeFriends(server.url, server.accessToken, next) | ||
183 | }, | ||
184 | function (next) { | ||
185 | setTimeout(next, 1000) | ||
186 | } | ||
187 | ], function (err) { | ||
188 | return callback(err, servers) | ||
189 | }) | ||
190 | } | ||
191 | |||
192 | function exitServers (servers, callback) { | ||
193 | if (!callback) callback = function () {} | ||
194 | |||
195 | servers.forEach(function (server) { | ||
196 | if (server.app) process.kill(-server.app.pid) | ||
197 | }) | ||
198 | |||
199 | if (flushAtExit) serversUtils.flushTests(callback) | ||
200 | } | ||
201 | |||
202 | function upload (servers, numServer, callback) { | ||
203 | if (!callback) callback = function () {} | ||
204 | |||
205 | console.log('Uploading video to server ' + numServer) | ||
206 | |||
207 | const videoAttributes = { | ||
208 | name: Date.now() + ' name', | ||
209 | category: 4, | ||
210 | nsfw: false, | ||
211 | licence: 2, | ||
212 | language: 1, | ||
213 | description: Date.now() + ' description', | ||
214 | tags: [ Date.now().toString().substring(0, 5) + 't1', Date.now().toString().substring(0, 5) + 't2' ], | ||
215 | fixture: 'video_short1.webm' | ||
216 | } | ||
217 | videosUtils.uploadVideo(servers[numServer].url, servers[numServer].accessToken, videoAttributes, callback) | ||
218 | } | ||
219 | |||
220 | function update (servers, numServer, callback) { | ||
221 | if (!callback) callback = function () {} | ||
222 | |||
223 | videosUtils.getVideosList(servers[numServer].url, function (err, res) { | ||
224 | if (err) throw err | ||
225 | |||
226 | const videos = res.body.data.filter(function (video) { return video.isLocal }) | ||
227 | if (videos.length === 0) return callback() | ||
228 | |||
229 | const toUpdate = videos[getRandomInt(0, videos.length)].id | ||
230 | const attributes = { | ||
231 | name: Date.now() + ' name', | ||
232 | description: Date.now() + ' description', | ||
233 | tags: [ Date.now().toString().substring(0, 5) + 't1', Date.now().toString().substring(0, 5) + 't2' ] | ||
234 | } | ||
235 | |||
236 | console.log('Updating video of server ' + numServer) | ||
237 | |||
238 | videosUtils.updateVideo(servers[numServer].url, servers[numServer].accessToken, toUpdate, attributes, callback) | ||
239 | }) | ||
240 | } | ||
241 | |||
242 | function remove (servers, numServer, callback) { | ||
243 | if (!callback) callback = function () {} | ||
244 | |||
245 | videosUtils.getVideosList(servers[numServer].url, function (err, res) { | ||
246 | if (err) throw err | ||
247 | |||
248 | const videos = res.body.data | ||
249 | if (videos.length === 0) return callback() | ||
250 | |||
251 | const toRemove = videos[getRandomInt(0, videos.length)].id | ||
252 | |||
253 | console.log('Removing video from server ' + numServer) | ||
254 | videosUtils.removeVideo(servers[numServer].url, servers[numServer].accessToken, toRemove, callback) | ||
255 | }) | ||
256 | } | ||
257 | |||
258 | function view (servers, numServer, callback) { | ||
259 | if (!callback) callback = function () {} | ||
260 | |||
261 | videosUtils.getVideosList(servers[numServer].url, function (err, res) { | ||
262 | if (err) throw err | ||
263 | |||
264 | const videos = res.body.data | ||
265 | if (videos.length === 0) return callback() | ||
266 | |||
267 | const toView = videos[getRandomInt(0, videos.length)].id | ||
268 | |||
269 | console.log('Viewing video from server ' + numServer) | ||
270 | videosUtils.getVideo(servers[numServer].url, toView, callback) | ||
271 | }) | ||
272 | } | ||
273 | |||
274 | function like (servers, numServer, callback) { | ||
275 | rate(servers, numServer, 'like', callback) | ||
276 | } | ||
277 | |||
278 | function dislike (servers, numServer, callback) { | ||
279 | rate(servers, numServer, 'dislike', callback) | ||
280 | } | ||
281 | |||
282 | function rate (servers, numServer, rating, callback) { | ||
283 | if (!callback) callback = function () {} | ||
284 | |||
285 | videosUtils.getVideosList(servers[numServer].url, function (err, res) { | ||
286 | if (err) throw err | ||
287 | |||
288 | const videos = res.body.data | ||
289 | if (videos.length === 0) return callback() | ||
290 | |||
291 | const toRate = videos[getRandomInt(0, videos.length)].id | ||
292 | |||
293 | console.log('Rating (%s) video from server %d', rating, numServer) | ||
294 | videosUtils.getVideo(servers[numServer].url, toRate, callback) | ||
295 | }) | ||
296 | } | ||
297 | |||
298 | function checkIntegrity (servers, callback) { | ||
299 | const videos = [] | ||
300 | each(servers, function (server, callback) { | ||
301 | videosUtils.getAllVideosListBy(server.url, function (err, res) { | ||
302 | if (err) throw err | ||
303 | const serverVideos = res.body.data | ||
304 | for (const serverVideo of serverVideos) { | ||
305 | delete serverVideo.id | ||
306 | delete serverVideo.isLocal | ||
307 | delete serverVideo.thumbnailPath | ||
308 | delete serverVideo.updatedAt | ||
309 | delete serverVideo.views | ||
310 | } | ||
311 | |||
312 | videos.push(serverVideos) | ||
313 | callback() | ||
314 | }) | ||
315 | }, function () { | ||
316 | let i = 0 | ||
317 | |||
318 | for (const video of videos) { | ||
319 | if (!isEqual(video, videos[0])) { | ||
320 | console.error('Integrity not ok with server %d!', i + 1) | ||
321 | |||
322 | if (displayDiffOnFail) { | ||
323 | console.log(differenceWith(videos[0], video, isEqual)) | ||
324 | console.log(differenceWith(video, videos[0], isEqual)) | ||
325 | } | ||
326 | |||
327 | process.exit(-1) | ||
328 | } | ||
329 | |||
330 | i++ | ||
331 | } | ||
332 | |||
333 | console.log('Integrity ok.') | ||
334 | return callback() | ||
335 | }) | ||
336 | } | ||
337 | |||
338 | function goodbye () { | ||
339 | return process.exit(-1) | ||
340 | } | ||
341 | |||
342 | function isThereAwaitingRequests (servers, callback) { | ||
343 | let noRequests = true | ||
344 | |||
345 | // Check is each server has awaiting requestq | ||
346 | each(servers, function (server, callbackEach) { | ||
347 | requestSchedulersUtils.getRequestsStats(server, server.accessToken, function (err, res) { | ||
348 | if (err) throw err | ||
349 | |||
350 | const stats = res.body | ||
351 | |||
352 | if ( | ||
353 | stats.requestScheduler.totalRequests !== 0 || | ||
354 | stats.requestVideoEventScheduler.totalRequests !== 0 || | ||
355 | stats.requestVideoQaduScheduler.totalRequests !== 0 | ||
356 | ) { | ||
357 | noRequests = false | ||
358 | } | ||
359 | |||
360 | callbackEach() | ||
361 | }) | ||
362 | }, function (err) { | ||
363 | if (err) throw err | ||
364 | |||
365 | return callback(null, noRequests === false) | ||
366 | }) | ||
367 | } | ||