@@ -21,7 +21,10 @@ describe('Segment.io retries 500s and 429', () => {
21
21
jest . restoreAllMocks ( )
22
22
23
23
options = { apiKey : 'foo' }
24
- analytics = new Analytics ( { writeKey : options . apiKey } )
24
+ analytics = new Analytics (
25
+ { writeKey : options . apiKey } ,
26
+ { retryQueue : true }
27
+ )
25
28
segment = await segmentio (
26
29
analytics ,
27
30
options ,
@@ -36,7 +39,7 @@ describe('Segment.io retries 500s and 429', () => {
36
39
const ctx = await analytics . track ( 'event' )
37
40
jest . runAllTimers ( )
38
41
39
- expect ( ctx . attempts ) . toBeGreaterThanOrEqual ( 3 ) // Gets incremented after use
42
+ expect ( ctx . attempts ) . toBeGreaterThanOrEqual ( 2 ) // Gets incremented after use
40
43
expect ( fetch . mock . calls . length ) . toBeGreaterThanOrEqual ( 2 )
41
44
expect ( fetch . mock . lastCall [ 1 ] . body ) . toContain ( '"retryCount":' )
42
45
} )
@@ -131,3 +134,57 @@ describe('Batches retry 500s and 429', () => {
131
134
expect ( fetch . mock . lastCall [ 1 ] . body ) . toContain ( '"retryCount":2' )
132
135
} )
133
136
} )
137
+
138
+ describe ( 'retryQueue' , ( ) => {
139
+ let options : SegmentioSettings
140
+ let analytics : Analytics
141
+ let segment : Plugin
142
+ beforeEach ( async ( ) => {
143
+ jest . useFakeTimers ( { advanceTimers : true } )
144
+ jest . resetAllMocks ( )
145
+ jest . restoreAllMocks ( )
146
+
147
+ options = {
148
+ apiKey : 'foo' ,
149
+ }
150
+
151
+ fetch . mockReturnValue ( createError ( { status : 500 } ) )
152
+ } )
153
+ afterEach ( ( ) => {
154
+ jest . useRealTimers ( )
155
+ } )
156
+
157
+ it ( 'Only attempts once if retryQueue is false' , async ( ) => {
158
+ analytics = new Analytics (
159
+ { writeKey : options . apiKey } ,
160
+ { retryQueue : false }
161
+ )
162
+ segment = await segmentio (
163
+ analytics ,
164
+ options ,
165
+ cdnSettingsMinimal . integrations
166
+ )
167
+ await analytics . register ( segment , envEnrichment )
168
+
169
+ await analytics . track ( 'foo' )
170
+ jest . runAllTimers ( )
171
+ expect ( fetch ) . toHaveBeenCalledTimes ( 1 )
172
+ } )
173
+
174
+ it ( 'Attempts multiple times if retryQueue is true' , async ( ) => {
175
+ analytics = new Analytics (
176
+ { writeKey : options . apiKey } ,
177
+ { retryQueue : true }
178
+ )
179
+ segment = await segmentio (
180
+ analytics ,
181
+ options ,
182
+ cdnSettingsMinimal . integrations
183
+ )
184
+ await analytics . register ( segment , envEnrichment )
185
+
186
+ await analytics . track ( 'foo' )
187
+ jest . runAllTimers ( )
188
+ expect ( fetch . mock . calls . length ) . toBeGreaterThanOrEqual ( 2 )
189
+ } )
190
+ } )
0 commit comments