Skip to content

Commit c1b87b3

Browse files
committed
Properly teardown cores & monitors in tests
1 parent 594b8d6 commit c1b87b3

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

test/all.js

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,20 @@ const Hypercore = require('hypercore')
44

55
const Hyperblobs = require('..')
66

7-
test('can get/put a large blob', async t => {
7+
async function create (t, opts) {
88
const core = new Hypercore(await t.tmp())
99
const blobs = new Hyperblobs(core)
1010

11+
await blobs.ready()
12+
13+
t.teardown(() => blobs.close(), { order: 1 })
14+
15+
return blobs
16+
}
17+
18+
test('can get/put a large blob', async t => {
19+
const blobs = await create(t)
20+
1121
const buf = b4a.alloc(5 * blobs.blockSize, 'abcdefg')
1222
const id = await blobs.put(buf)
1323
const result = await blobs.get(id)
@@ -16,8 +26,7 @@ test('can get/put a large blob', async t => {
1626
})
1727

1828
test('can put/get two blobs in one core', async t => {
19-
const core = new Hypercore(await t.tmp())
20-
const blobs = new Hyperblobs(core)
29+
const blobs = await create(t)
2130

2231
{
2332
const buf = b4a.alloc(5 * blobs.blockSize, 'abcdefg')
@@ -37,8 +46,7 @@ test('can put/get two blobs in one core', async t => {
3746
})
3847

3948
test('can seek to start/length within one blob, one block', async t => {
40-
const core = new Hypercore(await t.tmp())
41-
const blobs = new Hyperblobs(core)
49+
const blobs = await create(t)
4250

4351
const buf = b4a.alloc(5 * blobs.blockSize, 'abcdefg')
4452
const id = await blobs.put(buf)
@@ -48,8 +56,7 @@ test('can seek to start/length within one blob, one block', async t => {
4856
})
4957

5058
test('can seek to start/length within one blob, multiple blocks', async t => {
51-
const core = new Hypercore(await t.tmp())
52-
const blobs = new Hyperblobs(core, { blockSize: 10 })
59+
const blobs = await create(t, { blockSize: 10 })
5360

5461
const buf = b4a.concat([b4a.alloc(10, 'a'), b4a.alloc(10, 'b')])
5562
const id = await blobs.put(buf)
@@ -59,8 +66,7 @@ test('can seek to start/length within one blob, multiple blocks', async t => {
5966
})
6067

6168
test('can seek to start/length within one blob, multiple blocks, multiple blobs', async t => {
62-
const core = new Hypercore(await t.tmp())
63-
const blobs = new Hyperblobs(core, { blockSize: 10 })
69+
const blobs = await create(t, { blockSize: 10 })
6470

6571
{
6672
const buf = b4a.alloc(5 * blobs.blockSize, 'abcdefg')
@@ -78,8 +84,7 @@ test('can seek to start/length within one blob, multiple blocks, multiple blobs'
7884
})
7985

8086
test('can seek to start/end within one blob', async t => {
81-
const core = new Hypercore(await t.tmp())
82-
const blobs = new Hyperblobs(core)
87+
const blobs = await create(t)
8388

8489
const buf = b4a.alloc(5 * blobs.blockSize, 'abcdefg')
8590
const id = await blobs.put(buf)
@@ -89,8 +94,7 @@ test('can seek to start/end within one blob', async t => {
8994
})
9095

9196
test('basic seek', async t => {
92-
const core = new Hypercore(await t.tmp())
93-
const blobs = new Hyperblobs(core)
97+
const blobs = await create(t)
9498

9599
const buf = b4a.alloc(5 * blobs.blockSize, 'abcdefg')
96100
const id = await blobs.put(buf)
@@ -103,6 +107,12 @@ test('basic seek', async t => {
103107
test('can pass in a custom core', async t => {
104108
const core1 = new Hypercore(await t.tmp())
105109
const core2 = new Hypercore(await t.tmp())
110+
111+
t.teardown(async () => {
112+
await core1.close()
113+
await core2.close()
114+
})
115+
106116
const blobs = new Hyperblobs(core1)
107117
await core1.ready()
108118

@@ -117,9 +127,7 @@ test('can pass in a custom core', async t => {
117127
test('two write streams does not deadlock', async t => {
118128
t.plan(2)
119129

120-
const core = new Hypercore(await t.tmp())
121-
const blobs = new Hyperblobs(core)
122-
await core.ready()
130+
const blobs = await create(t)
123131

124132
const ws = blobs.createWriteStream()
125133

@@ -157,6 +165,7 @@ test('append error does not deadlock', async t => {
157165

158166
ws.on('close', async function () {
159167
const core2 = new Hypercore(await t.tmp())
168+
t.teardown(() => core2.close())
160169
const ws2 = blobs.createWriteStream({ core: core2 })
161170
ws2.write(b4a.from('hello'))
162171
ws2.end()
@@ -165,8 +174,7 @@ test('append error does not deadlock', async t => {
165174
})
166175

167176
test('can put/get a blob and clear it', async t => {
168-
const core = new Hypercore(await t.tmp())
169-
const blobs = new Hyperblobs(core)
177+
const blobs = await create(t)
170178

171179
const buf = b4a.alloc(5 * blobs.blockSize, 'abcdefg')
172180
const id = await blobs.put(buf)
@@ -177,7 +185,7 @@ test('can put/get a blob and clear it', async t => {
177185

178186
for (let i = 0; i < id.blockLength; i++) {
179187
const block = id.blockOffset + i
180-
t.absent(await core.has(block), `block ${block} cleared`)
188+
t.absent(await blobs.core.has(block), `block ${block} cleared`)
181189
}
182190
})
183191

@@ -288,8 +296,7 @@ test.skip('clear with diff option', async function (t) {
288296
t.comment('Hypercore Clear doesnt return correct value')
289297
t.plan(3)
290298

291-
const core = new Hypercore(await t.tmp())
292-
const blobs = new Hyperblobs(core)
299+
const blobs = await create(t)
293300

294301
const buf = b4a.alloc(128)
295302
const id = await blobs.put(buf)
@@ -326,6 +333,7 @@ test('upload/download can be monitored', async (t) => {
326333

327334
// Start monitoring upload
328335
const monitor = blobsA.monitor(id)
336+
t.teardown(() => monitor.close())
329337
monitor.on('update', () => {
330338
t.is(monitor.uploadStats.blocks, expectedBlocks.pop())
331339
t.is(monitor.uploadStats.monitoringBytes, expectedBytes.pop())
@@ -344,6 +352,7 @@ test('upload/download can be monitored', async (t) => {
344352
const expectedPercentage = [100, 50]
345353

346354
const monitor = blobsB.monitor(id)
355+
t.teardown(() => monitor.close())
347356
monitor.on('update', () => {
348357
t.is(monitor.downloadStats.blocks, expectedBlocks.pop())
349358
t.is(monitor.downloadStats.monitoringBytes, expectedBytes.pop())
@@ -364,21 +373,20 @@ test('upload/download can be monitored', async (t) => {
364373
})
365374

366375
test('monitor is removed from the Set on close', async (t) => {
367-
const core = new Hypercore(await t.tmp())
368-
const blobs = new Hyperblobs(core)
376+
const blobs = await create(t)
369377

370378
const bytes = 1024 * 100 // big enough to trigger more than one update event
371379
const buf = Buffer.alloc(bytes, '0')
372380
const id = await blobs.put(buf)
373381
const monitor = blobs.monitor(id)
382+
t.teardown(() => monitor.close())
374383
t.is(blobs._monitors.size, 1)
375384
monitor.close()
376385
t.is(blobs._monitors.size, 0)
377386
})
378387

379388
test('basic batch', async (t) => {
380-
const core = new Hypercore(await t.tmp())
381-
const blobs = new Hyperblobs(core)
389+
const blobs = await create(t)
382390
const batch = blobs.batch()
383391

384392
{
@@ -399,9 +407,11 @@ test('basic batch', async (t) => {
399407
async function createPair (t) {
400408
const a = new Hypercore(await t.tmp())
401409
await a.ready()
410+
t.teardown(() => a.close(), { order: 1 })
402411

403412
const b = new Hypercore(await t.tmp(), a.key)
404413
await b.ready()
414+
t.teardown(() => b.close(), { order: 1 })
405415

406416
replicate(a, b)
407417

0 commit comments

Comments
 (0)