diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/tests/real-world/real-world.js | 215 |
1 files changed, 215 insertions, 0 deletions
diff --git a/server/tests/real-world/real-world.js b/server/tests/real-world/real-world.js new file mode 100644 index 000000000..bc6ea859b --- /dev/null +++ b/server/tests/real-world/real-world.js | |||
@@ -0,0 +1,215 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const async = require('async') | ||
4 | const program = require('commander') | ||
5 | const isEqual = require('lodash/isEqual') | ||
6 | |||
7 | process.env.NODE_ENV = 'test' | ||
8 | const constants = require('../../initializers/constants') | ||
9 | |||
10 | const utils = require('../api/utils') | ||
11 | |||
12 | program | ||
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') | ||
19 | .parse(process.argv) | ||
20 | |||
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 | ||
26 | |||
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 | ||
32 | |||
33 | integrityInterval += waitForBeforeIntegrityCheck | ||
34 | |||
35 | console.log('Create weight: %d, remove weight: %d.', createWeight, removeWeight) | ||
36 | if (flushAtExit) { | ||
37 | console.log('Program will flush data on exit.') | ||
38 | } else { | ||
39 | console.log('Program will not flush data on exit.') | ||
40 | } | ||
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) | ||
44 | |||
45 | console.log('Run servers...') | ||
46 | runServers(numberOfPods, function (err, servers) { | ||
47 | if (err) throw err | ||
48 | |||
49 | process.on('exit', function () { | ||
50 | exitServers(servers, flushAtExit) | ||
51 | }) | ||
52 | process.on('SIGINT', goodbye) | ||
53 | process.on('SIGTERM', goodbye) | ||
54 | |||
55 | console.log('Servers runned') | ||
56 | |||
57 | let checking = false | ||
58 | |||
59 | setInterval(function () { | ||
60 | if (checking === true) return | ||
61 | |||
62 | const rand = getRandomInt(0, createWeight + removeWeight) | ||
63 | |||
64 | if (rand < createWeight) { | ||
65 | upload(servers, getRandomNumServer(servers)) | ||
66 | } else { | ||
67 | remove(servers, getRandomNumServer(servers)) | ||
68 | } | ||
69 | }, actionInterval) | ||
70 | |||
71 | setInterval(function () { | ||
72 | console.log('Checking integrity...') | ||
73 | checking = true | ||
74 | |||
75 | setTimeout(function () { | ||
76 | checkIntegrity(servers, function () { | ||
77 | checking = false | ||
78 | }) | ||
79 | }, waitForBeforeIntegrityCheck) | ||
80 | }, integrityInterval) | ||
81 | }) | ||
82 | |||
83 | // ---------------------------------------------------------------------------- | ||
84 | |||
85 | function getRandomInt (min, max) { | ||
86 | return Math.floor(Math.random() * (max - min)) + min | ||
87 | } | ||
88 | |||
89 | function getRandomNumServer (servers) { | ||
90 | return getRandomInt(0, servers.length) | ||
91 | } | ||
92 | |||
93 | function runServers (numberOfPods, callback) { | ||
94 | let servers = null | ||
95 | |||
96 | async.series([ | ||
97 | // Run servers | ||
98 | function (next) { | ||
99 | utils.flushAndRunMultipleServers(numberOfPods, function (serversRun) { | ||
100 | servers = serversRun | ||
101 | next() | ||
102 | }) | ||
103 | }, | ||
104 | // Get the access tokens | ||
105 | function (next) { | ||
106 | async.each(servers, function (server, callbackEach) { | ||
107 | utils.loginAndGetAccessToken(server, function (err, accessToken) { | ||
108 | if (err) return callbackEach(err) | ||
109 | |||
110 | server.accessToken = accessToken | ||
111 | callbackEach() | ||
112 | }) | ||
113 | }, next) | ||
114 | }, | ||
115 | function (next) { | ||
116 | const server = servers[1] | ||
117 | utils.makeFriends(server.url, server.accessToken, next) | ||
118 | }, | ||
119 | function (next) { | ||
120 | const server = servers[0] | ||
121 | utils.makeFriends(server.url, server.accessToken, next) | ||
122 | }, | ||
123 | function (next) { | ||
124 | setTimeout(next, 1000) | ||
125 | }, | ||
126 | function (next) { | ||
127 | const server = servers[3] | ||
128 | utils.makeFriends(server.url, server.accessToken, next) | ||
129 | }, | ||
130 | function (next) { | ||
131 | const server = servers[5] | ||
132 | utils.makeFriends(server.url, server.accessToken, next) | ||
133 | }, | ||
134 | function (next) { | ||
135 | const server = servers[4] | ||
136 | utils.makeFriends(server.url, server.accessToken, next) | ||
137 | }, | ||
138 | function (next) { | ||
139 | setTimeout(next, 1000) | ||
140 | } | ||
141 | ], function (err) { | ||
142 | return callback(err, servers) | ||
143 | }) | ||
144 | } | ||
145 | |||
146 | function exitServers (servers, callback) { | ||
147 | if (!callback) callback = function () {} | ||
148 | |||
149 | servers.forEach(function (server) { | ||
150 | if (server.app) process.kill(-server.app.pid) | ||
151 | }) | ||
152 | |||
153 | if (flushAtExit) utils.flushTests(callback) | ||
154 | } | ||
155 | |||
156 | function upload (servers, numServer, callback) { | ||
157 | if (!callback) callback = function () {} | ||
158 | |||
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' | ||
163 | |||
164 | console.log('Upload video to server ' + numServer) | ||
165 | |||
166 | utils.uploadVideo(servers[numServer].url, servers[numServer].accessToken, name, description, tags, file, callback) | ||
167 | } | ||
168 | |||
169 | function remove (servers, numServer, callback) { | ||
170 | if (!callback) callback = function () {} | ||
171 | |||
172 | utils.getVideosList(servers[numServer].url, function (err, res) { | ||
173 | if (err) throw err | ||
174 | |||
175 | const videos = res.body.data | ||
176 | if (videos.length === 0) return callback() | ||
177 | |||
178 | const toRemove = videos[getRandomInt(0, videos.length)].id | ||
179 | |||
180 | console.log('Removing video from server ' + numServer) | ||
181 | utils.removeVideo(servers[numServer].url, servers[numServer].accessToken, toRemove, callback) | ||
182 | }) | ||
183 | } | ||
184 | |||
185 | function checkIntegrity (servers, callback) { | ||
186 | const videos = [] | ||
187 | async.each(servers, function (server, callback) { | ||
188 | utils.getAllVideosListBy(server.url, function (err, res) { | ||
189 | if (err) throw err | ||
190 | const serverVideos = res.body.data | ||
191 | for (const serverVideo of serverVideos) { | ||
192 | delete serverVideo.id | ||
193 | delete serverVideo.isLocal | ||
194 | delete serverVideo.thumbnailPath | ||
195 | } | ||
196 | |||
197 | videos.push(serverVideos) | ||
198 | callback() | ||
199 | }) | ||
200 | }, function () { | ||
201 | for (const video of videos) { | ||
202 | if (!isEqual(video, videos[0])) { | ||
203 | console.error('Integrity not ok!') | ||
204 | process.exit(-1) | ||
205 | } | ||
206 | } | ||
207 | |||
208 | console.log('Integrity ok.') | ||
209 | return callback() | ||
210 | }) | ||
211 | } | ||
212 | |||
213 | function goodbye () { | ||
214 | return process.exit(-1) | ||
215 | } | ||