Skip to content
This repository has been archived by the owner on Jul 14, 2019. It is now read-only.

Commit

Permalink
Merge pull request #3 from mikeal/inmemory-set
Browse files Browse the repository at this point in the history
fix(inmemory): Fixing inmemory set reference to createHasher.
  • Loading branch information
mikeal committed Jul 16, 2017
2 parents 2bc0491 + 37ada32 commit 0fe0bb6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion inmemory.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class InMemoryContentAddressableStorage {

set (value, cb) {
let _value = bl()
let hasher = createHasher('sha256', (err, hash) => {
let hasher = this._createHasher('sha256', (err, hash) => {
if (err) return cb(err)
this._store.set(hash, _value)
cb(null, hash)
Expand Down
20 changes: 19 additions & 1 deletion lib/test-basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module.exports = (name, store) => {
})
})

test(`${name}: consistent hashing w/ hash API`, t => {
test(`${name}: consistent hashing w/ hash API, Buffer`, t => {
t.plan(3)
let buff = Buffer.from('asldkfjalskdjfldddkasdjf')
store.hash(buff, (err, hash) => {
Expand All @@ -61,6 +61,24 @@ module.exports = (name, store) => {
})
})

test(`${name}: consistent hashing w/ hash API, Stream`, t => {
t.plan(3)
let buff = Buffer.from('asldkfjalskdjfldddkasdjf')
let stream = bl()
store.hash(stream, (err, hash) => {
t.error(err)
stream = bl()
store.hash(buff, (err, _hash) => {
t.error(err)
t.equals(hash, _hash)
})
stream.write(buff)
stream.end()
})
stream.write(buff)
stream.end()
})

test(`${name}: get Buffer from key that has not been stored`, t => {
t.plan(1)
store.getBuffer('notfound', (err, buff) => {
Expand Down
27 changes: 27 additions & 0 deletions tests/test-inmemory.js
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
require('../lib/test-basics')('inmemory', require('../inmemory')())

const test = require('tap').test
const inmemory = require('../inmemory')
const through = require('through2')

const failHasher = (algo, cb) => {
process.nextTick(() => cb(new Error('Test Error')))
return through(() => {})
}

test('inmemory: (implementation) hash error in hash()', t => {
t.plan(1)
let store = inmemory()
store._createHasher = failHasher
store.hash(Buffer.from('asdf'), err => {
t.type(err, 'Error')
})
})

test('inmemory: (implementation) hash error in set()', t => {
t.plan(1)
let store = inmemory()
store._createHasher = failHasher
store.set(Buffer.from('asdf'), err => {
t.type(err, 'Error')
})
})

0 comments on commit 0fe0bb6

Please sign in to comment.