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

INT-3404 - Add tests to validate behavior of jobState.hasKey #694

Merged
merged 5 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions packages/integration-sdk-private-test-utils/src/graphObject.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Entity, ExplicitRelationship } from '@jupiterone/integration-sdk-core';
import { times } from 'lodash';
import { v4 as uuid } from 'uuid';

export function createTestEntity(partial?: Partial<Entity>): Entity {
Expand All @@ -11,6 +12,20 @@ export function createTestEntity(partial?: Partial<Entity>): Entity {
};
}

/**
* Create `n` test entities
*
* @param n {number} Number of test entities to create
* @returns {Entity[]}
*/
export function createTestEntities(n: number): Entity[] {
return times(n, (i) =>
createTestEntity({
_key: `entityKey:${i}`,
}),
);
}

export function createTestRelationship(
partial?: Partial<ExplicitRelationship>,
): ExplicitRelationship {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import {
createTestEntity,
createTestRelationship,
sleep,
createTestEntities,
} from '@jupiterone/integration-sdk-private-test-utils';
import {
createQueuedStepGraphObjectDataUploader,
CreateQueuedStepGraphObjectDataUploaderParams,
} from '../uploader';
import { FlushedGraphObjectData } from '../../storage/types';
import pMap from 'p-map';

jest.mock('fs');

Expand Down Expand Up @@ -172,6 +174,40 @@ describe('#hasKey', () => {
expect(await jobState.hasKey('A')).toBeTrue();
expect(await jobState.hasKey('a')).toBeTrue();
});

test('should handle concurrent reads from in-memory key store when using synchronous hasKey', async () => {
const jobState = createTestStepJobState();
const entities = createTestEntities(100);

const tenthEntity = entities[9];
entities.splice(10, 0, tenthEntity);

const results = await pMap(entities, async (e) => {
if (jobState.hasKey(e._key)) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that the documentation here

hasKey: (_key: string | undefined) => boolean | Promise<boolean>;
should change to provide reasons for why both sync and async forms are present, and when to use which.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Can tackle separately. Will open ticket. Thanks for suggestion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return await jobState.addEntity(e);
});

expect(results.length).toEqual(entities.length);
});

test('should fail to handle concurrent reads from in-memory key store when using asynchronous hasKey', async () => {
austinkelleher marked this conversation as resolved.
Show resolved Hide resolved
const jobState = createTestStepJobState();
const entities = createTestEntities(100);

const tenthEntity = entities[9];
entities.splice(10, 0, tenthEntity);

await expect(
pMap(entities, async (e) => {
// NOTE: Due to the event loop queue order in the Node.js, awaiting
// the `hasKey` promise while handling multiple entities concurrently,
// could result in a `hasKey` returning `false` because neither of the
// duplicate entities have been fully added to the job state yet.
if (await jobState.hasKey(e._key)) return;
await jobState.addEntity(e);
}),
).rejects.toThrowError('Duplicate _key detected (_key=entityKey:9)');
});
});

describe('upload callbacks', () => {
Expand Down