|
| 1 | +import { test } from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { DatabaseSync } from 'node:sqlite'; |
| 4 | +import type { Readable } from 'node:stream'; |
| 5 | +import { join } from 'node:path'; |
| 6 | + |
| 7 | +import { |
| 8 | + Replica, |
| 9 | + FileReplicaClient, |
| 10 | + type ReplicaClient, |
| 11 | + type FileInfo, |
| 12 | + type LTXFilesOptions, |
| 13 | + type OpenLTXFileOptions, |
| 14 | + type WriteLTXFileOptions, |
| 15 | +} from '../../src/index.ts'; |
| 16 | +import type { Logger } from '../../src/util/logger.ts'; |
| 17 | +import { createWALDB, makeTempDir } from '../helpers/make-db.ts'; |
| 18 | + |
| 19 | +async function waitUntil(fn: () => Promise<boolean>, timeoutMs = 8000): Promise<void> { |
| 20 | + const start = Date.now(); |
| 21 | + while (Date.now() - start < timeoutMs) { |
| 22 | + if (await fn()) return; |
| 23 | + await new Promise((r) => setTimeout(r, 5)); |
| 24 | + } |
| 25 | + throw new Error('timed out waiting for condition'); |
| 26 | +} |
| 27 | + |
| 28 | +/** A ReplicaClient that fails the first write, then delegates. */ |
| 29 | +class FlakyOnceClient implements ReplicaClient { |
| 30 | + private inner: FileReplicaClient; |
| 31 | + private failed = false; |
| 32 | + constructor(inner: FileReplicaClient) { |
| 33 | + this.inner = inner; |
| 34 | + } |
| 35 | + type(): string { |
| 36 | + return this.inner.type(); |
| 37 | + } |
| 38 | + init(): Promise<void> { |
| 39 | + return this.inner.init(); |
| 40 | + } |
| 41 | + ltxFiles(opts: LTXFilesOptions): AsyncIterable<FileInfo> { |
| 42 | + return this.inner.ltxFiles(opts); |
| 43 | + } |
| 44 | + openLtxFile(opts: OpenLTXFileOptions): Promise<Readable> { |
| 45 | + return this.inner.openLtxFile(opts); |
| 46 | + } |
| 47 | + async writeLtxFile( |
| 48 | + opts: WriteLTXFileOptions, |
| 49 | + body: AsyncIterable<Uint8Array | string>, |
| 50 | + ): Promise<FileInfo> { |
| 51 | + if (!this.failed) { |
| 52 | + this.failed = true; |
| 53 | + throw new Error('transient write failure'); |
| 54 | + } |
| 55 | + return this.inner.writeLtxFile(opts, body); |
| 56 | + } |
| 57 | + deleteLtxFiles(infos: FileInfo[]): Promise<void> { |
| 58 | + return this.inner.deleteLtxFiles(infos); |
| 59 | + } |
| 60 | + deleteAll(): Promise<void> { |
| 61 | + return this.inner.deleteAll(); |
| 62 | + } |
| 63 | + setLogger(logger: Logger): void { |
| 64 | + this.inner.setLogger(logger); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +test('run() continuously syncs new writes until aborted', async () => { |
| 69 | + const t = createWALDB(['a', 'b', 'c']); |
| 70 | + const client = new FileReplicaClient(makeTempDir('repl-')); |
| 71 | + const replica = new Replica(t.path, client); |
| 72 | + const ac = new AbortController(); |
| 73 | + |
| 74 | + const runP = replica.run({ intervalMs: 25, signal: ac.signal }); |
| 75 | + try { |
| 76 | + // Let the first sync run, then add more data. |
| 77 | + await new Promise((r) => setTimeout(r, 30)); |
| 78 | + t.db.exec("INSERT INTO t (name) VALUES ('d'), ('e'), ('f')"); |
| 79 | + |
| 80 | + // The continuous loop should pick up the new rows on a later iteration. |
| 81 | + await waitUntil(async () => (await replica.syncStatus()).localTXID >= 2n); |
| 82 | + |
| 83 | + ac.abort(); |
| 84 | + await runP; |
| 85 | + |
| 86 | + const out = join(makeTempDir('restore-'), 'db.db'); |
| 87 | + await replica.restore({ outputPath: out }); |
| 88 | + const r = new DatabaseSync(out); |
| 89 | + const count = r.prepare('SELECT count(*) AS n FROM t').get() as { n: number }; |
| 90 | + assert.equal(count.n, 6); |
| 91 | + const ic = r.prepare('PRAGMA integrity_check').get() as { integrity_check: string }; |
| 92 | + assert.equal(ic.integrity_check, 'ok'); |
| 93 | + r.close(); |
| 94 | + } finally { |
| 95 | + ac.abort(); |
| 96 | + replica.close(); |
| 97 | + t.close(); |
| 98 | + } |
| 99 | +}); |
| 100 | + |
| 101 | +test('run() keeps replicating after a transient write failure', async () => { |
| 102 | + const t = createWALDB(['a', 'b']); |
| 103 | + const client = new FlakyOnceClient(new FileReplicaClient(makeTempDir('repl-'))); |
| 104 | + const replica = new Replica(t.path, client); |
| 105 | + const ac = new AbortController(); |
| 106 | + |
| 107 | + const runP = replica.run({ intervalMs: 25, signal: ac.signal }); |
| 108 | + try { |
| 109 | + // Add data; the first upload will fail, but the loop retries and recovers. |
| 110 | + await new Promise((r) => setTimeout(r, 30)); |
| 111 | + t.db.exec("INSERT INTO t (name) VALUES ('c'), ('d')"); |
| 112 | + |
| 113 | + // localTXID advances on the local write; remoteTXID >= 2 means the retry |
| 114 | + // eventually uploaded everything despite the transient failure. |
| 115 | + await waitUntil(async () => (await replica.syncStatus()).remoteTXID >= 2n); |
| 116 | + |
| 117 | + ac.abort(); |
| 118 | + await runP; |
| 119 | + |
| 120 | + const out = join(makeTempDir('restore-'), 'db.db'); |
| 121 | + await replica.restore({ outputPath: out }); |
| 122 | + const r = new DatabaseSync(out); |
| 123 | + const count = r.prepare('SELECT count(*) AS n FROM t').get() as { n: number }; |
| 124 | + assert.equal(count.n, 4); |
| 125 | + const ic = r.prepare('PRAGMA integrity_check').get() as { integrity_check: string }; |
| 126 | + assert.equal(ic.integrity_check, 'ok'); |
| 127 | + r.close(); |
| 128 | + } finally { |
| 129 | + ac.abort(); |
| 130 | + replica.close(); |
| 131 | + t.close(); |
| 132 | + } |
| 133 | +}); |
0 commit comments