Skip to content

Commit 501a839

Browse files
committed
Only check for salt and store initialization if the store is non-empty
1 parent d3f5d9e commit 501a839

File tree

2 files changed

+63
-12
lines changed

2 files changed

+63
-12
lines changed

src/client/eppo-precomputed-client.spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import EppoPrecomputedClient, {
3030
PrecomputedFlagsRequestParameters,
3131
Subject,
3232
} from './eppo-precomputed-client';
33+
import { applicationLogger } from '..';
3334

3435
describe('EppoPrecomputedClient E2E test', () => {
3536
const precomputedConfigurationWire = readMockConfigurationWireResponse(
@@ -52,6 +53,7 @@ describe('EppoPrecomputedClient E2E test', () => {
5253
subjectKey: 'test-subject',
5354
subjectAttributes: { attr1: 'value1' },
5455
};
56+
5557
beforeEach(async () => {
5658
storage = new MemoryOnlyConfigurationStore<PrecomputedFlag>();
5759
storage.setFormat(FormatEnum.PRECOMPUTED);
@@ -110,6 +112,52 @@ describe('EppoPrecomputedClient E2E test', () => {
110112
});
111113
});
112114

115+
describe('store initialization logged errors', () => {
116+
let mockError: jest.SpyInstance;
117+
118+
beforeEach(() => {
119+
mockError = jest.spyOn(applicationLogger, 'error');
120+
});
121+
122+
afterEach(() => {
123+
mockError.mockRestore();
124+
});
125+
126+
it('logs error when initialized with store without salt', () => {
127+
const emptyStore = new MemoryOnlyConfigurationStore<PrecomputedFlag>();
128+
new EppoPrecomputedClient({
129+
precomputedFlagStore: emptyStore,
130+
subject: {
131+
subjectKey: '',
132+
subjectAttributes: {},
133+
},
134+
});
135+
expect(mockError).not.toHaveBeenCalledWith(
136+
'EppoPrecomputedClient requires a precomputedFlagStore with a salt if requestParameters are not provided',
137+
);
138+
});
139+
140+
it('logs error when initialized with store without salt', () => {
141+
const nonemptyStore = new MemoryOnlyConfigurationStore<PrecomputedFlag>();
142+
// Incorrectly initialized: no salt, not set to initialized
143+
jest.spyOn(nonemptyStore, 'getKeys').mockReturnValue(['some-key']);
144+
145+
new EppoPrecomputedClient({
146+
precomputedFlagStore: nonemptyStore,
147+
subject: {
148+
subjectKey: '',
149+
subjectAttributes: {},
150+
},
151+
});
152+
expect(mockError).toHaveBeenCalledWith(
153+
'[Eppo SDK] EppoPrecomputedClient requires an initialized precomputedFlagStore if requestParameters are not provided',
154+
);
155+
expect(mockError).toHaveBeenCalledWith(
156+
'[Eppo SDK] EppoPrecomputedClient requires a precomputedFlagStore with a salt if requestParameters are not provided',
157+
);
158+
});
159+
});
160+
113161
describe('setLogger', () => {
114162
let flagStorage: IConfigurationStore<PrecomputedFlag>;
115163
let subject: Subject;

src/client/eppo-precomputed-client.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,21 @@ export default class EppoPrecomputedClient {
100100
// Online-mode
101101
this.requestParameters = options.requestParameters;
102102
} else {
103-
// Offline-mode
104-
105-
// Offline mode depends on pre-populated IConfigurationStores (flags and bandits) to source configuration.
106-
if (!this.precomputedFlagStore.isInitialized()) {
107-
logger.error(
108-
`${loggerPrefix} EppoPrecomputedClient requires an initialized precomputedFlagStore if requestParameters are not provided`,
109-
);
103+
// Offline-mode -- depends on pre-populated IConfigurationStores (flags and bandits) to source configuration.
104+
105+
// Allow an empty precomputedFlagStore to be passed in, but if it has items, ensure it was initialized properly.
106+
if (this.precomputedFlagStore.getKeys().length > 0) {
107+
if (!this.precomputedFlagStore.isInitialized()) {
108+
logger.error(
109+
`${loggerPrefix} EppoPrecomputedClient requires an initialized precomputedFlagStore if requestParameters are not provided`,
110+
);
111+
}
112+
113+
if (!this.precomputedFlagStore.salt) {
114+
logger.error(
115+
`${loggerPrefix} EppoPrecomputedClient requires a precomputedFlagStore with a salt if requestParameters are not provided`,
116+
);
117+
}
110118
}
111119

112120
if (this.precomputedBanditStore && !this.precomputedBanditStore.isInitialized()) {
@@ -115,11 +123,6 @@ export default class EppoPrecomputedClient {
115123
);
116124
}
117125

118-
if (!this.precomputedFlagStore.salt) {
119-
logger.error(
120-
`${loggerPrefix} EppoPrecomputedClient requires a precomputedFlagStore with a salt if requestParameters are not provided`,
121-
);
122-
}
123126

124127
if (this.precomputedBanditStore && !this.precomputedBanditStore.salt) {
125128
logger.warn(

0 commit comments

Comments
 (0)