From f5c93fee370bbeb4c30d300a9d138494fff30c1e Mon Sep 17 00:00:00 2001 From: Liran Cohen Date: Mon, 21 Oct 2024 17:43:23 -0400 Subject: [PATCH] test sync error thrown --- .../agent/tests/sync-engine-level.spec.ts | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/packages/agent/tests/sync-engine-level.spec.ts b/packages/agent/tests/sync-engine-level.spec.ts index 4ce1a509d..95555c685 100644 --- a/packages/agent/tests/sync-engine-level.spec.ts +++ b/packages/agent/tests/sync-engine-level.spec.ts @@ -2002,6 +2002,44 @@ describe('SyncEngineLevel', () => { syncSpy.restore(); clock.restore(); }); + + it('should log sync errors, but continue syncing the next interval', async () => { + await testHarness.agent.sync.registerIdentity({ + did: alice.did.uri, + }); + + const clock = sinon.useFakeTimers({ shouldClearNativeTimers: true }); + const syncSpy = sinon.stub(SyncEngineLevel.prototype as any, 'sync'); + + syncSpy.returns(new Promise((resolve, reject) => { + clock.setTimeout(() => { + resolve(); + }, 100); + })); + + // first call is the initial sync, 2nd and onward are the intervals + // on the 2nd interval (3rd call), we reject the promise, a 4th call should be made + syncSpy.onThirdCall().rejects(new Error('Sync error')); + + // spy on console.error to check if the error message is logged + const consoleErrorSpy = sinon.stub(console, 'error').resolves(); + + testHarness.agent.sync.startSync({ interval: '500ms' }); + + // three intervals + await clock.tickAsync(1_500); + + // this should equal 4, once for the initial call and once for each interval call + expect(syncSpy.callCount).to.equal(4); + + // check if the error message is logged + expect(consoleErrorSpy.callCount).to.equal(1); + expect(consoleErrorSpy.args[0][0]).to.include('SyncEngineLevel: Error during sync operation'); + + syncSpy.restore(); + consoleErrorSpy.restore(); + clock.restore(); + }); }); describe('stopSync()', () => {