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

Commit

Permalink
test: core tests for bitswap and 3 peers
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed May 8, 2016
1 parent 868349b commit 2320cdf
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 143 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"homepage": "https://github.com/ipfs/js-ipfs#readme",
"devDependencies": {
"aegir": "^3.0.1",
"async": "^2.0.0-rc.3",
"async": "^2.0.0-rc.4",
"buffer-loader": "0.0.1",
"chai": "^3.5.0",
"expose-loader": "^0.7.1",
Expand All @@ -65,7 +65,7 @@
"ipfs-api": "^3.0.1",
"ipfs-bitswap": "^0.2.0",
"ipfs-block": "^0.3.0",
"ipfs-block-service": "^0.3.0",
"ipfs-block-service": "^0.4.0",
"ipfs-data-importing": "^0.3.3",
"ipfs-merkle-dag": "^0.5.0",
"ipfs-multipart": "^0.1.0",
Expand Down
188 changes: 157 additions & 31 deletions test/core-tests/test-bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
'use strict'

const expect = require('chai').expect
const _ = require('lodash')
const async = require('async')
const Block = require('ipfs-block')

const IPFS = require('../../src/core')
const createTempNode = require('../utils/temp-node')

describe('bitswap', () => {
let ipfs
Expand All @@ -13,57 +17,179 @@ describe('bitswap', () => {
ipfs.load(done)
})

describe('wantlist', (done) => {
it('throws if offline', () => {
expect(
() => ipfs.bitswap.wantlist()
).to.throw(/online/)
})
describe('connections', () => {
function getAndAssertBlock (node, key, block, cb) {
node.block.get(key, (err, b) => {
expect(err).to.not.exist
expect(b.data.toString()).to.be.eql(block.data.toString())
cb()
})
}

it('returns an array of wanted blocks', (done) => {
ipfs.goOnline((err) => {
function connectNodesSingle (node1, node2, done) {
node1.id((err, res) => {
expect(err).to.not.exist
node2.libp2p.swarm.connect(`${res.Addresses[0]}/ipfs/${res.ID}`, done)
})
}

expect(
ipfs.bitswap.wantlist()
).to.be.eql(
[]
)
function connectNodes (node1, node2, done) {
async.parallel([
(cb) => connectNodesSingle(node1, node2, cb),
(cb) => connectNodesSingle(node2, node1, cb)
], done)
}

function addNode (num, done) {
let node
async.waterfall([
(cb) => {
createTempNode(num, (err, _ipfs) => {
expect(err).to.not.exist
node = _ipfs
cb()
})
},
(cb) => node.goOnline(cb),
(cb) => connectNodes(node, ipfs, cb)
], (err) => {
done(err, node)
})
}

done()
describe('fetches a remote block', () => {
beforeEach((done) => {
ipfs.goOnline(done)
})

it('2 peers', (done) => {
const block = new Block('I am awesome, 2')
let node
async.series([
// 0. Start node
(cb) => addNode(6, (err, _ipfs) => {
node = _ipfs
cb(err)
}),
// 1. Add file to tmp instance
(cb) => node.block.put(block, cb),
// 2. Request file from local instance
(cb) => {
ipfs.block.get(block.key, (err, b) => {
expect(err).to.not.exist
// 3. Profit
expect(b.data.toString()).to.be.eql('I am awesome, 2')
cb()
})
},
(cb) => node.goOffline(cb)
], done)
})

it('3 peers', function (done) {
this.timeout(60 * 1000)

const blocks = _.range(6).map((i) => new Block(`I am awesome, ${i}`))
const keys = blocks.map((b) => b.key)
const nodes = []
async.series([
// 0. Start node 1
(cb) => addNode(6, (err, _ipfs) => {
nodes.push(_ipfs)
cb(err)
}),
// 1. Start node 2
(cb) => addNode(7, (err, _ipfs) => {
nodes.push(_ipfs)
cb(err)
}),
(cb) => connectNodes(nodes[0], nodes[1], cb),
// 2. Put blocks on all nodes
(cb) => nodes[0].block.put(blocks[0], cb),
(cb) => nodes[0].block.put(blocks[1], cb),
(cb) => nodes[1].block.put(blocks[2], cb),
(cb) => nodes[1].block.put(blocks[3], cb),
(cb) => ipfs.block.put(blocks[4], cb),
(cb) => ipfs.block.put(blocks[5], cb),
// 3. Fetch blocks on all nodes
(cb) => async.parallel([
(cb) => {
async.each(_.range(6), (i, innerCb) => {
getAndAssertBlock(nodes[0], keys[i], blocks[i], innerCb)
}, cb)
},
(cb) => {
async.each(_.range(6), (i, innerCb) => {
getAndAssertBlock(nodes[1], keys[i], blocks[i], innerCb)
}, cb)
},
(cb) => {
async.each(_.range(6), (i, innerCb) => {
getAndAssertBlock(ipfs, keys[i], blocks[i], innerCb)
}, cb)
}
], cb),
// 4. go offline
(cb) => nodes[0].goOffline(cb),
(cb) => nodes[1].goOffline(cb)
], done)
})
})
})

describe('stat', () => {
describe('commands', () => {
describe('wantlist', (done) => {
it('throws if offline', () => {
expect(
() => ipfs.bitswap.stat()
() => ipfs.bitswap.wantlist()
).to.throw(/online/)
})

it('returns the stats', (done) => {
it('returns an array of wanted blocks', (done) => {
ipfs.goOnline((err) => {
expect(err).to.not.exist

let stats = ipfs.bitswap.stat()
expect(
ipfs.bitswap.wantlist()
).to.be.eql(
[]
)

expect(stats).to.have.keys([
'blocksReceived',
'wantlist',
'peers',
'dupDataReceived',
'dupBlksReceived'
])
done()
})
})
})

describe('unwant', () => {
it('throws if offline', () => {
expect(
() => ipfs.bitswap.unwant('my key')
).to.throw(/online/)
describe('stat', () => {
it('throws if offline', () => {
expect(
() => ipfs.bitswap.stat()
).to.throw(/online/)
})

it('returns the stats', (done) => {
ipfs.goOnline((err) => {
expect(err).to.not.exist

let stats = ipfs.bitswap.stat()

expect(stats).to.have.keys([
'blocksReceived',
'wantlist',
'peers',
'dupDataReceived',
'dupBlksReceived'
])
done()
})
})
})

describe('unwant', () => {
it('throws if offline', () => {
expect(
() => ipfs.bitswap.unwant('my key')
).to.throw(/online/)
})
})
})
})
Expand Down
Loading

0 comments on commit 2320cdf

Please sign in to comment.