3 const async = require('async')
4 const chai = require('chai')
5 const expect = chai.expect
6 const pathUtils = require('path')
8 const utils = require('./utils')
9 const webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent'))
10 webtorrent.silent = true
12 describe('Test multiple pods', function () {
16 before(function (done) {
22 utils.flushAndRunMultipleServers(3, function (servers_run) {
27 // Get the access tokens
29 async.each(servers, function (server, callback_each) {
30 utils.loginAndGetAccessToken(server, function (err, access_token) {
31 if (err) return callback_each(err)
33 server.access_token = access_token
38 // The second pod make friend with the third
40 utils.makeFriends(servers[1].url, next)
42 // Wait for the request between pods
44 setTimeout(next, 10000)
46 // Pod 1 make friends too
48 utils.makeFriends(servers[0].url, next)
51 webtorrent.create({ host: 'client', port: '1' }, next)
56 it('Should not have videos for all pods', function (done) {
57 async.each(servers, function (server, callback) {
58 utils.getVideosList(server.url, function (err, res) {
61 expect(res.body).to.be.an('array')
62 expect(res.body.length).to.equal(0)
69 describe('Should upload the video and propagate on each pod', function () {
70 it('Should upload the video on pod 1 and propagate on each pod', function (done) {
75 utils.uploadVideo(servers[0].url, servers[0].access_token, 'my super name for pod 1', 'my super description for pod 1', 'video_short1.webm', next)
78 setTimeout(next, 11000)
80 // All pods should have this video
84 async.each(servers, function (server, callback) {
85 let base_magnet = null
87 utils.getVideosList(server.url, function (err, res) {
90 const videos = res.body
91 expect(videos).to.be.an('array')
92 expect(videos.length).to.equal(1)
93 const video = videos[0]
94 expect(video.name).to.equal('my super name for pod 1')
95 expect(video.description).to.equal('my super description for pod 1')
96 expect(video.podUrl).to.equal('http://localhost:9001')
97 expect(video.magnetUri).to.exist
99 // All pods should have the same magnet Uri
100 if (base_magnet === null) {
101 base_magnet = video.magnetUri
103 expect(video.magnetUri).to.equal.magnetUri
113 it('Should upload the video on pod 2 and propagate on each pod', function (done) {
118 utils.uploadVideo(servers[1].url, servers[1].access_token, 'my super name for pod 2', 'my super description for pod 2', 'video_short2.webm', next)
121 setTimeout(next, 11000)
123 // All pods should have this video
127 async.each(servers, function (server, callback) {
128 let base_magnet = null
130 utils.getVideosList(server.url, function (err, res) {
133 const videos = res.body
134 expect(videos).to.be.an('array')
135 expect(videos.length).to.equal(2)
136 const video = videos[1]
137 expect(video.name).to.equal('my super name for pod 2')
138 expect(video.description).to.equal('my super description for pod 2')
139 expect(video.podUrl).to.equal('http://localhost:9002')
140 expect(video.magnetUri).to.exist
142 // All pods should have the same magnet Uri
143 if (base_magnet === null) {
144 base_magnet = video.magnetUri
146 expect(video.magnetUri).to.equal.magnetUri
156 it('Should upload two videos on pod 3 and propagate on each pod', function (done) {
161 utils.uploadVideo(servers[2].url, servers[2].access_token, 'my super name for pod 3', 'my super description for pod 3', 'video_short3.webm', next)
164 utils.uploadVideo(servers[2].url, servers[2].access_token, 'my super name for pod 3-2', 'my super description for pod 3-2', 'video_short.webm', next)
167 setTimeout(next, 22000)
172 let base_magnet = null
173 // All pods should have this video
174 async.each(servers, function (server, callback) {
175 utils.getVideosList(server.url, function (err, res) {
178 const videos = res.body
179 expect(videos).to.be.an('array')
180 expect(videos.length).to.equal(4)
181 let video = videos[2]
182 expect(video.name).to.equal('my super name for pod 3')
183 expect(video.description).to.equal('my super description for pod 3')
184 expect(video.podUrl).to.equal('http://localhost:9003')
185 expect(video.magnetUri).to.exist
188 expect(video.name).to.equal('my super name for pod 3-2')
189 expect(video.description).to.equal('my super description for pod 3-2')
190 expect(video.podUrl).to.equal('http://localhost:9003')
191 expect(video.magnetUri).to.exist
193 // All pods should have the same magnet Uri
194 if (base_magnet === null) {
195 base_magnet = video.magnetUri
197 expect(video.magnetUri).to.equal.magnetUri
208 describe('Should seed the uploaded video', function () {
209 it('Should add the file 1 by asking pod 3', function (done) {
210 // Yes, this could be long
213 utils.getVideosList(servers[2].url, function (err, res) {
216 const video = res.body[0]
217 to_remove.push(res.body[2].id)
218 to_remove.push(res.body[3].id)
220 webtorrent.add(video.magnetUri, function (torrent) {
221 expect(torrent.files).to.exist
222 expect(torrent.files.length).to.equal(1)
223 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
230 it('Should add the file 2 by asking pod 1', function (done) {
231 // Yes, this could be long
234 utils.getVideosList(servers[0].url, function (err, res) {
237 const video = res.body[1]
239 webtorrent.add(video.magnetUri, function (torrent) {
240 expect(torrent.files).to.exist
241 expect(torrent.files.length).to.equal(1)
242 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
249 it('Should add the file 3 by asking pod 2', function (done) {
250 // Yes, this could be long
253 utils.getVideosList(servers[1].url, function (err, res) {
256 const video = res.body[2]
258 webtorrent.add(video.magnetUri, function (torrent) {
259 expect(torrent.files).to.exist
260 expect(torrent.files.length).to.equal(1)
261 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
268 it('Should add the file 3-2 by asking pod 1', function (done) {
269 // Yes, this could be long
272 utils.getVideosList(servers[0].url, function (err, res) {
275 const video = res.body[3]
277 webtorrent.add(video.magnetUri, function (torrent) {
278 expect(torrent.files).to.exist
279 expect(torrent.files.length).to.equal(1)
280 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
287 it('Should remove the file 3 and 3-2 by asking pod 3', function (done) {
292 utils.removeVideo(servers[2].url, servers[2].access_token, to_remove[0], next)
295 utils.removeVideo(servers[2].url, servers[2].access_token, to_remove[1], next)
299 setTimeout(done, 11000)
304 it('Should have videos 1 and 3 on each pod', function (done) {
305 async.each(servers, function (server, callback) {
306 utils.getVideosList(server.url, function (err, res) {
309 const videos = res.body
310 expect(videos).to.be.an('array')
311 expect(videos.length).to.equal(2)
312 expect(videos[0].id).not.to.equal(videos[1].id)
313 expect(videos[0].id).not.to.equal(to_remove[0])
314 expect(videos[1].id).not.to.equal(to_remove[0])
315 expect(videos[0].id).not.to.equal(to_remove[1])
316 expect(videos[1].id).not.to.equal(to_remove[1])
324 after(function (done) {
325 servers.forEach(function (server) {
326 process.kill(-server.app.pid)
328 process.kill(-webtorrent.app.pid)
330 // Keep the logs if the test failed
332 utils.flushTests(done)