]>
Commit | Line | Data |
---|---|---|
1 | ;(function () { | |
2 | 'use strict' | |
3 | ||
4 | var async = require('async') | |
5 | var chai = require('chai') | |
6 | var expect = chai.expect | |
7 | var pathUtils = require('path') | |
8 | ||
9 | var utils = require('./utils') | |
10 | var webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent')) | |
11 | webtorrent.silent = true | |
12 | ||
13 | describe('Test multiple pods', function () { | |
14 | var apps = [] | |
15 | var urls = [] | |
16 | var to_remove = [] | |
17 | ||
18 | before(function (done) { | |
19 | this.timeout(30000) | |
20 | ||
21 | async.series([ | |
22 | // Run servers | |
23 | function (next) { | |
24 | utils.flushAndRunMultipleServers(3, function (apps_run, urls_run) { | |
25 | apps = apps_run | |
26 | urls = urls_run | |
27 | next() | |
28 | }) | |
29 | }, | |
30 | // The second pod make friend with the third | |
31 | function (next) { | |
32 | utils.makeFriends(urls[1], next) | |
33 | }, | |
34 | // Wait for the request between pods | |
35 | function (next) { | |
36 | setTimeout(next, 10000) | |
37 | }, | |
38 | // Pod 1 make friends too | |
39 | function (next) { | |
40 | utils.makeFriends(urls[0], next) | |
41 | }, | |
42 | function (next) { | |
43 | webtorrent.create({ host: 'client', port: '1' }, next) | |
44 | } | |
45 | ], done) | |
46 | }) | |
47 | ||
48 | it('Should not have videos for all pods', function (done) { | |
49 | async.each(urls, function (url, callback) { | |
50 | utils.getVideosList(url, function (err, res) { | |
51 | if (err) throw err | |
52 | ||
53 | expect(res.body).to.be.an('array') | |
54 | expect(res.body.length).to.equal(0) | |
55 | ||
56 | callback() | |
57 | }) | |
58 | }, done) | |
59 | }) | |
60 | ||
61 | describe('Should upload the video and propagate on each pod', function () { | |
62 | it('Should upload the video on pod 1 and propagate on each pod', function (done) { | |
63 | this.timeout(15000) | |
64 | ||
65 | async.series([ | |
66 | function (next) { | |
67 | utils.uploadVideo(urls[0], 'my super name for pod 1', 'my super description for pod 1', 'video_short1.webm', next) | |
68 | }, | |
69 | function (next) { | |
70 | setTimeout(next, 11000) | |
71 | }], | |
72 | // All pods should have this video | |
73 | function (err) { | |
74 | if (err) throw err | |
75 | ||
76 | async.each(urls, function (url, callback) { | |
77 | var base_magnet = null | |
78 | ||
79 | utils.getVideosList(url, function (err, res) { | |
80 | if (err) throw err | |
81 | ||
82 | var videos = res.body | |
83 | expect(videos).to.be.an('array') | |
84 | expect(videos.length).to.equal(1) | |
85 | var video = videos[0] | |
86 | expect(video.name).to.equal('my super name for pod 1') | |
87 | expect(video.description).to.equal('my super description for pod 1') | |
88 | expect(video.podUrl).to.equal('http://localhost:9001') | |
89 | expect(video.magnetUri).to.exist | |
90 | ||
91 | // All pods should have the same magnet Uri | |
92 | if (base_magnet === null) { | |
93 | base_magnet = video.magnetUri | |
94 | } else { | |
95 | expect(video.magnetUri).to.equal.magnetUri | |
96 | } | |
97 | ||
98 | callback() | |
99 | }) | |
100 | }, done) | |
101 | } | |
102 | ) | |
103 | }) | |
104 | ||
105 | it('Should upload the video on pod 2 and propagate on each pod', function (done) { | |
106 | this.timeout(15000) | |
107 | ||
108 | async.series([ | |
109 | function (next) { | |
110 | utils.uploadVideo(urls[1], 'my super name for pod 2', 'my super description for pod 2', 'video_short2.webm', next) | |
111 | }, | |
112 | function (next) { | |
113 | setTimeout(next, 11000) | |
114 | }], | |
115 | // All pods should have this video | |
116 | function (err) { | |
117 | if (err) throw err | |
118 | ||
119 | async.each(urls, function (url, callback) { | |
120 | var base_magnet = null | |
121 | ||
122 | utils.getVideosList(url, function (err, res) { | |
123 | if (err) throw err | |
124 | ||
125 | var videos = res.body | |
126 | expect(videos).to.be.an('array') | |
127 | expect(videos.length).to.equal(2) | |
128 | var video = videos[1] | |
129 | expect(video.name).to.equal('my super name for pod 2') | |
130 | expect(video.description).to.equal('my super description for pod 2') | |
131 | expect(video.podUrl).to.equal('http://localhost:9002') | |
132 | expect(video.magnetUri).to.exist | |
133 | ||
134 | // All pods should have the same magnet Uri | |
135 | if (base_magnet === null) { | |
136 | base_magnet = video.magnetUri | |
137 | } else { | |
138 | expect(video.magnetUri).to.equal.magnetUri | |
139 | } | |
140 | ||
141 | callback() | |
142 | }) | |
143 | }, done) | |
144 | } | |
145 | ) | |
146 | }) | |
147 | ||
148 | it('Should upload two videos on pod 3 and propagate on each pod', function (done) { | |
149 | this.timeout(30000) | |
150 | ||
151 | async.series([ | |
152 | function (next) { | |
153 | utils.uploadVideo(urls[2], 'my super name for pod 3', 'my super description for pod 3', 'video_short3.webm', next) | |
154 | }, | |
155 | function (next) { | |
156 | utils.uploadVideo(urls[2], 'my super name for pod 3-2', 'my super description for pod 3-2', 'video_short.webm', next) | |
157 | }, | |
158 | function (next) { | |
159 | setTimeout(next, 22000) | |
160 | }], | |
161 | function (err) { | |
162 | if (err) throw err | |
163 | ||
164 | var base_magnet = null | |
165 | // All pods should have this video | |
166 | async.each(urls, function (url, callback) { | |
167 | utils.getVideosList(url, function (err, res) { | |
168 | if (err) throw err | |
169 | ||
170 | var videos = res.body | |
171 | expect(videos).to.be.an('array') | |
172 | expect(videos.length).to.equal(4) | |
173 | var video = videos[2] | |
174 | expect(video.name).to.equal('my super name for pod 3') | |
175 | expect(video.description).to.equal('my super description for pod 3') | |
176 | expect(video.podUrl).to.equal('http://localhost:9003') | |
177 | expect(video.magnetUri).to.exist | |
178 | ||
179 | video = videos[3] | |
180 | expect(video.name).to.equal('my super name for pod 3-2') | |
181 | expect(video.description).to.equal('my super description for pod 3-2') | |
182 | expect(video.podUrl).to.equal('http://localhost:9003') | |
183 | expect(video.magnetUri).to.exist | |
184 | ||
185 | // All pods should have the same magnet Uri | |
186 | if (base_magnet === null) { | |
187 | base_magnet = video.magnetUri | |
188 | } else { | |
189 | expect(video.magnetUri).to.equal.magnetUri | |
190 | } | |
191 | ||
192 | callback() | |
193 | }) | |
194 | }, done) | |
195 | } | |
196 | ) | |
197 | }) | |
198 | }) | |
199 | ||
200 | describe('Should seed the uploaded video', function () { | |
201 | it('Should add the file 1 by asking pod 3', function (done) { | |
202 | // Yes, this could be long | |
203 | this.timeout(200000) | |
204 | ||
205 | utils.getVideosList(urls[2], function (err, res) { | |
206 | if (err) throw err | |
207 | ||
208 | var video = res.body[0] | |
209 | to_remove.push(res.body[2]._id) | |
210 | to_remove.push(res.body[3]._id) | |
211 | ||
212 | webtorrent.add(video.magnetUri, function (torrent) { | |
213 | expect(torrent.files).to.exist | |
214 | expect(torrent.files.length).to.equal(1) | |
215 | expect(torrent.files[0].path).to.exist.and.to.not.equal('') | |
216 | ||
217 | done() | |
218 | }) | |
219 | }) | |
220 | }) | |
221 | ||
222 | it('Should add the file 2 by asking pod 1', function (done) { | |
223 | // Yes, this could be long | |
224 | this.timeout(200000) | |
225 | ||
226 | utils.getVideosList(urls[0], function (err, res) { | |
227 | if (err) throw err | |
228 | ||
229 | var video = res.body[1] | |
230 | ||
231 | webtorrent.add(video.magnetUri, function (torrent) { | |
232 | expect(torrent.files).to.exist | |
233 | expect(torrent.files.length).to.equal(1) | |
234 | expect(torrent.files[0].path).to.exist.and.to.not.equal('') | |
235 | ||
236 | done() | |
237 | }) | |
238 | }) | |
239 | }) | |
240 | ||
241 | it('Should add the file 3 by asking pod 2', function (done) { | |
242 | // Yes, this could be long | |
243 | this.timeout(200000) | |
244 | ||
245 | utils.getVideosList(urls[1], function (err, res) { | |
246 | if (err) throw err | |
247 | ||
248 | var video = res.body[2] | |
249 | ||
250 | webtorrent.add(video.magnetUri, function (torrent) { | |
251 | expect(torrent.files).to.exist | |
252 | expect(torrent.files.length).to.equal(1) | |
253 | expect(torrent.files[0].path).to.exist.and.to.not.equal('') | |
254 | ||
255 | done() | |
256 | }) | |
257 | }) | |
258 | }) | |
259 | ||
260 | it('Should add the file 3-2 by asking pod 1', function (done) { | |
261 | // Yes, this could be long | |
262 | this.timeout(200000) | |
263 | ||
264 | utils.getVideosList(urls[0], function (err, res) { | |
265 | if (err) throw err | |
266 | ||
267 | var video = res.body[3] | |
268 | ||
269 | webtorrent.add(video.magnetUri, function (torrent) { | |
270 | expect(torrent.files).to.exist | |
271 | expect(torrent.files.length).to.equal(1) | |
272 | expect(torrent.files[0].path).to.exist.and.to.not.equal('') | |
273 | ||
274 | done() | |
275 | }) | |
276 | }) | |
277 | }) | |
278 | ||
279 | it('Should remove the file 3 and 3-2 by asking pod 3', function (done) { | |
280 | this.timeout(15000) | |
281 | ||
282 | async.series([ | |
283 | function (next) { | |
284 | utils.removeVideo(urls[2], to_remove[0], next) | |
285 | }, | |
286 | function (next) { | |
287 | utils.removeVideo(urls[2], to_remove[1], next) | |
288 | }], | |
289 | function (err) { | |
290 | if (err) throw err | |
291 | setTimeout(done, 11000) | |
292 | } | |
293 | ) | |
294 | }) | |
295 | ||
296 | it('Should have videos 1 and 3 on each pod', function (done) { | |
297 | async.each(urls, function (url, callback) { | |
298 | utils.getVideosList(url, function (err, res) { | |
299 | if (err) throw err | |
300 | ||
301 | var videos = res.body | |
302 | expect(videos).to.be.an('array') | |
303 | expect(videos.length).to.equal(2) | |
304 | expect(videos[0]._id).not.to.equal(videos[1]._id) | |
305 | expect(videos[0]._id).not.to.equal(to_remove[0]) | |
306 | expect(videos[1]._id).not.to.equal(to_remove[0]) | |
307 | expect(videos[0]._id).not.to.equal(to_remove[1]) | |
308 | expect(videos[1]._id).not.to.equal(to_remove[1]) | |
309 | ||
310 | callback() | |
311 | }) | |
312 | }, done) | |
313 | }) | |
314 | }) | |
315 | ||
316 | after(function (done) { | |
317 | apps.forEach(function (app) { | |
318 | process.kill(-app.pid) | |
319 | }) | |
320 | process.kill(-webtorrent.app.pid) | |
321 | ||
322 | // Keep the logs if the test failed | |
323 | if (this.ok) { | |
324 | utils.flushTests(done) | |
325 | } else { | |
326 | done() | |
327 | } | |
328 | }) | |
329 | }) | |
330 | })() |