From 32245bcc13c0a90a16e70de0b4698b7b3519da28 Mon Sep 17 00:00:00 2001 From: Christophe Diederichs Date: Thu, 19 Sep 2024 09:46:13 +0100 Subject: [PATCH] add test for demoting indexer --- test/basic.js | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/test/basic.js b/test/basic.js index ee2c40f8..435b31ba 100644 --- a/test/basic.js +++ b/test/basic.js @@ -1460,6 +1460,68 @@ test('basic - promote writer to indexer', async t => { t.ok(b.isActiveIndexer) }) +test('basic - demote indexer to writer', async t => { + t.plan(13) + + const { bases } = await create(2, t, { apply }) + + const [a, b] = bases + + // add writer + await addWriterAndSync(a, b) + + t.is(a.linearizer.indexers.length, 2) + t.is(b.linearizer.indexers.length, 2) + + t.ok(b.isIndexer) + t.ok(b.isActiveIndexer) + + await b.append('message') + await confirm([a, b]) + + t.is(a.view.indexedLength, 1) + t.is(b.view.indexedLength, 1) + + // promote writer + await a.append({ remove: b4a.toString(b.local.key, 'hex') }) + + const event = new Promise(resolve => b.on('is-non-indexer', resolve)) + + await confirm([a, b]) + + t.is(a.linearizer.indexers.length, 1) + t.is(b.linearizer.indexers.length, 1) + + await replicateAndSync([a, b]) + + await t.execution(event) + + t.is(b.linearizer.indexers.length, 1) + t.absent(b.isIndexer) + t.absent(b.isActiveIndexer) + + // flush active writer set + await a.append(null) + + t.is(a.activeWriters.size, 1) + + async function apply (batch, view, base) { + for (const { value } of batch) { + if (value.add) { + await base.addWriter(b4a.from(value.add, 'hex')) + continue + } + + if (value.remove) { + await base.removeWriter(b4a.from(value.remove, 'hex')) + continue + } + + await view.append(value) + } + } +}) + test('basic - add new indexer after removing', async t => { const { bases } = await create(3, t, { apply: applyWithRemove }) const [a, b, c] = bases