P2-02: serialize persistence writes
This commit is contained in:
@@ -2,6 +2,7 @@ import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { createMockFacade } from '../src/facade/mockFacade'
|
||||
import { PROJECT_SCHEMA_MIGRATIONS, PROJECT_SCHEMA_SQL, PROJECT_SCHEMA_VERSION } from '../src/facade/projectSchema'
|
||||
import { PersistenceWriteQueue } from '../src/facade/projectStore'
|
||||
|
||||
test('facade exposes a stable initial document projection', () => {
|
||||
const facade = createMockFacade()
|
||||
@@ -19,6 +20,28 @@ test('project schema is versioned and covers the FreeCAD document graph', () =>
|
||||
assert.match(PROJECT_SCHEMA_SQL, /CREATE INDEX IF NOT EXISTS objects_document_ordinal/)
|
||||
})
|
||||
|
||||
test('persistence writes are serialized and continue after a failed write', async () => {
|
||||
const queue = new PersistenceWriteQueue()
|
||||
let inFlight = 0
|
||||
let maximumInFlight = 0
|
||||
const order: string[] = []
|
||||
const write = (id: string, shouldFail = false) => queue.run(async () => {
|
||||
inFlight += 1
|
||||
maximumInFlight = Math.max(maximumInFlight, inFlight)
|
||||
await new Promise((resolve) => setTimeout(resolve, 2))
|
||||
order.push(id)
|
||||
inFlight -= 1
|
||||
if (shouldFail) throw new Error('expected write failure')
|
||||
return id
|
||||
})
|
||||
const results = await Promise.allSettled([write('one'), write('two', true), write('three')])
|
||||
assert.equal(maximumInFlight, 1)
|
||||
assert.deepEqual(order, ['one', 'two', 'three'])
|
||||
assert.equal(results[0].status, 'fulfilled')
|
||||
assert.equal(results[1].status, 'rejected')
|
||||
assert.equal(results[2].status, 'fulfilled')
|
||||
})
|
||||
|
||||
test('workbench and selection changes are event driven', () => {
|
||||
const facade = createMockFacade()
|
||||
const events: string[] = []
|
||||
|
||||
Reference in New Issue
Block a user