Skip to content

Commit

Permalink
use sinon SinonFakeTimers for sync interval test, remove multiple dwn…
Browse files Browse the repository at this point in the history
… servers from tests
  • Loading branch information
LiranCohen committed Sep 22, 2023
1 parent 32349d4 commit 5944fb0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 58 deletions.
16 changes: 4 additions & 12 deletions .github/workflows/tests-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
4 changes: 1 addition & 3 deletions packages/agent/src/sync-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
78 changes: 42 additions & 36 deletions packages/agent/tests/sync-manager.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 2 additions & 7 deletions packages/dev-env/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 5944fb0

Please sign in to comment.