diff --git a/.github/workflows/tests-ci.yml b/.github/workflows/tests-ci.yml index 583ded74b..465d28f9b 100644 --- a/.github/workflows/tests-ci.yml +++ b/.github/workflows/tests-ci.yml @@ -53,17 +53,13 @@ jobs: - name: Start dwn-server container run: cd packages/dev-env && docker-compose up -d - - name: Wait for dwn-server1 to be ready + - name: Wait for dwn-server to be ready run: until curl -sf http://localhost:3000/health; do echo -n .; sleep .1; done - - name: Wait for dwn-server2 to be ready - run: until curl -sf http://localhost:3001/health; do echo -n .; sleep .1; done - - name: Run tests for all packages run: npm run test:node --ws -- --color env: - TEST_DWN_URLS: http://localhost:3000,http://localhost:3001 - MIN_SYNC_INTERVAL: 100 + TEST_DWN_URLS: http://localhost:3000 - name: Upload test coverage to Codecov uses: codecov/codecov-action@eaaf4bedf32dbdc6b720b63067d99c4d77d6047d # v3.1.4 @@ -110,14 +106,10 @@ jobs: - name: Start dwn-server container run: cd packages/dev-env && docker-compose up -d - - name: Wait for dwn-server1 to be ready + - name: Wait for dwn-server to be ready run: until curl -sf http://localhost:3000/health; do echo -n .; sleep .1; done - - name: Wait for dwn-server2 to be ready - run: until curl -sf http://localhost:3001/health; do echo -n .; sleep .1; done - - name: Run tests for all packages run: npm run test:browser --ws -- --color env: - TEST_DWN_URLS: http://localhost:3000,http://localhost:3001 - MIN_SYNC_INTERVAL: 100 + TEST_DWN_URLS: http://localhost:3000 diff --git a/packages/agent/src/sync-manager.ts b/packages/agent/src/sync-manager.ts index 94c139fe0..9100bf64a 100644 --- a/packages/agent/src/sync-manager.ts +++ b/packages/agent/src/sync-manager.ts @@ -17,10 +17,8 @@ import type { Web5ManagedAgent } from './types/agent.js'; import { webReadableToIsomorphicNodeReadable } from './utils.js'; -const checkNumber = (n?: string) => isNaN(parseInt(n || '')) ? undefined : parseInt(n || ''); // arbitrary number for now, but we should enforce some sane minimum -// allow for environment to set a minimum -const MIN_SYNC_INTERVAL = checkNumber(process?.env.MIN_SYNC_INTERVAL) ?? 5000; +export const MIN_SYNC_INTERVAL = 3000; type SyncDirection = 'pull' | 'push'; diff --git a/packages/agent/tests/sync-manager.spec.ts b/packages/agent/tests/sync-manager.spec.ts index cd51f0e27..d8f14c31a 100644 --- a/packages/agent/tests/sync-manager.spec.ts +++ b/packages/agent/tests/sync-manager.spec.ts @@ -1,14 +1,14 @@ import type { PortableDid } from '@web5/dids'; import { expect } from 'chai'; -import * as sinon from 'sinon'; +import sinon from 'sinon'; import type { ManagedIdentity } from '../src/identity-manager.js'; import { randomUuid } from '@web5/crypto/utils'; import { testDwnUrls } from './test-config.js'; -import { TestAgent, sleep } from './utils/test-agent.js'; -import { SyncManagerLevel } from '../src/sync-manager.js'; +import { TestAgent } from './utils/test-agent.js'; +import { MIN_SYNC_INTERVAL, SyncManagerLevel } from '../src/sync-manager.js'; import { TestManagedAgent } from '../src/test-managed-agent.js'; import { ProcessDwnRequest } from '../src/index.js'; @@ -74,6 +74,45 @@ describe('SyncManagerLevel', () => { await testAgent.closeStorage(); }); + describe('startSync()', () => { + let clock: sinon.SinonFakeTimers; + + beforeEach(() => { + clock = sinon.useFakeTimers(); + }); + + afterEach(() => { + clock.restore(); + }); + + it('check sync interval input', async () => { + const syncSpy = sinon.spy(testAgent.agent.syncManager, 'sync'); + await testAgent.agent.syncManager.registerIdentity({ + did: alice.did + }); + testAgent.agent.syncManager.startSync({ interval: 5000 }); + + clock.tick(3 * 5000); + + expect(syncSpy.callCount).to.equal(3); + syncSpy.restore(); + }); + + it('check sync default value passed', async () => { + const setIntervalSpy = sinon.spy(global, 'setInterval'); + await testAgent.agent.syncManager.registerIdentity({ + did: alice.did + }); + testAgent.agent.syncManager.startSync(); + + clock.tick( 1 * MIN_SYNC_INTERVAL); + + expect(setIntervalSpy.calledOnce).to.be.true; + expect(setIntervalSpy.getCall(0).args.at(1)).to.equal(MIN_SYNC_INTERVAL); + setIntervalSpy.restore(); + }); + }); + describe('sync()', () => { it('takes no action if no identities are registered', async () => { const didResolveSpy = sinon.spy(testAgent.agent.didResolver, 'resolve'); @@ -152,31 +191,6 @@ describe('SyncManagerLevel', () => { }).timeout(10_000); - // tests must be run with a low MIN_SYNC_INTERVAL - it('check sync interval input', async () => { - const syncSpy = sinon.spy(testAgent.agent.syncManager, 'sync'); - await testAgent.agent.syncManager.registerIdentity({ - did: alice.did - }); - testAgent.agent.syncManager.startSync({ interval: 300 }); - await sleep(1000); - expect(syncSpy.callCount).to.equal(3); - syncSpy.restore(); - }); - - // test must be run with MIN_SYNC_INTERVAL=100 - it('check sync default value passed', async () => { - const setIntervalSpy = sinon.spy(global, 'setInterval'); - await testAgent.agent.syncManager.registerIdentity({ - did: alice.did - }); - testAgent.agent.syncManager.startSync({ }); - await sleep(500); - expect(setIntervalSpy.calledOnce).to.be.true; - expect(setIntervalSpy.getCall(0).args.at(1)).to.equal(100); - setIntervalSpy.restore(); - }); - describe('batchOperations()', () => { it('should only call once per remote DWN if pull direction is passed', async () => { const batchOperationsSpy = sinon.spy(testAgent.agent.syncManager as any, 'batchOperations'); @@ -213,14 +227,6 @@ describe('SyncManagerLevel', () => { }); }); - describe('watermarks', async () =>{ - await testAgent.agent.syncManager.registerIdentity({ - did: alice.did - }); - testAgent.agent.syncManager.startSync({ }); - await sleep(500); - }); - describe('pull', () => { it('synchronizes records for 1 identity from remote DWN to local DWN', async () => { // Write a test record to Alice's remote DWN. diff --git a/packages/dev-env/docker-compose.yaml b/packages/dev-env/docker-compose.yaml index c12ad8c75..54a5ebab6 100644 --- a/packages/dev-env/docker-compose.yaml +++ b/packages/dev-env/docker-compose.yaml @@ -1,13 +1,8 @@ version: "3.98" services: - dwn-server2: - container_name: dwn-server2 - image: ghcr.io/tbd54566975/dwn-server:dwn-sdk-0.2.1 - ports: - - "3001:3000" - dwn-server1: - container_name: dwn-server1 + dwn-server: + container_name: dwn-server image: ghcr.io/tbd54566975/dwn-server:dwn-sdk-0.2.1 ports: - "3000:3000"