Skip to content

Commit

Permalink
sync interval tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Dec 11, 2023
1 parent 1acd342 commit 5911013
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
7 changes: 3 additions & 4 deletions packages/agent/src/sync-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ export class SyncManagerLevel implements SyncManager {
}): Promise<void> {
const { interval = 120_000 } = options;

const syncInterval = async () => {
const intervalSync = async () => {
if (this._syncIntervalId) {
clearInterval(this._syncIntervalId);
}
Expand All @@ -307,13 +307,12 @@ export class SyncManagerLevel implements SyncManager {
await this.pull();

// then we start sync again
this._syncIntervalId = setInterval(syncInterval, interval);
this._syncIntervalId = setInterval(intervalSync, interval);
};


return new Promise((resolve, reject) => {
try {
this._syncIntervalId = setInterval(syncInterval, interval);
this._syncIntervalId = setInterval(intervalSync, interval);
} catch(error) {
this.stopSync();
reject(error);
Expand Down
71 changes: 67 additions & 4 deletions packages/agent/tests/sync-manager.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import type { PortableDid } from '@web5/dids';

import { expect } from 'chai';
import * as sinon from 'sinon';
import { RecordsQueryReply, RecordsWriteMessage } from '@tbd54566975/dwn-sdk-js';
import sinon from 'sinon';
import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';

import type { ManagedIdentity } from '../src/identity-manager.js';
import type { PortableDid } from '@web5/dids';

import { RecordsQueryReply, RecordsWriteMessage } from '@tbd54566975/dwn-sdk-js';

import { testDwnUrl } from './utils/test-config.js';
import { TestAgent } from './utils/test-agent.js';
import { SyncManagerLevel } from '../src/sync-manager.js';
import { TestManagedAgent } from '../src/test-managed-agent.js';

chai.use(chaiAsPromised);

let testDwnUrls: string[] = [testDwnUrl];

describe('SyncManagerLevel', () => {
Expand Down Expand Up @@ -465,5 +469,64 @@ describe('SyncManagerLevel', () => {
expect(remoteDwnQueryReply.entries).to.have.length(1); // Record does exist on remote DWN.
});
});

describe('startSync()', () => {
it('calls push/pull in each interval', async () => {
await testAgent.agent.syncManager.registerIdentity({
did: alice.did
});

const pushSpy = sinon.stub(SyncManagerLevel.prototype, 'push');
pushSpy.resolves();

const pullSpy = sinon.stub(SyncManagerLevel.prototype, 'pull');
pullSpy.resolves();

const clock = sinon.useFakeTimers();

testAgent.agent.syncManager.startSync({ interval: 500 });

await clock.tickAsync(1_400); // just under 3 intervals
pushSpy.restore();
pullSpy.restore();
clock.restore();

expect(pushSpy.callCount).to.equal(2, 'push');
expect(pullSpy.callCount).to.equal(2, 'pull');
});

it('does not call push/pull again until a push/pull finishes', async () => {
await testAgent.agent.syncManager.registerIdentity({
did: alice.did
});

const clock = sinon.useFakeTimers();

const pushSpy = sinon.stub(SyncManagerLevel.prototype, 'push');
pushSpy.returns(new Promise((resolve) => {
clock.setTimeout(() => {
resolve();
}, 1_500); // more than the interval
}));

const pullSpy = sinon.stub(SyncManagerLevel.prototype, 'pull');
pullSpy.resolves();

testAgent.agent.syncManager.startSync({ interval: 500 });

await clock.tickAsync(1_400); // less time than the push

expect(pushSpy.callCount).to.equal(1, 'push');
expect(pullSpy.callCount).to.equal(0, 'pull'); // not called yet

await clock.tickAsync(100); //remaining time for pull to be called

expect(pullSpy.callCount).to.equal(1, 'pull');

pushSpy.restore();
pullSpy.restore();
clock.restore();
});
});
});
});

0 comments on commit 5911013

Please sign in to comment.