Skip to content

Commit

Permalink
add test for demoting indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
chm-diederichs committed Sep 19, 2024
1 parent e34377a commit 32245bc
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 32245bc

Please sign in to comment.