| ... | @@ -5,9 +5,11 @@ import vm from 'node:vm'; | ... | @@ -5,9 +5,11 @@ import vm from 'node:vm'; |
| 5 | | 5 | |
| 6 | const source = await readFile(new URL('../../crates/snowbound/web/glue.js', import.meta.url), 'utf8'); | 6 | const source = await readFile(new URL('../../crates/snowbound/web/glue.js', import.meta.url), 'utf8'); |
| 7 | | 7 | |
| 8 | test('an OPFS flush rejection retains ordered writes until storage recovers', async () => { | 8 | test('OPFS retries preserve write order and canonical handle ownership', async () => { |
| 9 | const files = new Map([['existing', new Uint8Array([0])]]); | 9 | const files = new Map([['existing', new Uint8Array([0])]]); |
| 10 | const flushed = []; | 10 | const flushed = []; |
| | 11 | const held = new Set(); |
| | 12 | const opened = []; |
| 11 | const replies = []; | 13 | const replies = []; |
| 12 | let unavailable = false; | 14 | let unavailable = false; |
| 13 | const root = { | 15 | const root = { |
| ... | @@ -16,11 +18,19 @@ test('an OPFS flush rejection retains ordered writes until storage recovers', as | ... | @@ -16,11 +18,19 @@ test('an OPFS flush rejection retains ordered writes until storage recovers', as |
| 16 | yield [name, { kind: 'file', getFile: async () => ({ arrayBuffer: async () => bytes.buffer }) }]; | 18 | yield [name, { kind: 'file', getFile: async () => ({ arrayBuffer: async () => bytes.buffer }) }]; |
| 17 | } | 19 | } |
| 18 | }, | 20 | }, |
| | 21 | async removeEntry(path) { |
| | 22 | assert.equal(held.has(path), false, 'Removal closes the existing access handle'); |
| | 23 | files.delete(path); |
| | 24 | }, |
| 19 | async getFileHandle(path) { | 25 | async getFileHandle(path) { |
| 20 | let bytes = files.get(path)?.slice() ?? new Uint8Array(); | 26 | let bytes = files.get(path)?.slice() ?? new Uint8Array(); |
| 21 | return { | 27 | return { |
| 22 | async createSyncAccessHandle() { | 28 | async createSyncAccessHandle() { |
| | 29 | if (held.has(path)) throw new Error('Another access handle is open for ' + path); |
| | 30 | held.add(path); |
| | 31 | opened.push(path); |
| 23 | return { | 32 | return { |
| | 33 | close() { held.delete(path); }, |
| 24 | write(part, { at }) { | 34 | write(part, { at }) { |
| 25 | if (at + part.length > bytes.length) { | 35 | if (at + part.length > bytes.length) { |
| 26 | const extended = new Uint8Array(at + part.length); | 36 | const extended = new Uint8Array(at + part.length); |
| ... | @@ -70,4 +80,12 @@ test('an OPFS flush rejection retains ordered writes until storage recovers', as | ... | @@ -70,4 +80,12 @@ test('an OPFS flush rejection retains ordered writes until storage recovers', as |
| 70 | assert.deepEqual(flushed, [['replica-wal', [1, 2]], ['replica-wal', [1, 3]], ['replica', [4]]]); | 80 | assert.deepEqual(flushed, [['replica-wal', [1, 2]], ['replica-wal', [1, 3]], ['replica', [4]]]); |
| 71 | assert.equal((await send({ kind: 'settle' })).error, null); | 81 | assert.equal((await send({ kind: 'settle' })).error, null); |
| 72 | assert.equal(flushed.length, 3); | 82 | assert.equal(flushed.length, 3); |
| | 83 | assert.equal((await send({ kind: 'store', changes: [['/replica/', 1, [[0, new Uint8Array([5])]]]] })).error, null); |
| | 84 | assert.deepEqual([...files.get('replica')], [5]); |
| | 85 | assert.equal(opened.filter(path => path === 'replica').length, 1, 'Aliases reuse the existing handle'); |
| | 86 | assert.equal((await send({ kind: 'store', changes: [['/replica/']] })).error, null); |
| | 87 | assert.equal(files.has('replica'), false); |
| | 88 | assert.equal((await send({ kind: 'store', changes: [['replica', 1, [[0, new Uint8Array([6])]]]] })).error, null); |
| | 89 | assert.deepEqual([...files.get('replica')], [6]); |
| | 90 | assert.equal(opened.filter(path => path === 'replica').length, 2, 'Recreation opens a new handle'); |
| 73 | }); | 91 | }); |