From 82e7f036706803ebb034d7c6dd81481121a46a91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Fri, 8 Oct 2021 11:32:18 +0200 Subject: [PATCH 01/10] Initial pass on porting tests to Brittle --- .gitignore | 1 + package.json | 4 +- test/basic.js | 36 +++---- test/bitfield.js | 24 ++--- test/core.js | 134 ++++++++++++------------ test/encodings.js | 14 +-- test/extension.js | 18 ++-- test/merkle-tree.js | 248 ++++++++++++++++++++++---------------------- test/mutex.js | 32 +++--- test/oplog.js | 90 ++++++++-------- test/preload.js | 28 ++--- test/replicate.js | 76 +++++++------- test/sessions.js | 42 ++++---- test/user-data.js | 16 +-- 14 files changed, 382 insertions(+), 381 deletions(-) diff --git a/.gitignore b/.gitignore index 29bb6657..13987af5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules package-lock.json +coverage sandbox.js sandbox/ diff --git a/package.json b/package.json index d1f4645b..0f54af91 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Hypercore 10", "main": "index.js", "scripts": { - "test": "standard && tape test/*.js" + "test": "standard && brittle test/*.js" }, "repository": { "type": "git", @@ -32,10 +32,10 @@ }, "devDependencies": { "@hyperswarm/replicator": "^1.8.0", + "brittle": "^1.3.6", "random-access-memory": "^3.1.2", "range-parser": "^1.2.1", "standard": "^16.0.3", - "tape": "^5.0.1", "tmp-promise": "^3.0.2" }, "optionalDependencies": { diff --git a/test/basic.js b/test/basic.js index 2a36c6e0..b6e885af 100644 --- a/test/basic.js +++ b/test/basic.js @@ -1,17 +1,17 @@ -const tape = require('tape') +const test = require('brittle') const ram = require('random-access-memory') const Hypercore = require('../') const { create } = require('./helpers') -tape('basic', async function (t) { +test('basic', async function (t) { const core = await create() let appends = 0 - t.same(core.length, 0) - t.same(core.byteLength, 0) - t.same(core.writable, true) - t.same(core.readable, true) + t.is(core.length, 0) + t.is(core.byteLength, 0) + t.is(core.writable, true) + t.is(core.readable, true) core.on('append', function () { appends++ @@ -20,25 +20,25 @@ tape('basic', async function (t) { await core.append('hello') await core.append('world') - t.same(core.length, 2) - t.same(core.byteLength, 10) - t.same(appends, 2) + t.is(core.length, 2) + t.is(core.byteLength, 10) + t.is(appends, 2) t.end() }) -tape('session', async function (t) { +test('session', async function (t) { const core = await create() const session = core.session() await session.append('test') - t.same(await core.get(0), Buffer.from('test')) - t.same(await session.get(0), Buffer.from('test')) + t.alike(await core.get(0), Buffer.from('test')) + t.alike(await session.get(0), Buffer.from('test')) t.end() }) -tape('close', async function (t) { +test('close', async function (t) { const core = await create() await core.append('hello world') @@ -52,7 +52,7 @@ tape('close', async function (t) { } }) -tape('close multiple', async function (t) { +test('close multiple', async function (t) { const core = await create() await core.append('hello world') @@ -64,16 +64,16 @@ tape('close multiple', async function (t) { core.close().then(() => done('close 3')) await core.close() - t.same(expected.length, 0, 'all event passed') + t.is(expected.length, 0, 'all event passed') function done (event) { - t.same(event, expected.shift()) + t.is(event, expected.shift()) } }) -tape('storage options', async function (t) { +test('storage options', async function (t) { const core = new Hypercore({ storage: ram }) await core.append('hello') - t.same(await core.get(0), Buffer.from('hello')) + t.alike(await core.get(0), Buffer.from('hello')) t.end() }) diff --git a/test/bitfield.js b/test/bitfield.js index 7e6e2a58..95f65e80 100644 --- a/test/bitfield.js +++ b/test/bitfield.js @@ -1,26 +1,26 @@ -const tape = require('tape') +const test = require('brittle') const ram = require('random-access-memory') const Bitfield = require('../lib/bitfield') -tape('bitfield - set and get', async function (t) { +test('bitfield - set and get', async function (t) { const b = await Bitfield.open(ram()) - t.false(b.get(42)) + t.absent(b.get(42)) b.set(42, true) - t.true(b.get(42)) + t.ok(b.get(42)) // bigger offsets - t.false(b.get(42000000)) + t.absent(b.get(42000000)) b.set(42000000, true) - t.true(b.get(42000000)) + t.ok(b.get(42000000)) b.set(42000000, false) - t.false(b.get(42000000)) + t.absent(b.get(42000000)) await b.flush() }) -tape('bitfield - random set and gets', async function (t) { +test('bitfield - random set and gets', async function (t) { const b = await Bitfield.open(ram()) const set = new Set() @@ -51,7 +51,7 @@ tape('bitfield - random set and gets', async function (t) { t.pass('all random set and gets pass') }) -tape('bitfield - reload', async function (t) { +test('bitfield - reload', async function (t) { const s = ram() { @@ -64,8 +64,8 @@ tape('bitfield - reload', async function (t) { { const b = await Bitfield.open(s) - t.true(b.get(142)) - t.true(b.get(40000)) - t.true(b.get(1424242424)) + t.ok(b.get(142)) + t.ok(b.get(40000)) + t.ok(b.get(1424242424)) } }) diff --git a/test/core.js b/test/core.js index 45acd2a7..e8a9694c 100644 --- a/test/core.js +++ b/test/core.js @@ -1,8 +1,8 @@ -const tape = require('tape') +const test = require('brittle') const RAM = require('random-access-memory') const Core = require('../lib/core') -tape('core - append', async function (t) { +test('core - append', async function (t) { const { core } = await create() { @@ -11,10 +11,10 @@ tape('core - append', async function (t) { Buffer.from('world') ]) - t.same(seq, 0) - t.same(core.tree.length, 2) - t.same(core.tree.byteLength, 10) - t.same([ + t.is(seq, 0) + t.is(core.tree.length, 2) + t.is(core.tree.byteLength, 10) + t.alike([ await core.blocks.get(0), await core.blocks.get(1) ], [ @@ -28,10 +28,10 @@ tape('core - append', async function (t) { Buffer.from('hej') ]) - t.same(seq, 2) - t.same(core.tree.length, 3) - t.same(core.tree.byteLength, 13) - t.same([ + t.is(seq, 2) + t.is(core.tree.length, 3) + t.is(core.tree.byteLength, 13) + t.alike([ await core.blocks.get(0), await core.blocks.get(1), await core.blocks.get(2) @@ -43,7 +43,7 @@ tape('core - append', async function (t) { } }) -tape('core - append and truncate', async function (t) { +test('core - append and truncate', async function (t) { const { core, reopen } = await create() await core.append([ @@ -55,10 +55,10 @@ tape('core - append and truncate', async function (t) { await core.truncate(3, 1) - t.same(core.tree.length, 3) - t.same(core.tree.byteLength, 12) - t.same(core.tree.fork, 1) - t.same(core.header.hints.reorgs, [{ from: 0, to: 1, ancestors: 3 }]) + t.is(core.tree.length, 3) + t.is(core.tree.byteLength, 12) + t.is(core.tree.fork, 1) + t.alike(core.header.hints.reorgs, [{ from: 0, to: 1, ancestors: 3 }]) await core.append([ Buffer.from('a'), @@ -69,14 +69,14 @@ tape('core - append and truncate', async function (t) { await core.truncate(3, 2) - t.same(core.tree.length, 3) - t.same(core.tree.byteLength, 12) - t.same(core.tree.fork, 2) - t.same(core.header.hints.reorgs, [{ from: 0, to: 1, ancestors: 3 }, { from: 1, to: 2, ancestors: 3 }]) + t.is(core.tree.length, 3) + t.is(core.tree.byteLength, 12) + t.is(core.tree.fork, 2) + t.alike(core.header.hints.reorgs, [{ from: 0, to: 1, ancestors: 3 }, { from: 1, to: 2, ancestors: 3 }]) await core.truncate(2, 3) - t.same(core.header.hints.reorgs, [{ from: 2, to: 3, ancestors: 2 }]) + t.alike(core.header.hints.reorgs, [{ from: 2, to: 3, ancestors: 2 }]) await core.append([Buffer.from('a')]) await core.truncate(2, 4) @@ -90,46 +90,46 @@ tape('core - append and truncate', async function (t) { await core.append([Buffer.from('a')]) await core.truncate(2, 7) - t.same(core.header.hints.reorgs.length, 4) + t.is(core.header.hints.reorgs.length, 4) // check that it was persisted const coreReopen = await reopen() - t.same(coreReopen.tree.length, 2) - t.same(coreReopen.tree.byteLength, 10) - t.same(coreReopen.tree.fork, 7) - t.same(coreReopen.header.hints.reorgs.length, 4) + t.is(coreReopen.tree.length, 2) + t.is(coreReopen.tree.byteLength, 10) + t.is(coreReopen.tree.fork, 7) + t.is(coreReopen.header.hints.reorgs.length, 4) }) -tape('core - user data', async function (t) { +test('core - user data', async function (t) { const { core, reopen } = await create() await core.userData('hello', Buffer.from('world')) - t.same(core.header.userData, [{ key: 'hello', value: Buffer.from('world') }]) + t.alike(core.header.userData, [{ key: 'hello', value: Buffer.from('world') }]) await core.userData('hej', Buffer.from('verden')) - t.same(core.header.userData, [ + t.alike(core.header.userData, [ { key: 'hello', value: Buffer.from('world') }, { key: 'hej', value: Buffer.from('verden') } ]) await core.userData('hello', null) - t.same(core.header.userData, [{ key: 'hej', value: Buffer.from('verden') }]) + t.alike(core.header.userData, [{ key: 'hej', value: Buffer.from('verden') }]) await core.userData('hej', Buffer.from('world')) - t.same(core.header.userData, [{ key: 'hej', value: Buffer.from('world') }]) + t.alike(core.header.userData, [{ key: 'hej', value: Buffer.from('world') }]) // check that it was persisted const coreReopen = await reopen() - t.same(coreReopen.header.userData, [{ key: 'hej', value: Buffer.from('world') }]) + t.alike(coreReopen.header.userData, [{ key: 'hej', value: Buffer.from('world') }]) }) -tape('core - verify', async function (t) { +test('core - verify', async function (t) { const { core } = await create() const { core: clone } = await create({ keyPair: { publicKey: core.header.signer.publicKey } }) - t.same(clone.header.signer.publicKey, core.header.signer.publicKey) + t.is(clone.header.signer.publicKey, core.header.signer.publicKey) await core.append([Buffer.from('a'), Buffer.from('b')]) @@ -138,8 +138,8 @@ tape('core - verify', async function (t) { await clone.verify(p) } - t.same(clone.header.tree.length, 2) - t.same(clone.header.tree.signature, core.header.tree.signature) + t.is(clone.header.tree.length, 2) + t.is(clone.header.tree.signature, core.header.tree.signature) { const p = await core.tree.proof({ block: { index: 1, nodes: await clone.tree.nodes(2), value: true } }) @@ -148,11 +148,11 @@ tape('core - verify', async function (t) { } }) -tape('core - verify parallel upgrades', async function (t) { +test('core - verify parallel upgrades', async function (t) { const { core } = await create() const { core: clone } = await create({ keyPair: { publicKey: core.header.signer.publicKey } }) - t.same(clone.header.signer.publicKey, core.header.signer.publicKey) + t.is(clone.header.signer.publicKey, core.header.signer.publicKey) await core.append([Buffer.from('a'), Buffer.from('b'), Buffer.from('c'), Buffer.from('d')]) @@ -167,20 +167,20 @@ tape('core - verify parallel upgrades', async function (t) { await v2 } - t.same(clone.header.tree.length, core.header.tree.length) - t.same(clone.header.tree.signature, core.header.tree.signature) + t.is(clone.header.tree.length, core.header.tree.length) + t.is(clone.header.tree.signature, core.header.tree.signature) }) -tape('core - update hook is triggered', async function (t) { +test('core - update hook is triggered', async function (t) { const { core } = await create() const { core: clone } = await create({ keyPair: { publicKey: core.header.signer.publicKey } }) let ran = 0 core.onupdate = (status, bitfield, value, from) => { - t.same(status, 0b01, 'was appended') - t.same(from, null, 'was local') - t.same(bitfield, { drop: false, start: 0, length: 4 }) + t.is(status, 0b01, 'was appended') + t.is(from, null, 'was local') + t.alike(bitfield, { drop: false, start: 0, length: 4 }) ran |= 1 } @@ -189,10 +189,10 @@ tape('core - update hook is triggered', async function (t) { const peer = {} clone.onupdate = (status, bitfield, value, from) => { - t.same(status, 0b01, 'was appended') - t.same(from, peer, 'was remote') - t.same(bitfield, { drop: false, start: 1, length: 1 }) - t.same(value, Buffer.from('b')) + t.is(status, 0b01, 'was appended') + t.is(from, peer, 'was remote') + t.alike(bitfield, { drop: false, start: 1, length: 1 }) + t.alike(value, Buffer.from('b')) ran |= 2 } @@ -203,10 +203,10 @@ tape('core - update hook is triggered', async function (t) { } clone.onupdate = (status, bitfield, value, from) => { - t.same(status, 0b00, 'no append or truncate') - t.same(from, peer, 'was remote') - t.same(bitfield, { drop: false, start: 3, length: 1 }) - t.same(value, Buffer.from('d')) + t.is(status, 0b00, 'no append or truncate') + t.is(from, peer, 'was remote') + t.alike(bitfield, { drop: false, start: 3, length: 1 }) + t.alike(value, Buffer.from('d')) ran |= 4 } @@ -217,27 +217,27 @@ tape('core - update hook is triggered', async function (t) { } core.onupdate = (status, bitfield, value, from) => { - t.same(status, 0b10, 'was truncated') - t.same(from, null, 'was local') - t.same(bitfield, { drop: true, start: 1, length: 3 }) + t.is(status, 0b10, 'was truncated') + t.is(from, null, 'was local') + t.alike(bitfield, { drop: true, start: 1, length: 3 }) ran |= 8 } await core.truncate(1, 1) core.onupdate = (status, bitfield, value, from) => { - t.same(status, 0b01, 'was appended') - t.same(from, null, 'was local') - t.same(bitfield, { drop: false, start: 1, length: 1 }) + t.is(status, 0b01, 'was appended') + t.is(from, null, 'was local') + t.alike(bitfield, { drop: false, start: 1, length: 1 }) ran |= 16 } await core.append([Buffer.from('e')]) clone.onupdate = (status, bitfield, value, from) => { - t.same(status, 0b11, 'was appended and truncated') - t.same(from, peer, 'was remote') - t.same(bitfield, { drop: true, start: 1, length: 3 }) + t.is(status, 0b11, 'was appended and truncated') + t.is(from, peer, 'was remote') + t.alike(bitfield, { drop: true, start: 1, length: 3 }) ran |= 32 } @@ -249,18 +249,18 @@ tape('core - update hook is triggered', async function (t) { } core.onupdate = (status, bitfield, value, from) => { - t.same(status, 0b10, 'was truncated') - t.same(from, null, 'was local') - t.same(bitfield, { drop: true, start: 1, length: 1 }) + t.is(status, 0b10, 'was truncated') + t.is(from, null, 'was local') + t.alike(bitfield, { drop: true, start: 1, length: 1 }) ran |= 64 } await core.truncate(1, 2) clone.onupdate = (status, bitfield, value, from) => { - t.same(status, 0b10, 'was truncated') - t.same(from, peer, 'was remote') - t.same(bitfield, { drop: true, start: 1, length: 1 }) + t.is(status, 0b10, 'was truncated') + t.is(from, peer, 'was remote') + t.alike(bitfield, { drop: true, start: 1, length: 1 }) ran |= 128 } @@ -271,7 +271,7 @@ tape('core - update hook is triggered', async function (t) { await clone.reorg(r, peer) } - t.same(ran, 255, 'ran all') + t.is(ran, 255, 'ran all') }) async function create (opts) { diff --git a/test/encodings.js b/test/encodings.js index 1e6ca9d8..96287db0 100644 --- a/test/encodings.js +++ b/test/encodings.js @@ -1,18 +1,18 @@ -const tape = require('tape') +const test = require('brittle') const { create } = require('./helpers') -tape('encodings - supports built ins', async function (t) { +test('encodings - supports built ins', async function (t) { const a = await create(null, { valueEncoding: 'json' }) await a.append({ hello: 'world' }) - t.same(await a.get(0), { hello: 'world' }) - t.same(await a.get(0, { valueEncoding: 'utf-8' }), '{"hello":"world"}') + t.alike(await a.get(0), { hello: 'world' }) + t.alike(await a.get(0, { valueEncoding: 'utf-8' }), '{"hello":"world"}') }) -tape('encodings - supports custom encoding', async function (t) { +test('encodings - supports custom encoding', async function (t) { const a = await create(null, { valueEncoding: { encode () { return Buffer.from('foo') }, decode () { return 'bar' } } }) await a.append({ hello: 'world' }) - t.same(await a.get(0), 'bar') - t.same(await a.get(0, { valueEncoding: 'utf-8' }), 'foo') + t.is(await a.get(0), 'bar') + t.alike(await a.get(0, { valueEncoding: 'utf-8' }), 'foo') }) diff --git a/test/extension.js b/test/extension.js index f60fb13e..d6a32c95 100644 --- a/test/extension.js +++ b/test/extension.js @@ -1,7 +1,7 @@ -const tape = require('tape') +const test = require('brittle') const { create, replicate } = require('./helpers') -tape('basic extension', async function (t) { +test('basic extension', async function (t) { const messages = ['world', 'hello'] const a = await create() @@ -9,7 +9,7 @@ tape('basic extension', async function (t) { encoding: 'utf-8', onmessage: (message, peer) => { t.ok(peer === a.peers[0]) - t.same(message, messages.pop()) + t.is(message, messages.pop()) } }) @@ -21,18 +21,18 @@ tape('basic extension', async function (t) { replicate(a, b) await new Promise(resolve => setImmediate(resolve)) - t.same(b.peers.length, 1) + t.is(b.peers.length, 1) bExt.send('hello', b.peers[0]) bExt.send('world', b.peers[0]) await new Promise(resolve => setImmediate(resolve)) - t.false(messages.length) + t.absent(messages.length) t.end() }) -tape('two extensions', async function (t) { +test('two extensions', async function (t) { const messages = ['world', 'hello'] const a = await create() @@ -48,7 +48,7 @@ tape('two extensions', async function (t) { }) await new Promise(resolve => setImmediate(resolve)) - t.same(b.peers.length, 1) + t.is(b.peers.length, 1) bExt2.send('world', b.peers[0]) @@ -58,14 +58,14 @@ tape('two extensions', async function (t) { encoding: 'utf-8', onmessage: (message, peer) => { t.ok(peer === a.peers[0]) - t.same(message, messages.pop()) + t.is(message, messages.pop()) } }) bExt2.send('hello', b.peers[0]) await new Promise(resolve => setImmediate(resolve)) - t.same(messages.length, 1) // First message gets ignored + t.is(messages.length, 1) // First message gets ignored t.end() }) diff --git a/test/merkle-tree.js b/test/merkle-tree.js index e8de0b95..e6443b31 100644 --- a/test/merkle-tree.js +++ b/test/merkle-tree.js @@ -1,8 +1,8 @@ -const tape = require('tape') +const test = require('brittle') const Tree = require('../lib/merkle-tree') const ram = require('random-access-memory') -tape('nodes', async function (t) { +test('nodes', async function (t) { const tree = await create() const b = tree.batch() @@ -13,27 +13,27 @@ tape('nodes', async function (t) { b.commit() - t.same(await tree.nodes(0), 0) + t.is(await tree.nodes(0), 0) t.end() }) -tape('proof only block', async function (t) { +test('proof only block', async function (t) { const tree = await create(10) const proof = await tree.proof({ block: { index: 4, nodes: 2, value: true } }) - t.same(proof.upgrade, null) - t.same(proof.seek, null) - t.same(proof.block.index, 4) - t.same(proof.block.nodes.length, 2) - t.same(proof.block.nodes.map(n => n.index), [10, 13]) + t.is(proof.upgrade, null) + t.is(proof.seek, null) + t.is(proof.block.index, 4) + t.is(proof.block.nodes.length, 2) + t.alike(proof.block.nodes.map(n => n.index), [10, 13]) t.end() }) -tape('proof with upgrade', async function (t) { +test('proof with upgrade', async function (t) { const tree = await create(10) const proof = await tree.proof({ @@ -41,19 +41,19 @@ tape('proof with upgrade', async function (t) { upgrade: { start: 0, length: 10 } }) - t.same(proof.seek, null) - t.same(proof.block.index, 4) - t.same(proof.block.nodes.length, 3) - t.same(proof.block.nodes.map(n => n.index), [10, 13, 3]) - t.same(proof.upgrade.start, 0) - t.same(proof.upgrade.length, 10) - t.same(proof.upgrade.nodes.map(n => n.index), [17]) - t.same(proof.upgrade.additionalNodes.map(n => n.index), []) + t.is(proof.seek, null) + t.is(proof.block.index, 4) + t.is(proof.block.nodes.length, 3) + t.alike(proof.block.nodes.map(n => n.index), [10, 13, 3]) + t.is(proof.upgrade.start, 0) + t.is(proof.upgrade.length, 10) + t.alike(proof.upgrade.nodes.map(n => n.index), [17]) + t.alike(proof.upgrade.additionalNodes.map(n => n.index), []) t.end() }) -tape('proof with upgrade + additional', async function (t) { +test('proof with upgrade + additional', async function (t) { const tree = await create(10) const proof = await tree.proof({ @@ -61,19 +61,19 @@ tape('proof with upgrade + additional', async function (t) { upgrade: { start: 0, length: 8 } }) - t.same(proof.seek, null) - t.same(proof.block.index, 4) - t.same(proof.block.nodes.length, 3) - t.same(proof.block.nodes.map(n => n.index), [10, 13, 3]) - t.same(proof.upgrade.start, 0) - t.same(proof.upgrade.length, 8) - t.same(proof.upgrade.nodes.map(n => n.index), []) - t.same(proof.upgrade.additionalNodes.map(n => n.index), [17]) + t.is(proof.seek, null) + t.is(proof.block.index, 4) + t.is(proof.block.nodes.length, 3) + t.alike(proof.block.nodes.map(n => n.index), [10, 13, 3]) + t.is(proof.upgrade.start, 0) + t.is(proof.upgrade.length, 8) + t.alike(proof.upgrade.nodes.map(n => n.index), []) + t.alike(proof.upgrade.additionalNodes.map(n => n.index), [17]) t.end() }) -tape('proof with upgrade from existing state', async function (t) { +test('proof with upgrade from existing state', async function (t) { const tree = await create(10) const proof = await tree.proof({ @@ -81,19 +81,19 @@ tape('proof with upgrade from existing state', async function (t) { upgrade: { start: 1, length: 9 } }) - t.same(proof.seek, null) - t.same(proof.block.index, 1) - t.same(proof.block.nodes.length, 0) - t.same(proof.block.nodes.map(n => n.index), []) - t.same(proof.upgrade.start, 1) - t.same(proof.upgrade.length, 9) - t.same(proof.upgrade.nodes.map(n => n.index), [5, 11, 17]) - t.same(proof.upgrade.additionalNodes.map(n => n.index), []) + t.is(proof.seek, null) + t.is(proof.block.index, 1) + t.is(proof.block.nodes.length, 0) + t.alike(proof.block.nodes.map(n => n.index), []) + t.is(proof.upgrade.start, 1) + t.is(proof.upgrade.length, 9) + t.alike(proof.upgrade.nodes.map(n => n.index), [5, 11, 17]) + t.alike(proof.upgrade.additionalNodes.map(n => n.index), []) t.end() }) -tape('proof with upgrade from existing state + additional', async function (t) { +test('proof with upgrade from existing state + additional', async function (t) { const tree = await create(10) const proof = await tree.proof({ @@ -101,19 +101,19 @@ tape('proof with upgrade from existing state + additional', async function (t) { upgrade: { start: 1, length: 5 } }) - t.same(proof.seek, null) - t.same(proof.block.index, 1) - t.same(proof.block.nodes.length, 0) - t.same(proof.block.nodes.map(n => n.index), []) - t.same(proof.upgrade.start, 1) - t.same(proof.upgrade.length, 5) - t.same(proof.upgrade.nodes.map(n => n.index), [5, 9]) - t.same(proof.upgrade.additionalNodes.map(n => n.index), [13, 17]) + t.is(proof.seek, null) + t.is(proof.block.index, 1) + t.is(proof.block.nodes.length, 0) + t.alike(proof.block.nodes.map(n => n.index), []) + t.is(proof.upgrade.start, 1) + t.is(proof.upgrade.length, 5) + t.alike(proof.upgrade.nodes.map(n => n.index), [5, 9]) + t.alike(proof.upgrade.additionalNodes.map(n => n.index), [13, 17]) t.end() }) -tape('proof block and seek, no upgrade', async function (t) { +test('proof block and seek, no upgrade', async function (t) { const tree = await create(10) const proof = await tree.proof({ @@ -121,16 +121,16 @@ tape('proof block and seek, no upgrade', async function (t) { block: { index: 4, nodes: 2, value: true } }) - t.same(proof.upgrade, null) - t.same(proof.seek, null) // seek included in the block - t.same(proof.block.index, 4) - t.same(proof.block.nodes.length, 2) - t.same(proof.block.nodes.map(n => n.index), [10, 13]) + t.is(proof.upgrade, null) + t.is(proof.seek, null) // seek included in the block + t.is(proof.block.index, 4) + t.is(proof.block.nodes.length, 2) + t.alike(proof.block.nodes.map(n => n.index), [10, 13]) t.end() }) -tape('proof block and seek #2, no upgrade', async function (t) { +test('proof block and seek #2, no upgrade', async function (t) { const tree = await create(10) const proof = await tree.proof({ @@ -138,16 +138,16 @@ tape('proof block and seek #2, no upgrade', async function (t) { block: { index: 4, nodes: 2, value: true } }) - t.same(proof.upgrade, null) - t.same(proof.seek, null) // seek included in the block - t.same(proof.block.index, 4) - t.same(proof.block.nodes.length, 2) - t.same(proof.block.nodes.map(n => n.index), [10, 13]) + t.is(proof.upgrade, null) + t.is(proof.seek, null) // seek included in the block + t.is(proof.block.index, 4) + t.is(proof.block.nodes.length, 2) + t.alike(proof.block.nodes.map(n => n.index), [10, 13]) t.end() }) -tape('proof block and seek #3, no upgrade', async function (t) { +test('proof block and seek #3, no upgrade', async function (t) { const tree = await create(10) const proof = await tree.proof({ @@ -155,16 +155,16 @@ tape('proof block and seek #3, no upgrade', async function (t) { block: { index: 4, nodes: 2, value: true } }) - t.same(proof.upgrade, null) - t.same(proof.seek.nodes.map(n => n.index), [12, 14]) - t.same(proof.block.index, 4) - t.same(proof.block.nodes.length, 1) - t.same(proof.block.nodes.map(n => n.index), [10]) + t.is(proof.upgrade, null) + t.alike(proof.seek.nodes.map(n => n.index), [12, 14]) + t.is(proof.block.index, 4) + t.is(proof.block.nodes.length, 1) + t.alike(proof.block.nodes.map(n => n.index), [10]) t.end() }) -tape('proof block and seek that results in tree, no upgrade', async function (t) { +test('proof block and seek that results in tree, no upgrade', async function (t) { const tree = await create(16) const proof = await tree.proof({ @@ -172,14 +172,14 @@ tape('proof block and seek that results in tree, no upgrade', async function (t) block: { index: 0, nodes: 4, value: true } }) - t.same(proof.upgrade, null) - t.same(proof.block.nodes.map(n => n.index), [2, 5, 11]) - t.same(proof.seek.nodes.map(n => n.index), [19, 27]) + t.is(proof.upgrade, null) + t.alike(proof.block.nodes.map(n => n.index), [2, 5, 11]) + t.alike(proof.seek.nodes.map(n => n.index), [19, 27]) t.end() }) -tape('proof block and seek, with upgrade', async function (t) { +test('proof block and seek, with upgrade', async function (t) { const tree = await create(10) const proof = await tree.proof({ @@ -188,19 +188,19 @@ tape('proof block and seek, with upgrade', async function (t) { upgrade: { start: 8, length: 2 } }) - t.same(proof.seek.nodes.map(n => n.index), [12, 14]) - t.same(proof.block.index, 4) - t.same(proof.block.nodes.length, 1) - t.same(proof.block.nodes.map(n => n.index), [10]) - t.same(proof.upgrade.start, 8) - t.same(proof.upgrade.length, 2) - t.same(proof.upgrade.nodes.map(n => n.index), [17]) - t.same(proof.upgrade.additionalNodes.map(n => n.index), []) + t.alike(proof.seek.nodes.map(n => n.index), [12, 14]) + t.is(proof.block.index, 4) + t.is(proof.block.nodes.length, 1) + t.alike(proof.block.nodes.map(n => n.index), [10]) + t.is(proof.upgrade.start, 8) + t.is(proof.upgrade.length, 2) + t.alike(proof.upgrade.nodes.map(n => n.index), [17]) + t.alike(proof.upgrade.additionalNodes.map(n => n.index), []) t.end() }) -tape('proof seek with upgrade', async function (t) { +test('proof seek with upgrade', async function (t) { const tree = await create(10) const proof = await tree.proof({ @@ -208,17 +208,17 @@ tape('proof seek with upgrade', async function (t) { upgrade: { start: 0, length: 10 } }) - t.same(proof.seek.nodes.map(n => n.index), [12, 14, 9, 3]) - t.same(proof.block, null) - t.same(proof.upgrade.start, 0) - t.same(proof.upgrade.length, 10) - t.same(proof.upgrade.nodes.map(n => n.index), [17]) - t.same(proof.upgrade.additionalNodes.map(n => n.index), []) + t.alike(proof.seek.nodes.map(n => n.index), [12, 14, 9, 3]) + t.is(proof.block, null) + t.is(proof.upgrade.start, 0) + t.is(proof.upgrade.length, 10) + t.alike(proof.upgrade.nodes.map(n => n.index), [17]) + t.alike(proof.upgrade.additionalNodes.map(n => n.index), []) t.end() }) -tape('verify proof #1', async function (t) { +test('verify proof #1', async function (t) { const tree = await create(10) const clone = await create() @@ -230,15 +230,15 @@ tape('verify proof #1', async function (t) { const b = await clone.verify(p) b.commit() - t.same(clone.length, tree.length) - t.same(clone.byteLength, tree.byteLength) - t.same(await clone.byteOffset(6), await tree.byteOffset(6)) - t.same(await clone.get(6), await tree.get(6)) + t.is(clone.length, tree.length) + t.is(clone.byteLength, tree.byteLength) + t.is(await clone.byteOffset(6), await tree.byteOffset(6)) + t.is(await clone.get(6), await tree.get(6)) t.end() }) -tape('verify proof #2', async function (t) { +test('verify proof #2', async function (t) { const tree = await create(10) const clone = await create() @@ -250,14 +250,14 @@ tape('verify proof #2', async function (t) { const b = await clone.verify(p) b.commit() - t.same(clone.length, tree.length) - t.same(clone.byteLength, tree.byteLength) - t.same(await clone.byteRange(10), await tree.byteRange(10)) + t.is(clone.length, tree.length) + t.is(clone.byteLength, tree.byteLength) + t.alike(await clone.byteRange(10), await tree.byteRange(10)) t.end() }) -tape('upgrade edgecase when no roots need upgrade', async function (t) { +test('upgrade edgecase when no roots need upgrade', async function (t) { const tree = await create(4) const clone = await create() @@ -283,41 +283,41 @@ tape('upgrade edgecase when no roots need upgrade', async function (t) { b.commit() } - t.same(tree.length, 5) + t.is(tree.length, 5) t.end() }) -tape('lowest common ancestor - small gap', async function (t) { +test('lowest common ancestor - small gap', async function (t) { const tree = await create(10) const clone = await create(8) const ancestors = await reorg(clone, tree) - t.same(ancestors, 8) - t.same(clone.length, tree.length) + t.is(ancestors, 8) + t.is(clone.length, tree.length) t.end() }) -tape('lowest common ancestor - bigger gap', async function (t) { +test('lowest common ancestor - bigger gap', async function (t) { const tree = await create(20) const clone = await create(1) const ancestors = await reorg(clone, tree) - t.same(ancestors, 1) - t.same(clone.length, tree.length) + t.is(ancestors, 1) + t.is(clone.length, tree.length) t.end() }) -tape('lowest common ancestor - remote is shorter than local', async function (t) { +test('lowest common ancestor - remote is shorter than local', async function (t) { const tree = await create(5) const clone = await create(10) const ancestors = await reorg(clone, tree) - t.same(ancestors, 5) - t.same(clone.length, tree.length) + t.is(ancestors, 5) + t.is(clone.length, tree.length) t.end() }) -tape('lowest common ancestor - simple fork', async function (t) { +test('lowest common ancestor - simple fork', async function (t) { const tree = await create(5) const clone = await create(5) @@ -335,12 +335,12 @@ tape('lowest common ancestor - simple fork', async function (t) { const ancestors = await reorg(clone, tree) - t.same(ancestors, 5) - t.same(clone.length, tree.length) + t.is(ancestors, 5) + t.is(clone.length, tree.length) t.end() }) -tape('lowest common ancestor - long fork', async function (t) { +test('lowest common ancestor - long fork', async function (t) { const tree = await create(5) const clone = await create(5) @@ -370,8 +370,8 @@ tape('lowest common ancestor - long fork', async function (t) { const ancestors = await reorg(clone, tree) - t.same(ancestors, 5) - t.same(clone.length, tree.length) + t.is(ancestors, 5) + t.is(clone.length, tree.length) t.ok(await audit(tree)) await tree.flush() @@ -380,36 +380,36 @@ tape('lowest common ancestor - long fork', async function (t) { t.end() }) -tape('tree hash', async function (t) { +test('tree hash', async function (t) { const a = await create(5) const b = await create(5) - t.same(a.hash(), b.hash()) + t.alike(a.hash(), b.hash()) { const b = a.batch() - t.same(b.hash(), a.hash()) + t.alike(b.hash(), a.hash()) b.append(Buffer.from('hi')) const h = b.hash() - t.notEqual(h, a.hash()) + t.unlike(h, a.hash()) b.commit() - t.same(h, a.hash()) + t.alike(h, a.hash()) } { const ba = b.batch() ba.append(Buffer.from('hi')) const h = ba.hash() - t.notEqual(h, b.hash()) - t.same(h, a.hash()) + t.unlike(h, b.hash()) + t.alike(h, a.hash()) ba.commit() - t.same(h, b.hash()) + t.alike(h, b.hash()) } t.end() }) -tape('basic tree seeks', async function (t) { +test('basic tree seeks', async function (t) { const a = await create(5) { @@ -422,8 +422,8 @@ tape('basic tree seeks', async function (t) { b.commit() } - t.same(a.length, 10) - t.same(a.byteLength, 33) + t.is(a.length, 10) + t.is(a.byteLength, 33) for (let i = 0; i < a.byteLength; i++) { const s = a.seek(i) @@ -432,7 +432,7 @@ tape('basic tree seeks', async function (t) { const expected = await linearSeek(a, i) if (actual[0] !== expected[0] || actual[1] !== expected[1]) { - t.same(actual, expected, 'bad seek at ' + i) + t.is(actual, expected, 'bad seek at ' + i) return } } @@ -449,14 +449,14 @@ tape('basic tree seeks', async function (t) { } }) -tape('clear full tree', async function (t) { +test('clear full tree', async function (t) { const a = await create(5) - t.same(a.length, 5) + t.is(a.length, 5) await a.clear() - t.same(a.length, 0) + t.is(a.length, 0) try { await a.get(2) diff --git a/test/mutex.js b/test/mutex.js index e52e304e..802220f0 100644 --- a/test/mutex.js +++ b/test/mutex.js @@ -1,7 +1,7 @@ -const tape = require('tape') +const test = require('brittle') const Mutex = require('../lib/mutex') -tape('mutex - lock after destroy', async function (t) { +test('mutex - lock after destroy', async function (t) { const mutex = new Mutex() mutex.destroy() try { @@ -12,7 +12,7 @@ tape('mutex - lock after destroy', async function (t) { } }) -tape('mutex - graceful destroy', async function (t) { +test('mutex - graceful destroy', async function (t) { t.plan(1) const mutex = new Mutex() @@ -29,10 +29,10 @@ tape('mutex - graceful destroy', async function (t) { await destroyed - t.same(resolveCount, 5) + t.is(resolveCount, 5) }) -tape('mutex - quick destroy', async function (t) { +test('mutex - quick destroy', async function (t) { t.plan(2) const mutex = new Mutex() @@ -50,11 +50,11 @@ tape('mutex - quick destroy', async function (t) { await destroyed - t.same(resolveCount, 1) - t.same(rejectCount, 4) + t.is(resolveCount, 1) + t.is(rejectCount, 4) }) -tape('mutex - graceful then quick destroy', async function (t) { +test('mutex - graceful then quick destroy', async function (t) { t.plan(2) const mutex = new Mutex() @@ -73,11 +73,11 @@ tape('mutex - graceful then quick destroy', async function (t) { await destroyed - t.same(resolveCount, 1) - t.same(rejectCount, 4) + t.is(resolveCount, 1) + t.is(rejectCount, 4) }) -tape('mutex - quick destroy with re-entry', async function (t) { +test('mutex - quick destroy with re-entry', async function (t) { t.plan(2) const mutex = new Mutex() @@ -95,8 +95,8 @@ tape('mutex - quick destroy with re-entry', async function (t) { await destroyed - t.same(resolveCount, 1) - t.same(rejectCount, 4) + t.is(resolveCount, 1) + t.is(rejectCount, 4) async function lock () { try { @@ -113,7 +113,7 @@ tape('mutex - quick destroy with re-entry', async function (t) { } }) -tape('mutex - error propagates', async function (t) { +test('mutex - error propagates', async function (t) { const mutex = new Mutex() let resolveCount = 0 @@ -132,6 +132,6 @@ tape('mutex - error propagates', async function (t) { t.ok(e === err) } - t.same(resolveCount, 1) - t.same(rejectErrors, [err, err, err, err]) + t.is(resolveCount, 1) + t.alike(rejectErrors, [err, err, err, err]) }) diff --git a/test/oplog.js b/test/oplog.js index e992b9d6..b8eefc6b 100644 --- a/test/oplog.js +++ b/test/oplog.js @@ -1,6 +1,6 @@ const p = require('path') const fs = require('fs') -const test = require('tape') +const test = require('brittle') const fsctl = require('fsctl') const raf = require('random-access-file') const c = require('compact-encoding') @@ -33,10 +33,10 @@ test('oplog - basic append', async function (t) { { const { header, entries } = await logRd.open() - t.same(header, Buffer.from('h')) - t.same(entries.length, 2) - t.same(entries[0], Buffer.from('a')) - t.same(entries[1], Buffer.from('b')) + t.alike(header, Buffer.from('h')) + t.is(entries.length, 2) + t.alike(entries[0], Buffer.from('a')) + t.alike(entries[1], Buffer.from('b')) } await logWr.flush(Buffer.from('i')) @@ -44,8 +44,8 @@ test('oplog - basic append', async function (t) { { const { header, entries } = await logRd.open() - t.same(header, Buffer.from('i')) - t.same(entries.length, 0) + t.alike(header, Buffer.from('i')) + t.is(entries.length, 0) } await logWr.append(Buffer.from('c')) @@ -53,9 +53,9 @@ test('oplog - basic append', async function (t) { { const { header, entries } = await logRd.open() - t.same(header, Buffer.from('i')) - t.same(entries.length, 1) - t.same(entries[0], Buffer.from('c')) + t.alike(header, Buffer.from('i')) + t.is(entries.length, 1) + t.alike(entries[0], Buffer.from('c')) } await cleanup(storage) @@ -77,10 +77,10 @@ test('oplog - custom encoding', async function (t) { const { header, entries } = await log.open() - t.same(header, 'one header') - t.same(entries.length, 2) - t.same(entries[0], 42) - t.same(entries[1], 43) + t.is(header, 'one header') + t.is(entries.length, 2) + t.is(entries[0], 42) + t.is(entries[1], 43) await cleanup(storage) t.end() @@ -97,21 +97,21 @@ test('oplog - alternating header writes', async function (t) { { const { header } = await log.open() - t.same(header, Buffer.from('2')) + t.alike(header, Buffer.from('2')) } await log.flush(Buffer.from('1')) // Should overwrite first header { const { header } = await log.open() - t.same(header, Buffer.from('1')) + t.alike(header, Buffer.from('1')) } await log.flush(Buffer.from('2')) // Should overwrite second header { const { header } = await log.open() - t.same(header, Buffer.from('2')) + t.alike(header, Buffer.from('2')) } await cleanup(storage) @@ -128,21 +128,21 @@ test('oplog - one fully-corrupted header', async function (t) { { const { header } = await log.open() - t.same(header, Buffer.from('header 1')) + t.alike(header, Buffer.from('header 1')) } await log.flush(Buffer.from('header 2')) { const { header } = await log.open() - t.same(header, Buffer.from('header 2')) + t.alike(header, Buffer.from('header 2')) } await log.flush(Buffer.from('header 3')) // should overwrite first header { const { header } = await log.open() - t.same(header, Buffer.from('header 3')) + t.alike(header, Buffer.from('header 3')) } // Corrupt the first header -- second header should win now @@ -155,7 +155,7 @@ test('oplog - one fully-corrupted header', async function (t) { { const { header } = await log.open() - t.same(header, Buffer.from('header 2'), 'one is corrupted or partially written') + t.alike(header, Buffer.from('header 2'), 'one is corrupted or partially written') } await cleanup(storage) @@ -173,7 +173,7 @@ test('oplog - header invalid checksum', async function (t) { { const { header } = await log.open() - t.same(header, Buffer.from('b')) + t.alike(header, Buffer.from('b')) } // Invalidate the first header's checksum -- second header should win now @@ -186,7 +186,7 @@ test('oplog - header invalid checksum', async function (t) { { const { header } = await log.open() - t.same(header, Buffer.from('a')) + t.alike(header, Buffer.from('a')) } // Invalidate the second header's checksum -- the hypercore is now corrupted @@ -233,9 +233,9 @@ test('oplog - malformed log entry gets overwritten', async function (t) { { const { entries } = await log.open() - t.same(entries.length, 2) // The partial entry should not be present - t.same(entries[0], Buffer.from('a')) - t.same(entries[1], Buffer.from('b')) + t.is(entries.length, 2) // The partial entry should not be present + t.alike(entries[0], Buffer.from('a')) + t.alike(entries[1], Buffer.from('b')) } // Write a valid oplog message now @@ -244,10 +244,10 @@ test('oplog - malformed log entry gets overwritten', async function (t) { { const { entries } = await log.open() - t.same(entries.length, 3) // The partial entry should not be present - t.same(entries[0], Buffer.from('a')) - t.same(entries[1], Buffer.from('b')) - t.same(entries[2], Buffer.from('c')) + t.is(entries.length, 3) // The partial entry should not be present + t.alike(entries[0], Buffer.from('a')) + t.alike(entries[1], Buffer.from('b')) + t.alike(entries[2], Buffer.from('c')) } await cleanup(storage) @@ -270,16 +270,16 @@ test('oplog - log not truncated when header write fails', async function (t) { try { await log.flush(Buffer.from('header two')) } catch (err) { - t.true(err.synthetic) + t.ok(err.synthetic) } { const { header, entries } = await log.open() - t.same(header, Buffer.from('header')) - t.same(entries.length, 2) - t.same(entries[0], Buffer.from('a')) - t.same(entries[1], Buffer.from('b')) + t.alike(header, Buffer.from('header')) + t.is(entries.length, 2) + t.alike(entries[0], Buffer.from('a')) + t.alike(entries[1], Buffer.from('b')) } // Re-enable header writes @@ -289,8 +289,8 @@ test('oplog - log not truncated when header write fails', async function (t) { { const { header, entries } = await log.open() - t.same(header, Buffer.from('header two')) - t.same(entries.length, 0) + t.alike(header, Buffer.from('header two')) + t.is(entries.length, 0) } await cleanup(storage) @@ -312,13 +312,13 @@ test('oplog - multi append', async function (t) { Buffer.from('4') ]) - t.same(log.length, 4) - t.same(log.byteLength, 32 + 1 + 2 + 3 + 1) + t.is(log.length, 4) + t.is(log.byteLength, 32 + 1 + 2 + 3 + 1) const { header, entries } = await log.open() - t.same(header, Buffer.from('a')) - t.same(entries, [ + t.alike(header, Buffer.from('a')) + t.alike(entries, [ Buffer.from('1'), Buffer.from('22'), Buffer.from('333'), @@ -345,8 +345,8 @@ test('oplog - multi append is atomic', async function (t) { Buffer.from('4') ]) - t.same(log.length, 5) - t.same(log.byteLength, 40 + 1 + 1 + 2 + 3 + 1) + t.is(log.length, 5) + t.is(log.byteLength, 40 + 1 + 1 + 2 + 3 + 1) // Corrupt the last write, should revert the full batch await new Promise((resolve, reject) => { @@ -358,8 +358,8 @@ test('oplog - multi append is atomic', async function (t) { const { entries } = await log.open() - t.same(log.length, 1) - t.same(entries, [ + t.is(log.length, 1) + t.alike(entries, [ Buffer.from('0') ]) diff --git a/test/preload.js b/test/preload.js index 58b3eca2..1fed1b49 100644 --- a/test/preload.js +++ b/test/preload.js @@ -1,9 +1,9 @@ const crypto = require('hypercore-crypto') -const tape = require('tape') +const test = require('brittle') const ram = require('random-access-memory') const Hypercore = require('../') -tape('preload - storage', async function (t) { +test('preload - storage', async function (t) { const core = new Hypercore(null, { preload: () => { return { storage: ram } @@ -12,13 +12,13 @@ tape('preload - storage', async function (t) { await core.ready() await core.append('hello world') - t.same(core.length, 1) - t.same(await core.get(0), Buffer.from('hello world')) + t.is(core.length, 1) + t.alike(await core.get(0), Buffer.from('hello world')) t.end() }) -tape('preload - from another core', async function (t) { +test('preload - from another core', async function (t) { t.plan(2) const first = new Hypercore(ram) @@ -31,11 +31,11 @@ tape('preload - from another core', async function (t) { }) await second.ready() - t.same(first.key, second.key) - t.same(first.sessions, second.sessions) + t.is(first.key, second.key) + t.is(first.sessions, second.sessions) }) -tape('preload - custom keypair', async function (t) { +test('preload - custom keypair', async function (t) { const keyPair = crypto.keyPair() const core = new Hypercore(ram, keyPair.publicKey, { preload: () => { @@ -44,13 +44,13 @@ tape('preload - custom keypair', async function (t) { }) await core.ready() - t.true(core.writable) - t.same(core.key, keyPair.publicKey) + t.ok(core.writable) + t.is(core.key, keyPair.publicKey) t.end() }) -tape('preload - sign/storage', async function (t) { +test('preload - sign/storage', async function (t) { const keyPair = crypto.keyPair() const core = new Hypercore(null, keyPair.publicKey, { valueEncoding: 'utf-8', @@ -63,10 +63,10 @@ tape('preload - sign/storage', async function (t) { }) await core.ready() - t.true(core.writable) + t.ok(core.writable) await core.append('hello world') - t.same(core.length, 1) - t.same(await core.get(0), 'hello world') + t.is(core.length, 1) + t.is(await core.get(0), 'hello world') t.end() }) diff --git a/test/replicate.js b/test/replicate.js index a029f95b..b4dc858f 100644 --- a/test/replicate.js +++ b/test/replicate.js @@ -1,8 +1,8 @@ -const tape = require('tape') +const test = require('brittle') const NoiseSecretStream = require('noise-secret-stream') const { create, replicate } = require('./helpers') -tape('basic replication', async function (t) { +test('basic replication', async function (t) { const a = await create() await a.append(['a', 'b', 'c', 'd', 'e']) @@ -18,17 +18,17 @@ tape('basic replication', async function (t) { await r.downloaded() - t.same(d, 5) + t.is(d, 5) }) -tape('basic replication from fork', async function (t) { +test('basic replication from fork', async function (t) { const a = await create() await a.append(['a', 'b', 'c', 'd', 'e']) await a.truncate(4) await a.append('e') - t.same(a.fork, 1) + t.is(a.fork, 1) const b = await create(a.key) @@ -41,11 +41,11 @@ tape('basic replication from fork', async function (t) { await r.downloaded() - t.same(d, 5) - t.same(a.fork, b.fork) + t.is(d, 5) + t.is(a.fork, b.fork) }) -tape('eager replication from bigger fork', async function (t) { +test('eager replication from bigger fork', async function (t) { const a = await create() const b = await create(a.key) @@ -55,7 +55,7 @@ tape('eager replication from bigger fork', async function (t) { await a.truncate(4) await a.append(['FORKED', 'g', 'h', 'i', 'j', 'k']) - t.same(a.fork, 1) + t.is(a.fork, 1) let d = 0 b.on('download', (index) => { @@ -66,11 +66,11 @@ tape('eager replication from bigger fork', async function (t) { await r.downloaded() - t.same(d, a.length) - t.same(a.fork, b.fork) + t.is(d, a.length) + t.is(a.fork, b.fork) }) -tape('eager replication of updates per default', async function (t) { +test('eager replication of updates per default', async function (t) { const a = await create() const b = await create(a.key) @@ -87,7 +87,7 @@ tape('eager replication of updates per default', async function (t) { await appended }) -tape('bigger download range', async function (t) { +test('bigger download range', async function (t) { const a = await create() const b = await create(a.key) @@ -104,13 +104,13 @@ tape('bigger download range', async function (t) { const r = b.download({ start: 0, end: a.length }) await r.downloaded() - t.same(b.length, a.length, 'same length') - t.same(downloaded.size, a.length, 'downloaded all') + t.is(b.length, a.length, 'same length') + t.is(downloaded.size, a.length, 'downloaded all') t.end() }) -tape('high latency reorg', async function (t) { +test('high latency reorg', async function (t) { const a = await create() const b = await create(a.key) @@ -145,12 +145,12 @@ tape('high latency reorg', async function (t) { if (ba.equals(bb)) same++ } - t.same(a.fork, 1) - t.same(a.fork, b.fork) - t.same(same, 80) + t.is(a.fork, 1) + t.is(a.fork, b.fork) + t.is(same, 80) }) -tape('invalid signature fails', async function (t) { +test('invalid signature fails', async function (t) { t.plan(2) const a = await create() @@ -167,7 +167,7 @@ tape('invalid signature fails', async function (t) { }) s2.on('error', (err) => { - t.same(err.message, 'Remote signature does not match') + t.is(err.message, 'Remote signature does not match') }) return new Promise((resolve) => { @@ -182,17 +182,17 @@ tape('invalid signature fails', async function (t) { }) }) -tape('update with zero length', async function (t) { +test('update with zero length', async function (t) { const a = await create() const b = await create(a.key) replicate(a, b) await b.update() // should not hang - t.same(b.length, 0) + t.is(b.length, 0) }) -tape('basic multiplexing', async function (t) { +test('basic multiplexing', async function (t) { const a1 = await create() const a2 = await create() @@ -205,13 +205,13 @@ tape('basic multiplexing', async function (t) { a.pipe(b).pipe(a) await a1.append('hi') - t.same(await b1.get(0), Buffer.from('hi')) + t.alike(await b1.get(0), Buffer.from('hi')) await a2.append('ho') - t.same(await b2.get(0), Buffer.from('ho')) + t.alike(await b2.get(0), Buffer.from('ho')) }) -tape('async multiplexing', async function (t) { +test('async multiplexing', async function (t) { const a1 = await create() const b1 = await create(a1.key) @@ -232,11 +232,11 @@ tape('async multiplexing', async function (t) { await new Promise(resolve => b2.once('peer-add', resolve)) - t.same(b2.peers.length, 1) - t.same(await b2.get(0), Buffer.from('ho')) + t.is(b2.peers.length, 1) + t.alike(await b2.get(0), Buffer.from('ho')) }) -tape('multiplexing with external noise stream', async function (t) { +test('multiplexing with external noise stream', async function (t) { const a1 = await create() const a2 = await create() @@ -253,13 +253,13 @@ tape('multiplexing with external noise stream', async function (t) { b2.replicate(n2) await a1.append('hi') - t.same(await b1.get(0), Buffer.from('hi')) + t.alike(await b1.get(0), Buffer.from('hi')) await a2.append('ho') - t.same(await b2.get(0), Buffer.from('ho')) + t.alike(await b2.get(0), Buffer.from('ho')) }) -tape('seeking while replicating', async function (t) { +test('seeking while replicating', async function (t) { const a = await create() const b = await create(a.key) @@ -267,10 +267,10 @@ tape('seeking while replicating', async function (t) { await a.append(['hello', 'this', 'is', 'test', 'data']) - t.same(await b.seek(6), [1, 1]) + t.alike(await b.seek(6), [1, 1]) }) -tape('multiplexing multiple times over the same stream', async function (t) { +test('multiplexing multiple times over the same stream', async function (t) { const a1 = await create() await a1.append('hi') @@ -288,9 +288,9 @@ tape('multiplexing multiple times over the same stream', async function (t) { b1.replicate(n2) t.ok(await b1.update(), 'update once') - t.notOk(await a1.update(), 'writer up to date') - t.notOk(await b1.update(), 'update again') + t.absent(await a1.update(), 'writer up to date') + t.absent(await b1.update(), 'update again') - t.same(b1.length, a1.length, 'same length') + t.is(b1.length, a1.length, 'same length') t.end() }) diff --git a/test/sessions.js b/test/sessions.js index 831a02ff..9046d6d6 100644 --- a/test/sessions.js +++ b/test/sessions.js @@ -1,10 +1,10 @@ -const tape = require('tape') +const test = require('brittle') const ram = require('random-access-memory') const crypto = require('hypercore-crypto') const Hypercore = require('../') -tape('sessions - can create writable sessions from a read-only core', async function (t) { +test('sessions - can create writable sessions from a read-only core', async function (t) { t.plan(5) const keyPair = crypto.keyPair() @@ -12,10 +12,10 @@ tape('sessions - can create writable sessions from a read-only core', async func valueEncoding: 'utf-8' }) await core.ready() - t.false(core.writable) + t.absent(core.writable) const session = core.session({ keyPair: { secretKey: keyPair.secretKey } }) - t.true(session.writable) + t.ok(session.writable) try { await core.append('hello') @@ -31,11 +31,11 @@ tape('sessions - can create writable sessions from a read-only core', async func t.fail('session append should not have thrown') } - t.same(core.length, 1) + t.is(core.length, 1) t.end() }) -tape('sessions - writable session with custom sign function', async function (t) { +test('sessions - writable session with custom sign function', async function (t) { t.plan(5) const keyPair = crypto.keyPair() @@ -43,10 +43,10 @@ tape('sessions - writable session with custom sign function', async function (t) valueEncoding: 'utf-8' }) await core.ready() - t.false(core.writable) + t.absent(core.writable) const session = core.session({ sign: signable => crypto.sign(signable, keyPair.secretKey) }) - t.true(session.writable) + t.ok(session.writable) try { await core.append('hello') @@ -62,11 +62,11 @@ tape('sessions - writable session with custom sign function', async function (t) t.fail('session append should not have thrown') } - t.same(core.length, 1) + t.is(core.length, 1) t.end() }) -tape('sessions - writable session with invalid keypair throws', async function (t) { +test('sessions - writable session with invalid keypair throws', async function (t) { t.plan(2) const keyPair1 = crypto.keyPair() @@ -89,7 +89,7 @@ tape('sessions - writable session with invalid keypair throws', async function ( } }) -tape('sessions - auto close', async function (t) { +test('sessions - auto close', async function (t) { const core = new Hypercore(ram, { autoClose: true }) let closed = false @@ -101,13 +101,13 @@ tape('sessions - auto close', async function (t) { const b = core.session() await a.close() - t.notOk(closed, 'not closed yet') + t.absent(closed, 'not closed yet') await b.close() t.ok(closed, 'all closed') }) -tape('sessions - auto close different order', async function (t) { +test('sessions - auto close different order', async function (t) { const core = new Hypercore(ram, { autoClose: true }) const a = core.session() @@ -119,13 +119,13 @@ tape('sessions - auto close different order', async function (t) { }) await core.close() - t.notOk(closed, 'not closed yet') + t.absent(closed, 'not closed yet') await b.close() t.ok(closed, 'all closed') }) -tape('sessions - auto close with all closing', async function (t) { +test('sessions - auto close with all closing', async function (t) { const core = new Hypercore(ram, { autoClose: true }) const a = core.session() @@ -137,10 +137,10 @@ tape('sessions - auto close with all closing', async function (t) { core.on('close', () => closed++) await Promise.all([core.close(), a.close(), b.close()]) - t.same(closed, 3, 'all closed') + t.is(closed, 3, 'all closed') }) -tape('sessions - auto close when using from option', async function (t) { +test('sessions - auto close when using from option', async function (t) { const core1 = new Hypercore(ram, { autoClose: true }) @@ -152,10 +152,10 @@ tape('sessions - auto close when using from option', async function (t) { } }) await core2.close() - t.true(core1.closed) + t.ok(core1.closed) }) -tape('sessions - close with from option', async function (t) { +test('sessions - close with from option', async function (t) { const core1 = new Hypercore(ram) await core1.append('hello world') @@ -168,6 +168,6 @@ tape('sessions - close with from option', async function (t) { }) await core2.close() - t.false(core1.closed) - t.same(await core1.get(0), Buffer.from('hello world')) + t.absent(core1.closed) + t.alike(await core1.get(0), Buffer.from('hello world')) }) diff --git a/test/user-data.js b/test/user-data.js index 0b4bdffe..3d512a32 100644 --- a/test/user-data.js +++ b/test/user-data.js @@ -1,30 +1,30 @@ -const tape = require('tape') +const test = require('brittle') const Hypercore = require('../') const tmp = require('tmp-promise') const { create } = require('./helpers') -tape('userdata - can set through setUserData', async function (t) { +test('userdata - can set through setUserData', async function (t) { const core = await create() await core.setUserData('hello', Buffer.from('world')) - t.same(await core.getUserData('hello'), Buffer.from('world')) + t.alike(await core.getUserData('hello'), Buffer.from('world')) t.end() }) -tape('userdata - can set through constructor option', async function (t) { +test('userdata - can set through constructor option', async function (t) { const core = await create({ userData: { hello: Buffer.from('world') } }) - t.same(await core.getUserData('hello'), Buffer.from('world')) + t.alike(await core.getUserData('hello'), Buffer.from('world')) t.end() }) -tape('userdata - persists across restarts', async function (t) { +test('userdata - persists across restarts', async function (t) { const dir = await tmp.dir() let core = new Hypercore(dir.path, { @@ -41,7 +41,7 @@ tape('userdata - persists across restarts', async function (t) { } }) - t.same(await core.getUserData('hello'), Buffer.from('world')) - t.same(await core.getUserData('other'), Buffer.from('another')) + t.alike(await core.getUserData('hello'), Buffer.from('world')) + t.alike(await core.getUserData('other'), Buffer.from('another')) t.end() }) From 6ff787ca2893a476baff0463d809ff143d158c48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Fri, 8 Oct 2021 11:47:18 +0200 Subject: [PATCH 02/10] Convert `test/oplog.js` to ESM and serialize tests --- package.json | 2 +- test/{oplog.js => oplog.mjs} | 42 +++++++++++++++++++----------------- 2 files changed, 23 insertions(+), 21 deletions(-) rename test/{oplog.js => oplog.mjs} (87%) diff --git a/package.json b/package.json index 0f54af91..734a82f6 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Hypercore 10", "main": "index.js", "scripts": { - "test": "standard && brittle test/*.js" + "test": "standard && brittle test/*.{,m}js" }, "repository": { "type": "git", diff --git a/test/oplog.js b/test/oplog.mjs similarity index 87% rename from test/oplog.js rename to test/oplog.mjs index b8eefc6b..7b365343 100644 --- a/test/oplog.js +++ b/test/oplog.mjs @@ -1,24 +1,26 @@ -const p = require('path') -const fs = require('fs') -const test = require('brittle') -const fsctl = require('fsctl') -const raf = require('random-access-file') -const c = require('compact-encoding') +import p from 'path' +import fs from 'fs' +import url from 'url' +import test from 'brittle' +import fsctl from 'fsctl' +import raf from 'random-access-file' +import * as c from 'compact-encoding' -const Oplog = require('../lib/oplog') +import Oplog from '../lib/oplog.js' +const DIRNAME = p.dirname(url.fileURLToPath(import.meta.url)); const STORAGE_FILE_NAME = 'oplog-test-storage' -const STORAGE_FILE_PATH = p.join(__dirname, STORAGE_FILE_NAME) +const STORAGE_FILE_PATH = p.join(DIRNAME, STORAGE_FILE_NAME) const SHOULD_ERROR = Symbol('hypercore-oplog-should-error') -test('oplog - reset storage', async function (t) { +await test('oplog - reset storage', async function (t) { // just to make sure to cleanup storage if it failed half way through before if (fs.existsSync(STORAGE_FILE_PATH)) await fs.promises.unlink(STORAGE_FILE_PATH) t.pass('data is reset') t.end() }) -test('oplog - basic append', async function (t) { +await test('oplog - basic append', async function (t) { const storage = testStorage() const logWr = new Oplog(storage) @@ -62,7 +64,7 @@ test('oplog - basic append', async function (t) { t.end() }) -test('oplog - custom encoding', async function (t) { +await test('oplog - custom encoding', async function (t) { const storage = testStorage() const log = new Oplog(storage, { @@ -86,7 +88,7 @@ test('oplog - custom encoding', async function (t) { t.end() }) -test('oplog - alternating header writes', async function (t) { +await test('oplog - alternating header writes', async function (t) { const storage = testStorage() const log = new Oplog(storage) @@ -118,7 +120,7 @@ test('oplog - alternating header writes', async function (t) { t.end() }) -test('oplog - one fully-corrupted header', async function (t) { +await test('oplog - one fully-corrupted header', async function (t) { const storage = testStorage() const log = new Oplog(storage) @@ -162,7 +164,7 @@ test('oplog - one fully-corrupted header', async function (t) { t.end() }) -test('oplog - header invalid checksum', async function (t) { +await test('oplog - header invalid checksum', async function (t) { const storage = testStorage() const log = new Oplog(storage) @@ -208,7 +210,7 @@ test('oplog - header invalid checksum', async function (t) { t.end() }) -test('oplog - malformed log entry gets overwritten', async function (t) { +await test('oplog - malformed log entry gets overwritten', async function (t) { let storage = testStorage() let log = new Oplog(storage) @@ -254,7 +256,7 @@ test('oplog - malformed log entry gets overwritten', async function (t) { t.end() }) -test('oplog - log not truncated when header write fails', async function (t) { +await test('oplog - log not truncated when header write fails', async function (t) { const storage = failingOffsetStorage(4096 * 2) const log = new Oplog(storage) @@ -297,7 +299,7 @@ test('oplog - log not truncated when header write fails', async function (t) { t.end() }) -test('oplog - multi append', async function (t) { +await test('oplog - multi append', async function (t) { const storage = testStorage() const log = new Oplog(storage) @@ -329,7 +331,7 @@ test('oplog - multi append', async function (t) { t.end() }) -test('oplog - multi append is atomic', async function (t) { +await test('oplog - multi append is atomic', async function (t) { const storage = testStorage() const log = new Oplog(storage) @@ -368,12 +370,12 @@ test('oplog - multi append is atomic', async function (t) { }) function testStorage () { - return raf(STORAGE_FILE_NAME, { directory: __dirname, lock: fsctl.lock }) + return raf(STORAGE_FILE_NAME, { directory: DIRNAME, lock: fsctl.lock }) } function failingOffsetStorage (offset) { let shouldError = false - const storage = raf(STORAGE_FILE_NAME, { directory: __dirname, lock: fsctl.lock }) + const storage = raf(STORAGE_FILE_NAME, { directory: DIRNAME, lock: fsctl.lock }) const write = storage.write.bind(storage) storage.write = (off, data, cb) => { From f38376ed3f64283eb2b79a22f2577f2005e4065c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Fri, 8 Oct 2021 11:51:35 +0200 Subject: [PATCH 03/10] Don't lint `test/oplog.mjs` --- package.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/package.json b/package.json index 734a82f6..a2470fdf 100644 --- a/package.json +++ b/package.json @@ -40,5 +40,10 @@ }, "optionalDependencies": { "fsctl": "^1.0.0" + }, + "standard": { + "ignore": [ + "test/oplog.mjs" + ] } } From ce339040b378efacd64d4f4ae790262f22ee4f10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Fri, 8 Oct 2021 13:15:53 +0200 Subject: [PATCH 04/10] Update `actions/setup-node` to v2 --- .github/workflows/test-node.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test-node.yml b/.github/workflows/test-node.yml index 4130b57c..df609a28 100644 --- a/.github/workflows/test-node.yml +++ b/.github/workflows/test-node.yml @@ -17,7 +17,7 @@ jobs: steps: - uses: actions/checkout@v2 - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v1 + uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install From fd4c6c553a9b10313abed86e1f093dd5fcde81d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Fri, 8 Oct 2021 13:30:44 +0200 Subject: [PATCH 05/10] Don't lint any ESM files --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a2470fdf..470f0c28 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ }, "standard": { "ignore": [ - "test/oplog.mjs" + "**/*.mjs" ] } } From 0ce765efb81c4bd40f5485d24142dc4e27f06ab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Fri, 8 Oct 2021 14:00:15 +0200 Subject: [PATCH 06/10] Update Brittle --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 470f0c28..2d399944 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ }, "devDependencies": { "@hyperswarm/replicator": "^1.8.0", - "brittle": "^1.3.6", + "brittle": "^1.3.9", "random-access-memory": "^3.1.2", "range-parser": "^1.2.1", "standard": "^16.0.3", From c59b6b2ac4f971f0cdeec310619cf42ef18adb7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Mon, 11 Oct 2021 09:15:32 +0200 Subject: [PATCH 07/10] Revert "Convert `test/oplog.js` to ESM and serialize tests" This reverts commit 6ff787ca2893a476baff0463d809ff143d158c48. --- package.json | 2 +- test/{oplog.mjs => oplog.js} | 42 +++++++++++++++++------------------- 2 files changed, 21 insertions(+), 23 deletions(-) rename test/{oplog.mjs => oplog.js} (87%) diff --git a/package.json b/package.json index 2d399944..8bf8cbfe 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Hypercore 10", "main": "index.js", "scripts": { - "test": "standard && brittle test/*.{,m}js" + "test": "standard && brittle test/*.js" }, "repository": { "type": "git", diff --git a/test/oplog.mjs b/test/oplog.js similarity index 87% rename from test/oplog.mjs rename to test/oplog.js index 7b365343..b8eefc6b 100644 --- a/test/oplog.mjs +++ b/test/oplog.js @@ -1,26 +1,24 @@ -import p from 'path' -import fs from 'fs' -import url from 'url' -import test from 'brittle' -import fsctl from 'fsctl' -import raf from 'random-access-file' -import * as c from 'compact-encoding' +const p = require('path') +const fs = require('fs') +const test = require('brittle') +const fsctl = require('fsctl') +const raf = require('random-access-file') +const c = require('compact-encoding') -import Oplog from '../lib/oplog.js' +const Oplog = require('../lib/oplog') -const DIRNAME = p.dirname(url.fileURLToPath(import.meta.url)); const STORAGE_FILE_NAME = 'oplog-test-storage' -const STORAGE_FILE_PATH = p.join(DIRNAME, STORAGE_FILE_NAME) +const STORAGE_FILE_PATH = p.join(__dirname, STORAGE_FILE_NAME) const SHOULD_ERROR = Symbol('hypercore-oplog-should-error') -await test('oplog - reset storage', async function (t) { +test('oplog - reset storage', async function (t) { // just to make sure to cleanup storage if it failed half way through before if (fs.existsSync(STORAGE_FILE_PATH)) await fs.promises.unlink(STORAGE_FILE_PATH) t.pass('data is reset') t.end() }) -await test('oplog - basic append', async function (t) { +test('oplog - basic append', async function (t) { const storage = testStorage() const logWr = new Oplog(storage) @@ -64,7 +62,7 @@ await test('oplog - basic append', async function (t) { t.end() }) -await test('oplog - custom encoding', async function (t) { +test('oplog - custom encoding', async function (t) { const storage = testStorage() const log = new Oplog(storage, { @@ -88,7 +86,7 @@ await test('oplog - custom encoding', async function (t) { t.end() }) -await test('oplog - alternating header writes', async function (t) { +test('oplog - alternating header writes', async function (t) { const storage = testStorage() const log = new Oplog(storage) @@ -120,7 +118,7 @@ await test('oplog - alternating header writes', async function (t) { t.end() }) -await test('oplog - one fully-corrupted header', async function (t) { +test('oplog - one fully-corrupted header', async function (t) { const storage = testStorage() const log = new Oplog(storage) @@ -164,7 +162,7 @@ await test('oplog - one fully-corrupted header', async function (t) { t.end() }) -await test('oplog - header invalid checksum', async function (t) { +test('oplog - header invalid checksum', async function (t) { const storage = testStorage() const log = new Oplog(storage) @@ -210,7 +208,7 @@ await test('oplog - header invalid checksum', async function (t) { t.end() }) -await test('oplog - malformed log entry gets overwritten', async function (t) { +test('oplog - malformed log entry gets overwritten', async function (t) { let storage = testStorage() let log = new Oplog(storage) @@ -256,7 +254,7 @@ await test('oplog - malformed log entry gets overwritten', async function (t) { t.end() }) -await test('oplog - log not truncated when header write fails', async function (t) { +test('oplog - log not truncated when header write fails', async function (t) { const storage = failingOffsetStorage(4096 * 2) const log = new Oplog(storage) @@ -299,7 +297,7 @@ await test('oplog - log not truncated when header write fails', async function ( t.end() }) -await test('oplog - multi append', async function (t) { +test('oplog - multi append', async function (t) { const storage = testStorage() const log = new Oplog(storage) @@ -331,7 +329,7 @@ await test('oplog - multi append', async function (t) { t.end() }) -await test('oplog - multi append is atomic', async function (t) { +test('oplog - multi append is atomic', async function (t) { const storage = testStorage() const log = new Oplog(storage) @@ -370,12 +368,12 @@ await test('oplog - multi append is atomic', async function (t) { }) function testStorage () { - return raf(STORAGE_FILE_NAME, { directory: DIRNAME, lock: fsctl.lock }) + return raf(STORAGE_FILE_NAME, { directory: __dirname, lock: fsctl.lock }) } function failingOffsetStorage (offset) { let shouldError = false - const storage = raf(STORAGE_FILE_NAME, { directory: DIRNAME, lock: fsctl.lock }) + const storage = raf(STORAGE_FILE_NAME, { directory: __dirname, lock: fsctl.lock }) const write = storage.write.bind(storage) storage.write = (off, data, cb) => { From 1d41d9c22d9f0047932a60d84b7a727de1d3762f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Mon, 11 Oct 2021 09:16:46 +0200 Subject: [PATCH 08/10] Serialize oplog tests --- test/oplog.js | 532 +++++++++++++++++++++++++------------------------- 1 file changed, 267 insertions(+), 265 deletions(-) diff --git a/test/oplog.js b/test/oplog.js index b8eefc6b..59ac1727 100644 --- a/test/oplog.js +++ b/test/oplog.js @@ -11,360 +11,362 @@ const STORAGE_FILE_NAME = 'oplog-test-storage' const STORAGE_FILE_PATH = p.join(__dirname, STORAGE_FILE_NAME) const SHOULD_ERROR = Symbol('hypercore-oplog-should-error') -test('oplog - reset storage', async function (t) { +test('oplog', async function (t) { + await t.test('oplog - reset storage', async function (t) { // just to make sure to cleanup storage if it failed half way through before - if (fs.existsSync(STORAGE_FILE_PATH)) await fs.promises.unlink(STORAGE_FILE_PATH) - t.pass('data is reset') - t.end() -}) + if (fs.existsSync(STORAGE_FILE_PATH)) await fs.promises.unlink(STORAGE_FILE_PATH) + t.pass('data is reset') + t.end() + }) -test('oplog - basic append', async function (t) { - const storage = testStorage() + await t.test('oplog - basic append', async function (t) { + const storage = testStorage() - const logWr = new Oplog(storage) + const logWr = new Oplog(storage) - await logWr.open() - await logWr.flush(Buffer.from('h')) - await logWr.append(Buffer.from('a')) - await logWr.append(Buffer.from('b')) + await logWr.open() + await logWr.flush(Buffer.from('h')) + await logWr.append(Buffer.from('a')) + await logWr.append(Buffer.from('b')) - const logRd = new Oplog(storage) + const logRd = new Oplog(storage) - { - const { header, entries } = await logRd.open() + { + const { header, entries } = await logRd.open() - t.alike(header, Buffer.from('h')) - t.is(entries.length, 2) - t.alike(entries[0], Buffer.from('a')) - t.alike(entries[1], Buffer.from('b')) - } + t.alike(header, Buffer.from('h')) + t.is(entries.length, 2) + t.alike(entries[0], Buffer.from('a')) + t.alike(entries[1], Buffer.from('b')) + } - await logWr.flush(Buffer.from('i')) + await logWr.flush(Buffer.from('i')) - { - const { header, entries } = await logRd.open() + { + const { header, entries } = await logRd.open() - t.alike(header, Buffer.from('i')) - t.is(entries.length, 0) - } + t.alike(header, Buffer.from('i')) + t.is(entries.length, 0) + } - await logWr.append(Buffer.from('c')) + await logWr.append(Buffer.from('c')) - { - const { header, entries } = await logRd.open() + { + const { header, entries } = await logRd.open() - t.alike(header, Buffer.from('i')) - t.is(entries.length, 1) - t.alike(entries[0], Buffer.from('c')) - } + t.alike(header, Buffer.from('i')) + t.is(entries.length, 1) + t.alike(entries[0], Buffer.from('c')) + } - await cleanup(storage) - t.end() -}) + await cleanup(storage) + t.end() + }) -test('oplog - custom encoding', async function (t) { - const storage = testStorage() + await t.test('oplog - custom encoding', async function (t) { + const storage = testStorage() - const log = new Oplog(storage, { - headerEncoding: c.string, - entryEncoding: c.uint - }) + const log = new Oplog(storage, { + headerEncoding: c.string, + entryEncoding: c.uint + }) - await log.open() - await log.flush('one header') - await log.append(42) - await log.append(43) + await log.open() + await log.flush('one header') + await log.append(42) + await log.append(43) - const { header, entries } = await log.open() + const { header, entries } = await log.open() - t.is(header, 'one header') - t.is(entries.length, 2) - t.is(entries[0], 42) - t.is(entries[1], 43) + t.is(header, 'one header') + t.is(entries.length, 2) + t.is(entries[0], 42) + t.is(entries[1], 43) - await cleanup(storage) - t.end() -}) + await cleanup(storage) + t.end() + }) -test('oplog - alternating header writes', async function (t) { - const storage = testStorage() + await t.test('oplog - alternating header writes', async function (t) { + const storage = testStorage() - const log = new Oplog(storage) + const log = new Oplog(storage) - await log.open() - await log.flush(Buffer.from('1')) - await log.flush(Buffer.from('2')) + await log.open() + await log.flush(Buffer.from('1')) + await log.flush(Buffer.from('2')) - { - const { header } = await log.open() - t.alike(header, Buffer.from('2')) - } + { + const { header } = await log.open() + t.alike(header, Buffer.from('2')) + } - await log.flush(Buffer.from('1')) // Should overwrite first header + await log.flush(Buffer.from('1')) // Should overwrite first header - { - const { header } = await log.open() - t.alike(header, Buffer.from('1')) - } + { + const { header } = await log.open() + t.alike(header, Buffer.from('1')) + } - await log.flush(Buffer.from('2')) // Should overwrite second header + await log.flush(Buffer.from('2')) // Should overwrite second header - { - const { header } = await log.open() - t.alike(header, Buffer.from('2')) - } + { + const { header } = await log.open() + t.alike(header, Buffer.from('2')) + } - await cleanup(storage) - t.end() -}) + await cleanup(storage) + t.end() + }) -test('oplog - one fully-corrupted header', async function (t) { - const storage = testStorage() + await t.test('oplog - one fully-corrupted header', async function (t) { + const storage = testStorage() - const log = new Oplog(storage) + const log = new Oplog(storage) - await log.open() - await log.flush(Buffer.from('header 1')) + await log.open() + await log.flush(Buffer.from('header 1')) - { - const { header } = await log.open() - t.alike(header, Buffer.from('header 1')) - } + { + const { header } = await log.open() + t.alike(header, Buffer.from('header 1')) + } - await log.flush(Buffer.from('header 2')) + await log.flush(Buffer.from('header 2')) - { - const { header } = await log.open() - t.alike(header, Buffer.from('header 2')) - } + { + const { header } = await log.open() + t.alike(header, Buffer.from('header 2')) + } - await log.flush(Buffer.from('header 3')) // should overwrite first header + await log.flush(Buffer.from('header 3')) // should overwrite first header - { - const { header } = await log.open() - t.alike(header, Buffer.from('header 3')) - } + { + const { header } = await log.open() + t.alike(header, Buffer.from('header 3')) + } - // Corrupt the first header -- second header should win now - await new Promise((resolve, reject) => { - storage.write(0, Buffer.from('hello world'), err => { - if (err) return reject(err) - return resolve() + // Corrupt the first header -- second header should win now + await new Promise((resolve, reject) => { + storage.write(0, Buffer.from('hello world'), err => { + if (err) return reject(err) + return resolve() + }) }) - }) - { - const { header } = await log.open() - t.alike(header, Buffer.from('header 2'), 'one is corrupted or partially written') - } + { + const { header } = await log.open() + t.alike(header, Buffer.from('header 2'), 'one is corrupted or partially written') + } - await cleanup(storage) - t.end() -}) + await cleanup(storage) + t.end() + }) -test('oplog - header invalid checksum', async function (t) { - const storage = testStorage() + await t.test('oplog - header invalid checksum', async function (t) { + const storage = testStorage() - const log = new Oplog(storage) + const log = new Oplog(storage) - await log.open() - await log.flush(Buffer.from('a')) - await log.flush(Buffer.from('b')) + await log.open() + await log.flush(Buffer.from('a')) + await log.flush(Buffer.from('b')) - { - const { header } = await log.open() - t.alike(header, Buffer.from('b')) - } + { + const { header } = await log.open() + t.alike(header, Buffer.from('b')) + } - // Invalidate the first header's checksum -- second header should win now - await new Promise((resolve, reject) => { - storage.write(4096 + 8, Buffer.from('a'), err => { - if (err) return reject(err) - return resolve() + // Invalidate the first header's checksum -- second header should win now + await new Promise((resolve, reject) => { + storage.write(4096 + 8, Buffer.from('a'), err => { + if (err) return reject(err) + return resolve() + }) }) - }) - { - const { header } = await log.open() - t.alike(header, Buffer.from('a')) - } + { + const { header } = await log.open() + t.alike(header, Buffer.from('a')) + } - // Invalidate the second header's checksum -- the hypercore is now corrupted - await new Promise((resolve, reject) => { - storage.write(8, Buffer.from('b'), err => { - if (err) return reject(err) - return resolve() + // Invalidate the second header's checksum -- the hypercore is now corrupted + await new Promise((resolve, reject) => { + storage.write(8, Buffer.from('b'), err => { + if (err) return reject(err) + return resolve() + }) }) - }) - try { - await log.open() - t.fail('corruption should have been detected') - } catch { - t.pass('corruption was correctly detected') - } + try { + await log.open() + t.fail('corruption should have been detected') + } catch { + t.pass('corruption was correctly detected') + } - await cleanup(storage) - t.end() -}) + await cleanup(storage) + t.end() + }) -test('oplog - malformed log entry gets overwritten', async function (t) { - let storage = testStorage() - let log = new Oplog(storage) + await t.test('oplog - malformed log entry gets overwritten', async function (t) { + let storage = testStorage() + let log = new Oplog(storage) - await log.flush(Buffer.from('header')) - await log.append(Buffer.from('a')) - await log.append(Buffer.from('b')) - await log.close() + await log.flush(Buffer.from('header')) + await log.append(Buffer.from('a')) + await log.append(Buffer.from('b')) + await log.close() - const offset = log.byteLength + const offset = log.byteLength - storage = testStorage() - log = new Oplog(storage) + storage = testStorage() + log = new Oplog(storage) - // Write a bad oplog message at the end (simulates a failed append) - await new Promise((resolve, reject) => { - storage.write(offset + 4096 * 2, Buffer.from([0, 0, 0, 0, 4, 0, 0, 0]), err => { - if (err) return reject(err) - return resolve() + // Write a bad oplog message at the end (simulates a failed append) + await new Promise((resolve, reject) => { + storage.write(offset + 4096 * 2, Buffer.from([0, 0, 0, 0, 4, 0, 0, 0]), err => { + if (err) return reject(err) + return resolve() + }) }) - }) - { - const { entries } = await log.open() + { + const { entries } = await log.open() - t.is(entries.length, 2) // The partial entry should not be present - t.alike(entries[0], Buffer.from('a')) - t.alike(entries[1], Buffer.from('b')) - } - - // Write a valid oplog message now - await log.append(Buffer.from('c')) - - { - const { entries } = await log.open() - - t.is(entries.length, 3) // The partial entry should not be present - t.alike(entries[0], Buffer.from('a')) - t.alike(entries[1], Buffer.from('b')) - t.alike(entries[2], Buffer.from('c')) - } - - await cleanup(storage) - t.end() -}) + t.is(entries.length, 2) // The partial entry should not be present + t.alike(entries[0], Buffer.from('a')) + t.alike(entries[1], Buffer.from('b')) + } -test('oplog - log not truncated when header write fails', async function (t) { - const storage = failingOffsetStorage(4096 * 2) + // Write a valid oplog message now + await log.append(Buffer.from('c')) - const log = new Oplog(storage) + { + const { entries } = await log.open() - await log.flush(Buffer.from('header')) - await log.append(Buffer.from('a')) - await log.append(Buffer.from('b')) + t.is(entries.length, 3) // The partial entry should not be present + t.alike(entries[0], Buffer.from('a')) + t.alike(entries[1], Buffer.from('b')) + t.alike(entries[2], Buffer.from('c')) + } - // Make subsequent header writes fail - storage[SHOULD_ERROR](true) + await cleanup(storage) + t.end() + }) - // The flush should fail because the header can't be updated -- log should still have entries after this - try { - await log.flush(Buffer.from('header two')) - } catch (err) { - t.ok(err.synthetic) - } + await t.test('oplog - log not truncated when header write fails', async function (t) { + const storage = failingOffsetStorage(4096 * 2) - { - const { header, entries } = await log.open() + const log = new Oplog(storage) - t.alike(header, Buffer.from('header')) - t.is(entries.length, 2) - t.alike(entries[0], Buffer.from('a')) - t.alike(entries[1], Buffer.from('b')) - } + await log.flush(Buffer.from('header')) + await log.append(Buffer.from('a')) + await log.append(Buffer.from('b')) - // Re-enable header writes - storage[SHOULD_ERROR](false) - await log.flush(Buffer.from('header two')) // Should correctly truncate the oplog now + // Make subsequent header writes fail + storage[SHOULD_ERROR](true) - { - const { header, entries } = await log.open() + // The flush should fail because the header can't be updated -- log should still have entries after this + try { + await log.flush(Buffer.from('header two')) + } catch (err) { + t.ok(err.synthetic) + } - t.alike(header, Buffer.from('header two')) - t.is(entries.length, 0) - } + { + const { header, entries } = await log.open() - await cleanup(storage) - t.end() -}) + t.alike(header, Buffer.from('header')) + t.is(entries.length, 2) + t.alike(entries[0], Buffer.from('a')) + t.alike(entries[1], Buffer.from('b')) + } -test('oplog - multi append', async function (t) { - const storage = testStorage() + // Re-enable header writes + storage[SHOULD_ERROR](false) + await log.flush(Buffer.from('header two')) // Should correctly truncate the oplog now - const log = new Oplog(storage) + { + const { header, entries } = await log.open() - await log.open() - await log.flush(Buffer.from('a')) + t.alike(header, Buffer.from('header two')) + t.is(entries.length, 0) + } - await log.append([ - Buffer.from('1'), - Buffer.from('22'), - Buffer.from('333'), - Buffer.from('4') - ]) + await cleanup(storage) + t.end() + }) - t.is(log.length, 4) - t.is(log.byteLength, 32 + 1 + 2 + 3 + 1) + await t.test('oplog - multi append', async function (t) { + const storage = testStorage() - const { header, entries } = await log.open() + const log = new Oplog(storage) - t.alike(header, Buffer.from('a')) - t.alike(entries, [ - Buffer.from('1'), - Buffer.from('22'), - Buffer.from('333'), - Buffer.from('4') - ]) + await log.open() + await log.flush(Buffer.from('a')) - await cleanup(storage) - t.end() -}) + await log.append([ + Buffer.from('1'), + Buffer.from('22'), + Buffer.from('333'), + Buffer.from('4') + ]) -test('oplog - multi append is atomic', async function (t) { - const storage = testStorage() + t.is(log.length, 4) + t.is(log.byteLength, 32 + 1 + 2 + 3 + 1) - const log = new Oplog(storage) + const { header, entries } = await log.open() - await log.open() - await log.flush(Buffer.from('a')) + t.alike(header, Buffer.from('a')) + t.alike(entries, [ + Buffer.from('1'), + Buffer.from('22'), + Buffer.from('333'), + Buffer.from('4') + ]) + + await cleanup(storage) + t.end() + }) - await log.append(Buffer.from('0')) - await log.append([ - Buffer.from('1'), - Buffer.from('22'), - Buffer.from('333'), - Buffer.from('4') - ]) + await t.test('oplog - multi append is atomic', async function (t) { + const storage = testStorage() - t.is(log.length, 5) - t.is(log.byteLength, 40 + 1 + 1 + 2 + 3 + 1) + const log = new Oplog(storage) - // Corrupt the last write, should revert the full batch - await new Promise((resolve, reject) => { - storage.write(8192 + log.byteLength - 1, Buffer.from('x'), err => { - if (err) return reject(err) - return resolve() + await log.open() + await log.flush(Buffer.from('a')) + + await log.append(Buffer.from('0')) + await log.append([ + Buffer.from('1'), + Buffer.from('22'), + Buffer.from('333'), + Buffer.from('4') + ]) + + t.is(log.length, 5) + t.is(log.byteLength, 40 + 1 + 1 + 2 + 3 + 1) + + // Corrupt the last write, should revert the full batch + await new Promise((resolve, reject) => { + storage.write(8192 + log.byteLength - 1, Buffer.from('x'), err => { + if (err) return reject(err) + return resolve() + }) }) - }) - const { entries } = await log.open() + const { entries } = await log.open() - t.is(log.length, 1) - t.alike(entries, [ - Buffer.from('0') - ]) + t.is(log.length, 1) + t.alike(entries, [ + Buffer.from('0') + ]) - await cleanup(storage) - t.end() + await cleanup(storage) + t.end() + }) }) function testStorage () { From aced4de8db9550dbcb2d0b41ebc20524cebbaeca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Mon, 11 Oct 2021 16:43:35 +0200 Subject: [PATCH 09/10] Update Brittle and make oplog tests serial --- package.json | 2 +- test/oplog.js | 534 +++++++++++++++++++++++++------------------------- 2 files changed, 268 insertions(+), 268 deletions(-) diff --git a/package.json b/package.json index 8bf8cbfe..32fa6609 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ }, "devDependencies": { "@hyperswarm/replicator": "^1.8.0", - "brittle": "^1.3.9", + "brittle": "^1.4.1", "random-access-memory": "^3.1.2", "range-parser": "^1.2.1", "standard": "^16.0.3", diff --git a/test/oplog.js b/test/oplog.js index 59ac1727..c52e7691 100644 --- a/test/oplog.js +++ b/test/oplog.js @@ -11,362 +11,362 @@ const STORAGE_FILE_NAME = 'oplog-test-storage' const STORAGE_FILE_PATH = p.join(__dirname, STORAGE_FILE_NAME) const SHOULD_ERROR = Symbol('hypercore-oplog-should-error') -test('oplog', async function (t) { - await t.test('oplog - reset storage', async function (t) { +test.configure({ serial: true }) + +test('oplog - reset storage', async function (t) { // just to make sure to cleanup storage if it failed half way through before - if (fs.existsSync(STORAGE_FILE_PATH)) await fs.promises.unlink(STORAGE_FILE_PATH) - t.pass('data is reset') - t.end() - }) + if (fs.existsSync(STORAGE_FILE_PATH)) await fs.promises.unlink(STORAGE_FILE_PATH) + t.pass('data is reset') + t.end() +}) - await t.test('oplog - basic append', async function (t) { - const storage = testStorage() +test('oplog - basic append', async function (t) { + const storage = testStorage() - const logWr = new Oplog(storage) + const logWr = new Oplog(storage) - await logWr.open() - await logWr.flush(Buffer.from('h')) - await logWr.append(Buffer.from('a')) - await logWr.append(Buffer.from('b')) + await logWr.open() + await logWr.flush(Buffer.from('h')) + await logWr.append(Buffer.from('a')) + await logWr.append(Buffer.from('b')) - const logRd = new Oplog(storage) + const logRd = new Oplog(storage) - { - const { header, entries } = await logRd.open() + { + const { header, entries } = await logRd.open() - t.alike(header, Buffer.from('h')) - t.is(entries.length, 2) - t.alike(entries[0], Buffer.from('a')) - t.alike(entries[1], Buffer.from('b')) - } + t.alike(header, Buffer.from('h')) + t.is(entries.length, 2) + t.alike(entries[0], Buffer.from('a')) + t.alike(entries[1], Buffer.from('b')) + } - await logWr.flush(Buffer.from('i')) + await logWr.flush(Buffer.from('i')) - { - const { header, entries } = await logRd.open() + { + const { header, entries } = await logRd.open() - t.alike(header, Buffer.from('i')) - t.is(entries.length, 0) - } + t.alike(header, Buffer.from('i')) + t.is(entries.length, 0) + } - await logWr.append(Buffer.from('c')) + await logWr.append(Buffer.from('c')) - { - const { header, entries } = await logRd.open() + { + const { header, entries } = await logRd.open() - t.alike(header, Buffer.from('i')) - t.is(entries.length, 1) - t.alike(entries[0], Buffer.from('c')) - } + t.alike(header, Buffer.from('i')) + t.is(entries.length, 1) + t.alike(entries[0], Buffer.from('c')) + } - await cleanup(storage) - t.end() - }) + await cleanup(storage) + t.end() +}) - await t.test('oplog - custom encoding', async function (t) { - const storage = testStorage() +test('oplog - custom encoding', async function (t) { + const storage = testStorage() - const log = new Oplog(storage, { - headerEncoding: c.string, - entryEncoding: c.uint - }) + const log = new Oplog(storage, { + headerEncoding: c.string, + entryEncoding: c.uint + }) - await log.open() - await log.flush('one header') - await log.append(42) - await log.append(43) + await log.open() + await log.flush('one header') + await log.append(42) + await log.append(43) - const { header, entries } = await log.open() + const { header, entries } = await log.open() - t.is(header, 'one header') - t.is(entries.length, 2) - t.is(entries[0], 42) - t.is(entries[1], 43) + t.is(header, 'one header') + t.is(entries.length, 2) + t.is(entries[0], 42) + t.is(entries[1], 43) - await cleanup(storage) - t.end() - }) + await cleanup(storage) + t.end() +}) - await t.test('oplog - alternating header writes', async function (t) { - const storage = testStorage() +test('oplog - alternating header writes', async function (t) { + const storage = testStorage() - const log = new Oplog(storage) + const log = new Oplog(storage) - await log.open() - await log.flush(Buffer.from('1')) - await log.flush(Buffer.from('2')) + await log.open() + await log.flush(Buffer.from('1')) + await log.flush(Buffer.from('2')) - { - const { header } = await log.open() - t.alike(header, Buffer.from('2')) - } + { + const { header } = await log.open() + t.alike(header, Buffer.from('2')) + } - await log.flush(Buffer.from('1')) // Should overwrite first header + await log.flush(Buffer.from('1')) // Should overwrite first header - { - const { header } = await log.open() - t.alike(header, Buffer.from('1')) - } + { + const { header } = await log.open() + t.alike(header, Buffer.from('1')) + } - await log.flush(Buffer.from('2')) // Should overwrite second header + await log.flush(Buffer.from('2')) // Should overwrite second header - { - const { header } = await log.open() - t.alike(header, Buffer.from('2')) - } + { + const { header } = await log.open() + t.alike(header, Buffer.from('2')) + } - await cleanup(storage) - t.end() - }) + await cleanup(storage) + t.end() +}) - await t.test('oplog - one fully-corrupted header', async function (t) { - const storage = testStorage() +test('oplog - one fully-corrupted header', async function (t) { + const storage = testStorage() - const log = new Oplog(storage) + const log = new Oplog(storage) - await log.open() - await log.flush(Buffer.from('header 1')) + await log.open() + await log.flush(Buffer.from('header 1')) - { - const { header } = await log.open() - t.alike(header, Buffer.from('header 1')) - } + { + const { header } = await log.open() + t.alike(header, Buffer.from('header 1')) + } - await log.flush(Buffer.from('header 2')) + await log.flush(Buffer.from('header 2')) - { - const { header } = await log.open() - t.alike(header, Buffer.from('header 2')) - } + { + const { header } = await log.open() + t.alike(header, Buffer.from('header 2')) + } - await log.flush(Buffer.from('header 3')) // should overwrite first header + await log.flush(Buffer.from('header 3')) // should overwrite first header - { - const { header } = await log.open() - t.alike(header, Buffer.from('header 3')) - } + { + const { header } = await log.open() + t.alike(header, Buffer.from('header 3')) + } - // Corrupt the first header -- second header should win now - await new Promise((resolve, reject) => { - storage.write(0, Buffer.from('hello world'), err => { - if (err) return reject(err) - return resolve() - }) + // Corrupt the first header -- second header should win now + await new Promise((resolve, reject) => { + storage.write(0, Buffer.from('hello world'), err => { + if (err) return reject(err) + return resolve() }) + }) - { - const { header } = await log.open() - t.alike(header, Buffer.from('header 2'), 'one is corrupted or partially written') - } + { + const { header } = await log.open() + t.alike(header, Buffer.from('header 2'), 'one is corrupted or partially written') + } - await cleanup(storage) - t.end() - }) + await cleanup(storage) + t.end() +}) - await t.test('oplog - header invalid checksum', async function (t) { - const storage = testStorage() +test('oplog - header invalid checksum', async function (t) { + const storage = testStorage() - const log = new Oplog(storage) + const log = new Oplog(storage) - await log.open() - await log.flush(Buffer.from('a')) - await log.flush(Buffer.from('b')) + await log.open() + await log.flush(Buffer.from('a')) + await log.flush(Buffer.from('b')) - { - const { header } = await log.open() - t.alike(header, Buffer.from('b')) - } + { + const { header } = await log.open() + t.alike(header, Buffer.from('b')) + } - // Invalidate the first header's checksum -- second header should win now - await new Promise((resolve, reject) => { - storage.write(4096 + 8, Buffer.from('a'), err => { - if (err) return reject(err) - return resolve() - }) + // Invalidate the first header's checksum -- second header should win now + await new Promise((resolve, reject) => { + storage.write(4096 + 8, Buffer.from('a'), err => { + if (err) return reject(err) + return resolve() }) + }) - { - const { header } = await log.open() - t.alike(header, Buffer.from('a')) - } + { + const { header } = await log.open() + t.alike(header, Buffer.from('a')) + } - // Invalidate the second header's checksum -- the hypercore is now corrupted - await new Promise((resolve, reject) => { - storage.write(8, Buffer.from('b'), err => { - if (err) return reject(err) - return resolve() - }) + // Invalidate the second header's checksum -- the hypercore is now corrupted + await new Promise((resolve, reject) => { + storage.write(8, Buffer.from('b'), err => { + if (err) return reject(err) + return resolve() }) + }) - try { - await log.open() - t.fail('corruption should have been detected') - } catch { - t.pass('corruption was correctly detected') - } + try { + await log.open() + t.fail('corruption should have been detected') + } catch { + t.pass('corruption was correctly detected') + } - await cleanup(storage) - t.end() - }) + await cleanup(storage) + t.end() +}) - await t.test('oplog - malformed log entry gets overwritten', async function (t) { - let storage = testStorage() - let log = new Oplog(storage) +test('oplog - malformed log entry gets overwritten', async function (t) { + let storage = testStorage() + let log = new Oplog(storage) - await log.flush(Buffer.from('header')) - await log.append(Buffer.from('a')) - await log.append(Buffer.from('b')) - await log.close() + await log.flush(Buffer.from('header')) + await log.append(Buffer.from('a')) + await log.append(Buffer.from('b')) + await log.close() - const offset = log.byteLength + const offset = log.byteLength - storage = testStorage() - log = new Oplog(storage) + storage = testStorage() + log = new Oplog(storage) - // Write a bad oplog message at the end (simulates a failed append) - await new Promise((resolve, reject) => { - storage.write(offset + 4096 * 2, Buffer.from([0, 0, 0, 0, 4, 0, 0, 0]), err => { - if (err) return reject(err) - return resolve() - }) + // Write a bad oplog message at the end (simulates a failed append) + await new Promise((resolve, reject) => { + storage.write(offset + 4096 * 2, Buffer.from([0, 0, 0, 0, 4, 0, 0, 0]), err => { + if (err) return reject(err) + return resolve() }) + }) - { - const { entries } = await log.open() + { + const { entries } = await log.open() - t.is(entries.length, 2) // The partial entry should not be present - t.alike(entries[0], Buffer.from('a')) - t.alike(entries[1], Buffer.from('b')) - } + t.is(entries.length, 2) // The partial entry should not be present + t.alike(entries[0], Buffer.from('a')) + t.alike(entries[1], Buffer.from('b')) + } - // Write a valid oplog message now - await log.append(Buffer.from('c')) + // Write a valid oplog message now + await log.append(Buffer.from('c')) - { - const { entries } = await log.open() + { + const { entries } = await log.open() - t.is(entries.length, 3) // The partial entry should not be present - t.alike(entries[0], Buffer.from('a')) - t.alike(entries[1], Buffer.from('b')) - t.alike(entries[2], Buffer.from('c')) - } + t.is(entries.length, 3) // The partial entry should not be present + t.alike(entries[0], Buffer.from('a')) + t.alike(entries[1], Buffer.from('b')) + t.alike(entries[2], Buffer.from('c')) + } - await cleanup(storage) - t.end() - }) + await cleanup(storage) + t.end() +}) - await t.test('oplog - log not truncated when header write fails', async function (t) { - const storage = failingOffsetStorage(4096 * 2) +test('oplog - log not truncated when header write fails', async function (t) { + const storage = failingOffsetStorage(4096 * 2) - const log = new Oplog(storage) + const log = new Oplog(storage) - await log.flush(Buffer.from('header')) - await log.append(Buffer.from('a')) - await log.append(Buffer.from('b')) + await log.flush(Buffer.from('header')) + await log.append(Buffer.from('a')) + await log.append(Buffer.from('b')) - // Make subsequent header writes fail - storage[SHOULD_ERROR](true) + // Make subsequent header writes fail + storage[SHOULD_ERROR](true) - // The flush should fail because the header can't be updated -- log should still have entries after this - try { - await log.flush(Buffer.from('header two')) - } catch (err) { - t.ok(err.synthetic) - } + // The flush should fail because the header can't be updated -- log should still have entries after this + try { + await log.flush(Buffer.from('header two')) + } catch (err) { + t.ok(err.synthetic) + } - { - const { header, entries } = await log.open() + { + const { header, entries } = await log.open() - t.alike(header, Buffer.from('header')) - t.is(entries.length, 2) - t.alike(entries[0], Buffer.from('a')) - t.alike(entries[1], Buffer.from('b')) - } + t.alike(header, Buffer.from('header')) + t.is(entries.length, 2) + t.alike(entries[0], Buffer.from('a')) + t.alike(entries[1], Buffer.from('b')) + } - // Re-enable header writes - storage[SHOULD_ERROR](false) - await log.flush(Buffer.from('header two')) // Should correctly truncate the oplog now + // Re-enable header writes + storage[SHOULD_ERROR](false) + await log.flush(Buffer.from('header two')) // Should correctly truncate the oplog now - { - const { header, entries } = await log.open() + { + const { header, entries } = await log.open() - t.alike(header, Buffer.from('header two')) - t.is(entries.length, 0) - } + t.alike(header, Buffer.from('header two')) + t.is(entries.length, 0) + } - await cleanup(storage) - t.end() - }) + await cleanup(storage) + t.end() +}) - await t.test('oplog - multi append', async function (t) { - const storage = testStorage() +test('oplog - multi append', async function (t) { + const storage = testStorage() - const log = new Oplog(storage) + const log = new Oplog(storage) - await log.open() - await log.flush(Buffer.from('a')) + await log.open() + await log.flush(Buffer.from('a')) - await log.append([ - Buffer.from('1'), - Buffer.from('22'), - Buffer.from('333'), - Buffer.from('4') - ]) + await log.append([ + Buffer.from('1'), + Buffer.from('22'), + Buffer.from('333'), + Buffer.from('4') + ]) - t.is(log.length, 4) - t.is(log.byteLength, 32 + 1 + 2 + 3 + 1) + t.is(log.length, 4) + t.is(log.byteLength, 32 + 1 + 2 + 3 + 1) - const { header, entries } = await log.open() + const { header, entries } = await log.open() - t.alike(header, Buffer.from('a')) - t.alike(entries, [ - Buffer.from('1'), - Buffer.from('22'), - Buffer.from('333'), - Buffer.from('4') - ]) - - await cleanup(storage) - t.end() - }) + t.alike(header, Buffer.from('a')) + t.alike(entries, [ + Buffer.from('1'), + Buffer.from('22'), + Buffer.from('333'), + Buffer.from('4') + ]) - await t.test('oplog - multi append is atomic', async function (t) { - const storage = testStorage() + await cleanup(storage) + t.end() +}) - const log = new Oplog(storage) +test('oplog - multi append is atomic', async function (t) { + const storage = testStorage() - await log.open() - await log.flush(Buffer.from('a')) - - await log.append(Buffer.from('0')) - await log.append([ - Buffer.from('1'), - Buffer.from('22'), - Buffer.from('333'), - Buffer.from('4') - ]) - - t.is(log.length, 5) - t.is(log.byteLength, 40 + 1 + 1 + 2 + 3 + 1) - - // Corrupt the last write, should revert the full batch - await new Promise((resolve, reject) => { - storage.write(8192 + log.byteLength - 1, Buffer.from('x'), err => { - if (err) return reject(err) - return resolve() - }) - }) + const log = new Oplog(storage) - const { entries } = await log.open() + await log.open() + await log.flush(Buffer.from('a')) - t.is(log.length, 1) - t.alike(entries, [ - Buffer.from('0') - ]) + await log.append(Buffer.from('0')) + await log.append([ + Buffer.from('1'), + Buffer.from('22'), + Buffer.from('333'), + Buffer.from('4') + ]) - await cleanup(storage) - t.end() + t.is(log.length, 5) + t.is(log.byteLength, 40 + 1 + 1 + 2 + 3 + 1) + + // Corrupt the last write, should revert the full batch + await new Promise((resolve, reject) => { + storage.write(8192 + log.byteLength - 1, Buffer.from('x'), err => { + if (err) return reject(err) + return resolve() + }) }) + + const { entries } = await log.open() + + t.is(log.length, 1) + t.alike(entries, [ + Buffer.from('0') + ]) + + await cleanup(storage) + t.end() }) function testStorage () { From bcda232ace720b75664a69ded3d041c496405a5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Tue, 12 Oct 2021 12:02:02 +0200 Subject: [PATCH 10/10] Remove Standard config in `package.json` --- package.json | 5 ----- 1 file changed, 5 deletions(-) diff --git a/package.json b/package.json index 32fa6609..91afa070 100644 --- a/package.json +++ b/package.json @@ -40,10 +40,5 @@ }, "optionalDependencies": { "fsctl": "^1.0.0" - }, - "standard": { - "ignore": [ - "**/*.mjs" - ] } }