From 98241a84f543cc1eccbca80ee756d2633f1d84c8 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Thu, 14 Nov 2019 17:46:32 +0000 Subject: [PATCH 1/4] fix: close root datastore blocks, keys and datastore are being closed in repo.close() but root is not. --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 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() From 0932283b5118d830595878fb930b818bd91332ca Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Tue, 19 Nov 2019 11:40:57 -0600 Subject: [PATCH 2/4] tests: add test for closing all the stores --- test/repo-test.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/test/repo-test.js b/test/repo-test.js index 8765c47f..5def9c74 100644 --- a/test/repo-test.js +++ b/test/repo-test.js @@ -120,6 +120,52 @@ module.exports = (repo) => { expect.fail('Did not throw') }) + it.only('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 { From 7b7eb83e7fc7652662d8a12dbdf415589ce2d24f Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Tue, 19 Nov 2019 11:41:20 -0600 Subject: [PATCH 3/4] chore: remove only --- test/repo-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/repo-test.js b/test/repo-test.js index 5def9c74..00e11546 100644 --- a/test/repo-test.js +++ b/test/repo-test.js @@ -120,7 +120,7 @@ module.exports = (repo) => { expect.fail('Did not throw') }) - it.only('should close all the datastores', async () => { + it('should close all the datastores', async () => { let count = 0 class FakeDatastore { constructor () { From 4a318a2a53ac8c7defb6e07866bc20eb44066a73 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Tue, 19 Nov 2019 12:16:03 -0600 Subject: [PATCH 4/4] chore: fix lint --- test/repo-test.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/repo-test.js b/test/repo-test.js index 00e11546..c4e53ab0 100644 --- a/test/repo-test.js +++ b/test/repo-test.js @@ -126,26 +126,34 @@ module.exports = (repo) => { 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++