From 3bb47cae86387953952a4ac36ec5da39b976d45b Mon Sep 17 00:00:00 2001 From: LiranCohen Date: Sun, 10 Dec 2023 10:59:07 -0500 Subject: [PATCH] add sleep between initial write and update (#639) --- tests/handlers/records-write.spec.ts | 45 +++++++++++++++++----------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/tests/handlers/records-write.spec.ts b/tests/handlers/records-write.spec.ts index 7b2f8530b..3fc341437 100644 --- a/tests/handlers/records-write.spec.ts +++ b/tests/handlers/records-write.spec.ts @@ -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(); - messageStoreStub.query.resolves({ messages: [ initialWriteMessage ] }); + const didResolverStub = TestStubGenerator.createDidResolverStub(author); - const dataStoreStub = stubInterface(); - 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(); + 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(); + 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()'); }); }); }