Skip to content

Commit 72bae9d

Browse files
hugomrdiasjacobheun
authored andcommitted
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
1 parent 64c8006 commit 72bae9d

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ class IpfsRepo {
228228
}
229229
}
230230

231-
await Promise.all([this.blocks, this.keys, this.datastore].map((store) => store.close()))
231+
await Promise.all([this.root, this.blocks, this.keys, this.datastore].map((store) => store.close()))
232232
log('unlocking')
233233
this.closed = true
234234
await this._closeLock()

test/repo-test.js

+54
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,60 @@ module.exports = (repo) => {
120120
expect.fail('Did not throw')
121121
})
122122

123+
it('should close all the datastores', async () => {
124+
let count = 0
125+
class FakeDatastore {
126+
constructor () {
127+
this.data = {}
128+
}
129+
130+
async open () {}
131+
132+
// eslint-disable-next-line require-await
133+
async put (key, val) {
134+
this.data[key.toString()] = val
135+
}
136+
137+
async get (key) {
138+
const exists = await this.has(key)
139+
if (!exists) throw Errors.notFoundError()
140+
return this.data[key.toString()]
141+
}
142+
143+
// eslint-disable-next-line require-await
144+
async has (key) {
145+
return this.data[key.toString()] !== undefined
146+
}
147+
148+
// eslint-disable-next-line require-await
149+
async delete (key) {
150+
delete this.data[key.toString()]
151+
}
152+
153+
batch () {}
154+
155+
query (q) {}
156+
157+
// eslint-disable-next-line require-await
158+
async close () {
159+
count++
160+
}
161+
}
162+
const repo = new IPFSRepo(path.join(os.tmpdir(), 'repo-' + Date.now()), {
163+
lock: 'memory',
164+
storageBackends: {
165+
root: FakeDatastore,
166+
blocks: FakeDatastore,
167+
keys: FakeDatastore,
168+
datastore: FakeDatastore
169+
}
170+
})
171+
await repo.init({})
172+
await repo.open()
173+
await repo.close()
174+
expect(count).to.be.eq(4)
175+
})
176+
123177
it('open twice throws error', async () => {
124178
await repo.open()
125179
try {

0 commit comments

Comments
 (0)