Skip to content

[FSSDK-9615] log error when datafile fetch request fails #904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/modules/datafile-manager/httpPollingDatafileManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2022-2023, Optimizely
* Copyright 2022-2024, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -325,6 +325,7 @@ export default abstract class HttpPollingDatafileManager implements DatafileMana
if (isSuccessStatusCode(response.statusCode)) {
return response.body;
}
logger.error(`Datafile fetch request failed with status: ${response.statusCode}`);
return '';
}

Expand Down
32 changes: 31 additions & 1 deletion tests/httpPollingDatafileManager.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2022, Optimizely
* Copyright 2022, 2024, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,7 @@ import { DatafileManagerConfig } from '../lib/modules/datafile-manager/datafileM
import { advanceTimersByTime, getTimerCount } from './testUtils';
import PersistentKeyValueCache from '../lib/modules/datafile-manager/persistentKeyValueCache';


jest.mock('../lib/modules/datafile-manager/backoffController', () => {
return jest.fn().mockImplementation(() => {
const getDelayMock = jest.fn().mockImplementation(() => 0);
Expand All @@ -32,6 +33,8 @@ jest.mock('../lib/modules/datafile-manager/backoffController', () => {
});

import BackoffController from '../lib/modules/datafile-manager/backoffController';
import { LoggerFacade, getLogger } from '../lib/modules/logging';
import { resetCalls, spy, verify } from 'ts-mockito';

// Test implementation:
// - Does not make any real requests: just resolves with queued responses (tests push onto queuedResponses)
Expand Down Expand Up @@ -93,8 +96,19 @@ const testCache: PersistentKeyValueCache = {
};

describe('httpPollingDatafileManager', () => {

let spiedLogger: LoggerFacade;

const loggerName = 'DatafileManager';

beforeAll(() => {
const actualLogger = getLogger(loggerName);
spiedLogger = spy(actualLogger);
});

beforeEach(() => {
jest.useFakeTimers();
resetCalls(spiedLogger);
});

let manager: TestDatafileManager;
Expand Down Expand Up @@ -179,6 +193,22 @@ describe('httpPollingDatafileManager', () => {
manager = new TestDatafileManager({ sdkKey: '123', updateInterval: 1000, autoUpdate: true });
});

it('logs an error if fetching datafile fails', async () => {
manager.queuedResponses.push(
{
statusCode: 500,
body: '',
headers: {},
}
);

manager.start();
await advanceTimersByTime(1000);
await manager.responsePromises[0];

verify(spiedLogger.error('Datafile fetch request failed with status: 500')).once();
});

describe('initial state', () => {
it('returns null from get before becoming ready', () => {
expect(manager.get()).toEqual('');
Expand Down