@@ -18,7 +18,7 @@ import { TextEncoder } from 'util';
18
18
19
19
import { createTransport } from '../../../src' ;
20
20
import 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' ;
22
22
23
23
const ERROR_ENVELOPE = createEnvelope < EventEnvelope > ( { event_id : 'aa3ff046696b4bc6b609ce6d28fde9e2' , sent_at : '123' } , [
24
24
[ { type : 'event' } , { event_id : 'aa3ff046696b4bc6b609ce6d28fde9e2' } ] as EventItem ,
@@ -115,8 +115,19 @@ function createTestStore(...popResults: MockResult<Envelope | undefined>[]): {
115
115
} ;
116
116
}
117
117
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
+ } ) ;
120
131
}
121
132
122
133
describe ( 'makeOfflineTransport' , ( ) => {
@@ -138,7 +149,7 @@ describe('makeOfflineTransport', () => {
138
149
expect ( queuedCount ) . toEqual ( 0 ) ;
139
150
expect ( getSendCount ( ) ) . toEqual ( 1 ) ;
140
151
141
- await delay ( MIN_DELAY * 2 ) ;
152
+ await waitUntil ( ( ) => getCalls ( ) . length == 1 , 1_000 ) ;
142
153
143
154
// After a successful send, the store should be checked
144
155
expect ( getCalls ( ) ) . toEqual ( [ 'pop' ] ) ;
@@ -152,7 +163,7 @@ describe('makeOfflineTransport', () => {
152
163
153
164
expect ( result ) . toEqual ( { statusCode : 200 } ) ;
154
165
155
- await delay ( MIN_DELAY * 3 ) ;
166
+ await waitUntil ( ( ) => getCalls ( ) . length == 2 , 1_000 ) ;
156
167
157
168
expect ( getSendCount ( ) ) . toEqual ( 2 ) ;
158
169
// After a successful send from the store, the store should be checked again to ensure it's empty
@@ -175,7 +186,7 @@ describe('makeOfflineTransport', () => {
175
186
176
187
expect ( result ) . toEqual ( { } ) ;
177
188
178
- await delay ( MIN_DELAY * 2 ) ;
189
+ await waitUntil ( ( ) => getCalls ( ) . length === 1 , 1_000 ) ;
179
190
180
191
expect ( getSendCount ( ) ) . toEqual ( 0 ) ;
181
192
expect ( queuedCount ) . toEqual ( 1 ) ;
@@ -198,7 +209,7 @@ describe('makeOfflineTransport', () => {
198
209
199
210
expect ( result ) . toEqual ( { statusCode : 500 } ) ;
200
211
201
- await delay ( MIN_DELAY * 2 ) ;
212
+ await waitUntil ( ( ) => getSendCount ( ) === 1 , 1_000 ) ;
202
213
203
214
expect ( getSendCount ( ) ) . toEqual ( 1 ) ;
204
215
expect ( queuedCount ) . toEqual ( 0 ) ;
@@ -215,7 +226,7 @@ describe('makeOfflineTransport', () => {
215
226
expect ( result ) . toEqual ( { } ) ;
216
227
expect ( getCalls ( ) ) . toEqual ( [ 'add' ] ) ;
217
228
218
- await delay ( START_DELAY + 1_000 ) ;
229
+ await waitUntil ( ( ) => getCalls ( ) . length === 3 && getSendCount ( ) === 1 , START_DELAY * 2 ) ;
219
230
220
231
expect ( getSendCount ( ) ) . toEqual ( 1 ) ;
221
232
expect ( getCalls ( ) ) . toEqual ( [ 'add' , 'pop' , 'pop' ] ) ;
@@ -235,7 +246,7 @@ describe('makeOfflineTransport', () => {
235
246
flushAtStartup : true ,
236
247
} ) ;
237
248
238
- await delay ( START_DELAY + 1_000 ) ;
249
+ await waitUntil ( ( ) => getCalls ( ) . length === 3 && getSendCount ( ) === 2 , START_DELAY * 2 ) ;
239
250
240
251
expect ( getSendCount ( ) ) . toEqual ( 2 ) ;
241
252
expect ( getCalls ( ) ) . toEqual ( [ 'pop' , 'pop' , 'pop' ] ) ;
@@ -277,40 +288,44 @@ describe('makeOfflineTransport', () => {
277
288
expect ( getCalls ( ) ) . toEqual ( [ ] ) ;
278
289
} ) ;
279
290
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 ) ;
300
313
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
+ } ) ;
305
318
306
- await delay ( MIN_DELAY * 2 ) ;
319
+ await waitUntil ( ( ) => getSendCount ( ) === 1 , 500 ) ;
307
320
308
- expect ( getSendCount ( ) ) . toEqual ( 1 ) ;
321
+ expect ( getSendCount ( ) ) . toEqual ( 1 ) ;
309
322
310
- await delay ( 3_000 ) ;
323
+ await waitUntil ( ( ) => getCalls ( ) . length === 2 , START_DELAY * 2 ) ;
311
324
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
+ ) ;
316
331
} ) ;
0 commit comments