@@ -18,7 +18,7 @@ import { TextEncoder } from 'util';
1818
1919import { createTransport } from '../../../src' ;
2020import type { CreateOfflineStore , OfflineTransportOptions } from '../../../src/transports/offline' ;
21- import { makeOfflineTransport , MIN_DELAY , START_DELAY } from '../../../src/transports/offline' ;
21+ import { makeOfflineTransport , START_DELAY } from '../../../src/transports/offline' ;
2222
2323const ERROR_ENVELOPE = createEnvelope < EventEnvelope > ( { event_id : 'aa3ff046696b4bc6b609ce6d28fde9e2' , sent_at : '123' } , [
2424 [ { type : 'event' } , { event_id : 'aa3ff046696b4bc6b609ce6d28fde9e2' } ] as EventItem ,
@@ -115,8 +115,19 @@ function createTestStore(...popResults: MockResult<Envelope | undefined>[]): {
115115 } ;
116116}
117117
118- function delay ( ms : number ) : Promise < void > {
119- return new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
118+ function waitUntil ( fn : ( ) => boolean , timeout : number ) : Promise < void > {
119+ return new Promise ( resolve => {
120+ let runtime = 0 ;
121+
122+ const interval = setInterval ( ( ) => {
123+ runtime += 100 ;
124+
125+ if ( fn ( ) || runtime >= timeout ) {
126+ clearTimeout ( interval ) ;
127+ resolve ( ) ;
128+ }
129+ } , 100 ) ;
130+ } ) ;
120131}
121132
122133describe ( 'makeOfflineTransport' , ( ) => {
@@ -138,7 +149,7 @@ describe('makeOfflineTransport', () => {
138149 expect ( queuedCount ) . toEqual ( 0 ) ;
139150 expect ( getSendCount ( ) ) . toEqual ( 1 ) ;
140151
141- await delay ( MIN_DELAY * 2 ) ;
152+ await waitUntil ( ( ) => getCalls ( ) . length == 1 , 1_000 ) ;
142153
143154 // After a successful send, the store should be checked
144155 expect ( getCalls ( ) ) . toEqual ( [ 'pop' ] ) ;
@@ -152,7 +163,7 @@ describe('makeOfflineTransport', () => {
152163
153164 expect ( result ) . toEqual ( { statusCode : 200 } ) ;
154165
155- await delay ( MIN_DELAY * 3 ) ;
166+ await waitUntil ( ( ) => getCalls ( ) . length == 2 , 1_000 ) ;
156167
157168 expect ( getSendCount ( ) ) . toEqual ( 2 ) ;
158169 // After a successful send from the store, the store should be checked again to ensure it's empty
@@ -175,7 +186,7 @@ describe('makeOfflineTransport', () => {
175186
176187 expect ( result ) . toEqual ( { } ) ;
177188
178- await delay ( MIN_DELAY * 2 ) ;
189+ await waitUntil ( ( ) => getCalls ( ) . length === 1 , 1_000 ) ;
179190
180191 expect ( getSendCount ( ) ) . toEqual ( 0 ) ;
181192 expect ( queuedCount ) . toEqual ( 1 ) ;
@@ -198,7 +209,7 @@ describe('makeOfflineTransport', () => {
198209
199210 expect ( result ) . toEqual ( { statusCode : 500 } ) ;
200211
201- await delay ( MIN_DELAY * 2 ) ;
212+ await waitUntil ( ( ) => getSendCount ( ) === 1 , 1_000 ) ;
202213
203214 expect ( getSendCount ( ) ) . toEqual ( 1 ) ;
204215 expect ( queuedCount ) . toEqual ( 0 ) ;
@@ -215,7 +226,7 @@ describe('makeOfflineTransport', () => {
215226 expect ( result ) . toEqual ( { } ) ;
216227 expect ( getCalls ( ) ) . toEqual ( [ 'add' ] ) ;
217228
218- await delay ( START_DELAY + 1_000 ) ;
229+ await waitUntil ( ( ) => getCalls ( ) . length === 3 && getSendCount ( ) === 1 , START_DELAY * 2 ) ;
219230
220231 expect ( getSendCount ( ) ) . toEqual ( 1 ) ;
221232 expect ( getCalls ( ) ) . toEqual ( [ 'add' , 'pop' , 'pop' ] ) ;
@@ -235,7 +246,7 @@ describe('makeOfflineTransport', () => {
235246 flushAtStartup : true ,
236247 } ) ;
237248
238- await delay ( START_DELAY + 1_000 ) ;
249+ await waitUntil ( ( ) => getCalls ( ) . length === 3 && getSendCount ( ) === 2 , START_DELAY * 2 ) ;
239250
240251 expect ( getSendCount ( ) ) . toEqual ( 2 ) ;
241252 expect ( getCalls ( ) ) . toEqual ( [ 'pop' , 'pop' , 'pop' ] ) ;
@@ -277,40 +288,44 @@ describe('makeOfflineTransport', () => {
277288 expect ( getCalls ( ) ) . toEqual ( [ ] ) ;
278289 } ) ;
279290
280- it ( 'Follows the Retry-After header' , async ( ) => {
281- const { getCalls, store } = createTestStore ( ERROR_ENVELOPE ) ;
282- const { getSendCount, baseTransport } = createTestTransport (
283- {
284- statusCode : 429 ,
285- headers : { 'x-sentry-rate-limits' : '' , 'retry-after' : '1' } ,
286- } ,
287- { statusCode : 200 } ,
288- ) ;
289-
290- let queuedCount = 0 ;
291- const transport = makeOfflineTransport ( baseTransport ) ( {
292- ...transportOptions ,
293- createStore : store ,
294- shouldStore : ( ) => {
295- queuedCount += 1 ;
296- return true ;
297- } ,
298- } ) ;
299- const result = await transport . send ( ERROR_ENVELOPE ) ;
291+ it (
292+ 'Follows the Retry-After header' ,
293+ async ( ) => {
294+ const { getCalls, store } = createTestStore ( ERROR_ENVELOPE ) ;
295+ const { getSendCount, baseTransport } = createTestTransport (
296+ {
297+ statusCode : 429 ,
298+ headers : { 'x-sentry-rate-limits' : '' , 'retry-after' : '3' } ,
299+ } ,
300+ { statusCode : 200 } ,
301+ ) ;
302+
303+ let queuedCount = 0 ;
304+ const transport = makeOfflineTransport ( baseTransport ) ( {
305+ ...transportOptions ,
306+ createStore : store ,
307+ shouldStore : ( ) => {
308+ queuedCount += 1 ;
309+ return true ;
310+ } ,
311+ } ) ;
312+ const result = await transport . send ( ERROR_ENVELOPE ) ;
300313
301- expect ( result ) . toEqual ( {
302- statusCode : 429 ,
303- headers : { 'x-sentry-rate-limits' : '' , 'retry-after' : '1 ' } ,
304- } ) ;
314+ expect ( result ) . toEqual ( {
315+ statusCode : 429 ,
316+ headers : { 'x-sentry-rate-limits' : '' , 'retry-after' : '3 ' } ,
317+ } ) ;
305318
306- await delay ( MIN_DELAY * 2 ) ;
319+ await waitUntil ( ( ) => getSendCount ( ) === 1 , 500 ) ;
307320
308- expect ( getSendCount ( ) ) . toEqual ( 1 ) ;
321+ expect ( getSendCount ( ) ) . toEqual ( 1 ) ;
309322
310- await delay ( 3_000 ) ;
323+ await waitUntil ( ( ) => getCalls ( ) . length === 2 , START_DELAY * 2 ) ;
311324
312- expect ( getSendCount ( ) ) . toEqual ( 2 ) ;
313- expect ( queuedCount ) . toEqual ( 0 ) ;
314- expect ( getCalls ( ) ) . toEqual ( [ 'pop' , 'pop' ] ) ;
315- } , 7_000 ) ;
325+ expect ( getSendCount ( ) ) . toEqual ( 2 ) ;
326+ expect ( queuedCount ) . toEqual ( 0 ) ;
327+ expect ( getCalls ( ) ) . toEqual ( [ 'pop' , 'pop' ] ) ;
328+ } ,
329+ START_DELAY * 3 ,
330+ ) ;
316331} ) ;
0 commit comments