Skip to content

Commit 53db694

Browse files
committed
Check attempts before dispatching
1 parent aa86141 commit 53db694

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

packages/browser/src/plugins/segmentio/__tests__/retries.test.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('Segment.io retries 500s and 429', () => {
3636
const ctx = await analytics.track('event')
3737
jest.runAllTimers()
3838

39-
expect(ctx.attempts).toBeGreaterThanOrEqual(3) // Gets incremented after use
39+
expect(ctx.attempts).toBeGreaterThanOrEqual(2) // Gets incremented after use
4040
expect(fetch.mock.calls.length).toBeGreaterThanOrEqual(2)
4141
expect(fetch.mock.lastCall[1].body).toContain('"retryCount":')
4242
})
@@ -131,3 +131,54 @@ describe('Batches retry 500s and 429', () => {
131131
expect(fetch.mock.lastCall[1].body).toContain('"retryCount":2')
132132
})
133133
})
134+
135+
describe('retryQueue', () => {
136+
let options: SegmentioSettings
137+
let analytics: Analytics
138+
let segment: Plugin
139+
beforeEach(async () => {
140+
jest.useRealTimers()
141+
jest.resetAllMocks()
142+
jest.restoreAllMocks()
143+
144+
options = {
145+
apiKey: 'foo',
146+
}
147+
148+
fetch.mockReturnValue(createError({ status: 500 }))
149+
})
150+
151+
it('Only attempts once if retryQueue is false', async () => {
152+
analytics = new Analytics(
153+
{ writeKey: options.apiKey },
154+
{ retryQueue: false }
155+
)
156+
segment = await segmentio(
157+
analytics,
158+
options,
159+
cdnSettingsMinimal.integrations
160+
)
161+
await analytics.register(segment, envEnrichment)
162+
163+
await analytics.track('foo')
164+
await new Promise((resolve) => setTimeout(resolve, 1000))
165+
expect(fetch).toHaveBeenCalledTimes(1)
166+
})
167+
168+
it('Attempts multiple times if retryQueue is not false', async () => {
169+
analytics = new Analytics(
170+
{ writeKey: options.apiKey },
171+
{ retryQueue: true }
172+
)
173+
segment = await segmentio(
174+
analytics,
175+
options,
176+
cdnSettingsMinimal.integrations
177+
)
178+
await analytics.register(segment, envEnrichment)
179+
180+
await analytics.track('foo')
181+
await new Promise((resolve) => setTimeout(resolve, 1000))
182+
expect(fetch.mock.calls.length).toBeGreaterThanOrEqual(2)
183+
})
184+
})

packages/browser/src/plugins/segmentio/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ export function segmentio(
112112
json = onAlias(analytics, json)
113113
}
114114

115+
if (buffer.getAttempts(ctx) >= buffer.maxAttempts) {
116+
inflightEvents.delete(ctx)
117+
return ctx
118+
}
119+
115120
return client
116121
.dispatch(
117122
`${remote}/${path}`,

0 commit comments

Comments
 (0)