From 4410efe37b80ededfb15854848bf64d49b9bbcf1 Mon Sep 17 00:00:00 2001 From: Christophe Diederichs <45171645+chm-diederichs@users.noreply.github.com> Date: Wed, 21 Feb 2024 14:11:50 +0000 Subject: [PATCH] Refactor test helpers (#54) * refactor test helpers for easier teardown * workaround corestore bug --- test/autoack.js | 122 ++++++---- test/basic.js | 103 +++++---- test/core.js | 24 +- test/dags.js | 18 +- test/fast-forward.js | 148 ++---------- test/helpers/index.js | 59 +++-- test/linearizer.js | 17 +- test/snapshots.js | 15 +- test/suspend.js | 520 ++++++------------------------------------ test/upgrade.js | 20 +- 10 files changed, 342 insertions(+), 704 deletions(-) diff --git a/test/autoack.js b/test/autoack.js index 6fbfc7ce..a4694d0a 100644 --- a/test/autoack.js +++ b/test/autoack.js @@ -2,7 +2,6 @@ const test = require('brittle') const { create, - apply, addWriter, addWriterAndSync, confirm, @@ -15,7 +14,12 @@ const { test('autoack - simple', async t => { t.plan(6) - const [a, b] = await create(2, apply, store => store.get('test', { valueEncoding: 'json' }), null, { ackInterval: 10, ackThreshold: 0 }) + const { bases } = await create(2, t, { + ackInterval: 10, + ackThreshold: 0 + }) + + const [a, b] = bases const unreplicate = replicate([a, b]) t.teardown(unreplicate) @@ -40,15 +44,14 @@ test('autoack - simple', async t => { test('autoack - 5 writers', async t => { t.plan(26) - const bases = await create(5, apply, store => store.get('test', { valueEncoding: 'json' }), null, { ackInterval: 10, ackThreshold: 0 }) - const [a, b, c, d, e] = bases + const { bases } = await create(5, t, { + ackInterval: 10, + ackThreshold: 0 + }) - const unreplicate = replicate(bases) + const [a, b, c, d, e] = bases - t.teardown(async () => { - await unreplicate() - await Promise.all(bases.map(b => b.close())) - }) + t.teardown(replicate(bases)) await addWriterAndSync(a, b) await addWriterAndSync(a, c) @@ -117,15 +120,14 @@ test('autoack - 5 writers', async t => { test('autoack - concurrent', async t => { t.plan(10) - const bases = await create(5, apply, store => store.get('test', { valueEncoding: 'json' }), null, { ackInterval: 20, ackThreshold: 0 }) - const [a, b, c, d, e] = bases + const { bases } = await create(5, t, { + ackInterval: 20, + ackThreshold: 0 + }) - const unreplicate = replicate(bases) + const [a, b, c, d, e] = bases - t.teardown(async () => { - await unreplicate() - await Promise.all(bases.map(b => b.close())) - }) + t.teardown(replicate(bases)) await addWriterAndSync(a, b) await addWriterAndSync(a, c) @@ -169,15 +171,14 @@ test('autoack - concurrent', async t => { test('autoack - threshold', async t => { t.plan(2) - const [a, b] = await create(2, apply, store => store.get('test', { valueEncoding: 'json' }), null, { ackInterval: 0, ackThreshold: 1 }) + const { bases } = await create(2, t, { + ackInterval: 0, + ackThreshold: 1 + }) - const unreplicate = replicate([a, b]) + const [a, b] = bases - t.teardown(async () => { - await unreplicate() - await a.close() - await b.close() - }) + t.teardown(await replicate([a, b])) await addWriterAndSync(a, b) @@ -198,15 +199,14 @@ test('autoack - threshold with interval', async t => { t.plan(3) const ackInterval = 800 - const [a, b] = await create(2, apply, store => store.get('test', { valueEncoding: 'json' }), null, { ackInterval, ackThreshold: 1 }) + const { bases } = await create(2, t, { + ackInterval, + ackThreshold: 1 + }) - const unreplicate = replicate([a, b]) + const [a, b] = bases - t.teardown(async () => { - await unreplicate() - await a.close() - await b.close() - }) + t.teardown(await replicate([a, b])) await addWriterAndSync(a, b) @@ -234,10 +234,14 @@ test('autoack - threshold with interval', async t => { test('autoack - no null acks', async t => { t.plan(2) - const [a, b] = await create(2, apply, store => store.get('test', { valueEncoding: 'json' }), null, { ackInterval: 10, ackThreshold: 0 }) + const { bases } = await create(2, t, { + ackInterval: 10, + ackThreshold: 0 + }) - const unreplicate = replicate([a, b]) - t.teardown(unreplicate) + const [a, b] = bases + + t.teardown(await replicate([a, b])) await addWriterAndSync(a, b) await confirm([a, b]) @@ -256,10 +260,14 @@ test('autoack - no null acks', async t => { test('autoack - value beneath null values', async t => { t.plan(4) - const [a, b] = await create(2, apply, store => store.get('test', { valueEncoding: 'json' }), null, { ackInterval: 10, ackThreshold: 0 }) + const { bases } = await create(2, t, { + ackInterval: 10, + ackThreshold: 0 + }) - const unreplicate = replicate([a, b]) - t.teardown(unreplicate) + const [a, b] = bases + + t.teardown(await replicate([a, b])) await addWriterAndSync(a, b) await confirm([a, b]) @@ -283,7 +291,12 @@ test('autoack - value beneath null values', async t => { test('autoack - merge', async t => { t.plan(2) - const [a, b] = await create(2, apply, store => store.get('test', { valueEncoding: 'json' }), null, { ackInterval: 0, ackThreshold: 0 }) + const { bases } = await create(2, t, { + ackInterval: 0, + ackThreshold: 0 + }) + + const [a, b] = bases await addWriterAndSync(a, b) await replicateAndSync([a, b]) @@ -303,7 +316,12 @@ test('autoack - merge', async t => { test('autoack - merge when not head', async t => { t.plan(12) - const [a, b, c] = await create(3, apply, store => store.get('test', { valueEncoding: 'json' }), null, { ackInterval: 0, ackThreshold: 0 }) + const { bases } = await create(3, t, { + ackInterval: 0, + ackThreshold: 0 + }) + + const [a, b, c] = bases await addWriterAndSync(a, b) await addWriterAndSync(a, c) @@ -341,7 +359,12 @@ test('autoack - merge when not head', async t => { test('autoack - more merges', async t => { t.plan(8) - const [a, b, c] = await create(3, apply, store => store.get('test', { valueEncoding: 'json' }), null, { ackInterval: 0, ackThreshold: 0 }) + const { bases } = await create(3, t, { + ackInterval: 0, + ackThreshold: 0 + }) + + const [a, b, c] = bases await addWriterAndSync(a, b) await confirm([a, b, c]) @@ -376,7 +399,12 @@ test('autoack - more merges', async t => { test('autoack - pending writers', async t => { t.plan(5) - const [a, b, c] = await create(3, apply, store => store.get('test', { valueEncoding: 'json' }), null, { ackInterval: 0, ackThreshold: 0 }) + const { bases } = await create(3, t, { + ackInterval: 0, + ackThreshold: 0 + }) + + const [a, b, c] = bases t.teardown(await replicate([a, b, c])) @@ -396,7 +424,12 @@ test('autoack - pending writers', async t => { test.skip('autoack - pending migrates', async t => { t.plan(3) - const [a, b] = await create(2, apply, store => store.get('test', { valueEncoding: 'json' }), null, { ackInterval: 0, ackThreshold: 0 }) + const { bases } = await create(2, t, { + ackInterval: 0, + ackThreshold: 0 + }) + + const [a, b] = bases t.teardown(await replicate([a, b])) @@ -419,7 +452,12 @@ test.skip('autoack - pending migrates', async t => { }) test('autoack - minority indexers who are both tails', async t => { - const [a, b, c, d] = await create(4, apply, store => store.get('test', { valueEncoding: 'json' }), null, { ackInterval: 10, ackThreshold: 0 }) + const { bases } = await create(4, t, { + ackInterval: 10, + ackThreshold: 0 + }) + + const [a, b, c, d] = bases await addWriterAndSync(a, b) await addWriterAndSync(a, c) diff --git a/test/basic.js b/test/basic.js index 9605c855..17236435 100644 --- a/test/basic.js +++ b/test/basic.js @@ -8,8 +8,8 @@ const Autobase = require('..') const { create, + createStores, replicateAndSync, - apply, addWriter, addWriterAndSync, confirm, @@ -19,7 +19,8 @@ const { } = require('./helpers') test('basic - two writers', async t => { - const [base1, base2, base3] = await create(3, apply) + const { bases } = await create(3, t, { open: null }) + const [base1, base2, base3] = bases await addWriter(base1, base2) @@ -42,7 +43,8 @@ test('basic - two writers', async t => { test('basic - writable event fires', async t => { t.plan(1) - const [base1, base2] = await create(2, apply) + const { bases } = await create(2, t, { open: null }) + const [base1, base2] = bases base2.on('writable', () => { t.ok(base2.writable, 'Writable event fired when autobase writable') @@ -55,7 +57,8 @@ test('basic - writable event fires', async t => { test('basic - local key pair', async t => { const keyPair = crypto.keyPair() - const [base] = await create(1, apply, store => store.get('test', { valueEncoding: 'json' }), null, { keyPair }) + const { bases } = await create(1, t, { keyPair }) + const [base] = bases const block = { message: 'hello, world!' } await base.append(block) @@ -66,7 +69,8 @@ test('basic - local key pair', async t => { }) test('basic - view', async t => { - const [base] = await create(1, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(1, t) + const [base] = bases const block = { message: 'hello, world!' } await base.append(block) @@ -77,7 +81,8 @@ test('basic - view', async t => { }) test('basic - view with close', async t => { - const [base] = await create(1, apply, open, close) + const { bases } = await create(1, t, { open, close }) + const [base] = bases const block = { message: 'hello, world!' } await base.append(block) @@ -106,7 +111,7 @@ test('basic - view with close', async t => { }) test('basic - view/writer userdata is set', async t => { - const bases = await create(2, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(2, t) const [base1, base2] = bases await addWriter(base1, base2) @@ -130,7 +135,7 @@ test('basic - view/writer userdata is set', async t => { }) test('basic - compare views', async t => { - const bases = await create(2, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(2, t) const [a, b] = bases await addWriter(a, b) @@ -148,7 +153,7 @@ test('basic - compare views', async t => { }) test('basic - online majority', async t => { - const bases = await create(3, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(3, t) const [a, b, c] = bases @@ -188,7 +193,7 @@ test('basic - online majority', async t => { }) test('basic - rotating majority', async t => { - const bases = await create(3, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(3, t) const [a, b, c] = bases @@ -268,7 +273,7 @@ test('basic - rotating majority', async t => { }) test('basic - throws', async t => { - const bases = await create(2, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(2, t) const [a, b] = bases @@ -284,7 +289,7 @@ test('basic - throws', async t => { }) test('basic - add 5 writers', async t => { - const bases = await create(5, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(5, t) const [a, b, c, d, e] = bases @@ -317,7 +322,7 @@ test('basic - add 5 writers', async t => { }) test('basic - online minorities', async t => { - const bases = await create(5, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(5, t) const [a, b, c, d, e] = bases @@ -412,7 +417,7 @@ test('basic - restarting sets bootstrap correctly', async t => { }) test('batch append', async t => { - const bases = await create(2, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(2, t) const [a, b] = bases a.on('error', (e) => console.error(e)) @@ -427,7 +432,7 @@ test('batch append', async t => { }) test('undoing a batch', async t => { - const bases = await create(2, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(2, t) const [a, b] = bases a.on('error', (e) => console.error(e)) @@ -449,7 +454,7 @@ test('undoing a batch', async t => { }) test('append during reindex', async t => { - const bases = await create(4, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(4, t) const [a, b, c, d] = bases @@ -484,7 +489,8 @@ test('append during reindex', async t => { }) test('closing an autobase', async t => { - const [base] = await create(1, apply, store => store.get('test')) + const { bases } = await create(1, t) + const [base] = bases // Sanity check t.is(base.local.closed, false) @@ -494,7 +500,7 @@ test('closing an autobase', async t => { }) test('flush after reindex', async t => { - const bases = await create(9, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(9, t) const root = bases[0] const adds = [] @@ -524,7 +530,7 @@ test('flush after reindex', async t => { }) test('reindex', async t => { - const bases = await create(5, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(5, t) const [a, b, c, d, e] = bases @@ -584,7 +590,7 @@ test('reindex', async t => { }) test('sequential restarts', async t => { - const bases = await create(9, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(9, t) const root = bases[0] const adds = [] @@ -649,7 +655,8 @@ test('sequential restarts', async t => { }) test('two writers write many messages, third writer joins', async t => { - const [base1, base2, base3] = await create(3, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(3, t) + const [base1, base2, base3] = bases await addWriter(base1, base2) @@ -668,7 +675,8 @@ test('two writers write many messages, third writer joins', async t => { }) test('basic - gc indexed nodes', async t => { - const [base] = await create(1, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(1, t) + const [base] = bases await base.append({ message: '0' }) await base.append({ message: '1' }) @@ -688,7 +696,8 @@ test('basic - gc indexed nodes', async t => { }) test('basic - isAutobase', async t => { - const [base1, base2, base3] = await create(3, apply) + const { bases } = await create(3, t, { open: null }) + const [base1, base2, base3] = bases await addWriter(base1, base2) @@ -711,7 +720,9 @@ test('basic - isAutobase', async t => { test('basic - catch apply throws', async t => { t.plan(1) - const [a] = await create(1, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(1, t) + const [a] = bases + const b = new Autobase(new Corestore(ram, { primaryKey: Buffer.alloc(32).fill(1) }), a.local.key, { apply: applyThrow, valueEncoding: 'json', @@ -754,7 +765,8 @@ test('basic - catch apply throws', async t => { }) test('basic - non-indexed writer', async t => { - const [a, b] = await create(2, applyWriter, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(2, t, { apply: applyWriter }) + const [a, b] = bases await a.append({ add: b.local.key.toString('hex'), indexer: false }) @@ -825,7 +837,8 @@ test('basic - non-indexed writer', async t => { }) test('basic - non-indexed writers 3-of-5', async t => { - const [a, b, c, d, e] = await create(5, applyWriter, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(5, t, { apply: applyWriter }) + const [a, b, c, d, e] = bases await a.append({ add: b.local.key.toString('hex'), indexer: true }) await a.append({ add: c.local.key.toString('hex'), indexer: true }) @@ -947,7 +960,8 @@ test('autobase should not detach the original store', async t => { }) test('basic - oplog digest', async t => { - const [base1, base2] = await create(2, apply) + const { bases } = await create(2, t, { open: null }) + const [base1, base2] = bases await base1.append({ add: base2.local.key.toString('hex'), @@ -967,10 +981,13 @@ test('basic - oplog digest', async t => { t.is(last.digest.indexers?.length, 2) }) +// todo: use normal helper once we have hypercore session manager test('basic - close during apply', async t => { - const [a] = await create( - 1, - async function apply (nodes, view, base) { + t.plan(1) + + const [store] = await createStores(1, t) + const a = new Autobase(store, null, { + async apply (nodes, view, base) { for (const node of nodes) { if (node.value.add) { await base.addWriter(b4a.from(node.value.add, 'hex')) @@ -981,8 +998,8 @@ test('basic - close during apply', async t => { await core.get(core.length) // can never resolve } }, - store => store.get('test', { valueEncoding: 'json' }) - ) + valueEncoding: 'json' + }) const promise = a.append('hello') setImmediate(() => a.close()) @@ -991,7 +1008,7 @@ test('basic - close during apply', async t => { }) test('basic - constructor throws', async t => { - await t.exception(create(1, undefined, open), /Synthetic./) + await t.exception(create(1, t, { apply: undefined, open }), /Synthetic./) function open () { throw new Error('Synthetic.') @@ -999,7 +1016,7 @@ test('basic - constructor throws', async t => { }) test('basic - never sign past pending migration', async t => { - const bases = await create(5, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(5, t) const [a, b, c, d, e] = bases @@ -1026,7 +1043,8 @@ test('basic - never sign past pending migration', async t => { }) test('basic - remove writer', async t => { - const [a, b, c] = await create(3, applyWithRemove) + const { bases } = await create(3, t, { apply: applyWithRemove, open: null }) + const [a, b, c] = bases await addWriter(a, b, false) @@ -1048,7 +1066,8 @@ test('basic - remove writer', async t => { }) test('basic - remove indexer', async t => { - const [a, b, c] = await create(3, applyWithRemove) + const { bases } = await create(3, t, { apply: applyWithRemove, open: null }) + const [a, b, c] = bases await addWriterAndSync(a, b) await b.append(null) @@ -1078,7 +1097,8 @@ test('basic - remove indexer', async t => { }) test('basic - remove indexer and continue indexing', async t => { - const [a, b, c] = await create(3, applyWithRemove, store => store.get('test')) + const { bases } = await create(3, t, { apply: applyWithRemove }) + const [a, b, c] = bases await addWriterAndSync(a, b) await addWriterAndSync(b, c) @@ -1107,7 +1127,8 @@ test('basic - remove indexer and continue indexing', async t => { }) test('basic - remove indexer back to previously used indexer set', async t => { - const [a, b, c] = await create(3, applyWithRemove, store => store.get('test')) + const { bases } = await create(3, t, { apply: applyWithRemove }) + const [a, b, c] = bases await addWriterAndSync(a, b) @@ -1151,7 +1172,8 @@ test('basic - remove indexer back to previously used indexer set', async t => { }) test('basic - remove an indexer when 2-of-2', async t => { - const [a, b] = await create(2, applyWithRemove, store => store.get('test')) + const { bases } = await create(2, t, { apply: applyWithRemove }) + const [a, b] = bases await addWriterAndSync(a, b) @@ -1190,7 +1212,8 @@ test('basic - remove an indexer when 2-of-2', async t => { }) test('basic - remove multiple indexers concurrently', async t => { - const [a, b, c] = await create(3, apply, store => store.get('test')) + const { bases } = await create(3, t, { apply }) + const [a, b, c] = bases await addWriterAndSync(a, b) await addWriterAndSync(b, c) diff --git a/test/core.js b/test/core.js index 2c1d2015..291b2856 100644 --- a/test/core.js +++ b/test/core.js @@ -4,7 +4,6 @@ const Hypercore = require('hypercore') const { create, - apply, addWriter, addWriterAndSync, confirm, @@ -12,7 +11,9 @@ const { } = require('./helpers') test('core - no new session if closed (hypercore compat)', async t => { - const [base] = await create(1, apply, store => store.get('test')) + const { bases } = await create(1, t) + const [base] = bases + const normalCore = new Hypercore(ram) const linearizedSessionCore = base.view const snapshotSession = linearizedSessionCore.snapshot() @@ -37,7 +38,8 @@ test('core - no new session if closed (hypercore compat)', async t => { }) test('core - seek', async t => { - const [base] = await create(1, apply, store => store.get('test')) + const { bases } = await create(1, t) + const [base] = bases const b1 = base.view.byteLength await base.append('hello') @@ -63,7 +65,12 @@ test('core - seek', async t => { }) test('core - seek multi writer', async t => { - const [a, b] = await create(2, scopedApply, store => store.get('test')) + const { bases } = await create(2, t, { + apply: scopedApply, + open: store => store.get('test') + }) + + const [a, b] = bases await addWriter(a, b) await confirm([a, b]) @@ -112,7 +119,8 @@ test('core - seek multi writer', async t => { }) test('core - userData', async t => { - const [base] = await create(1, apply, store => store.get('test')) + const { bases } = await create(1, t) + const [base] = bases await base.view.setUserData('first', Buffer.from('hello')) await base.view.setUserData('second', Buffer.from('goodbye')) @@ -131,7 +139,8 @@ test('core - userData', async t => { }) test('core - properties', async t => { - const [base] = await create(1, apply, store => store.get('test')) + const { bases } = await create(1, t) + const [base] = bases await base.append('hello, world!') @@ -142,7 +151,8 @@ test('core - properties', async t => { }) test('core - indexed view', async t => { - const [a, b] = await create(2, apply, store => store.get('test')) + const { bases } = await create(2, t) + const [a, b] = bases const normal = a.view.session({ snapshot: false }) const indexed = a.view.session({ snapshot: false, indexed: true }) diff --git a/test/dags.js b/test/dags.js index e679f597..d6f85a4a 100644 --- a/test/dags.js +++ b/test/dags.js @@ -4,7 +4,6 @@ const { create, replicateAndSync, addWriterAndSync, - apply, confirm, compareViews } = require('./helpers') @@ -12,7 +11,7 @@ const { // a - b - a test('simple 2', async t => { - const bases = await create(2, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(2, t) const [a, b] = bases @@ -49,7 +48,7 @@ c - b - a - c - b - a */ test('simple 3', async t => { - const bases = await create(3, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(3, t) const [a, b, c] = bases @@ -109,7 +108,7 @@ c a // known: test fails for current linearizer test.skip('convergence', async t => { - const bases = await create(3, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(3, t) const [a, b, c] = bases @@ -189,7 +188,7 @@ test.skip('convergence', async t => { // known: test fails for current linearizer test.skip('inner majority', async t => { - const bases = await create(5, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(5, t) const [a, b, c, d, e] = bases @@ -251,7 +250,7 @@ test.skip('inner majority', async t => { */ test('majority alone - convergence', async t => { - const bases = await create(5, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(5, t) const [a, b, c, d, e] = bases @@ -302,7 +301,8 @@ test('majority alone - convergence', async t => { }) test('add writer', async t => { - const [a, b, c] = await create(3, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(3, t) + const [a, b, c] = bases let ai = 0 let bi = 0 @@ -370,7 +370,7 @@ test('add writer', async t => { */ test('majority alone - non-convergence', async t => { - const bases = await create(5, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(5, t) const [a, b, c, d, e] = bases @@ -450,7 +450,7 @@ test('majority alone - non-convergence', async t => { */ test('double fork', async t => { - const bases = await create(5, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(5, t) const [a, b, c, d, e] = bases diff --git a/test/fast-forward.js b/test/fast-forward.js index 5bd928f1..c9d784f6 100644 --- a/test/fast-forward.js +++ b/test/fast-forward.js @@ -1,9 +1,7 @@ const test = require('brittle') -const Corestore = require('corestore') const tmpDir = require('test-tmp') const cenc = require('compact-encoding') -const Autobase = require('..') const { SystemPointer } = require('../lib/messages') const { @@ -12,47 +10,19 @@ const { replicate, eventFlush, confirm, - apply, - create + create, + createBase } = require('./helpers') test('fast-forward - simple', async t => { t.plan(1) - const store = new Corestore(await tmpDir(t), { - primaryKey: Buffer.alloc(32).fill(0) - }) - - const store2 = new Corestore(await tmpDir(t), { - primaryKey: Buffer.alloc(32).fill(1) - }) - - const a = new Autobase(store.session(), null, { - apply, - open: store => store.get('view', { valueEncoding: 'json' }), - valueEncoding: 'json', - ackInterval: 0, - ackThreshold: 0 - }) - - await a.ready() - - const b = new Autobase(store2.session(), a.local.key, { - apply, - open: store => store.get('view', { valueEncoding: 'json' }), - valueEncoding: 'json', - ackInterval: 0, - ackThreshold: 0 + const { bases } = await create(2, t, { + fastForward: true, + storage: () => tmpDir(t) }) - await b.ready() - - t.teardown(async () => { - await a.close() - await b.close() - await store.close() - await store2.close() - }) + const [a, b] = bases for (let i = 0; i < 1000; i++) { await a.append('a' + i) @@ -70,20 +40,12 @@ test('fast-forward - simple', async t => { test('fast-forward - migrate', async t => { t.plan(3) - const open = store => store.get('view', { valueEncoding: 'json' }) - const [a, b, c] = await create(3, apply, open, null, { - valueEncoding: 'json', - ackInterval: 0, - ackThreshold: 0, + const { bases } = await create(3, t, { fastForward: true, storage: () => tmpDir(t) }) - t.teardown(async () => { - await a.close() - await b.close() - await c.close() - }) + const [a, b, c] = bases for (let i = 0; i < 2000; i++) { await a.append('a' + i) @@ -107,20 +69,12 @@ test('fast-forward - migrate', async t => { test('fast-forward - fast forward after migrate', async t => { t.plan(3) - const open = store => store.get('view', { valueEncoding: 'json' }) - const [a, b, c] = await create(3, apply, open, null, { - valueEncoding: 'json', - ackInterval: 0, - ackThreshold: 0, + const { bases } = await create(3, t, { fastForward: true, storage: () => tmpDir(t) }) - t.teardown(async () => { - await a.close() - await b.close() - await c.close() - }) + const [a, b, c] = bases for (let i = 0; i < 2000; i++) { await a.append('a' + i) @@ -166,21 +120,12 @@ test('fast-forward - multiple writers added', async t => { const MESSAGES_PER_ROUND = 200 - const open = store => store.get('view', { valueEncoding: 'json' }) - const [a, b, c, d] = await create(4, apply, open, null, { - valueEncoding: 'json', - ackInterval: 0, - ackThreshold: 0, + const { bases } = await create(4, t, { fastForward: true, storage: () => tmpDir(t) }) - t.teardown(async () => { - await a.close() - await b.close() - await c.close() - await d.close() - }) + const [a, b, c, d] = bases await addWriterAndSync(a, b) await addWriterAndSync(a, c) @@ -225,21 +170,12 @@ test('fast-forward - multiple writers added', async t => { }) test('fast-forward - multiple queues', async t => { - const open = store => store.get('view', { valueEncoding: 'json' }) - const [a, b, c, d] = await create(4, apply, open, null, { - valueEncoding: 'json', - ackInterval: 0, - ackThreshold: 0, + const { bases } = await create(4, t, { fastForward: true, storage: () => tmpDir(t) }) - t.teardown(async () => { - await a.close() - await b.close() - await c.close() - await d.close() - }, { order: 1 }) + const [a, b, c, d] = bases // this value should be long enough that 2 fast-forwards are // queued (ie. we don't just replicate the last state), but short @@ -310,31 +246,13 @@ test('fast-forward - multiple queues', async t => { }) test('fast-forward - open with no remote io', async t => { - const store = new Corestore(await tmpDir(t), { - primaryKey: Buffer.alloc(32).fill(0) - }) - - const store2 = new Corestore(await tmpDir(t), { - primaryKey: Buffer.alloc(32).fill(1) - }) - - const a = new Autobase(store.session(), null, { + const { bases, stores } = await create(2, t, { apply: applyOldState, - open: store => store.get('view', { valueEncoding: 'json' }), - valueEncoding: 'json', - ackInterval: 0, - ackThreshold: 0 + fastForward: true, + storage: () => tmpDir(t) }) - await a.ready() - - const b = new Autobase(store2.session(), a.local.key, { - apply: applyOldState, - open: store => store.get('view', { valueEncoding: 'json' }), - valueEncoding: 'json', - ackInterval: 0, - ackThreshold: 0 - }) + const [a, b] = bases await b.ready() @@ -362,7 +280,7 @@ test('fast-forward - open with no remote io', async t => { await b.close() const local = a.local - const remote = store2.get({ key: local.key }) + const remote = stores[1].get({ key: local.key }) const s1 = local.replicate(true) const s2 = remote.replicate(false) @@ -370,22 +288,12 @@ test('fast-forward - open with no remote io', async t => { s1.pipe(s2).pipe(s1) await remote.download({ end: local.length }).downloaded() + s1.destroy() await new Promise(resolve => s2.on('close', resolve)) - const b2 = new Autobase(store2.session(), a.local.key, { - apply: applyOldState, - open: store => store.get('view', { valueEncoding: 'json' }), - valueEncoding: 'json', - ackInterval: 0, - ackThreshold: 0 - }) - - t.teardown(async () => { - await a.close() - await b2.close() - await store.close() - await store2.close() + const b2 = await createBase(stores[1].session(), a.local.key, t, { + apply: applyOldState }) await b2.ready() @@ -412,20 +320,12 @@ test('fast-forward - open with no remote io', async t => { test('fast-forward - force reset then ff', async t => { t.plan(9) - const open = store => store.get('view', { valueEncoding: 'json' }) - const [a, b, c] = await create(3, apply, open, null, { - valueEncoding: 'json', - ackInterval: 0, - ackThreshold: 0, + const { bases } = await create(3, t, { fastForward: true, storage: () => tmpDir(t) }) - t.teardown(async () => { - await a.close() - await b.close() - await c.close() - }) + const [a, b, c] = bases await addWriterAndSync(a, b) await addWriterAndSync(a, c) diff --git a/test/helpers/index.js b/test/helpers/index.js index 6c82a6fc..34c138e2 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -9,6 +9,7 @@ const encryptionKey = process.env && process.env.ENCRYPT_ALL ? b4a.alloc(32).fil module.exports = { createStores, + createBase, create, addWriter, addWriterAndSync, @@ -20,27 +21,59 @@ module.exports = { ...helpers } -async function createStores (n, opts = {}) { +async function createStores (n, t, opts = {}) { const storage = opts.storage || (() => ram.reusable()) + const offset = opts.offset || 0 + const stores = [] - for (let i = 0; i < n; i++) { - stores.push(new Corestore(await storage(), { primaryKey: Buffer.alloc(32).fill(i), encryptionKey })) + for (let i = offset; i < n + offset; i++) { + const primaryKey = Buffer.alloc(32, i) + stores.push(new Corestore(await storage(), { primaryKey, encryptionKey })) } + + t.teardown(() => Promise.all(stores.map(s => s.close())), { order: 2 }) + return stores } -async function create (n, apply, open, close, opts = {}) { - const storage = opts.storage || (() => ram.reusable()) - const moreOpts = { apply, open, close, valueEncoding: 'json', ackInterval: 0, ackThreshold: 0, fastForward: false, ...opts } - const bases = [new Autobase(new Corestore(await storage(), { primaryKey: Buffer.alloc(32).fill(0), encryptionKey }), null, moreOpts)] - await bases[0].ready() - if (n === 1) return bases +async function create (n, t, opts = {}) { + const stores = await createStores(n, t, opts) + const bases = [await createBase(stores[0], null, t, opts)] + + if (n === 1) return { stores, bases } + for (let i = 1; i < n; i++) { - const base = new Autobase(new Corestore(await storage(), { primaryKey: Buffer.alloc(32).fill(i), encryptionKey }), bases[0].local.key, moreOpts) - await base.ready() - bases.push(base) + bases.push(await createBase(stores[i], bases[0].local.key, t, opts)) + } + + return { + stores, + bases } - return bases +} + +async function createBase (store, key, t, opts = {}) { + const moreOpts = { + apply, + open, + close: undefined, + valueEncoding: 'json', + ackInterval: 0, + ackThreshold: 0, + fastForward: false, + ...opts + } + + const base = new Autobase(store.session(), key, moreOpts) + await base.ready() + + t.teardown(() => base.close(), { order: 1 }) + + return base +} + +function open (store) { + return store.get('view', { valueEncoding: 'json' }) } async function addWriter (base, add, indexer = true) { diff --git a/test/linearizer.js b/test/linearizer.js index 48ab9f4d..789a0877 100644 --- a/test/linearizer.js +++ b/test/linearizer.js @@ -5,8 +5,7 @@ const { create, confirm, replicateAndSync, - addWriterAndSync, - apply + addWriterAndSync } = require('./helpers') /* @@ -16,7 +15,7 @@ c - b - a - c - b - a */ test('linearizer - simple', async t => { - const bases = await create(3, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(3, t) const [a, b, c] = bases @@ -113,7 +112,7 @@ test('linearizer - simple', async t => { }) test('linearizer - compete', async t => { - const bases = await create(3, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(3, t) const [a, b, c] = bases @@ -208,7 +207,7 @@ test('linearizer - compete', async t => { }) test('linearizer - count ordering', async t => { - const bases = await create(3, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(3, t) const [a, b, c] = bases @@ -336,7 +335,7 @@ test('linearizer - count ordering', async t => { }) test('linearizer - reordering', async t => { - const bases = await create(3, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(3, t) const [a, b, c] = bases @@ -382,7 +381,7 @@ test('linearizer - reordering', async t => { }) test('linearizer - reordering after restart', async t => { - const bases = await create(3, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(3, t) const [a, b, c] = bases @@ -414,7 +413,7 @@ test('linearizer - reordering after restart', async t => { }) test('linearizer - shouldAck', async t => { - const bases = await create(3, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(3, t) const [a, b, c] = bases @@ -447,7 +446,7 @@ test('linearizer - shouldAck', async t => { // review: test passes, but not sure what this test is for? test.skip('linearizer - no loop', async t => { - const bases = await create(4, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(4, t) const [a, b, c, d] = bases diff --git a/test/snapshots.js b/test/snapshots.js index abb9ab4d..b50e295f 100644 --- a/test/snapshots.js +++ b/test/snapshots.js @@ -4,7 +4,6 @@ const Hyperbee = require('hyperbee') const { create, confirm, - apply, addWriter, replicateAndSync } = require('./helpers') @@ -12,7 +11,7 @@ const { const beeOpts = { extension: false, keyEncoding: 'binary', valueEncoding: 'binary' } test('check snapshot of snapshot after rebase', async t => { - const bases = await create(3, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(3, t) const [base1, base2, base3] = bases await addWriter(base1, base2) @@ -49,7 +48,7 @@ test('check snapshot of snapshot after rebase', async t => { }) test('no inconsistent snapshot entries when truncated', async t => { - const bases = await create(3, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(3, t) const [base1, base2, base3] = bases await base1.append({ add: base2.local.key.toString('hex') }) @@ -78,7 +77,7 @@ test('no inconsistent snapshot entries when truncated', async t => { }) test('no inconsistent snapshot-of-snapshot entries when truncated', async t => { - const bases = await create(3, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(3, t) const [base1, base2, base3] = bases await base1.append({ add: base2.local.key.toString('hex') }) @@ -113,7 +112,11 @@ test('no inconsistent snapshot-of-snapshot entries when truncated', async t => { test('no inconsistent entries when using snapshot core in bee (bee snapshot)', async t => { // Setup - const bases = await create(3, (...args) => applyForBee(t, ...args), openForBee) + const { bases } = await create(3, t, { + apply (...args) { return applyForBee(t, ...args) }, + open: openForBee + }) + const [base1, base2, base3] = bases await base1.append({ add: base2.local.key.toString('hex') }) @@ -161,7 +164,7 @@ test('no inconsistent entries when using snapshot core in bee (bee snapshot)', a }) test('check cloning detached snapshot', async t => { - const bases = await create(4, apply, store => store.get('test', { valueEncoding: 'json' })) + const { bases } = await create(4, t) const [base1, base2, base3, base4] = bases await addWriter(base1, base2) diff --git a/test/suspend.js b/test/suspend.js index d07f6443..d85adab4 100644 --- a/test/suspend.js +++ b/test/suspend.js @@ -1,13 +1,13 @@ const test = require('brittle') -const ram = require('random-access-memory') const tmpDir = require('test-tmp') -const Corestore = require('corestore') const b4a = require('b4a') const Autobase = require('..') const { create, + createBase, + createStores, replicate, replicateAndSync, apply, @@ -18,15 +18,9 @@ const { } = require('./helpers') test('suspend - pass exisiting store', async t => { - const [base1] = await create(1, apply) + const { stores, bases } = await create(2, t) - const store = new Corestore(ram.reusable(), { - primaryKey: Buffer.alloc(32).fill(1) - }) - - const session2 = store.session() - const base2 = new Autobase(session2, base1.local.key, { apply, valueEncoding: 'json', ackInterval: 0, ackThreshold: 0 }) - await base2.ready() + const [base1, base2] = bases await base1.append({ add: base2.local.key.toString('hex'), @@ -49,8 +43,7 @@ test('suspend - pass exisiting store', async t => { await base2.close() - const session3 = store.session() - const base3 = new Autobase(session3, base1.local.key, { apply, valueEncoding: 'json', ackInterval: 0, ackThreshold: 0, fastForward: false }) + const base3 = await createBase(stores[1], base1.local.key, t) await base3.ready() t.is(base3.activeWriters.size, 2) @@ -61,15 +54,15 @@ test('suspend - pass exisiting store', async t => { }) test('suspend - pass exisiting fs store', async t => { - const [base1] = await create(1, apply) + const { bases } = await create(1, t, { open: null }) + const [base1] = bases - const store = new Corestore(await tmpDir(t), { - primaryKey: Buffer.alloc(32).fill(1) + const [store] = await createStores(1, t, { + offset: 1, + storage: () => tmpDir(t) }) - const session2 = store.session() - const base2 = new Autobase(session2, base1.local.key, { apply, valueEncoding: 'json', ackInterval: 0, ackThreshold: 0, fastForward: false }) - await base2.ready() + const base2 = await createBase(store, base1.local.key, t, { open: null }) await base1.append({ add: base2.local.key.toString('hex'), @@ -92,16 +85,9 @@ test('suspend - pass exisiting fs store', async t => { await base2.close() - const session3 = store.session() - const base3 = new Autobase(session3, base1.local.key, { apply, valueEncoding: 'json', ackInterval: 0, ackThreshold: 0, fastForward: false }) + const base3 = await createBase(store, base1.local.key, t, { open: null }) await base3.ready() - t.teardown(async () => { - await base1.close() - await base3.close() - await store.close() - }) - t.is(base3.activeWriters.size, 2) await base3.append('final') @@ -110,20 +96,9 @@ test('suspend - pass exisiting fs store', async t => { }) test('suspend - 2 exisiting fs stores', async t => { - const store = new Corestore(await tmpDir(t), { - primaryKey: Buffer.alloc(32).fill(0) - }) - - const base1 = new Autobase(store, null, { apply, valueEncoding: 'json', ackInterval: 0, ackThreshold: 0, fastForward: false }) - await base1.ready() - - const store2 = new Corestore(await tmpDir(t), { - primaryKey: Buffer.alloc(32).fill(1) - }) + const { bases, stores } = await create(2, t, { storage: () => tmpDir(t) }) - const session2 = store2.session() - const base2 = new Autobase(session2, base1.local.key, { apply, valueEncoding: 'json', ackInterval: 0, ackThreshold: 0, fastForward: false }) - await base2.ready() + const [base1, base2] = bases await base1.append({ add: base2.local.key.toString('hex'), @@ -146,16 +121,7 @@ test('suspend - 2 exisiting fs stores', async t => { await base2.close() - const session3 = store2.session() - const base3 = new Autobase(session3, base1.local.key, { apply, valueEncoding: 'json', ackInterval: 0, ackThreshold: 0, fastForward: false }) - await base3.ready() - - t.teardown(async () => { - await base1.close() - await base3.close() - await store.close() - await store2.close() - }) + const base3 = await createBase(stores[1], base1.local.key, t) t.is(base3.activeWriters.size, 2) @@ -165,25 +131,9 @@ test('suspend - 2 exisiting fs stores', async t => { }) test('suspend - reopen after index', async t => { - const [a] = await create(1, apply, open) - - const store = new Corestore(await tmpDir(t), { - primaryKey: Buffer.alloc(32).fill(1) - }) - - const session1 = store.session() - const b = new Autobase(session1, a.local.key, { - apply, - valueEncoding: 'json', - open: store => store.get('view', { - valueEncoding: 'json' - }), - ackInterval: 0, - ackThreshold: 0, - fastForward: false - }) + const { bases, stores } = await create(2, t, { storage: () => tmpDir(t) }) - await b.ready() + const [a, b] = bases await addWriter(a, b) @@ -206,25 +156,8 @@ test('suspend - reopen after index', async t => { await b.close() - const session2 = store.session() - const b2 = new Autobase(session2, a.local.key, { - apply, - valueEncoding: 'json', - open: store => store.get('view', { - valueEncoding: 'json' - }), - ackInterval: 0, - ackThreshold: 0, - fastForward: false - }) - - t.teardown(async () => { - await a.close() - await b2.close() - await store.close() - }) + const b2 = await createBase(stores[1], a.local.key, t) - await b2.ready() await b2.update() t.is(b2.view.length, order.length) @@ -245,23 +178,9 @@ test('suspend - reopen after index', async t => { }) test('suspend - reopen with sync in middle', async t => { - const [a] = await create(1, apply, open) - - const store = new Corestore(await tmpDir(t), { - primaryKey: Buffer.alloc(32).fill(1) - }) + const { bases, stores } = await create(2, t, { storage: () => tmpDir(t) }) - const session1 = store.session() - const b = new Autobase(session1, a.local.key, { - apply, - valueEncoding: 'json', - open, - ackInterval: 0, - ackThreshold: 0, - fastForward: false - }) - - await b.ready() + const [a, b] = bases await addWriter(a, b) @@ -279,7 +198,8 @@ test('suspend - reopen with sync in middle', async t => { await b.close() - const unreplicate = replicate([a.store, store]) + const bstore = stores[1] + const unreplicate = replicate([a.store, bstore]) await a.update() @@ -287,7 +207,7 @@ test('suspend - reopen with sync in middle', async t => { for (const { key } of [b.system.core, b.view]) { if (!key) continue - const core = store.get({ key, compat: false }) + const core = bstore.get({ key, compat: false }) const remote = a.store.get({ key, compat: false }) await core.ready() @@ -298,39 +218,22 @@ test('suspend - reopen with sync in middle', async t => { // sync next views for (const ac of [a.system.core, a.view]) { const remote = ac.getBackingCore().session - const local = store.get({ key: remote.key, compat: false }) + const local = bstore.get({ key: remote.key, compat: false }) await local.ready() await local.download({ start: 0, end: remote.length }).done() } // sync writers for (const core of [a.local]) { - const remote = store.get({ key: core.key, compat: false }) + const remote = bstore.get({ key: core.key, compat: false }) await remote.ready() await remote.download({ start: 0, end: core.length }).done() } await unreplicate() - const session2 = store.session() - await session2.ready() - - const b2 = new Autobase(session2, a.local.key, { - apply, - valueEncoding: 'json', - open, - ackInterval: 0, - ackThreshold: 0, - fastForward: false - }) + const b2 = await createBase(bstore, a.local.key, t) - t.teardown(async () => { - await a.close() - await b2.close() - await store.close() - }) - - await b2.ready() await b2.update() t.is(b2.activeWriters.size, 2) @@ -346,23 +249,9 @@ test('suspend - reopen with sync in middle', async t => { }) test('suspend - reopen with indexing in middle', async t => { - const [a, b] = await create(2, apply, open) + const { bases, stores } = await create(3, t, { storage: () => tmpDir(t) }) - const store = new Corestore(await tmpDir(t), { - primaryKey: Buffer.alloc(32).fill(2) - }) - - const session1 = store.session() - const c = new Autobase(session1, a.local.key, { - apply, - valueEncoding: 'json', - open, - ackInterval: 0, - ackThreshold: 0, - fastForward: false - }) - - await c.ready() + const [a, b, c] = bases await addWriterAndSync(a, b) await addWriterAndSync(a, c) @@ -391,25 +280,8 @@ test('suspend - reopen with indexing in middle', async t => { await confirm([a, b]) - const session2 = store.session() - await session2.ready() - - const c2 = new Autobase(session2, a.local.key, { - apply, - valueEncoding: 'json', - open, - ackInterval: 0, - ackThreshold: 0, - fastForward: false - }) - - t.teardown(async () => { - await a.close() - await c2.close() - await store.close() - }) + const c2 = await createBase(stores[2], a.local.key, t) - await c2.ready() await c2.update() t.is(c2.view.length, order.length) @@ -431,23 +303,9 @@ test('suspend - reopen with indexing in middle', async t => { }) test.skip('suspend - reopen with indexing + sync in middle', async t => { - const [a, b] = await create(2, apply, open) - - const store = new Corestore(await tmpDir(t), { - primaryKey: Buffer.alloc(32).fill(2) - }) + const { bases, stores } = await create(2, t, { storage: () => tmpDir(t) }) - const session1 = store.session() - const c = new Autobase(session1, a.local.key, { - apply, - valueEncoding: 'json', - open, - ackInterval: 0, - ackThreshold: 0, - fastForward: false - }) - - await c.ready() + const [a, b, c] = bases await addWriterAndSync(a, b) await addWriterAndSync(a, c) @@ -465,6 +323,7 @@ test.skip('suspend - reopen with indexing + sync in middle', async t => { } await c.close() + const cstore = stores[2] // majority continues @@ -476,7 +335,7 @@ test.skip('suspend - reopen with indexing + sync in middle', async t => { await confirm([a, b]) - const unreplicate = replicate([a.store, store]) + const unreplicate = replicate([a.store, cstore]) await a.update() @@ -484,7 +343,7 @@ test.skip('suspend - reopen with indexing + sync in middle', async t => { for (const { key } of [c.system.core, c.view]) { if (!key) continue - const core = store.get({ key, compat: false }) + const core = cstore.get({ key, compat: false }) const remote = a.store.get({ key, compat: false }) await core.ready() @@ -495,7 +354,7 @@ test.skip('suspend - reopen with indexing + sync in middle', async t => { // sync next views for (const ac of [a.system.core, a.view]) { const remote = ac.getBackingCore().session - const local = store.get({ key: remote.key, compat: false }) + const local = cstore.get({ key: remote.key, compat: false }) await local.ready() await local.download({ start: 0, end: remote.length }).done() } @@ -503,32 +362,14 @@ test.skip('suspend - reopen with indexing + sync in middle', async t => { // sync writers for (const core of [a.local, a.store.get({ key: b.local.key, compat: false })]) { await core.ready() - const remote = store.get({ key: core.key, compat: false }) + const remote = cstore.get({ key: core.key, compat: false }) await remote.ready() await remote.download({ start: 0, end: core.length }).done() } await unreplicate() - const session2 = store.session() - await session2.ready() - - const c2 = new Autobase(session2, a.local.key, { - apply, - valueEncoding: 'json', - open, - ackInterval: 0, - ackThreshold: 0, - fastForward: false - }) - - t.teardown(async () => { - await a.close() - await c2.close() - await store.close() - }) - - await c2.ready() + const c2 = await createBase(cstore, a.local.key, t) t.is(c2.view.length, order.length) @@ -550,23 +391,13 @@ test.skip('suspend - reopen with indexing + sync in middle', async t => { }) test('suspend - non-indexed writer', async t => { - const [a] = await create(1, applyWriter, open) - - const store = new Corestore(await tmpDir(t), { - primaryKey: b4a.alloc(32).fill(1) - }) - - const session1 = store.session() - const b = new Autobase(session1, a.local.key, { + const { bases, stores } = await create(2, t, { apply: applyWriter, - valueEncoding: 'json', - open, - ackInterval: 0, - ackThreshold: 0, - fastForward: false + storage: () => tmpDir(t) }) - await b.ready() + const [a, b] = bases + await b.view.ready() await a.append({ add: b.local.key.toString('hex'), indexer: false }) @@ -582,7 +413,7 @@ test('suspend - non-indexed writer', async t => { await confirm([a, b]) - const unreplicate = replicate([a.store, store]) + const unreplicate = replicate([a.store, stores[1]]) await eventFlush() await a.update() @@ -592,25 +423,7 @@ test('suspend - non-indexed writer', async t => { await b.close() - const session2 = store.session() - await session2.ready() - - const b2 = new Autobase(session2, a.local.key, { - apply: applyWriter, - valueEncoding: 'json', - open, - ackInterval: 0, - ackThreshold: 0, - fastForward: false - }) - - t.teardown(async () => { - await a.close() - await b2.close() - await store.close() - }) - - await b2.ready() + const b2 = await createBase(stores[1], a.local.key, t) t.is(b2.view.indexedLength, a.view.indexedLength) t.is(b2.view.length, a.view.length) @@ -628,23 +441,13 @@ test('suspend - non-indexed writer', async t => { }) test('suspend - open new index after reopen', async t => { - const [a] = await create(1, applyMultiple, openMultiple) - - const store = new Corestore(await tmpDir(t), { - primaryKey: Buffer.alloc(32).fill(1) - }) - - const session1 = store.session() - const b = new Autobase(session1, a.local.key, { - valueEncoding: 'json', + const { bases, stores } = await create(2, t, { apply: applyMultiple, open: openMultiple, - ackInterval: 0, - ackThreshold: 0, - fastForward: false + storage: () => tmpDir(t) }) - await b.ready() + const [a, b] = bases await addWriterAndSync(a, b) @@ -670,20 +473,9 @@ test('suspend - open new index after reopen', async t => { await b.close() - const session2 = store.session() - const b2 = new Autobase(session2, a.local.key, { - valueEncoding: 'json', + const b2 = await createBase(stores[1], a.local.key, t, { apply: applyMultiple, - open: openMultiple, - ackInterval: 0, - ackThreshold: 0, - fastForward: false - }) - - t.teardown(async () => { - await a.close() - await b2.close() - await store.close() + open: openMultiple }) await b2.ready() @@ -733,23 +525,13 @@ test('suspend - open new index after reopen', async t => { }) test('suspend - reopen multiple indexes', async t => { - const [a] = await create(1, applyMultiple, openMultiple) - - const store = new Corestore(await tmpDir(t), { - primaryKey: Buffer.alloc(32).fill(1) - }) - - const session1 = store.session() - const b = new Autobase(session1, a.local.key, { - valueEncoding: 'json', + const { bases, stores } = await create(2, t, { apply: applyMultiple, open: openMultiple, - ackInterval: 0, - ackThreshold: 0, - fastForward: false + storage: () => tmpDir(t) }) - await b.ready() + const [a, b] = bases await addWriterAndSync(a, b) @@ -778,23 +560,11 @@ test('suspend - reopen multiple indexes', async t => { await b.close() - const session2 = store.session() - const b2 = new Autobase(session2, a.local.key, { - valueEncoding: 'json', + const b2 = await createBase(stores[1], a.local.key, t, { apply: applyMultiple, - open: openMultiple, - ackInterval: 0, - ackThreshold: 0, - fastForward: false - }) - - t.teardown(async () => { - await a.close() - await b2.close() - await store.close() + open: openMultiple }) - await b2.ready() await b2.update() for (let i = 0; i < b2.view.first.length; i++) { @@ -842,8 +612,7 @@ test('suspend - reopen multiple indexes', async t => { }) test('restart non writer', async t => { - const storeA = new Corestore(ram.reusable()) - const storeB = new Corestore(ram.reusable()) + const [storeA, storeB] = await createStores(2, t) const base = new Autobase(storeA, { apply, valueEncoding: 'json', fastForward: false }) await base.append({ hello: 'world' }) @@ -863,23 +632,13 @@ test('restart non writer', async t => { }) test('suspend - non-indexed writer catches up', async t => { - const [a] = await create(1, applyWriter, open) - - const store = new Corestore(await tmpDir(t), { - primaryKey: b4a.alloc(32).fill(1) - }) - - const session1 = store.session() - const b = new Autobase(session1, a.local.key, { + const { bases, stores } = await create(2, t, { apply: applyWriter, - valueEncoding: 'json', - open, - ackInterval: 0, - ackThreshold: 0, - fastForward: false + storage: () => tmpDir(t) }) - await b.ready() + const [a, b] = bases + await b.view.ready() await a.append({ add: b.local.key.toString('hex'), indexer: false }) @@ -895,7 +654,7 @@ test('suspend - non-indexed writer catches up', async t => { await confirm([a, b]) - const unreplicate = replicate([a.store, store]) + const unreplicate = replicate([a.store, stores[1]]) await eventFlush() await a.update() @@ -908,25 +667,7 @@ test('suspend - non-indexed writer catches up', async t => { await b.close() - const session2 = store.session() - await session2.ready() - - const b2 = new Autobase(session2, a.local.key, { - applyWriter, - valueEncoding: 'json', - open, - ackInterval: 0, - ackThreshold: 0, - fastForward: false - }) - - t.teardown(async () => { - await a.close() - await b2.close() - await store.close() - }) - - await b2.ready() + await t.execution(createBase(stores[1], a.local.key, t)) t.pass('did not fail on open') @@ -943,24 +684,13 @@ test('suspend - non-indexed writer catches up', async t => { }) test.skip('suspend - append but not indexed then reopen', async t => { - const [a, b] = await create(2, applyMultiple, openMultiple) - - const store = new Corestore(await tmpDir(t), { - primaryKey: Buffer.alloc(32).fill(2) - }) - - const session1 = store.session() - const c = new Autobase(session1, a.local.key, { - valueEncoding: 'json', + const { bases, stores } = await create(3, t, { apply: applyMultiple, open: openMultiple, - ackInterval: 0, - ackThreshold: 0, - fastForward: false + storage: () => tmpDir(t) }) - await b.ready() - await c.ready() + const [a, b, c] = bases await addWriterAndSync(a, b) await addWriterAndSync(a, c) @@ -986,20 +716,9 @@ test.skip('suspend - append but not indexed then reopen', async t => { await c.close() - const session2 = store.session() - const c2 = new Autobase(session2, a.local.key, { - valueEncoding: 'json', + const c2 = await createBase(stores[2], a.local.key, t, { apply: applyMultiple, - open: openMultiple, - ackInterval: 0, - ackThreshold: 0, - fastForward: false - }) - - t.teardown(async () => { - await a.close() - await c2.close() - await store.close() + open: openMultiple }) await c2.ready() @@ -1047,11 +766,9 @@ test.skip('suspend - append but not indexed then reopen', async t => { }) test('suspend - migrations', async t => { - const [a] = await create(1, apply, open) + const { bases, stores } = await create(3, t, { storage: () => tmpDir(t) }) - const store = new Corestore(await tmpDir(t), { - primaryKey: Buffer.alloc(32).fill(1) - }) + const [a, b] = bases await a.append('a0') await a.append('a1') @@ -1059,18 +776,6 @@ test('suspend - migrations', async t => { t.is(a.view.indexedLength, 2) t.is(a.view.signedLength, 2) - const session1 = store.session() - const b = new Autobase(session1, a.local.key, { - apply, - valueEncoding: 'json', - open: store => store.get('view', { - valueEncoding: 'json' - }), - ackInterval: 0, - ackThreshold: 0, - fastForward: false - }) - await b.ready() await addWriter(a, b) @@ -1097,25 +802,7 @@ test('suspend - migrations', async t => { await b.close() - const session2 = store.session() - const b2 = new Autobase(session2, a.local.key, { - apply, - valueEncoding: 'json', - open: store => store.get('view', { - valueEncoding: 'json' - }), - ackInterval: 0, - ackThreshold: 0, - fastForward: false - }) - - t.teardown(async () => { - await a.close() - await b2.close() - await store.close() - }) - - await b2.ready() + const b2 = await createBase(stores[1], a.local.key, t) t.is(b2.view.indexedLength, 3) t.is(b2.view.signedLength, 3) @@ -1140,22 +827,13 @@ test('suspend - migrations', async t => { }) test('suspend - append waits for drain after boot', async t => { - const [a] = await create(1, applyMultiple, openMultiple) - - const store = new Corestore(await tmpDir(t), { - primaryKey: Buffer.alloc(32).fill(1) - }) - - const b = new Autobase(store.session(), a.local.key, { - valueEncoding: 'json', + const { bases, stores } = await create(3, t, { apply: applyMultiple, open: openMultiple, - ackInterval: 0, - ackThreshold: 0, - fastForward: false + storage: () => tmpDir(t) }) - await b.ready() + const [a, b] = bases await addWriterAndSync(a, b) @@ -1165,19 +843,9 @@ test('suspend - append waits for drain after boot', async t => { await b.close() - const b2 = new Autobase(store.session(), a.local.key, { - valueEncoding: 'json', + const b2 = await createBase(stores[1], a.local.key, t, { apply: applyMultiple, - open: openMultiple, - ackInterval: 0, - ackThreshold: 0, - fastForward: false - }) - - t.teardown(async () => { - await a.close() - await b2.close() - await store.close() + open: openMultiple }) await b2.append({ last: true }) @@ -1188,11 +856,9 @@ test('suspend - append waits for drain after boot', async t => { }) test('suspend - incomplete migrate', async t => { - const [a] = await create(1, apply, open) + const { bases, stores } = await create(3, t, { storage: () => tmpDir(t) }) - const store = new Corestore(await tmpDir(t), { - primaryKey: Buffer.alloc(32).fill(1) - }) + const [a, b] = bases await a.append('a0') await a.append('a1') @@ -1200,20 +866,6 @@ test('suspend - incomplete migrate', async t => { t.is(a.view.indexedLength, 2) t.is(a.view.signedLength, 2) - const session1 = store.session() - const b = new Autobase(session1, a.local.key, { - apply, - valueEncoding: 'json', - open: store => store.get('view', { - valueEncoding: 'json' - }), - ackInterval: 0, - ackThreshold: 0, - fastForward: false - }) - - await b.ready() - await addWriter(a, b) await replicateAndSync([a, b]) @@ -1232,23 +884,7 @@ test('suspend - incomplete migrate', async t => { await b.close() - const session2 = store.session() - const b2 = new Autobase(session2, a.local.key, { - apply, - valueEncoding: 'json', - open: store => store.get('view', { - valueEncoding: 'json' - }), - ackInterval: 0, - ackThreshold: 0, - fastForward: false - }) - - t.teardown(async () => { - await a.close() - await b2.close() - await store.close() - }) + const b2 = await createBase(stores[1], a.local.key, t) await b2.ready() @@ -1267,10 +903,6 @@ test('suspend - incomplete migrate', async t => { await t.execution(replicateAndSync([a, b2])) }) -function open (store) { - return store.get('view', { valueEncoding: 'json' }) -} - function openMultiple (store) { return { first: store.get('first', { valueEncoding: 'json' }), diff --git a/test/upgrade.js b/test/upgrade.js index 2f63e33e..114f18f9 100644 --- a/test/upgrade.js +++ b/test/upgrade.js @@ -13,7 +13,7 @@ const { } = require('./helpers') test('upgrade - do not proceed', async t => { - const [s1, s2] = await createStores(2) + const [s1, s2] = await createStores(2, t) const a0 = new Autobase(s1.session(), null, { apply: applyv0, @@ -65,7 +65,7 @@ test('upgrade - do not proceed', async t => { }) test('upgrade - proceed', async t => { - const [s1, s2] = await createStores(2) + const [s1, s2] = await createStores(2, t) const a0 = new Autobase(s1.session(), null, { apply: applyv0, @@ -130,7 +130,7 @@ test('upgrade - proceed', async t => { }) test('upgrade - consensus', async t => { - const [s1, s2] = await createStores(2) + const [s1, s2] = await createStores(2, t) const a0 = new Autobase(s1.session(), null, { apply: applyv0, @@ -205,7 +205,7 @@ test('upgrade - consensus', async t => { }) test('upgrade - consensus 3 writers', async t => { - const [s1, s2, s3] = await createStores(3) + const [s1, s2, s3] = await createStores(3, t) const a0 = new Autobase(s1.session(), null, { apply: applyv0, @@ -318,7 +318,7 @@ test('upgrade - consensus 3 writers', async t => { }) test('upgrade - writer cannot append while behind', async t => { - const [s1, s2, s3] = await createStores(3) + const [s1, s2, s3] = await createStores(3, t) const a0 = new Autobase(s1.session(), null, { apply: applyv0, @@ -405,7 +405,7 @@ test('upgrade - writer cannot append while behind', async t => { }) test('upgrade - onindex hook', async t => { - const [s1, s2] = await createStores(2) + const [s1, s2] = await createStores(2, t) let aversion = 0 let bversion = 0 @@ -491,7 +491,7 @@ test('upgrade - onindex hook', async t => { }) test('autobase upgrade - do not proceed', async t => { - const [s1, s2] = await createStores(2) + const [s1, s2] = await createStores(2, t) const a0 = new Autobase(s1.session(), null, { apply, @@ -546,7 +546,7 @@ test('autobase upgrade - do not proceed', async t => { }) test('autobase upgrade - proceed', async t => { - const [s1, s2] = await createStores(2) + const [s1, s2] = await createStores(2, t) const a0 = new Autobase(s1.session(), null, { apply, @@ -617,7 +617,7 @@ test('autobase upgrade - proceed', async t => { }) test('autobase upgrade - consensus', async t => { - const [s1, s2] = await createStores(2) + const [s1, s2] = await createStores(2, t) const a0 = new Autobase(s1.session(), null, { apply, @@ -695,7 +695,7 @@ test('autobase upgrade - consensus', async t => { }) test('autobase upgrade - consensus 3 writers', async t => { - const [s1, s2, s3] = await createStores(3) + const [s1, s2, s3] = await createStores(3, t) const a0 = new Autobase(s1.session(), null, { apply,