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

chore(test): Streaming mock fixes. Mock cleanup. #549

Merged
merged 3 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
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
7 changes: 2 additions & 5 deletions packages/shared/mocks/src/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import type { Hasher } from '@common';

// eslint-disable-next-line import/no-mutable-exports
export let hasher: Hasher;

export const setupCrypto = () => {
let counter = 0;
hasher = {
update: jest.fn(() => hasher),
const hasher = {
update: jest.fn((): Hasher => hasher),
digest: jest.fn(() => '1234567890123456'),
};

Expand Down
25 changes: 0 additions & 25 deletions packages/shared/mocks/src/hasher.ts
Copy link
Member Author

Choose a reason for hiding this comment

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

File was redundant.

This file was deleted.

24 changes: 19 additions & 5 deletions packages/shared/mocks/src/streamingProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export const setupMockStreamingProcessor = (
errorTimeoutSeconds: number = 0,
initTimeoutMs: number = 0,
) => {
let initTimeoutHandle: any;
Copy link
Member Author

Choose a reason for hiding this comment

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

The mock stream was not closing its timer handles so they interfered with other tests.

let patchTimeoutHandle: any;
let deleteTimeoutHandle: any;

MockStreamingProcessor.mockImplementation(
(
sdkKey: string,
Expand All @@ -28,26 +32,36 @@ export const setupMockStreamingProcessor = (
) => ({
start: jest.fn(async () => {
if (shouldError) {
setTimeout(() => {
initTimeoutHandle = setTimeout(() => {
const unauthorized = new Error('test-error') as LDStreamingError;
// @ts-ignore
unauthorized.code = 401;
errorHandler(unauthorized);
}, errorTimeoutSeconds * 1000);
} else {
// execute put which will resolve the identify promise
setTimeout(() => listeners.get('put')?.processJson(putResponseJson), initTimeoutMs);
initTimeoutHandle = setTimeout(() => {
listeners.get('put')?.processJson(putResponseJson);
}, initTimeoutMs);

if (patchResponseJson) {
setTimeout(() => listeners.get('patch')?.processJson(patchResponseJson));
patchTimeoutHandle = setTimeout(() =>
listeners.get('patch')?.processJson(patchResponseJson),
);
}

if (deleteResponseJson) {
setTimeout(() => listeners.get('delete')?.processJson(deleteResponseJson));
deleteTimeoutHandle = setTimeout(() =>
listeners.get('delete')?.processJson(deleteResponseJson),
);
}
}
}),
close: jest.fn(),
close: jest.fn(() => {
clearTimeout(initTimeoutHandle);
clearTimeout(patchTimeoutHandle);
clearTimeout(deleteTimeoutHandle);
}),
eventSource: {},
}),
);
Expand Down
9 changes: 5 additions & 4 deletions packages/shared/sdk-client/src/LDClientImpl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ describe('sdk-client object', () => {
defaultPutResponse = clone<Flags>(mockResponseJson);
setupMockStreamingProcessor(false, defaultPutResponse);
mockPlatform.crypto.randomUUID.mockReturnValue('random1');
const hasher: Hasher = {
update: jest.fn(() => hasher),
const hasher = {
update: jest.fn((): Hasher => hasher),
digest: jest.fn(() => 'digested1'),
};
mockPlatform.crypto.createHash = jest.fn(() => hasher);
mockPlatform.crypto.createHash.mockReturnValue(hasher);

ldc = new LDClientImpl(testSdkKey, AutoEnvAttributes.Enabled, mockPlatform, {
logger,
Expand All @@ -72,7 +72,8 @@ describe('sdk-client object', () => {
.mockReturnValue('/stream/path');
});

afterEach(() => {
afterEach(async () => {
await ldc.close();
Copy link
Member Author

Choose a reason for hiding this comment

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

Close the client so timers and things stop.

jest.resetAllMocks();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ describe('Bucketer.test', () => {
],
])('given bucketing parameters', (context, key, attr, salt, kindForRollout, seed, expected) => {
it('hashes the correct string', () => {
const hasher: Hasher = {
update: jest.fn(() => hasher),
const hasher = {
update: jest.fn((): Hasher => hasher),
digest: jest.fn(() => '1234567890123456'),
};
mockPlatform.crypto.createHash = jest.fn(() => hasher);
mockPlatform.crypto.createHash.mockReturnValue(hasher);

const validatedContext = Context.fromLDContext(context);
const attrRef = new AttributeReference(attr);
Expand Down Expand Up @@ -120,11 +120,11 @@ describe('Bucketer.test', () => {
['bad', 'key'],
])('when given a non string or integer reference', (kind, attr) => {
it('buckets to 0 when given bad data', () => {
const hasher: Hasher = {
update: jest.fn(() => hasher),
const hasher = {
update: jest.fn((): Hasher => hasher),
digest: jest.fn(() => 'digested1'),
};
mockPlatform.crypto.createHash = jest.fn(() => hasher);
mockPlatform.crypto.createHash.mockReturnValue(hasher);

const validatedContext = Context.fromLDContext({
key: 'context-key',
Expand Down