Skip to content

Commit

Permalink
fix: close root datastore (#214)
Browse files Browse the repository at this point in the history
* fix: close root datastore

blocks, keys and datastore are being closed in repo.close() but root is not.

* tests: add test for closing all the stores

* chore: remove only

* chore: fix lint
  • Loading branch information
hugomrdias authored and jacobheun committed Nov 19, 2019
1 parent 64c8006 commit 72bae9d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class IpfsRepo {
}
}

await Promise.all([this.blocks, this.keys, this.datastore].map((store) => store.close()))
await Promise.all([this.root, this.blocks, this.keys, this.datastore].map((store) => store.close()))
log('unlocking')
this.closed = true
await this._closeLock()
Expand Down
54 changes: 54 additions & 0 deletions test/repo-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,60 @@ module.exports = (repo) => {
expect.fail('Did not throw')
})

it('should close all the datastores', async () => {
let count = 0
class FakeDatastore {
constructor () {
this.data = {}
}

async open () {}

// eslint-disable-next-line require-await
async put (key, val) {
this.data[key.toString()] = val
}

async get (key) {
const exists = await this.has(key)
if (!exists) throw Errors.notFoundError()
return this.data[key.toString()]
}

// eslint-disable-next-line require-await
async has (key) {
return this.data[key.toString()] !== undefined
}

// eslint-disable-next-line require-await
async delete (key) {
delete this.data[key.toString()]
}

batch () {}

query (q) {}

// eslint-disable-next-line require-await
async close () {
count++
}
}
const repo = new IPFSRepo(path.join(os.tmpdir(), 'repo-' + Date.now()), {
lock: 'memory',
storageBackends: {
root: FakeDatastore,
blocks: FakeDatastore,
keys: FakeDatastore,
datastore: FakeDatastore
}
})
await repo.init({})
await repo.open()
await repo.close()
expect(count).to.be.eq(4)
})

it('open twice throws error', async () => {
await repo.open()
try {
Expand Down

0 comments on commit 72bae9d

Please sign in to comment.