Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sleep between initial write and update to prevent intermittent test failures. #639

Merged
merged 1 commit into from
Dec 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
thehenrytsai marked this conversation as resolved.
Show resolved Hide resolved
});

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()');
});
});
}