From 72bae9d2a46685deb35a20b64563042ab5d2ecb2 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Tue, 19 Nov 2019 12:27:06 -0600 Subject: [PATCH] fix: close root datastore (#214) * 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 --- src/index.js | 2 +- test/repo-test.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index a05b4d4f..bedec856 100644 --- a/src/index.js +++ b/src/index.js @@ -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() diff --git a/test/repo-test.js b/test/repo-test.js index 8765c47f..c4e53ab0 100644 --- a/test/repo-test.js +++ b/test/repo-test.js @@ -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 {