Skip to content
This repository was archived by the owner on Dec 28, 2022. It is now read-only.

Commit d147c9e

Browse files
Add additional non-sparse tests (#112)
* Add additional non-sparse tests * Test non-sparse snapshot replication * Add non-sparse partial snapshot test * Adjust assertions * Feedback from @mafintosh * non sparse snaphots should be upgraded contig length changes as well * Correct assertion * Check update after snapshot * fix upgraded going to false on non-sparse in edgecases Co-authored-by: Mathias Buus <[email protected]>
1 parent 6a268ea commit d147c9e

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,14 +589,17 @@ module.exports = class Hypercore extends EventEmitter {
589589
const activeRequests = (opts && opts.activeRequests) || this.activeRequests
590590
const req = this.replicator.addUpgrade(activeRequests)
591591

592-
const upgraded = await req.promise
592+
let upgraded = await req.promise
593593

594594
if (!this.sparse) {
595595
// Download all available blocks in non-sparse mode
596596
const start = this.length
597597
const end = this.core.tree.length
598+
const contig = this.contiguousLength
598599

599600
await this.download({ start, end, ifAvailable: true }).downloaded()
601+
602+
if (!upgraded) upgraded = this.contiguousLength !== contig
600603
}
601604

602605
if (!upgraded) return false

test/replicate.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,5 +736,48 @@ test('download available blocks on non-sparse update', async function (t) {
736736
await a.append(['a', 'b', 'c', 'd', 'e'])
737737
await b.update()
738738

739-
t.is(b.contiguousLength, 5)
739+
t.is(b.contiguousLength, b.length)
740+
})
741+
742+
test('non-sparse snapshot during replication', async function (t) {
743+
const a = await create()
744+
const b = await create(a.key, { sparse: false })
745+
746+
replicate(a, b, t)
747+
748+
await a.append(['a', 'b', 'c', 'd', 'e'])
749+
await b.update()
750+
751+
const s = b.snapshot()
752+
753+
await a.append(['f', 'g', 'h', 'i', 'j'])
754+
await s.update()
755+
756+
t.is(s.contiguousLength, s.length)
757+
})
758+
759+
test('non-sparse snapshot during partial replication', async function (t) {
760+
const a = await create()
761+
const b = await create(a.key)
762+
const c = await create(a.key, { sparse: false })
763+
764+
replicate(a, b, t)
765+
replicate(b, c, t)
766+
767+
await a.append(['a', 'b', 'c', 'd', 'e'])
768+
769+
await b.download({ start: 0, end: 3 }).downloaded()
770+
await c.update()
771+
772+
const s = c.snapshot()
773+
t.is(s.contiguousLength, s.length)
774+
775+
await s.update()
776+
t.is(s.contiguousLength, s.length)
777+
778+
await b.download({ start: 3, end: 5 }).downloaded()
779+
t.is(s.contiguousLength, s.length)
780+
781+
await s.update()
782+
t.is(s.contiguousLength, s.length)
740783
})

test/sessions.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const test = require('brittle')
22
const ram = require('random-access-memory')
33
const crypto = require('hypercore-crypto')
44
const codecs = require('codecs')
5+
const { create } = require('./helpers')
56

67
const Hypercore = require('../')
78

@@ -212,3 +213,10 @@ test('sessions - custom preload hook on first/later sessions', async function (t
212213

213214
await preloadsTest
214215
})
216+
217+
test('session inherits non-sparse setting', async function (t) {
218+
const a = await create({ sparse: false })
219+
const s = a.session()
220+
221+
t.is(s.sparse, false)
222+
})

0 commit comments

Comments
 (0)