Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish committed Dec 30, 2023
1 parent f629f06 commit a7f98f1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export const localVariablesSync: IntegrationFn = (
options: Options = {},
session: DebugSession | undefined = tryNewAsyncSession(),
) => {
const _cachedFrames: LRUMap<string, FrameVariables[]> = new LRUMap(20);
const cachedFrames: LRUMap<string, FrameVariables[]> = new LRUMap(20);
let rateLimiter: RateLimitIncrement | undefined;
let shouldProcessEvent = false;

Expand All @@ -242,7 +242,7 @@ export const localVariablesSync: IntegrationFn = (
}

const { add, next } = createCallbackList<FrameVariables[]>(frames => {
_cachedFrames.set(exceptionHash, frames);
cachedFrames.set(exceptionHash, frames);
complete();
});

Expand Down Expand Up @@ -284,7 +284,7 @@ export const localVariablesSync: IntegrationFn = (

// Check if we have local variables for an exception that matches the hash
// remove is identical to get but also removes the entry from the cache
const cachedFrame = _cachedFrames.remove(hash);
const cachedFrame = cachedFrames.remove(hash);

if (cachedFrame === undefined) {
return;
Expand Down Expand Up @@ -375,6 +375,13 @@ export const localVariablesSync: IntegrationFn = (

return event;
},
// These are entirely for testing
_getCachedFramesCount(): number {
return cachedFrames.size;
},
_getFirstCachedFrame(): FrameVariables[] | undefined {
return cachedFrames.values()[0];
},
};
};

Expand Down
28 changes: 13 additions & 15 deletions packages/node/test/integrations/localvariables.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { LRUMap } from '@sentry/utils';
import type { Debugger, InspectorNotification } from 'inspector';

import { NodeClient, defaultStackParser } from '../../src';
import { createRateLimiter } from '../../src/integrations/localvariables/common';
import type { FrameVariables } from '../../src/integrations/localvariables/common';
import type { DebugSession } from '../../src/integrations/localvariables/localvariables-sync';
import { LocalVariablesSync, createCallbackList } from '../../src/integrations/localvariables/localvariables-sync';
import { createRateLimiter } from '../../src/integrations/local-variables/common';
import type { FrameVariables } from '../../src/integrations/local-variables/common';
import type { DebugSession } from '../../src/integrations/local-variables/local-variables-sync';
import { LocalVariablesSync, createCallbackList } from '../../src/integrations/local-variables/local-variables-sync';
import { NODE_VERSION } from '../../src/nodeVersion';
import { getDefaultNodeClientOptions } from '../../test/helper/node-client-options';

Expand Down Expand Up @@ -52,7 +51,8 @@ class MockDebugSession implements DebugSession {
}

interface LocalVariablesPrivate {
_cachedFrames: LRUMap<string, FrameVariables[]>;
_getCachedFramesCount(): number;
_getFirstCachedFrame(): FrameVariables[] | undefined;
}

const exceptionEvent = {
Expand Down Expand Up @@ -175,9 +175,9 @@ describeIf(NODE_VERSION.major >= 18)('LocalVariables', () => {

await session.runPause(exceptionEvent);

expect((localVariables as unknown as LocalVariablesPrivate)._cachedFrames.size).toBe(1);
expect((localVariables as unknown as LocalVariablesPrivate)._getCachedFramesCount()).toBe(1);

const frames: FrameVariables[] = (localVariables as unknown as LocalVariablesPrivate)._cachedFrames.values()[0];
const frames = (localVariables as unknown as LocalVariablesPrivate)._getFirstCachedFrame();

expect(frames).toBeDefined();

Expand Down Expand Up @@ -244,7 +244,7 @@ describeIf(NODE_VERSION.major >= 18)('LocalVariables', () => {
expect(event?.exception?.values?.[0].stacktrace?.frames?.[3]?.vars).toEqual({ arr: [1, 2, 3] });
expect(event?.exception?.values?.[0].stacktrace?.frames?.[4]?.vars).toEqual({ name: 'tim' });

expect((localVariables as unknown as LocalVariablesPrivate)._cachedFrames.size).toBe(0);
expect((localVariables as unknown as LocalVariablesPrivate)._getCachedFramesCount()).toBe(0);
});

it('Only considers the first 5 frames', async () => {
Expand All @@ -261,9 +261,9 @@ describeIf(NODE_VERSION.major >= 18)('LocalVariables', () => {

await session.runPause(exceptionEvent100Frames);

expect((localVariables as unknown as LocalVariablesPrivate)._cachedFrames.size).toBe(1);
expect((localVariables as unknown as LocalVariablesPrivate)._getCachedFramesCount()).toBe(1);

const frames: FrameVariables[] = (localVariables as unknown as LocalVariablesPrivate)._cachedFrames.values()[0];
const frames = (localVariables as unknown as LocalVariablesPrivate)._getFirstCachedFrame();

expect(frames).toBeDefined();

Expand Down Expand Up @@ -291,7 +291,7 @@ describeIf(NODE_VERSION.major >= 18)('LocalVariables', () => {

await session.runPause(nonExceptionEvent);

expect((localVariables as unknown as LocalVariablesPrivate)._cachedFrames.size).toBe(0);
expect((localVariables as unknown as LocalVariablesPrivate)._getCachedFramesCount()).toBe(0);
});

it('Should not initialize when disabled', async () => {
Expand All @@ -309,7 +309,6 @@ describeIf(NODE_VERSION.major >= 18)('LocalVariables', () => {
const eventProcessor = eventProcessors.find(processor => processor.id === 'LocalVariablesSync');

expect(eventProcessor).toBeDefined();
expect(localVariables['_shouldProcessEvent']).toBe(false);
});

it('Should not initialize when inspector not loaded', async () => {
Expand All @@ -326,7 +325,6 @@ describeIf(NODE_VERSION.major >= 18)('LocalVariables', () => {
const eventProcessor = eventProcessors.find(processor => processor.id === 'LocalVariablesSync');

expect(eventProcessor).toBeDefined();
expect(localVariables['_shouldProcessEvent']).toBe(false);
});

it('Should cache identical uncaught exception events', async () => {
Expand All @@ -350,7 +348,7 @@ describeIf(NODE_VERSION.major >= 18)('LocalVariables', () => {
await session.runPause(exceptionEvent);
await session.runPause(exceptionEvent);

expect((localVariables as unknown as LocalVariablesPrivate)._cachedFrames.size).toBe(1);
expect((localVariables as unknown as LocalVariablesPrivate)._getCachedFramesCount()).toBe(1);
});

describe('createCallbackList', () => {
Expand Down

0 comments on commit a7f98f1

Please sign in to comment.