Skip to content

Commit a505cfc

Browse files
committed
audit is an iterator
1 parent 377ea6b commit a505cfc

File tree

2 files changed

+13
-25
lines changed

2 files changed

+13
-25
lines changed

lib/audit.js

+2-17
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,14 @@
1-
module.exports = async function audit (store, { dryRun = false } = {}) {
2-
const stats = { cores: 0, skipped: 0, rootless: 0, dropped: 0 }
3-
1+
module.exports = async function * audit (store, { dryRun = false } = {}) {
42
for await (const { discoveryKey } of store.storage.createCoreStream()) {
5-
stats.cores++
6-
73
const core = store.get({ discoveryKey, active: false })
84
await core.ready()
95

10-
const audit = await core.core.audit({ dryRun })
6+
yield { discoveryKey, key: core.key, audit: await core.core.audit({ dryRun }) }
117

128
try {
139
await core.close()
1410
} catch {
1511
// ignore if failed, we are auditing...
1612
}
17-
18-
if (audit === null || audit.corrupt) {
19-
stats.rootless++
20-
continue
21-
}
22-
23-
if (audit.droppedTreeNodes || audit.droppedBlocks || audit.droppedBits) {
24-
stats.dropped++
25-
}
2613
}
27-
28-
return stats
2914
}

test/basic.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -240,18 +240,21 @@ test('audit', async function (t) {
240240
const d = store.get({ name: 'another' })
241241

242242
for (let i = 0; i < 100; i++) {
243-
await a.append(i.toString())
244-
await b.append(i.toString())
245-
await c.append(i.toString())
243+
if (i < 20) await a.append(i.toString())
244+
if (i < 40) await b.append(i.toString())
245+
if (i < 80) await c.append(i.toString())
246246
await d.append(i.toString())
247247
}
248248

249-
const result = await store.audit()
249+
let n = 0
250+
for await (const { discoveryKey, audit } of store.audit()) {
251+
n++
252+
if (audit.droppedBits || audit.droppedBlocks || audit.droppedTreeNodes || audit.corrupt) {
253+
t.fail('bad core')
254+
}
255+
}
250256

251-
t.is(result.cores, 4)
252-
t.is(result.skipped, 0)
253-
t.is(result.rootless, 0)
254-
t.is(result.dropped, 0)
257+
t.is(n, 4)
255258

256259
await a.close()
257260
await b.close()

0 commit comments

Comments
 (0)