Skip to content

Commit

Permalink
fix(flags): Enqueue follow up requests without dropping
Browse files Browse the repository at this point in the history
  • Loading branch information
neilkakkar committed Sep 14, 2023
1 parent 233e604 commit c0419c9
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 20 deletions.
25 changes: 19 additions & 6 deletions functional_tests/feature-flags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ test('person properties set in identify() with the same distinct_id are sent to
})
})

test('identify() doesnt trigger new request automatically if first request takes too long', async () => {
// TODO: Make this experience nicer, and queue requests, rather than leave
// it upto the user to call reloadFeatureFlags() manually.

test('identify() triggers new request in queue after first request', async () => {
const token = v4()
const posthog = await createPosthogInstance(token, { advanced_disable_decide: false })

Expand All @@ -130,12 +127,28 @@ test('identify() doesnt trigger new request automatically if first request takes
resetRequests(token)

// don't wait for decide callback

posthog.identify('test-id', {
email: 'test@email.com',
email: 'test2@email.com',
})

await waitFor(() => {
expect(getRequests(token)['/decide/']).toEqual([])
})

// wait for decide callback
// eslint-disable-next-line compat/compat
await new Promise((res) => setTimeout(res, 500))

// now second call should've fired
await waitFor(() => {
expect(getRequests(token)['/decide/']).toEqual([
{
$anon_distinct_id: anonymousId,
distinct_id: 'test-id',
groups: {},
person_properties: { email: '[email protected]' },
token,
},
])
})
})
2 changes: 1 addition & 1 deletion src/__tests__/compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ describe('Payload Compression', () => {
},
featureFlags: {
receivedFeatureFlags: jest.fn(),
resetRequestQueue: jest.fn(),
setReloadingPaused: jest.fn(),
_startReloadTimer: jest.fn(),
},
_hasBootstrappedFeatureFlags: jest.fn(),
get_property: (property_key) =>
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/decide.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ describe('Decide', () => {
},
featureFlags: {
receivedFeatureFlags: jest.fn(),
resetRequestQueue: jest.fn(),
setReloadingPaused: jest.fn(),
_startReloadTimer: jest.fn(),
},
_hasBootstrappedFeatureFlags: jest.fn(),
getGroups: () => ({ organization: '5' }),
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/posthog-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ describe('_loaded()', () => {
capture: jest.fn(),
featureFlags: {
setReloadingPaused: jest.fn(),
resetRequestQueue: jest.fn(),
_startReloadTimer: jest.fn(),
},
_start_queue_if_opted_in: jest.fn(),
}))
Expand Down
16 changes: 10 additions & 6 deletions src/__tests__/posthog-core.loaded.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('loaded() with flags', () => {
capture: jest.fn(),
featureFlags: {
setReloadingPaused: jest.fn(),
resetRequestQueue: jest.fn(),
_startReloadTimer: jest.fn(),
receivedFeatureFlags: jest.fn(),
},
_start_queue_if_opted_in: jest.fn(),
Expand Down Expand Up @@ -48,24 +48,28 @@ describe('loaded() with flags', () => {
beforeEach(() => {
jest.spyOn(given.lib.featureFlags, 'setGroupPropertiesForFlags')
jest.spyOn(given.lib.featureFlags, 'setReloadingPaused')
jest.spyOn(given.lib.featureFlags, 'resetRequestQueue')
jest.spyOn(given.lib.featureFlags, '_startReloadTimer')
jest.spyOn(given.lib.featureFlags, '_reloadFeatureFlagsRequest')
})

it('doesnt call flags while initial load is happening', () => {
given.subject()

jest.runAllTimers()
jest.runOnlyPendingTimers()

expect(given.lib.featureFlags.setGroupPropertiesForFlags).toHaveBeenCalled() // loaded ph.group() calls setGroupPropertiesForFlags
expect(given.lib.featureFlags.setReloadingPaused).toHaveBeenCalledWith(true)
expect(given.lib.featureFlags.resetRequestQueue).toHaveBeenCalledTimes(1)
expect(given.lib.featureFlags._startReloadTimer).toHaveBeenCalled()
expect(given.lib.featureFlags.setReloadingPaused).toHaveBeenCalledWith(false)

// even if the decide request returned late, we should not call _reloadFeatureFlagsRequest
// we should call _reloadFeatureFlagsRequest for `group` only after the initial load
// because it ought to be paused until decide returns
expect(given.overrides._send_request).toHaveBeenCalledTimes(1)
expect(given.lib.featureFlags._reloadFeatureFlagsRequest).toHaveBeenCalledTimes(0)

jest.runOnlyPendingTimers()
expect(given.overrides._send_request).toHaveBeenCalledTimes(2)
expect(given.lib.featureFlags._reloadFeatureFlagsRequest).toHaveBeenCalledTimes(1)
})
})

Expand All @@ -74,7 +78,7 @@ describe('loaded() with flags', () => {

expect(given.overrides.featureFlags.setReloadingPaused).toHaveBeenCalledWith(true)
expect(given.overrides.featureFlags.setReloadingPaused).toHaveBeenCalledWith(false)
expect(given.overrides.featureFlags.resetRequestQueue).toHaveBeenCalledTimes(1)
expect(given.overrides.featureFlags._startReloadTimer).toHaveBeenCalled()
expect(given.overrides.featureFlags.receivedFeatureFlags).toHaveBeenCalledTimes(1)
})
})
3 changes: 2 additions & 1 deletion src/decide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ export class Decide {
}

parseDecideResponse(response: DecideResponse): void {
this.instance.featureFlags.resetRequestQueue()
this.instance.featureFlags.setReloadingPaused(false)
// :TRICKY: Reload - start another request if queued!
this.instance.featureFlags._startReloadTimer()

if (response?.status === 0) {
console.error('Failed to fetch feature flags from PostHog.')
Expand Down
4 changes: 0 additions & 4 deletions src/posthog-featureflags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,6 @@ export class PostHogFeatureFlags {
this.reloadFeatureFlagsInAction = isPaused
}

resetRequestQueue(): void {
this.reloadFeatureFlagsQueued = false
}

_startReloadTimer(): void {
if (this.reloadFeatureFlagsQueued && !this.reloadFeatureFlagsInAction) {
setTimeout(() => {
Expand Down

0 comments on commit c0419c9

Please sign in to comment.