diff --git a/src/serviceFeatures/replication/ReplicateResultProcessor.ts b/src/serviceFeatures/replication/ReplicateResultProcessor.ts index 075ebd46..8c481064 100644 --- a/src/serviceFeatures/replication/ReplicateResultProcessor.ts +++ b/src/serviceFeatures/replication/ReplicateResultProcessor.ts @@ -521,6 +521,8 @@ export class ReplicateResultProcessor { releaser = await this._semaphore.acquire(); await this._applyToDatabase(doc); } catch (e) { + // An exception during application must not be mistaken for a fully reflected Vault. + this.context.onVaultReflectionFailure?.(); this.log(`Error while processing replication result`, LOG_LEVEL_NOTICE); this.logError(e); } finally { @@ -630,6 +632,7 @@ export class ReplicateResultProcessor { // Note that this is not a common case. return true; } else { + this.context.onVaultReflectionFailure?.(); this.log( `Failed to get existing document for ${path} (${shortenId(dbDoc._id)}, ${shortenRev(dbDoc._rev)}) `, LOG_LEVEL_NOTICE diff --git a/src/serviceFeatures/replication/ReplicateResultProcessor.unit.spec.ts b/src/serviceFeatures/replication/ReplicateResultProcessor.unit.spec.ts index c1bf5680..54141175 100644 --- a/src/serviceFeatures/replication/ReplicateResultProcessor.unit.spec.ts +++ b/src/serviceFeatures/replication/ReplicateResultProcessor.unit.spec.ts @@ -38,6 +38,8 @@ function note(id: string): PouchDB.Core.ExistingDocument { type SetupOptions = { onVaultReflectionFailure?: () => void; + getRaw?: (id: string) => Promise; + getDBEntryFromMeta?: (entry: object) => Promise; applicationReady?: boolean; databaseReady?: boolean; maxMTimeForReflectEvents?: number; @@ -58,7 +60,7 @@ function setup(options: SetupOptions = {}) { const onCloseActiveReplication = vi.fn(async () => true); const isReady = vi.fn(() => options.applicationReady ?? true); const isValidPath = vi.fn(options.isValidPath ?? (() => true)); - const getDBEntryFromMeta = vi.fn(async (entry: object) => ({ ...entry, data: "x" })); + const getDBEntryFromMeta = vi.fn(options.getDBEntryFromMeta ?? (async (entry: object) => ({ ...entry, data: "x" }))); const localPhysicalDatabase = { ...(options.databaseId ? { id: vi.fn(async () => options.databaseId) } : {}), } as PouchDB.Database; @@ -90,6 +92,7 @@ function setup(options: SetupOptions = {}) { if (options.localVersionInfo === undefined) throw { status: 404 }; return options.localVersionInfo; } + if (options.getRaw) return await options.getRaw(id); return { _id: id, _rev: "1-test" }; }), getDBEntryFromMeta, @@ -126,6 +129,27 @@ function setup(options: SetupOptions = {}) { } describe("ReplicateResultProcessor", () => { + it("reports an IndexedDB read error as failed reflection", async () => { + const onVaultReflectionFailure = vi.fn(); + const { processor, processSynchroniseResult } = setup({ + onVaultReflectionFailure, + getRaw: async () => { throw new Error("IndexedDB read failed"); }, + }); + processor.enqueueAll([note("unreadable")]); + await vi.waitFor(() => expect(onVaultReflectionFailure).toHaveBeenCalledOnce()); + expect(processSynchroniseResult).not.toHaveBeenCalled(); + }); + + it("reports a thrown document reconstruction as failed reflection", async () => { + const onVaultReflectionFailure = vi.fn(); + const { processor } = setup({ + onVaultReflectionFailure, + getDBEntryFromMeta: async () => { throw new Error("Cannot read chunks"); }, + }); + processor.enqueueAll([note("missing-chunks")]); + await vi.waitFor(() => expect(onVaultReflectionFailure).toHaveBeenCalledOnce()); + }); + it("publishes pending received documents synchronously even if snapshot persistence stalls", () => { const { processor, replicationResultCount, processSynchroniseResult } = setup({ applicationReady: false,