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

Commit

Permalink
fix(block-service): use correct cb interface for writes
Browse files Browse the repository at this point in the history
fixes #13
  • Loading branch information
dignifiedquire committed Apr 24, 2016
1 parent ba62423 commit bec44b3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 30 deletions.
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
19 changes: 19 additions & 0 deletions test/block-service-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,24 @@ module.exports = (repo) => {
done()
})
})

it('stores and gets lots of blocks', (done) => {
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()
})
})
})
})
}

0 comments on commit bec44b3

Please sign in to comment.