Skip to content

feat: trying to callbackify ipfs-repo #208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
250 changes: 250 additions & 0 deletions callback-test/blockstore-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
/* eslint max-nested-callbacks: ["error", 8] */
/* eslint-env mocha */
'use strict'

const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const Block = require('ipfs-block')
const CID = require('cids')
const parallel = require('async/parallel')
const waterfall = require('async/waterfall')
const each = require('async/each')
const map = require('async/map')
const _ = require('lodash')
const multihashing = require('multihashing-async')

module.exports = (repo) => {
describe('blockstore', () => {
const blockData = _.range(100).map((i) => Buffer.from(`hello-${i}-${Math.random()}`))
const bData = Buffer.from('hello world')
let b

before((done) => {
multihashing(bData, 'sha2-256', (err, h) => {
if (err) {
return done(err)
}

b = new Block(bData, new CID(h))
done()
})
})

describe('.put', () => {
it('simple', (done) => {
repo.blocks.put(b, done)
})

it('multi write (locks)', (done) => {
parallel([
(cb) => repo.blocks.put(b, cb),
(cb) => repo.blocks.put(b, cb)
], done)
})

it('empty value', (done) => {
const d = Buffer.alloc(0)
multihashing(d, 'sha2-256', (err, multihash) => {
expect(err).to.not.exist()
const empty = new Block(d, new CID(multihash))
repo.blocks.put(empty, done)
})
})

it('massive multiwrite', function (done) {
this.timeout(15000) // add time for ci
waterfall([
(cb) => map(_.range(100), (i, cb) => {
multihashing(blockData[i], 'sha2-256', cb)
}, cb),
(hashes, cb) => each(_.range(100), (i, cb) => {
const block = new Block(blockData[i], new CID(hashes[i]))
repo.blocks.put(block, cb)
}, cb)
], done)
})

it('.putMany', function (done) {
this.timeout(15000) // add time for ci
waterfall([
(cb) => map(_.range(50), (i, cb) => {
const d = Buffer.from('many' + Math.random())
multihashing(d, 'sha2-256', (err, hash) => {
if (err) {
return cb(err)
}
cb(null, new Block(d, new CID(hash)))
})
}, cb),
(blocks, cb) => {
repo.blocks.putMany(blocks, (err) => {
expect(err).to.not.exist()
map(blocks, (b, cb) => {
repo.blocks.get(b.cid, cb)
}, (err, res) => {
expect(err).to.not.exist()
expect(res).to.be.eql(blocks)
cb()
})
})
}
], done)
})

it('returns an error on invalid block', (done) => {
repo.blocks.put('hello', (err) => {
expect(err).to.exist()
done()
})
})
})

describe('.get', () => {
it('simple', (done) => {
repo.blocks.get(b.cid, (err, block) => {
expect(err).to.not.exist()
expect(block).to.be.eql(b)
done()
})
})

it('massive read', function (done) {
this.timeout(15000) // add time for ci
parallel(_.range(20 * 100).map((i) => (cb) => {
const j = i % blockData.length
waterfall([
(cb) => multihashing(blockData[j], 'sha2-256', cb),
(h, cb) => {
const cid = new CID(h)
repo.blocks.get(cid, cb)
},
(block, cb) => {
expect(block.data).to.be.eql(blockData[j])
cb()
}
], cb)
}), done)
})

it('returns an error on invalid block', (done) => {
repo.blocks.get('woot', (err, val) => {
expect(err).to.exist()
expect(val).to.not.exist()
done()
})
})

it('should get block stored under v0 CID with a v1 CID', done => {
const data = Buffer.from(`TEST${Date.now()}`)

multihashing(data, 'sha2-256', (err, hash) => {
if (err) return done(err)

const cid = new CID(hash)

repo.blocks.put(new Block(data, cid), err => {
if (err) return done(err)

repo.blocks.get(cid.toV1(), (err, block) => {
expect(err).to.not.exist()
expect(block.data).to.eql(data)
done()
})
})
})
})

it('should get block stored under v1 CID with a v0 CID', done => {
const data = Buffer.from(`TEST${Date.now()}`)

multihashing(data, 'sha2-256', (err, hash) => {
if (err) return done(err)

const cid = new CID(1, 'dag-pb', hash)

repo.blocks.put(new Block(data, cid), err => {
if (err) return done(err)

repo.blocks.get(cid.toV0(), (err, block) => {
expect(err).to.not.exist()
expect(block.data).to.eql(data)
done()
})
})
})
})
})

describe('.has', () => {
it('existing block', (done) => {
repo.blocks.has(b.cid, (err, exists) => {
expect(err).to.not.exist()
expect(exists).to.eql(true)
done()
})
})

it('non existent block', (done) => {
repo.blocks.has(new CID('QmbcpFjzamCj5ZZdduW32ctWUPvbGMwQZk2ghWK6PrKswE'), (err, exists) => {
expect(err).to.not.exist()
expect(exists).to.eql(false)
done()
})
})

it('should have block stored under v0 CID with a v1 CID', done => {
const data = Buffer.from(`TEST${Date.now()}`)

multihashing(data, 'sha2-256', (err, hash) => {
if (err) return done(err)

const cid = new CID(hash)

repo.blocks.put(new Block(data, cid), err => {
if (err) return done(err)

repo.blocks.has(cid.toV1(), (err, exists) => {
expect(err).to.not.exist()
expect(exists).to.eql(true)
done()
})
})
})
})

it('should have block stored under v1 CID with a v0 CID', done => {
const data = Buffer.from(`TEST${Date.now()}`)

multihashing(data, 'sha2-256', (err, hash) => {
if (err) return done(err)

const cid = new CID(1, 'dag-pb', hash)

repo.blocks.put(new Block(data, cid), err => {
if (err) return done(err)

repo.blocks.has(cid.toV0(), (err, exists) => {
expect(err).to.not.exist()
expect(exists).to.eql(true)
done()
})
})
})
})
})

describe('.delete', () => {
it('simple', (done) => {
waterfall([
(cb) => repo.blocks.delete(b.cid, cb),
(cb) => repo.blocks.has(b.cid, cb)
], (err, exists) => {
expect(err).to.not.exist()
expect(exists).to.equal(false)
done()
})
})
})
})
}
28 changes: 28 additions & 0 deletions callback-test/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-env mocha */

'use strict'

const series = require('async/series')

const IPFSRepo = require('../src')

describe('IPFS Repo Tests on the Browser', () => {
require('./options-test')
const repo = new IPFSRepo('myrepo')

before((done) => {
series([
(cb) => repo.init({}, cb),
(cb) => repo.open(cb)
], done)
})

after((done) => {
repo.close(done)
})

require('./repo-test')(repo)
require('./blockstore-test')(repo)
require('./datastore-test')(repo)
require('./keystore-test')(repo)
})
93 changes: 93 additions & 0 deletions callback-test/datastore-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/* eslint max-nested-callbacks: ["error", 8] */
/* eslint-env mocha */
'use strict'

const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const parallel = require('async/parallel')
const waterfall = require('async/waterfall')
const each = require('async/each')
const _ = require('lodash')
const Key = require('interface-datastore').Key

module.exports = (repo) => {
describe('datastore', () => {
const dataList = _.range(100).map((i) => Buffer.from(`hello-${i}-${Math.random()}`))
const data = Buffer.from('hello world')
const b = new Key('hello')

describe('.put', () => {
it('simple', (done) => {
repo.datastore.put(b, data, done)
})

it('multi write (locks)', (done) => {
parallel([
(cb) => repo.datastore.put(b, data, cb),
(cb) => repo.datastore.put(b, data, cb)
], done)
})

it('massive multiwrite', function (done) {
this.timeout(15000) // add time for ci
each(_.range(100), (i, cb) => {
repo.datastore.put(new Key('hello' + i), dataList[i], cb)
}, done)
})
})

describe('.get', () => {
it('simple', (done) => {
repo.datastore.get(b, (err, val) => {
expect(err).to.not.exist()
expect(val).to.be.eql(data)
done()
})
})

it('massive read', function (done) {
this.timeout(15000) // add time for ci
parallel(_.range(20 * 100).map((i) => (cb) => {
const j = i % dataList.length
repo.datastore.get(new Key('hello' + j), (err, val) => {
expect(err).to.not.exist()
expect(val).to.be.eql(dataList[j])
cb()
})
}), done)
}).timeout(10 * 1000)
})

describe('.has', () => {
it('existing entry', (done) => {
repo.datastore.has(b, (err, exists) => {
expect(err).to.not.exist()
expect(exists).to.eql(true)
done()
})
})

it('non existent block', (done) => {
repo.datastore.has(new Key('world'), (err, exists) => {
expect(err).to.not.exist()
expect(exists).to.eql(false)
done()
})
})
})

describe('.delete', () => {
it('simple', (done) => {
waterfall([
(cb) => repo.datastore.delete(b, cb),
(cb) => repo.datastore.has(b, cb)
], (err, exists) => {
expect(err).to.not.exist()
expect(exists).to.equal(false)
done()
})
})
})
})
}
Loading