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 () {
17 before(function (done) {
23 utils.flushAndRunMultipleServers(3, function (apps_run, urls_run) {
29 // The second pod make friend with the third
31 utils.makeFriends(urls[1], next)
33 // Wait for the request between pods
35 setTimeout(next, 10000)
37 // Pod 1 make friends too
39 utils.makeFriends(urls[0], next)
42 webtorrent.create({ host: 'client', port: '1' }, next)
47 it('Should not have videos for all pods', function (done) {
48 async.each(urls, function (url, callback) {
49 utils.getVideosList(url, function (err, res) {
52 expect(res.body).to.be.an('array')
53 expect(res.body.length).to.equal(0)
60 describe('Should upload the video and propagate on each pod', function () {
61 it('Should upload the video on pod 1 and propagate on each pod', function (done) {
66 utils.uploadVideo(urls[0], 'my super name for pod 1', 'my super description for pod 1', 'video_short1.webm', next)
69 setTimeout(next, 11000)
71 // All pods should have this video
75 async.each(urls, function (url, callback) {
76 let base_magnet = null
78 utils.getVideosList(url, function (err, res) {
81 const videos = res.body
82 expect(videos).to.be.an('array')
83 expect(videos.length).to.equal(1)
84 const video = videos[0]
85 expect(video.name).to.equal('my super name for pod 1')
86 expect(video.description).to.equal('my super description for pod 1')
87 expect(video.podUrl).to.equal('http://localhost:9001')
88 expect(video.magnetUri).to.exist
90 // All pods should have the same magnet Uri
91 if (base_magnet === null) {
92 base_magnet = video.magnetUri
94 expect(video.magnetUri).to.equal.magnetUri
104 it('Should upload the video on pod 2 and propagate on each pod', function (done) {
109 utils.uploadVideo(urls[1], 'my super name for pod 2', 'my super description for pod 2', 'video_short2.webm', next)
112 setTimeout(next, 11000)
114 // All pods should have this video
118 async.each(urls, function (url, callback) {
119 let base_magnet = null
121 utils.getVideosList(url, function (err, res) {
124 const videos = res.body
125 expect(videos).to.be.an('array')
126 expect(videos.length).to.equal(2)
127 const video = videos[1]
128 expect(video.name).to.equal('my super name for pod 2')
129 expect(video.description).to.equal('my super description for pod 2')
130 expect(video.podUrl).to.equal('http://localhost:9002')
131 expect(video.magnetUri).to.exist
133 // All pods should have the same magnet Uri
134 if (base_magnet === null) {
135 base_magnet = video.magnetUri
137 expect(video.magnetUri).to.equal.magnetUri
147 it('Should upload two videos on pod 3 and propagate on each pod', function (done) {
152 utils.uploadVideo(urls[2], 'my super name for pod 3', 'my super description for pod 3', 'video_short3.webm', next)
155 utils.uploadVideo(urls[2], 'my super name for pod 3-2', 'my super description for pod 3-2', 'video_short.webm', next)
158 setTimeout(next, 22000)
163 let base_magnet = null
164 // All pods should have this video
165 async.each(urls, function (url, callback) {
166 utils.getVideosList(url, function (err, res) {
169 const videos = res.body
170 expect(videos).to.be.an('array')
171 expect(videos.length).to.equal(4)
172 let video = videos[2]
173 expect(video.name).to.equal('my super name for pod 3')
174 expect(video.description).to.equal('my super description for pod 3')
175 expect(video.podUrl).to.equal('http://localhost:9003')
176 expect(video.magnetUri).to.exist
179 expect(video.name).to.equal('my super name for pod 3-2')
180 expect(video.description).to.equal('my super description for pod 3-2')
181 expect(video.podUrl).to.equal('http://localhost:9003')
182 expect(video.magnetUri).to.exist
184 // All pods should have the same magnet Uri
185 if (base_magnet === null) {
186 base_magnet = video.magnetUri
188 expect(video.magnetUri).to.equal.magnetUri
199 describe('Should seed the uploaded video', function () {
200 it('Should add the file 1 by asking pod 3', function (done) {
201 // Yes, this could be long
204 utils.getVideosList(urls[2], function (err, res) {
207 const video = res.body[0]
208 to_remove.push(res.body[2]._id)
209 to_remove.push(res.body[3]._id)
211 webtorrent.add(video.magnetUri, function (torrent) {
212 expect(torrent.files).to.exist
213 expect(torrent.files.length).to.equal(1)
214 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
221 it('Should add the file 2 by asking pod 1', function (done) {
222 // Yes, this could be long
225 utils.getVideosList(urls[0], function (err, res) {
228 const video = res.body[1]
230 webtorrent.add(video.magnetUri, function (torrent) {
231 expect(torrent.files).to.exist
232 expect(torrent.files.length).to.equal(1)
233 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
240 it('Should add the file 3 by asking pod 2', function (done) {
241 // Yes, this could be long
244 utils.getVideosList(urls[1], function (err, res) {
247 const video = res.body[2]
249 webtorrent.add(video.magnetUri, function (torrent) {
250 expect(torrent.files).to.exist
251 expect(torrent.files.length).to.equal(1)
252 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
259 it('Should add the file 3-2 by asking pod 1', function (done) {
260 // Yes, this could be long
263 utils.getVideosList(urls[0], function (err, res) {
266 const video = res.body[3]
268 webtorrent.add(video.magnetUri, function (torrent) {
269 expect(torrent.files).to.exist
270 expect(torrent.files.length).to.equal(1)
271 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
278 it('Should remove the file 3 and 3-2 by asking pod 3', function (done) {
283 utils.removeVideo(urls[2], to_remove[0], next)
286 utils.removeVideo(urls[2], to_remove[1], next)
290 setTimeout(done, 11000)
295 it('Should have videos 1 and 3 on each pod', function (done) {
296 async.each(urls, function (url, callback) {
297 utils.getVideosList(url, function (err, res) {
300 const videos = res.body
301 expect(videos).to.be.an('array')
302 expect(videos.length).to.equal(2)
303 expect(videos[0]._id).not.to.equal(videos[1]._id)
304 expect(videos[0]._id).not.to.equal(to_remove[0])
305 expect(videos[1]._id).not.to.equal(to_remove[0])
306 expect(videos[0]._id).not.to.equal(to_remove[1])
307 expect(videos[1]._id).not.to.equal(to_remove[1])
315 after(function (done) {
316 apps.forEach(function (app) {
317 process.kill(-app.pid)
319 process.kill(-webtorrent.app.pid)
321 // Keep the logs if the test failed
323 utils.flushTests(done)