Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

fix(block-service): use correct cb interface for writes #14

Merged
merged 2 commits into from
Apr 24, 2016
Merged
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
31 changes: 1 addition & 30 deletions src/block-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,9 @@ const async = require('async')
// It uses an internal `datastore.Datastore` instance to store values.
function BlockService (ipfsRepo, exchange) {
this.addBlock = (block, callback) => {
const ws = ipfsRepo.datastore.createWriteStream(block.key, block.extension)

let done = false
const ws = ipfsRepo.datastore.createWriteStream(block.key, block.extension, callback)

ws.write(block.data)

ws.once('error', (err) => {
done = true
callback(err)
})

ws.once('finish', () => {
if (!done) {
// Important to note: Writing to a stream
// isn't an atomic process, because streams can be
// piped, and the finish of one only represents that
// the data was buffered to the next one.
// This is something known and 'accepted' on the
// streams API, however, since we expose a callback
// interface on BlockService and a streams one,
// the users will expect for the callback to be fired
// when the final write was concluded. We add a
// timeout to ensure that.
// TODO: Create an elegant way to understand when
// the block was actually flushed to disk. This
// means changing how the blob-stores and repo are
// implemented.
// One option, is polling till we check it
// is written.
setTimeout(callback, 150)
}
})
ws.end()
}

Expand Down
21 changes: 21 additions & 0 deletions test/block-service-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,26 @@ module.exports = (repo) => {
done()
})
})

it('stores and gets lots of blocks', function (done) {
this.timeout(60 * 1000)

const blocks = []
const count = 1000
while (blocks.length < count) {
blocks.push(new Block('hello-' + Math.random()))
}

bs.addBlocks(blocks, (err) => {
expect(err).to.not.exist

bs.getBlocks(blocks.map((b) => b.key), (err, res) => {
expect(err).to.not.exist
expect(Object.keys(res)).to.have.length(count)

done()
})
})
})
})
}