@@ -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+ } )
0 commit comments