Skip to content

Commit

Permalink
IndexMigrator: fix non blocking migration wrapper promise rejection (#…
Browse files Browse the repository at this point in the history
…77018) (#77092)

* fix transformNonBlocking

* add test for indexMigrator
  • Loading branch information
pgayvallet authored Sep 9, 2020
1 parent eeacf9e commit fa1a8cf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,30 @@ describe('IndexMigrator', () => {
],
});
});

test('rejects when the migration function throws an error', async () => {
const { client } = testOpts;
const migrateDoc = jest.fn((doc: SavedObjectUnsanitizedDoc) => {
throw new Error('error migrating document');
});

testOpts.documentMigrator = {
migrationVersion: { foo: '1.2.3' },
migrate: migrateDoc,
};

withIndex(client, {
numOutOfDate: 1,
docs: [
[{ _id: 'foo:1', _source: { type: 'foo', foo: { name: 'Bar' } } }],
[{ _id: 'foo:2', _source: { type: 'foo', foo: { name: 'Baz' } } }],
],
});

await expect(new IndexMigrator(testOpts).migrate()).rejects.toThrowErrorMatchingInlineSnapshot(
`"error migrating document"`
);
});
});

function withIndex(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,18 @@ describe('migrateRawDocs', () => {

expect(logger.error).toBeCalledTimes(1);
});

test('rejects when the transform function throws an error', async () => {
const transform = jest.fn<any, any>((doc: any) => {
throw new Error('error during transform');
});
await expect(
migrateRawDocs(
new SavedObjectsSerializer(new SavedObjectTypeRegistry()),
transform,
[{ _id: 'a:b', _source: { type: 'a', a: { name: 'AAA' } } }],
createSavedObjectsMigrationLoggerMock()
)
).rejects.toThrowErrorMatchingInlineSnapshot(`"error during transform"`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,14 @@ function transformNonBlocking(
): (doc: SavedObjectUnsanitizedDoc) => Promise<SavedObjectUnsanitizedDoc> {
// promises aren't enough to unblock the event loop
return (doc: SavedObjectUnsanitizedDoc) =>
new Promise((resolve) => {
new Promise((resolve, reject) => {
// set immediate is though
setImmediate(() => {
resolve(transform(doc));
try {
resolve(transform(doc));
} catch (e) {
reject(e);
}
});
});
}

0 comments on commit fa1a8cf

Please sign in to comment.