Skip to content

Commit

Permalink
add sleep between initial write and update (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen authored Dec 10, 2023
1 parent 41d3550 commit 3bb47ca
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions tests/handlers/records-write.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4318,29 +4318,38 @@ export function testRecordsWriteHandler(): void {
});
});

it('should throw if `recordsWriteHandler.processMessageWithoutDataStream()` throws unknown error', async () => {
// simulate an initial write to test non-data path, as initial writes without data are always accepted (bot not readable)
// https://github.com/TBD54566975/dwn-sdk-js/issues/628
const { author, message: initialWriteMessage, recordsWrite: initialWrite } = await TestDataGenerator.generateRecordsWrite();
const { message, dataStream } = await TestDataGenerator.generateFromRecordsWrite({ author, existingWrite: initialWrite });
const tenant = author.did;
describe('unknown error', () => {
beforeEach(() => {
sinon.restore(); // wipe all previous stubs/spies/mocks/fakes
});

const didResolverStub = TestStubGenerator.createDidResolverStub(author);
it('should throw if `recordsWriteHandler.processMessageWithoutDataStream()` throws unknown error', async () => {
// simulate an initial write to test non-data path, as initial writes without data are always accepted (bot not readable)
// https://github.com/TBD54566975/dwn-sdk-js/issues/628
const { author, message: initialWriteMessage, recordsWrite: initialWrite } = await TestDataGenerator.generateRecordsWrite();
await Time.minimalSleep();

const { message, dataStream } = await TestDataGenerator.generateFromRecordsWrite({ author, existingWrite: initialWrite });
const tenant = author.did;

const messageStoreStub = stubInterface<MessageStore>();
messageStoreStub.query.resolves({ messages: [ initialWriteMessage ] });
const didResolverStub = TestStubGenerator.createDidResolverStub(author);

const dataStoreStub = stubInterface<DataStore>();
const recordsWriteHandler = new RecordsWriteHandler(didResolverStub, messageStoreStub, dataStoreStub, eventLog);
// simulate throwing unexpected error
sinon.stub(recordsWriteHandler as any, 'processMessageWithoutDataStream').throws(new Error('an unknown error in recordsWriteHandler.processMessageWithoutDataStream()'));
sinon.stub(recordsWriteHandler as any, 'processMessageWithDataStream').throws(new Error('an unknown error in recordsWriteHandler.processMessageWithDataStream()'));
const messageStoreStub = stubInterface<MessageStore>();
messageStoreStub.query.resolves({ messages: [ initialWriteMessage ] });

let handlerPromise = recordsWriteHandler.handle({ tenant, message, dataStream: dataStream! }); // with data stream
await expect(handlerPromise).to.be.rejectedWith('an unknown error in recordsWriteHandler.processMessageWithDataStream()');
const dataStoreStub = stubInterface<DataStore>();
const recordsWriteHandler = new RecordsWriteHandler(didResolverStub, messageStoreStub, dataStoreStub, eventLog);
// simulate throwing unexpected error
sinon.stub(recordsWriteHandler as any, 'processMessageWithoutDataStream').throws(new Error('an unknown error in recordsWriteHandler.processMessageWithoutDataStream()'));
sinon.stub(recordsWriteHandler as any, 'processMessageWithDataStream').throws(new Error('an unknown error in recordsWriteHandler.processMessageWithDataStream()'));

let handlerPromise = recordsWriteHandler.handle({ tenant, message, dataStream: dataStream! }); // with data stream
await expect(handlerPromise).to.be.rejectedWith('an unknown error in recordsWriteHandler.processMessageWithDataStream()');

handlerPromise = recordsWriteHandler.handle({ tenant, message }); // without data stream
await expect(handlerPromise).to.be.rejectedWith('an unknown error in recordsWriteHandler.processMessageWithoutDataStream()');
});

handlerPromise = recordsWriteHandler.handle({ tenant, message }); // without data stream
await expect(handlerPromise).to.be.rejectedWith('an unknown error in recordsWriteHandler.processMessageWithoutDataStream()');
});
});
}

0 comments on commit 3bb47ca

Please sign in to comment.