|
| 1 | +/* eslint max-nested-callbacks: ["error", 8] */ |
| 2 | +/* eslint-env mocha */ |
| 3 | +'use strict' |
| 4 | + |
| 5 | +const expect = require('chai').expect |
| 6 | +const parallel = require('async/parallel') |
| 7 | +const series = require('async/series') |
| 8 | +const IPFS = require('../../../src/core') |
| 9 | + |
| 10 | +const createTempRepo = require('../../utils/temp-repo') |
| 11 | + |
| 12 | +const sigServer = require('libp2p-webrtc-star/src/sig-server') |
| 13 | + |
| 14 | +/* |
| 15 | + * Note: |
| 16 | + * Do not run these tests with other nodes in the same host with MDNS enabled |
| 17 | + */ |
| 18 | + |
| 19 | +// This keeps getting better, really need to finish the |
| 20 | +// improving init thing |
| 21 | +function createNode (webrtcStar, callback) { |
| 22 | + const repo = createTempRepo() |
| 23 | + const node = new IPFS(repo) |
| 24 | + |
| 25 | + series([ |
| 26 | + (cb) => node.init({ emptyRepo: true, bits: 1024 }, cb), |
| 27 | + (cb) => { |
| 28 | + repo.config.get((err, config) => { |
| 29 | + expect(err).to.not.exist |
| 30 | + |
| 31 | + config.Addresses = { |
| 32 | + Swarm: ['/ip4/127.0.0.1/tcp/0'], |
| 33 | + API: '', |
| 34 | + Gateway: '' |
| 35 | + } |
| 36 | + if (webrtcStar) { |
| 37 | + const peerIdStr = config.Identity.PeerID |
| 38 | + const wstarAddr = '/libp2p-webrtc-star/ip4/127.0.0.1/tcp/33333/ws/ipfs/' + peerIdStr |
| 39 | + |
| 40 | + config.Addresses.Swarm.push(wstarAddr) |
| 41 | + } |
| 42 | + repo.config.set(config, cb) |
| 43 | + }) |
| 44 | + }, |
| 45 | + (cb) => node.load(cb) |
| 46 | + ], (err) => callback(err, node)) |
| 47 | +} |
| 48 | + |
| 49 | +describe.only('discovery', () => { |
| 50 | + let nodeA // only mdns |
| 51 | + let nodeB // mdns + webrtc-star discovery |
| 52 | + let nodeC // mdns + webrtc-star discovery |
| 53 | + |
| 54 | + let ss |
| 55 | + |
| 56 | + before((done) => { |
| 57 | + parallel([ |
| 58 | + (cb) => { |
| 59 | + sigServer.start({ |
| 60 | + port: 33333 |
| 61 | + }, (err, server) => { |
| 62 | + expect(err).to.not.exist |
| 63 | + ss = server |
| 64 | + cb() |
| 65 | + }) |
| 66 | + }, |
| 67 | + // create 4 nodes |
| 68 | + (cb) => { |
| 69 | + createNode(false, (err, node) => { |
| 70 | + expect(err).to.not.exist |
| 71 | + nodeA = node |
| 72 | + cb() |
| 73 | + }) |
| 74 | + }, |
| 75 | + (cb) => { |
| 76 | + createNode(true, (err, node) => { |
| 77 | + expect(err).to.not.exist |
| 78 | + nodeB = node |
| 79 | + cb() |
| 80 | + }) |
| 81 | + }, |
| 82 | + (cb) => { |
| 83 | + createNode(true, (err, node) => { |
| 84 | + expect(err).to.not.exist |
| 85 | + nodeC = node |
| 86 | + cb() |
| 87 | + }) |
| 88 | + } |
| 89 | + ], done) |
| 90 | + }) |
| 91 | + |
| 92 | + after((done) => { |
| 93 | + series([ |
| 94 | + (cb) => nodeA.goOffline(cb), |
| 95 | + (cb) => nodeB.goOffline(cb), |
| 96 | + (cb) => nodeC.goOffline(cb), |
| 97 | + (cb) => ss.stop(cb) |
| 98 | + ], done) |
| 99 | + }) |
| 100 | + |
| 101 | + it('boot nodeA', (done) => { |
| 102 | + nodeA.goOnline(done) |
| 103 | + }) |
| 104 | + |
| 105 | + it('boot nodeB, verify that MDNS worked', (done) => { |
| 106 | + nodeB.goOnline(done) |
| 107 | + }) |
| 108 | + |
| 109 | + it('boot nodeC, verify that MDNS or webrtc-star worked without conflict', (done) => { |
| 110 | + nodeC.goOnline(done) |
| 111 | + }) |
| 112 | +}) |
0 commit comments