Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
test: verify that multiple discovery systems do not cause conflict'
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Jan 18, 2017
1 parent cfd8e40 commit c90bde2
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 38 deletions.
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const API = require('./src/http-api')
let nodes = []

function startNode (num, done) {
createTempNode(num, (err, node) => {
createTempNode({ num: num }, (err, node) => {
if (err) {
return done(err)
}
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"form-data": "^2.1.2",
"fs-pull-blob-store": "^0.4.1",
"gulp": "^3.9.1",
"interface-ipfs-core": "^0.23.4",
"interface-ipfs-core": "^0.23.5",
"ipfsd-ctl": "^0.18.1",
"left-pad": "^1.1.3",
"lodash": "^4.17.4",
Expand Down Expand Up @@ -107,16 +107,16 @@
"ipld-resolver": "^0.4.1",
"isstream": "^0.1.2",
"libp2p-floodsub": "0.7.1",
"joi": "^10.1.0",
"libp2p-ipfs-nodejs": "^0.17.3",
"libp2p-ipfs-browser": "^0.17.4",
"joi": "^10.2.0",
"libp2p-ipfs-nodejs": "^0.17.4",
"libp2p-ipfs-browser": "^0.17.5",
"lodash.flatmap": "^4.5.0",
"lodash.get": "^4.4.2",
"lodash.has": "^4.5.2",
"lodash.set": "^4.3.2",
"lodash.sortby": "^4.7.0",
"lodash.values": "^4.3.0",
"mafmt": "^2.1.2",
"mafmt": "^2.1.5",
"multiaddr": "^2.1.3",
"multihashes": "^0.3.1",
"path-exists": "^3.0.0",
Expand Down Expand Up @@ -170,4 +170,4 @@
"nginnever <ginneversource@gmail.com>",
"npmcdn-to-unpkg-bot <npmcdn-to-unpkg-bot@users.noreply.github.com>"
]
}
}
2 changes: 1 addition & 1 deletion test/cli/test-bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('bitswap', () => {
let node

before((done) => {
createTempNode(38, (err, _node) => {
createTempNode({ num: 38 }, (err, _node) => {
expect(err).to.not.exist
node = _node
node.goOnline(done)
Expand Down
2 changes: 1 addition & 1 deletion test/cli/test-pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe.skip('pubsub', () => {
let id

before((done) => {
createTempNode(1, (err, _node) => {
createTempNode({ num: 1 }, (err, _node) => {
expect(err).to.not.exist
node = _node
node.goOnline(done)
Expand Down
2 changes: 1 addition & 1 deletion test/cli/test-swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('swarm', function () {
let nodeAddr

before((done) => {
createTempNode(1, (err, _node) => {
createTempNode({ num: 1 }, (err, _node) => {
expect(err).to.not.exist
node = _node
node.goOnline((err) => {
Expand Down
206 changes: 206 additions & 0 deletions test/core/node-only/test-discovery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/* eslint max-nested-callbacks: ["error", 8] */
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const parallel = require('async/parallel')
const series = require('async/series')
const IPFS = require('../../../src/core')

const createTempRepo = require('../../utils/temp-repo')

const sigServer = require('libp2p-webrtc-star/src/sig-server')

/*
* Note:
* Do not run these tests with other nodes in the same host with MDNS enabled
*/

// This keeps getting better, really need to finish the
// improving init thing
function createNode (webrtcStar, mdns, callback) {
const repo = createTempRepo()
const node = new IPFS(repo)

series([
(cb) => node.init({ emptyRepo: true, bits: 1024 }, cb),
(cb) => {
repo.config.get((err, config) => {
expect(err).to.not.exist

config.Addresses = {
Swarm: ['/ip4/127.0.0.1/tcp/0'],
API: '',
Gateway: ''
}
if (webrtcStar) {
const peerIdStr = config.Identity.PeerID
const wstarAddr = '/libp2p-webrtc-star/ip4/127.0.0.1/tcp/33333/ws/ipfs/' + peerIdStr

config.Addresses.Swarm.push(wstarAddr)
config.Discovery.MDNS.Enabled = mdns
}
repo.config.set(config, cb)
})
},
(cb) => node.load(cb)
], (err) => callback(err, node))
}

describe.only('discovery', () => {
let nodeA // only mdns
let nodeB // mdns + webrtc-star discovery
let nodeC // mdns + webrtc-star discovery
let nodeD // only webrtc-star discovery

let ss

before((done) => {
parallel([
(cb) => {
sigServer.start({
port: 33333
}, (err, server) => {
expect(err).to.not.exist
ss = server
cb()
})
},
// create 4 nodesconst IPFS = require('../../src/core')

(cb) => {
createNode(false, true, (err, node) => {
expect(err).to.not.exist
nodeA = node
cb()
})
},
(cb) => {
createNode(true, true, (err, node) => {
expect(err).to.not.exist
nodeB = node
cb()
})
},
(cb) => {
createNode(true, true, (err, node) => {
expect(err).to.not.exist
nodeC = node
cb()
})
},
(cb) => {
createNode(true, false, (err, node) => {
expect(err).to.not.exist
nodeD = node
cb()
})
}

], done)
})

after((done) => {
series([
(cb) => nodeA.goOffline(cb),
(cb) => nodeB.goOffline(cb),
(cb) => nodeC.goOffline(cb),
(cb) => nodeD.goOffline(cb),
(cb) => ss.stop(cb)
], done)
})

it('boot nodeA', (done) => {
nodeA.goOnline(done)
})

it('boot nodeB, verify that MDNS worked', (done) => {
nodeB.goOnline(() => setTimeout(check, 40000))

function check () {
parallel([
(cb) => {
nodeA.swarm.peers((err, peers) => {
expect(err).to.not.exist
expect(peers.length).to.equal(1)
cb()
})
},
(cb) => {
nodeB.swarm.peers((err, peers) => {
expect(err).to.not.exist
expect(peers.length).to.equal(1)
cb()
})
}
], done)
}
})

it('boot nodeC, verify that MDNS or webrtc-star worked without conflict', (done) => {
nodeC.goOnline(() => setTimeout(check, 60000))

function check () {
parallel([
(cb) => {
nodeA.swarm.peers((err, peers) => {
expect(err).to.not.exist
expect(peers.length).to.equal(2)
cb()
})
},
(cb) => {
nodeB.swarm.peers((err, peers) => {
expect(err).to.not.exist
expect(peers.length).to.equal(2)
cb()
})
},
(cb) => {
nodeC.swarm.peers((err, peers) => {
expect(err).to.not.exist
expect(peers.length).to.equal(2)
cb()
})
}
], done)
}
})

it('boot nodeD, verify that MDNS or webrtc-star worked without conflict', (done) => {
nodeD.goOnline(() => setTimeout(check, 60000))

function check () {
parallel([
(cb) => {
nodeA.swarm.peers((err, peers) => {
expect(err).to.not.exist
expect(peers.length).to.equal(2)
cb()
})
},
(cb) => {
nodeB.swarm.peers((err, peers) => {
expect(err).to.not.exist
expect(peers.length).to.equal(3)
cb()
})
},
(cb) => {
nodeC.swarm.peers((err, peers) => {
expect(err).to.not.exist
expect(peers.length).to.equal(3)
cb()
})
},
(cb) => {
nodeD.swarm.peers((err, peers) => {
expect(err).to.not.exist
expect(peers.length).to.equal(2)
cb()
})
}
], done)
}
})
})
2 changes: 1 addition & 1 deletion test/http-api/custom-ipfs-api/test-swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = (ctl) => {
let remoteNodeAddr

before((done) => {
createTempNode(6, (err, _remoteNode) => {
createTempNode({ num: 6 }, (err, _remoteNode) => {
expect(err).to.not.exist
remoteNode = _remoteNode
remoteNode.goOnline(() => {
Expand Down
2 changes: 1 addition & 1 deletion test/http-api/inject/test-pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = (http) => {
before((done) => {
api = http.api.server.select('API')

createTempNode(47, (err, _ipfs) => {
createTempNode({ num: 47 }, (err, _ipfs) => {
expect(err).to.not.exist
tmpNode = _ipfs
tmpNode.goOnline((err) => {
Expand Down
2 changes: 1 addition & 1 deletion test/http-api/inject/test-swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = (http) => {
before((done) => {
api = http.api.server.select('API')

createTempNode(47, (err, _ipfs) => {
createTempNode({ num: 47 }, (err, _ipfs) => {
expect(err).to.not.exist
tmpNode = _ipfs
tmpNode.goOnline((err) => {
Expand Down
55 changes: 30 additions & 25 deletions test/utils/temp-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,51 @@ const series = require('async/series')
const IPFS = require('../../src/core')
const createTempRepo = require('./temp-repo')

function setAddresses (repo, num, callback) {
function setAddresses (repo, addresses, callback) {
repo.config.get((err, config) => {
if (err) {
return callback(err)
}

config.Addresses = {
Swarm: [
`/ip4/127.0.0.1/tcp/10${num}`,
`/ip4/127.0.0.1/tcp/20${num}/ws`
],
API: `/ip4/127.0.0.1/tcp/31${num}`,
Gateway: `/ip4/127.0.0.1/tcp/32${num}`
}
config.Addresses = addresses

config.Discovery.MDNS.Enabled = false

repo.config.set(config, callback)
})
}

function createTempNode (num, callback) {
const repo = createTempRepo()
const ipfs = new IPFS(repo)
/*
* options.repo - repo to use
* options.num - generate addrs based on num for port
* options.addresses - uses this addrs instead of default
*/
function createTempNode (options, callback) {
const repo = options.repo ? options.repo : createTempRepo()
const node = new IPFS(repo)

num = leftPad(num, 3, 0)
let addresses

series([
(cb) => ipfs.init({
emptyRepo: true,
bits: 1024
}, cb),
(cb) => setAddresses(repo, num, cb),
(cb) => ipfs.load(cb)
], (err) => {
if (err) {
return callback(err)
if (options.num) {
const pad = leftPad(options.num, 3, 0)

addresses = {
Swarm: [
`/ip4/127.0.0.1/tcp/10${pad}`,
`/ip4/127.0.0.1/tcp/20${pad}/ws`
],
API: `/ip4/127.0.0.1/tcp/31${pad}`,
Gateway: `/ip4/127.0.0.1/tcp/32${pad}`
}
callback(null, ipfs)
})
} else if (options.addresses) {
addresses = options.addresses
}

series([
(cb) => node.init({ emptyRepo: true, bits: 1024 }, cb),
(cb) => setAddresses(repo, addresses, cb),
(cb) => node.load(cb)
], (err) => callback(err, node))
}

module.exports = createTempNode

0 comments on commit c90bde2

Please sign in to comment.