Skip to content

fix: close root datastore #214

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

Merged
merged 4 commits into from
Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
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
46 changes: 46 additions & 0 deletions test/repo-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,52 @@ 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