Skip to content

Commit aef6dba

Browse files
committed
Finialize test and make handler synchronous
1 parent d371316 commit aef6dba

File tree

3 files changed

+17
-52
lines changed
  • dev-packages/browser-integration-tests/suites/integrations/featureFlags/launchdarkly
  • packages/browser/src/integrations

3 files changed

+17
-52
lines changed

dev-packages/browser-integration-tests/suites/integrations/featureFlags/launchdarkly/init.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Sentry.init({
1111
integrations: [window.LDIntegration],
1212
});
1313

14-
window.MockLaunchDarkly = {
14+
const MockLaunchDarkly = {
1515
initialize(_clientId, context, options) {
1616
const flagUsedHandler = (options && options.inspectors) ? options.inspectors[0].method : undefined;
1717

@@ -27,7 +27,7 @@ window.MockLaunchDarkly = {
2727
};
2828

2929
window.InitializeLD = () => {
30-
return window.MockLaunchDarkly.initialize(
30+
return MockLaunchDarkly.initialize(
3131
'example-client-id',
3232
{ kind: 'user', key: 'example-context-key' },
3333
{ inspectors: [buildLaunchDarklyFlagUsedHandler()] },

dev-packages/browser-integration-tests/suites/integrations/featureFlags/launchdarkly/test.ts

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,9 @@ import { expect } from '@playwright/test';
33
import { sentryTest } from '../../../../utils/fixtures';
44

55
import { envelopeRequestParser, waitForErrorRequest } from '../../../../utils/helpers';
6-
import type { LDContext, LDOptions, LDFlagValue, LDClient, LDEvaluationDetail } from 'launchdarkly-js-client-sdk';
7-
import type { Event } from '@sentry/types';
8-
9-
// const MockLaunchDarkly = { //TODO: remove in favor of window.MockLaunchDarkly from init.js
10-
// initialize(
11-
// _clientId: string,
12-
// context: LDContext,
13-
// options: LDOptions,
14-
// ) {
15-
// const flagUsedHandler = options?.inspectors?.[0].method;
16-
// const wellTypedHandler = flagUsedHandler as ((
17-
// flagKey: string,
18-
// flagDetail: LDEvaluationDetail,
19-
// context: LDContext,
20-
// ) => void) | undefined;
21-
22-
// return {
23-
// variation(key: string, defaultValue: LDFlagValue) {
24-
// wellTypedHandler?.(key, { value: defaultValue }, context);
25-
// return defaultValue;
26-
// },
27-
// };
28-
// },
29-
// };
306

317
sentryTest('e2e test', async ({ getLocalTestPath, page }) => {
32-
let errorEventId: string = 'invalid_id';
338
await page.route('https://dsn.ingest.sentry.io/**/*', route => {
34-
const event = envelopeRequestParser(route.request());
35-
// error events have no type field
36-
if (event && !event.type && event.event_id) {
37-
errorEventId = event.event_id;
38-
}
39-
409
return route.fulfill({
4110
status: 200,
4211
contentType: 'application/json',
@@ -47,30 +16,27 @@ sentryTest('e2e test', async ({ getLocalTestPath, page }) => {
4716
const url = await getLocalTestPath({ testDir: __dirname, skipDsnRouteHandler: true });
4817
await page.goto(url);
4918

50-
// TODO: could this be in init.js?
5119
await page.waitForFunction(() => {
5220
const ldClient = (window as any).InitializeLD();
53-
ldClient.variation('feat1', false);
54-
ldClient.variation('feat2', false);
55-
ldClient.variation('feat3', false);
56-
ldClient.variation('feat2', true);
21+
for (let i = 1; i <= 100; i++) { // TODO: import constant for buffer size
22+
ldClient.variation(`feat${i}`, false);
23+
}
24+
ldClient.variation('feat101', true); // eviction
25+
ldClient.variation('feat3', true); // update
5726
return true;
5827
});
5928

60-
61-
// TODO: eviction not tested
62-
63-
// trigger error
6429
const reqPromise = waitForErrorRequest(page);
6530
await page.locator('#error').click();
6631
const req = await reqPromise;
67-
68-
// console.log(errorEventId);
6932
const event = envelopeRequestParser(req);
7033

71-
expect(event.contexts?.flags?.values).toEqual([
72-
{ flag: 'feat1', result: false },
73-
{ flag: 'feat3', result: false },
74-
{ flag: 'feat2', result: true },
75-
]);
34+
const expectedFlags = [{ flag: 'feat2', result: false }];
35+
for (let i = 4; i <= 100; i++) {
36+
expectedFlags.push({ flag: `feat${i}`, result: false });
37+
}
38+
expectedFlags.push({ flag: 'feat101', result: true });
39+
expectedFlags.push({ flag: 'feat3', result: true });
40+
41+
expect(event.contexts?.flags?.values).toEqual(expectedFlags);
7642
});

packages/browser/src/integrations/launchdarkly.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ export function buildLaunchDarklyFlagUsedHandler(): LDInspectionFlagUsedHandler
4747
name: 'sentry-flag-auditor',
4848
type: 'flag-used',
4949

50-
// We don't want the handler to impact the performance of the user's flag evaluations.
51-
synchronous: false, // TODO: this could lead to race conditions where an error directly after an eval might not contain the eval
52-
// TODO: the flag buffer itself isn't thread-safe, yet this handler and the event processor could access it at the same time.
50+
synchronous: true,
5351

5452
/**
5553
* Handle a flag evaluation by storing its name and value on the current scope.
@@ -62,6 +60,7 @@ export function buildLaunchDarklyFlagUsedHandler(): LDInspectionFlagUsedHandler
6260
}
6361
const flagBuffer = scopeContexts.flags.values;
6462
insertToFlagBuffer(flagBuffer, flagKey, flagDetail.value);
63+
// TODO: the flag buffer isn't thread-safe, yet this handler and the event processor could access it at the same time.
6564
}
6665
return;
6766
},

0 commit comments

Comments
 (0)