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

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Apr 18, 2016
1 parent 218e8f8 commit aeb5971
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 100 deletions.
9 changes: 5 additions & 4 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function IPFS (repo) {
const dagS = new DAGService(blockS)
var peerInfo
var libp2pNode
var peers = {}
var peerInfoBook = {}

this.load = (callback) => {
repo.exists((err, exists) => {
Expand Down Expand Up @@ -331,14 +331,14 @@ function IPFS (repo) {
return callback(OFFLINE_ERROR)
}

callback(null, peers)
callback(null, peerInfoBook)
},
// all the addrs we know
addrs: (callback) => {
if (!libp2pNode) {
return callback(OFFLINE_ERROR)
}

// TODO
notImpl()
},
localAddrs: (callback) => {
Expand All @@ -363,7 +363,8 @@ function IPFS (repo) {
ma = ma.toString().replace(/\/ipfs\/(.*)/, '') // FIXME remove this when multiaddr supports ipfs

peer.multiaddr.add(multiaddr(ma))
peers[peer.id.toB58String()] = peer
peerInfoBook[peer.id.toB58String()] = peer

libp2pNode.swarm.dial(peer, (err) => {
callback(err, id)
})
Expand Down
11 changes: 9 additions & 2 deletions src/http-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ log.error = debug('api:error')

exports = module.exports

exports.start = (callback) => {
const ipfs = exports.ipfs = new IPFS()
exports.start = (repo, callback) => {
if (typeof repo === 'function') {
callback = repo
repo = undefined
}

const ipfs = exports.ipfs = new IPFS(repo)
ipfs.load(() => {
const repoPath = process.env.IPFS_PATH || os.homedir() + '/.ipfs'
try {
Expand Down Expand Up @@ -67,7 +72,9 @@ exports.start = (callback) => {
exports.stop = (callback) => {
const repoPath = process.env.IPFS_PATH || os.homedir() + '/.ipfs'
fs.unlinkSync(repoPath + '/api')
console.log('->', 'going to stop libp2p')
exports.ipfs.libp2p.stop(() => {
console.log('->', 'going to stop api server')
exports.server.stop(callback)
})
}
2 changes: 1 addition & 1 deletion test/core-tests/test-swarm-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('swarm', function () {
it('localAddrs', (done) => {
ipfsB.libp2p.swarm.localAddrs((err, res) => {
expect(err).to.not.exist
expect(res).to.have.length.above(1)
expect(res.length).to.equal(1)
done()
})
})
Expand Down
16 changes: 15 additions & 1 deletion test/http-api-tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,24 @@ describe('http api', () => {

after((done) => {
api.stop((err) => {
console.log('->', 'going to clean up')
expect(err).to.not.exist
var cleaned = false

// cleaning up on CI takes a millenia
setTimeout(() => {
if (!cleaned) {
cleaned = true
console.log('->', 'last after all')
done()
}
}, 2000)
rimraf(repoTests, (err) => {
expect(err).to.not.exist
done()
if (!cleaned) {
cleaned = true
done()
}
})
})
})
Expand Down
184 changes: 94 additions & 90 deletions test/http-api-tests/test-swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('swarm', function () {

describe('api', () => {
var api
var ipfs
var ipfs // tmp node
var ipfsAddr

before((done) => {
Expand All @@ -28,69 +28,71 @@ describe('swarm', function () {
})
})

it('api', (done) => {
after((done) => {
console.log('after all')
ipfs.libp2p.stop(() => {
console.log('after all called')
done()
})
})

it('gets the api obj', (done) => {
api = require('../../src/http-api').server.select('API')
done()
})

describe('/swarm/connect', () => {
it('returns 400 for request without argument', (done) => {
api.inject({
method: 'GET',
url: '/api/v0/swarm/connect'
}, (res) => {
expect(res.statusCode).to.equal(400)
expect(res.result).to.be.a('string')
done()
})
})

it('returns 500 for request with invalid argument', (done) => {
api.inject({
method: 'GET',
url: '/api/v0/swarm/connect?arg=invalid'
}, (res) => {
expect(res.statusCode).to.equal(500)
expect(res.result.Code).to.equal(0)
expect(res.result.Message).to.be.a('string')
done()
})
})

it('returns value', (done) => {
api.inject({
method: 'GET',
url: `/api/v0/swarm/connect?arg=${ipfsAddr}`
}, (res) => {
expect(res.statusCode).to.equal(200)
done()
})
})
})

describe('/swarm/peers', () => {
it('returns value', (done) => {
api.inject({
method: 'GET',
url: '/api/v0/swarm/peers'
}, (res) => {
expect(res.statusCode).to.equal(200)
expect(res.result.Strings).to.have.length.above(0)
done()
})
})
})

describe('/swarm/addrs/local', () => {
it('returns value', (done) => {
api.inject({
method: 'GET',
url: '/api/v0/swarm/addrs/local'
}, (res) => {
expect(res.statusCode).to.equal(200)
expect(res.result.Strings).to.have.length.above(0)
done()
})
it('/swarm/connect returns 400 for request without argument', (done) => {
api.inject({
method: 'GET',
url: '/api/v0/swarm/connect'
}, (res) => {
expect(res.statusCode).to.equal(400)
expect(res.result).to.be.a('string')
done()
})
})

it('/swarm/connect returns 500 for request with invalid argument', (done) => {
api.inject({
method: 'GET',
url: '/api/v0/swarm/connect?arg=invalid'
}, (res) => {
expect(res.statusCode).to.equal(500)
expect(res.result.Code).to.equal(0)
expect(res.result.Message).to.be.a('string')
done()
})
})

it('/swarm/connect returns value', (done) => {
api.inject({
method: 'GET',
url: `/api/v0/swarm/connect?arg=${ipfsAddr}`
}, (res) => {
expect(res.statusCode).to.equal(200)
done()
})
})

it('/swarm/peers returns value', (done) => {
api.inject({
method: 'GET',
url: '/api/v0/swarm/peers'
}, (res) => {
expect(res.statusCode).to.equal(200)
expect(res.result.Strings).to.have.length.above(0)
done()
})
})

it('/swarm/addrs/local returns value', (done) => {
api.inject({
method: 'GET',
url: '/api/v0/swarm/addrs/local'
}, (res) => {
expect(res.statusCode).to.equal(200)
expect(res.result.Strings).to.have.length.above(0)
done()
})
})
})
Expand All @@ -116,51 +118,53 @@ describe('swarm', function () {
})
})

after((done) => {
console.log('after all')
ipfs.libp2p.stop(() => {
console.log('after all called')
done()
})
})

it('start IPFS API ctl', (done) => {
ctl = APIctl('/ip4/127.0.0.1/tcp/6001')
done()
})

describe('ipfs.swarm.connect', () => {
it('returns error for request without argument', (done) => {
ctl.swarm.connect(null, (err, result) => {
expect(err).to.exist
done()
})
it('ipfs.swarm.connect returns error for request without argument', (done) => {
ctl.swarm.connect(null, (err, result) => {
expect(err).to.exist
done()
})
})

it('returns error for request with invalid argument', (done) => {
ctl.swarm.connect('invalid', (err, result) => {
expect(err).to.exist
done()
})
it('ipfs.swarm.connect returns error for request with invalid argument', (done) => {
ctl.swarm.connect('invalid', (err, result) => {
expect(err).to.exist
done()
})
})

it('returns value', (done) => {
ctl.swarm.connect(ipfsAddr, (err, result) => {
expect(err).to.not.exist
done()
})
it('ipfs.swarm.connect returns value', (done) => {
ctl.swarm.connect(ipfsAddr, (err, result) => {
expect(err).to.not.exist
done()
})
})

describe('ipfs.swarm.peers', () => {
it('returns value', (done) => {
ctl.swarm.peers((err, result) => {
expect(err).to.not.exist
expect(result.Strings).to.have.length.above(0)
done()
})
it('ipfs.swarm.peers returns value', (done) => {
ctl.swarm.peers((err, result) => {
expect(err).to.not.exist
expect(result.Strings).to.have.length.above(0)
done()
})
})

describe('ipfs.swarm.localAddrs', () => {
it('returns value', (done) => {
ctl.swarm.localAddrs((err, result) => {
expect(err).to.not.exist
expect(result.Strings).to.have.length.above(0)
done()
})
it('ipfs.swarm.localAddrsreturns value', (done) => {
ctl.swarm.localAddrs((err, result) => {
expect(err).to.not.exist
expect(result.Strings).to.have.length.above(0)
done()
})
})
})
Expand Down
3 changes: 1 addition & 2 deletions test/utils/temp-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ function setAddresses (repo, num, callback) {
expect(err).to.not.exist
config.Addresses = {
Swarm: [
`/ip4/127.0.0.1/tcp/1000${num}`,
`/ip6/::/tcp/1000${num}`
`/ip4/127.0.0.1/tcp/1000${num}`
],
API: `/ip4/127.0.0.1/tcp/1100${num}`,
Gateway: `/ip4/127.0.0.1/tcp/1200${num}`
Expand Down

0 comments on commit aeb5971

Please sign in to comment.