@@ -312,117 +312,62 @@ describe("ConfigServiceBaseTests", () => {
312
312
service . dispose ( ) ;
313
313
} ) ;
314
314
315
- it ( "AutoPollConfigService - Should wait maxInitWaitTime in offline mode when external cache is expired" , async ( ) => {
315
+ for ( const expectedCacheState of [ ClientCacheState . NoFlagData , ClientCacheState . HasCachedFlagDataOnly , ClientCacheState . HasUpToDateFlagData ] ) {
316
+ it ( `AutoPollConfigService - Should emit clientReady in offline mode when sync with external cache is completed - expectedCacheState: ${ expectedCacheState } ` , async ( ) => {
316
317
317
- // Arrange
318
-
319
- const pollIntervalSeconds = 1 ;
320
- const maxInitWaitTimeSeconds = 2.5 ;
321
-
322
- const frOld : FetchResult = createFetchResult ( "oldEtag" ) ;
323
- const projectConfigOld = createConfigFromFetchResult ( frOld )
324
- . with ( ProjectConfig . generateTimestamp ( ) - ( 1.5 * pollIntervalSeconds * 1000 ) + 0.5 * POLL_EXPIRATION_TOLERANCE_MS ) ;
325
-
326
- const logger = new LoggerWrapper ( new FakeLogger ( ) ) ;
327
- const cache = new ExternalConfigCache ( new FakeExternalCache ( ) , logger ) ;
328
-
329
- const options = createAutoPollOptions (
330
- "APIKEY" ,
331
- {
332
- pollIntervalSeconds,
333
- maxInitWaitTimeSeconds,
334
- offline : true ,
335
- } ,
336
- createKernel ( { defaultCacheFactory : ( ) => cache } )
337
- ) ;
318
+ // Arrange
338
319
339
- cache . set ( options . getCacheKey ( ) , projectConfigOld ) ;
320
+ const pollIntervalSeconds = 1 ;
340
321
341
- const fetcherMock = new Mock < IConfigFetcher > ( ) ;
342
-
343
- // Act
344
-
345
- const service : AutoPollConfigService = new AutoPollConfigService (
346
- fetcherMock . object ( ) ,
347
- options ) ;
322
+ let projectConfig : ProjectConfig | undefined ;
323
+ if ( expectedCacheState !== ClientCacheState . NoFlagData ) {
324
+ const fr : FetchResult = createFetchResult ( "oldEtag" ) ;
325
+ projectConfig = createConfigFromFetchResult ( fr ) ;
348
326
349
- const { readyPromise } = service ;
350
- const delayAbortToken = new AbortToken ( ) ;
351
- const delayPromise = delay ( maxInitWaitTimeSeconds * 1000 - 250 , delayAbortToken ) ;
352
- const raceResult = await Promise . race ( [ readyPromise , delayPromise ] ) ;
353
- delayAbortToken . abort ( ) ;
354
-
355
- // Assert
356
-
357
- assert . strictEqual ( raceResult , true ) ;
358
-
359
- // Cleanup
360
-
361
- service . dispose ( ) ;
362
- } ) ;
327
+ if ( expectedCacheState === ClientCacheState . HasCachedFlagDataOnly ) {
328
+ projectConfig = projectConfig
329
+ . with ( ProjectConfig . generateTimestamp ( ) - ( 1.5 * pollIntervalSeconds * 1000 ) + 0.5 * POLL_EXPIRATION_TOLERANCE_MS ) ;
330
+ }
331
+ }
363
332
364
- it ( "AutoPollConfigService - Should initialize in offline mode when external cache becomes up-to-date" , async ( ) => {
333
+ const logger = new LoggerWrapper ( new FakeLogger ( ) ) ;
334
+ const cache = new ExternalConfigCache ( new FakeExternalCache ( ) , logger ) ;
365
335
366
- // Arrange
367
-
368
- const pollIntervalSeconds = 1 ;
369
- const maxInitWaitTimeSeconds = 2.5 ;
370
- const cacheSetDelayMs = 0.5 * pollIntervalSeconds * 1000 ;
371
-
372
- const frOld : FetchResult = createFetchResult ( "oldEtag" ) ;
373
- const projectConfigOld = createConfigFromFetchResult ( frOld )
374
- . with ( ProjectConfig . generateTimestamp ( ) - ( 1.5 * pollIntervalSeconds * 1000 ) + 0.5 * POLL_EXPIRATION_TOLERANCE_MS ) ;
375
-
376
- const logger = new LoggerWrapper ( new FakeLogger ( ) ) ;
377
- const cache = new ExternalConfigCache ( new FakeExternalCache ( ) , logger ) ;
378
-
379
- const options = createAutoPollOptions (
380
- "APIKEY" ,
381
- {
382
- pollIntervalSeconds,
383
- maxInitWaitTimeSeconds,
384
- offline : true ,
385
- } ,
386
- createKernel ( { defaultCacheFactory : ( ) => cache } )
387
- ) ;
336
+ const options = createAutoPollOptions (
337
+ "APIKEY" ,
338
+ {
339
+ pollIntervalSeconds,
340
+ offline : true ,
341
+ } ,
342
+ createKernel ( { defaultCacheFactory : ( ) => cache } )
343
+ ) ;
388
344
389
- cache . set ( options . getCacheKey ( ) , projectConfigOld ) ;
345
+ if ( projectConfig ) {
346
+ cache . set ( options . getCacheKey ( ) , projectConfig ) ;
347
+ }
390
348
391
- const fetcherMock = new Mock < IConfigFetcher > ( ) ;
349
+ const fetcherMock = new Mock < IConfigFetcher > ( ) ;
392
350
393
- // Act
351
+ // Act
394
352
395
- const service : AutoPollConfigService = new AutoPollConfigService (
396
- fetcherMock . object ( ) ,
397
- options ) ;
353
+ const service : AutoPollConfigService = new AutoPollConfigService (
354
+ fetcherMock . object ( ) ,
355
+ options ) ;
398
356
399
- const { readyPromise } = service ;
400
- const delayAbortToken = new AbortToken ( ) ;
401
- const delayPromise = delay ( maxInitWaitTimeSeconds * 1000 - 250 , delayAbortToken ) ;
402
- const racePromise = Promise . race ( [ readyPromise , delayPromise ] ) ;
357
+ const { readyPromise } = service ;
358
+ const delayAbortToken = new AbortToken ( ) ;
359
+ const delayPromise = delay ( pollIntervalSeconds * 1000 - 250 , delayAbortToken ) ;
360
+ const raceResult = await Promise . race ( [ readyPromise , delayPromise ] ) ;
403
361
404
- const cacheSetDelayAbortToken = new AbortToken ( ) ;
405
- const cacheSetDelayPromise = delay ( cacheSetDelayMs , cacheSetDelayAbortToken ) ;
406
- const cacheSetRaceResult = await Promise . race ( [ readyPromise , cacheSetDelayPromise ] ) ;
407
- cacheSetDelayAbortToken . abort ( ) ;
408
- assert . strictEqual ( cacheSetRaceResult , true ) ;
362
+ // Assert
409
363
410
- const frNew : FetchResult = createFetchResult ( "newEtag" ) ;
411
- const projectConfigNew : ProjectConfig = createConfigFromFetchResult ( frNew )
412
- . with ( ProjectConfig . generateTimestamp ( ) + ( pollIntervalSeconds * 1000 ) - cacheSetDelayMs + 0.5 * POLL_EXPIRATION_TOLERANCE_MS ) ;
413
- cache . set ( options . getCacheKey ( ) , projectConfigNew ) ;
364
+ assert . strictEqual ( raceResult , expectedCacheState ) ;
414
365
415
- const raceResult = await racePromise ;
416
- delayAbortToken . abort ( ) ;
366
+ // Cleanup
417
367
418
- // Assert
419
-
420
- assert . strictEqual ( raceResult , ClientCacheState . HasUpToDateFlagData ) ;
421
-
422
- // Cleanup
423
-
424
- service . dispose ( ) ;
425
- } ) ;
368
+ service . dispose ( ) ;
369
+ } ) ;
370
+ }
426
371
427
372
it ( "LazyLoadConfigService - ProjectConfig is different in the cache - should fetch a new config and put into cache" , async ( ) => {
428
373
0 commit comments