Skip to content

Commit

Permalink
create test for syncing multiple records in both directions
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Jan 9, 2024
1 parent ec9b5c3 commit 04e81ec
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 15 deletions.
31 changes: 16 additions & 15 deletions packages/agent/src/sync-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,25 +298,26 @@ export class SyncManagerLevel implements SyncManager {
}): Promise<void> {
const { interval = 120_000 } = options;

const intervalSync = async () => {
if (this._syncIntervalId) {
clearInterval(this._syncIntervalId);
}
return new Promise((resolve, reject) => {

await this.push();
await this.pull();
const intervalSync = async () => {
if (this._syncIntervalId) {
clearInterval(this._syncIntervalId);
}

// then we start sync again
this._syncIntervalId = setInterval(intervalSync, interval);
};
try {
await this.push();
await this.pull();
} catch (error) {
this.stopSync();
reject(error);
}

return new Promise((resolve, reject) => {
try {
// then we start sync again
this._syncIntervalId = setInterval(intervalSync, interval);
} catch(error) {
this.stopSync();
reject(error);
}
};

this._syncIntervalId = setInterval(intervalSync, interval);
});
}

Expand Down
95 changes: 95 additions & 0 deletions packages/agent/tests/sync-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,101 @@ describe('SyncManagerLevel', () => {
await testAgent.closeStorage();
});

it('syncs multiple records in both directions', async () => {
// create 3 local records.
const localRecords: string[] = [];
for (let i = 0; i < 3; i++) {
const writeResponse = await testAgent.agent.dwnManager.processRequest({
author : alice.did,
target : alice.did,
messageType : 'RecordsWrite',
messageOptions : {
dataFormat: 'text/plain'
},
dataStream: new Blob([`Hello, ${i}`])
});

localRecords.push((writeResponse.message as RecordsWriteMessage).recordId);
}

// create 3 remote records
const remoteRecords: string[] = [];
for (let i = 0; i < 3; i++) {
let writeResponse = await testAgent.agent.dwnManager.sendRequest({
author : alice.did,
target : alice.did,
messageType : 'RecordsWrite',
messageOptions : {
dataFormat: 'text/plain'
},
dataStream: new Blob([`Hello, ${i}`])
});
remoteRecords.push((writeResponse.message as RecordsWriteMessage).recordId);
}

// query local and check for only local records
let localQueryResponse = await testAgent.agent.dwnManager.processRequest({
author : alice.did,
target : alice.did,
messageType : 'RecordsQuery',
messageOptions : { filter: { dataFormat: 'text/plain' } }
});
let localDwnQueryReply = localQueryResponse.reply as RecordsQueryReply;
expect(localDwnQueryReply.status.code).to.equal(200);
expect(localDwnQueryReply.entries).to.have.length(3);
let localRecordsFromQuery = localDwnQueryReply.entries?.map(entry => entry.recordId);
expect(localRecordsFromQuery).to.have.members(localRecords)

// query remote and check for only remote records
let remoteQueryResponse = await testAgent.agent.dwnManager.sendRequest({
author : alice.did,
target : alice.did,
messageType : 'RecordsQuery',
messageOptions : { filter: { dataFormat: 'text/plain' } }
});
let remoteDwnQueryReply = remoteQueryResponse.reply as RecordsQueryReply;
expect(remoteDwnQueryReply.status.code).to.equal(200);
expect(remoteDwnQueryReply.entries).to.have.length(3);
let remoteRecordsFromQuery = remoteDwnQueryReply.entries?.map(entry => entry.recordId);
expect(remoteRecordsFromQuery).to.have.members(remoteRecords);

// Register Alice's DID to be synchronized.
await testAgent.agent.syncManager.registerIdentity({
did: alice.did
});

// Execute Sync to pull all records from Alice's remote DWN to Alice's local DWN.
await testAgent.agent.syncManager.push();
await testAgent.agent.syncManager.pull();

// query local node to see all records
localQueryResponse = await testAgent.agent.dwnManager.processRequest({
author : alice.did,
target : alice.did,
messageType : 'RecordsQuery',
messageOptions : { filter: { dataFormat: 'text/plain' } }
});
localDwnQueryReply = localQueryResponse.reply as RecordsQueryReply;
expect(localDwnQueryReply.status.code).to.equal(200);
expect(localDwnQueryReply.entries).to.have.length(6);
localRecordsFromQuery = localDwnQueryReply.entries?.map(entry => entry.recordId);
expect(localRecordsFromQuery).to.have.members([...localRecords, ...remoteRecords]);

// query remote node to see all results
remoteQueryResponse = await testAgent.agent.dwnManager.sendRequest({
author : alice.did,
target : alice.did,
messageType : 'RecordsQuery',
messageOptions : { filter: { dataFormat: 'text/plain' } }
});
remoteDwnQueryReply = remoteQueryResponse.reply as RecordsQueryReply;
expect(remoteDwnQueryReply.status.code).to.equal(200);
expect(remoteDwnQueryReply.entries).to.have.length(6);
remoteRecordsFromQuery = remoteDwnQueryReply.entries?.map(entry => entry.recordId);
expect(remoteRecordsFromQuery).to.have.members([...localRecords, ...remoteRecords]);

});

describe('pull()', () => {
it('takes no action if no identities are registered', async () => {
const didResolveSpy = sinon.spy(testAgent.agent.didResolver, 'resolve');
Expand Down

0 comments on commit 04e81ec

Please sign in to comment.