diff options
Diffstat (limited to 'shared/extra-utils/server')
-rw-r--r-- | shared/extra-utils/server/servers.ts | 86 |
1 files changed, 76 insertions, 10 deletions
diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts index b0cb869f4..480d12330 100644 --- a/shared/extra-utils/server/servers.ts +++ b/shared/extra-utils/server/servers.ts | |||
@@ -12,6 +12,10 @@ interface ServerInfo { | |||
12 | app: ChildProcess, | 12 | app: ChildProcess, |
13 | url: string | 13 | url: string |
14 | host: string | 14 | host: string |
15 | |||
16 | port: number | ||
17 | parallel: boolean | ||
18 | internalServerNumber: number | ||
15 | serverNumber: number | 19 | serverNumber: number |
16 | 20 | ||
17 | client: { | 21 | client: { |
@@ -76,12 +80,27 @@ function flushTests (serverNumber?: number) { | |||
76 | }) | 80 | }) |
77 | } | 81 | } |
78 | 82 | ||
79 | function flushAndRunServer (serverNumber: number, configOverride?: Object, args = []) { | 83 | function randomServer () { |
84 | const low = 10 | ||
85 | const high = 10000 | ||
86 | |||
87 | return Math.floor(Math.random() * (high - low) + low) | ||
88 | } | ||
89 | |||
90 | function flushAndRunServer (serverNumber: number, configOverrideArg?: Object, args = []) { | ||
91 | const parallel = process.env.MOCHA_PARALLEL === 'true' | ||
92 | |||
93 | const internalServerNumber = parallel ? randomServer() : serverNumber | ||
94 | const port = 9000 + internalServerNumber | ||
95 | |||
80 | const server: ServerInfo = { | 96 | const server: ServerInfo = { |
81 | app: null, | 97 | app: null, |
82 | serverNumber: serverNumber, | 98 | port, |
83 | url: `http://localhost:${9000 + serverNumber}`, | 99 | internalServerNumber, |
84 | host: `localhost:${9000 + serverNumber}`, | 100 | parallel, |
101 | serverNumber: internalServerNumber, | ||
102 | url: `http://localhost:${port}`, | ||
103 | host: `localhost:${port}`, | ||
85 | client: { | 104 | client: { |
86 | id: null, | 105 | id: null, |
87 | secret: null | 106 | secret: null |
@@ -96,7 +115,7 @@ function flushAndRunServer (serverNumber: number, configOverride?: Object, args | |||
96 | const serverRunString = { | 115 | const serverRunString = { |
97 | 'Server listening': false | 116 | 'Server listening': false |
98 | } | 117 | } |
99 | const key = 'Database peertube_test' + serverNumber + ' is ready' | 118 | const key = 'Database peertube_test' + internalServerNumber + ' is ready' |
100 | serverRunString[key] = false | 119 | serverRunString[key] = false |
101 | 120 | ||
102 | const regexps = { | 121 | const regexps = { |
@@ -111,10 +130,44 @@ function flushAndRunServer (serverNumber: number, configOverride?: Object, args | |||
111 | env['NODE_ENV'] = 'test' | 130 | env['NODE_ENV'] = 'test' |
112 | env['NODE_APP_INSTANCE'] = serverNumber.toString() | 131 | env['NODE_APP_INSTANCE'] = serverNumber.toString() |
113 | 132 | ||
114 | if (configOverride !== undefined) { | 133 | let configOverride: any = {} |
115 | env['NODE_CONFIG'] = JSON.stringify(configOverride) | 134 | |
135 | if (parallel) { | ||
136 | configOverride = { | ||
137 | listen: { | ||
138 | port: port | ||
139 | }, | ||
140 | webserver: { | ||
141 | port: port | ||
142 | }, | ||
143 | database: { | ||
144 | suffix: '_test' + internalServerNumber | ||
145 | }, | ||
146 | storage: { | ||
147 | tmp: `test${internalServerNumber}/tmp/`, | ||
148 | avatars: `test${internalServerNumber}/avatars/`, | ||
149 | videos: `test${internalServerNumber}/videos/`, | ||
150 | streaming_playlists: `test${internalServerNumber}/streaming-playlists/`, | ||
151 | redundancy: `test${internalServerNumber}/redundancy/`, | ||
152 | logs: `test${internalServerNumber}/logs/`, | ||
153 | previews: `test${internalServerNumber}/previews/`, | ||
154 | thumbnails: `test${internalServerNumber}/thumbnails/`, | ||
155 | torrents: `test${internalServerNumber}/torrents/`, | ||
156 | captions: `test${internalServerNumber}/captions/`, | ||
157 | cache: `test${internalServerNumber}/cache/` | ||
158 | }, | ||
159 | admin: { | ||
160 | email: `admin${internalServerNumber}@example.com` | ||
161 | } | ||
162 | } | ||
163 | } | ||
164 | |||
165 | if (configOverrideArg !== undefined) { | ||
166 | Object.assign(configOverride, configOverrideArg) | ||
116 | } | 167 | } |
117 | 168 | ||
169 | env['NODE_CONFIG'] = JSON.stringify(configOverride) | ||
170 | |||
118 | const options = { | 171 | const options = { |
119 | silent: true, | 172 | silent: true, |
120 | env: env, | 173 | env: env, |
@@ -122,7 +175,7 @@ function flushAndRunServer (serverNumber: number, configOverride?: Object, args | |||
122 | } | 175 | } |
123 | 176 | ||
124 | return new Promise<ServerInfo>(res => { | 177 | return new Promise<ServerInfo>(res => { |
125 | flushTests(serverNumber) | 178 | flushTests(internalServerNumber) |
126 | .then(() => { | 179 | .then(() => { |
127 | 180 | ||
128 | server.app = fork(join(root(), 'dist', 'server.js'), args, options) | 181 | server.app = fork(join(root(), 'dist', 'server.js'), args, options) |
@@ -155,8 +208,7 @@ function flushAndRunServer (serverNumber: number, configOverride?: Object, args | |||
155 | process.on('exit', () => { | 208 | process.on('exit', () => { |
156 | try { | 209 | try { |
157 | process.kill(server.app.pid) | 210 | process.kill(server.app.pid) |
158 | } catch { /* empty */ | 211 | } catch { /* empty */ } |
159 | } | ||
160 | }) | 212 | }) |
161 | 213 | ||
162 | res(server) | 214 | res(server) |
@@ -194,6 +246,19 @@ function killallServers (servers: ServerInfo[]) { | |||
194 | } | 246 | } |
195 | } | 247 | } |
196 | 248 | ||
249 | function cleanupTests (servers: ServerInfo[]) { | ||
250 | killallServers(servers) | ||
251 | |||
252 | const p: Promise<any>[] = [] | ||
253 | for (const server of servers) { | ||
254 | if (server.parallel) { | ||
255 | p.push(flushTests(server.internalServerNumber)) | ||
256 | } | ||
257 | } | ||
258 | |||
259 | return Promise.all(p) | ||
260 | } | ||
261 | |||
197 | async function waitUntilLog (server: ServerInfo, str: string, count = 1) { | 262 | async function waitUntilLog (server: ServerInfo, str: string, count = 1) { |
198 | const logfile = join(root(), 'test' + server.serverNumber, 'logs/peertube.log') | 263 | const logfile = join(root(), 'test' + server.serverNumber, 'logs/peertube.log') |
199 | 264 | ||
@@ -213,6 +278,7 @@ export { | |||
213 | checkDirectoryIsEmpty, | 278 | checkDirectoryIsEmpty, |
214 | checkTmpIsEmpty, | 279 | checkTmpIsEmpty, |
215 | ServerInfo, | 280 | ServerInfo, |
281 | cleanupTests, | ||
216 | flushAndRunMultipleServers, | 282 | flushAndRunMultipleServers, |
217 | flushTests, | 283 | flushTests, |
218 | flushAndRunServer, | 284 | flushAndRunServer, |