1
1
/**
2
- * Copyright 2022, Optimizely
2
+ * Copyright 2022, 2024, Optimizely
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import { DatafileManagerConfig } from '../lib/modules/datafile-manager/datafileM
20
20
import { advanceTimersByTime , getTimerCount } from './testUtils' ;
21
21
import PersistentKeyValueCache from '../lib/modules/datafile-manager/persistentKeyValueCache' ;
22
22
23
+
23
24
jest . mock ( '../lib/modules/datafile-manager/backoffController' , ( ) => {
24
25
return jest . fn ( ) . mockImplementation ( ( ) => {
25
26
const getDelayMock = jest . fn ( ) . mockImplementation ( ( ) => 0 ) ;
@@ -32,6 +33,8 @@ jest.mock('../lib/modules/datafile-manager/backoffController', () => {
32
33
} ) ;
33
34
34
35
import BackoffController from '../lib/modules/datafile-manager/backoffController' ;
36
+ import { LoggerFacade , getLogger } from '../lib/modules/logging' ;
37
+ import { resetCalls , spy , verify } from 'ts-mockito' ;
35
38
36
39
// Test implementation:
37
40
// - Does not make any real requests: just resolves with queued responses (tests push onto queuedResponses)
@@ -93,8 +96,19 @@ const testCache: PersistentKeyValueCache = {
93
96
} ;
94
97
95
98
describe ( 'httpPollingDatafileManager' , ( ) => {
99
+
100
+ let spiedLogger : LoggerFacade ;
101
+
102
+ const loggerName = 'DatafileManager' ;
103
+
104
+ beforeAll ( ( ) => {
105
+ const actualLogger = getLogger ( loggerName ) ;
106
+ spiedLogger = spy ( actualLogger ) ;
107
+ } ) ;
108
+
96
109
beforeEach ( ( ) => {
97
110
jest . useFakeTimers ( ) ;
111
+ resetCalls ( spiedLogger ) ;
98
112
} ) ;
99
113
100
114
let manager : TestDatafileManager ;
@@ -179,6 +193,22 @@ describe('httpPollingDatafileManager', () => {
179
193
manager = new TestDatafileManager ( { sdkKey : '123' , updateInterval : 1000 , autoUpdate : true } ) ;
180
194
} ) ;
181
195
196
+ it ( 'logs an error if fetching datafile fails' , async ( ) => {
197
+ manager . queuedResponses . push (
198
+ {
199
+ statusCode : 500 ,
200
+ body : '' ,
201
+ headers : { } ,
202
+ }
203
+ ) ;
204
+
205
+ manager . start ( ) ;
206
+ await advanceTimersByTime ( 1000 ) ;
207
+ await manager . responsePromises [ 0 ] ;
208
+
209
+ verify ( spiedLogger . error ( 'Datafile fetch request failed with status: 500' ) ) . once ( ) ;
210
+ } ) ;
211
+
182
212
describe ( 'initial state' , ( ) => {
183
213
it ( 'returns null from get before becoming ready' , ( ) => {
184
214
expect ( manager . get ( ) ) . toEqual ( '' ) ;
0 commit comments