Skip to content

Commit

Permalink
fix: swarm addrs test (ipfs#454)
Browse files Browse the repository at this point in the history
* fix: swarm addrs test

If the common factory creates a node with no bootstrap nodes then getting a list of swarm addresses could be empty (or could also be empty anyway if the connection to the bootstrap nodes takes a long time). This PR spawns and connects another IPFS node to ensure that there should be swarm addresses available.

License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
  • Loading branch information
Alan Shaw authored Apr 4, 2019
1 parent 0f256bf commit 16ad830
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
6 changes: 3 additions & 3 deletions SPEC/SWARM.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@
##### `ipfs.swarm.addrs([callback])`

`callback` must follow `function (err, addrs) {}` signature, where `err` is an error if the operation was not successful. `addrs` will be an array of [`PeerInfo`](https://github.com/libp2p/js-peer-info)s.
`callback` must follow `function (err, peerInfos) {}` signature, where `err` is an error if the operation was not successful. `peerInfos` will be an array of [`PeerInfo`](https://github.com/libp2p/js-peer-info)s.

If no `callback` is passed, a promise is returned.

**Example:**

```JavaScript
ipfs.swarm.addrs(function (err, addrs) {
ipfs.swarm.addrs(function (err, peerInfos) {
if (err) {
throw err
}
console.log(addrs)
console.log(peerInfos)
})
```

Expand Down
24 changes: 14 additions & 10 deletions src/swarm/addrs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-env mocha */
'use strict'

const PeerInfo = require('peer-info')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const { spawnNodesWithId } = require('../utils/spawn')

module.exports = (createCommon, options) => {
const describe = getDescribe(options)
Expand All @@ -11,7 +13,7 @@ module.exports = (createCommon, options) => {
describe('.swarm.addrs', function () {
this.timeout(80 * 1000)

let ipfs
let ipfsA, ipfsB

before(function (done) {
// CI takes longer to instantiate the daemon, so we need to increase the
Expand All @@ -20,29 +22,31 @@ module.exports = (createCommon, options) => {

common.setup((err, factory) => {
expect(err).to.not.exist()
factory.spawnNode((err, node) => {

spawnNodesWithId(2, factory, (err, nodes) => {
expect(err).to.not.exist()
ipfs = node
done()
ipfsA = nodes[0]
ipfsB = nodes[1]
ipfsA.swarm.connect(ipfsB.peerId.addresses[0], done)
})
})
})

after((done) => common.teardown(done))

it('should get a list of node addresses', (done) => {
ipfs.swarm.addrs((err, multiaddrs) => {
ipfsA.swarm.addrs((err, peerInfos) => {
expect(err).to.not.exist()
expect(multiaddrs).to.not.be.empty()
expect(multiaddrs).to.be.an('array')
expect(multiaddrs[0].constructor.name).to.be.eql('PeerInfo')
expect(peerInfos).to.not.be.empty()
expect(peerInfos).to.be.an('array')
peerInfos.forEach(m => expect(PeerInfo.isPeerInfo(m)).to.be.true())
done()
})
})

it('should get a list of node addresses (promised)', () => {
return ipfs.swarm.addrs().then((multiaddrs) => {
expect(multiaddrs).to.have.length.above(0)
return ipfsA.swarm.addrs().then((peerInfos) => {
expect(peerInfos).to.have.length.above(0)
})
})
})
Expand Down

0 comments on commit 16ad830

Please sign in to comment.