Skip to content

Commit d145636

Browse files
author
kjelko
committed
clean up after merging main
1 parent 2bbe42f commit d145636

File tree

3 files changed

+127
-90
lines changed

3 files changed

+127
-90
lines changed

packages/remote-config/src/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
Value,
2525
RemoteConfigOptions
2626
} from './public_types';
27-
import { RemoteConfigAbortSignal, FetchResponse } from './client/remote_config_fetch_client';
27+
import { RemoteConfigAbortSignal } from './client/remote_config_fetch_client';
2828
import {
2929
RC_COMPONENT_NAME,
3030
RC_CUSTOM_SIGNAL_KEY_MAX_LENGTH,

packages/remote-config/src/storage/storage.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,34 @@ export class InMemoryStorage extends Storage {
372372
}
373373

374374
async setCustomSignals(customSignals: CustomSignals): Promise<CustomSignals> {
375-
return Promise.resolve({});
375+
const combinedSignals = {
376+
...this.storage['custom_signals'] as CustomSignals,
377+
...customSignals,
378+
};
379+
380+
const updatedSignals = Object.fromEntries(
381+
Object.entries(combinedSignals)
382+
.filter(([_, v]) => v !== null)
383+
.map(([k, v]) => {
384+
// Stringify numbers to store a map of string keys and values which can be sent
385+
// as-is in a fetch call.
386+
if (typeof v === 'number') {
387+
return [k, v.toString()];
388+
}
389+
return [k, v];
390+
})
391+
);
392+
393+
if (
394+
Object.keys(updatedSignals).length > RC_CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS
395+
) {
396+
throw ERROR_FACTORY.create(ErrorCode.CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS, {
397+
maxSignals: RC_CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS
398+
});
399+
}
400+
401+
this.storage['custom_signals'] = updatedSignals;
402+
403+
return Promise.resolve(this.storage['custom_signals'] as CustomSignals);
376404
}
377405
}

packages/remote-config/test/storage/storage.test.ts

Lines changed: 97 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import {
2222
openDatabase,
2323
APP_NAMESPACE_STORE,
2424
IndexedDbStorage,
25-
InMemoryStorage
25+
InMemoryStorage,
26+
Storage
2627
} from '../../src/storage/storage';
2728
import { FetchResponse } from '../../src/client/remote_config_fetch_client';
2829

@@ -34,15 +35,15 @@ async function clearDatabase(): Promise<void> {
3435
.clear();
3536
}
3637

37-
describe('IndexedDbStorage', () => {
38+
describe('Storage', () => {
3839

3940
const indexedDbTestCase = {
40-
storage: new IndexedDbStorage('appId', 'appName', 'namespace'),
41+
getStorage: () => new IndexedDbStorage('appId', 'appName', 'namespace'),
4142
name: 'IndexedDbStorage',
4243
};
4344

4445
const inMemoryStorage = {
45-
storage: new InMemoryStorage(),
46+
getStorage: () => new InMemoryStorage(),
4647
name: 'InMemoryStorage',
4748
};
4849

@@ -52,135 +53,143 @@ describe('IndexedDbStorage', () => {
5253

5354
it(`${indexedDbTestCase.name} constructs a composite key`, async () => {
5455
// This is defensive, but the cost of accidentally changing the key composition is high.
55-
expect(indexedDbTestCase.storage.createCompositeKey('throttle_metadata')).to.eq(
56+
expect(indexedDbTestCase.getStorage().createCompositeKey('throttle_metadata')).to.eq(
5657
'appId,appName,namespace,throttle_metadata'
5758
);
5859
});
5960

60-
for (const { name, storage } of [indexedDbTestCase, inMemoryStorage]) {
61-
it(`${name} sets and gets last fetch attempt status`, async () => {
62-
const expectedStatus = 'success';
61+
for (const { name, getStorage } of [indexedDbTestCase, inMemoryStorage]) {
62+
describe(name, () => {
63+
let storage: Storage;
6364

64-
await storage.setLastFetchStatus(expectedStatus);
65+
beforeEach(() => {
66+
storage = getStorage();
67+
});
6568

66-
const actualStatus = await storage.getLastFetchStatus();
69+
it(`sets and gets last fetch attempt status`, async () => {
70+
const expectedStatus = 'success';
6771

68-
expect(actualStatus).to.deep.eq(expectedStatus);
69-
});
72+
await storage.setLastFetchStatus(expectedStatus);
7073

71-
it(`${name} sets and gets last fetch success timestamp`, async () => {
72-
const lastSuccessfulFetchTimestampMillis = 123;
74+
const actualStatus = await storage.getLastFetchStatus();
7375

74-
await storage.setLastSuccessfulFetchTimestampMillis(
75-
lastSuccessfulFetchTimestampMillis
76-
);
76+
expect(actualStatus).to.deep.eq(expectedStatus);
77+
});
7778

78-
const actualMetadata =
79-
await storage.getLastSuccessfulFetchTimestampMillis();
79+
it(`sets and gets last fetch success timestamp`, async () => {
80+
const lastSuccessfulFetchTimestampMillis = 123;
8081

81-
expect(actualMetadata).to.deep.eq(lastSuccessfulFetchTimestampMillis);
82-
});
82+
await storage.setLastSuccessfulFetchTimestampMillis(
83+
lastSuccessfulFetchTimestampMillis
84+
);
8385

84-
it(`${name} sets and gets last successful fetch response`, async () => {
85-
const lastSuccessfulFetchResponse = { status: 200 } as FetchResponse;
86+
const actualMetadata =
87+
await storage.getLastSuccessfulFetchTimestampMillis();
8688

87-
await storage.setLastSuccessfulFetchResponse(lastSuccessfulFetchResponse);
89+
expect(actualMetadata).to.deep.eq(lastSuccessfulFetchTimestampMillis);
90+
});
8891

89-
const actualConfig = await storage.getLastSuccessfulFetchResponse();
92+
it(`sets and gets last successful fetch response`, async () => {
93+
const lastSuccessfulFetchResponse = { status: 200 } as FetchResponse;
9094

91-
expect(actualConfig).to.deep.eq(lastSuccessfulFetchResponse);
92-
});
95+
await storage.setLastSuccessfulFetchResponse(lastSuccessfulFetchResponse);
9396

94-
it(`${name} sets and gets active config`, async () => {
95-
const expectedConfig = { key: 'value' };
97+
const actualConfig = await storage.getLastSuccessfulFetchResponse();
9698

97-
await storage.setActiveConfig(expectedConfig);
99+
expect(actualConfig).to.deep.eq(lastSuccessfulFetchResponse);
100+
});
98101

99-
const storedConfig = await storage.getActiveConfig();
102+
it(`sets and gets active config`, async () => {
103+
const expectedConfig = { key: 'value' };
100104

101-
expect(storedConfig).to.deep.eq(expectedConfig);
102-
});
105+
await storage.setActiveConfig(expectedConfig);
103106

104-
it(`${name} sets and gets active config etag`, async () => {
105-
const expectedEtag = 'etag';
107+
const storedConfig = await storage.getActiveConfig();
106108

107-
await storage.setActiveConfigEtag(expectedEtag);
109+
expect(storedConfig).to.deep.eq(expectedConfig);
110+
});
108111

109-
const storedConfigEtag = await storage.getActiveConfigEtag();
112+
it(`sets and gets active config etag`, async () => {
113+
const expectedEtag = 'etag';
110114

111-
expect(storedConfigEtag).to.deep.eq(expectedEtag);
112-
});
115+
await storage.setActiveConfigEtag(expectedEtag);
113116

114-
it(`${name} sets, gets and deletes throttle metadata`, async () => {
115-
const expectedMetadata = {
116-
throttleEndTimeMillis: 1
117-
} as ThrottleMetadata;
117+
const storedConfigEtag = await storage.getActiveConfigEtag();
118118

119-
await storage.setThrottleMetadata(expectedMetadata);
119+
expect(storedConfigEtag).to.deep.eq(expectedEtag);
120+
});
120121

121-
let actualMetadata = await storage.getThrottleMetadata();
122+
it(`sets, gets and deletes throttle metadata`, async () => {
123+
const expectedMetadata = {
124+
throttleEndTimeMillis: 1
125+
} as ThrottleMetadata;
122126

123-
expect(actualMetadata).to.deep.eq(expectedMetadata);
127+
await storage.setThrottleMetadata(expectedMetadata);
124128

125-
await storage.deleteThrottleMetadata();
129+
let actualMetadata = await storage.getThrottleMetadata();
126130

127-
actualMetadata = await storage.getThrottleMetadata();
131+
expect(actualMetadata).to.deep.eq(expectedMetadata);
128132

129-
expect(actualMetadata).to.be.undefined;
130-
});
133+
await storage.deleteThrottleMetadata();
131134

132-
it('sets and gets custom signals', async () => {
133-
const customSignals = { key: 'value', key1: 'value1', key2: 1 };
134-
const customSignalsInStorage = {
135-
key: 'value',
136-
key1: 'value1',
137-
key2: '1'
138-
};
135+
actualMetadata = await storage.getThrottleMetadata();
139136

140-
await storage.setCustomSignals(customSignals);
137+
expect(actualMetadata).to.be.undefined;
138+
});
141139

142-
const storedCustomSignals = await storage.getCustomSignals();
140+
it(`sets and gets custom signals`, async () => {
141+
const customSignals = { key: 'value', key1: 'value1', key2: 1 };
142+
const customSignalsInStorage = {
143+
key: 'value',
144+
key1: 'value1',
145+
key2: '1'
146+
};
143147

144-
expect(storedCustomSignals).to.deep.eq(customSignalsInStorage);
145-
});
148+
await storage.setCustomSignals(customSignals);
146149

147-
it('upserts custom signals when key is present in storage', async () => {
148-
const customSignals = { key: 'value', key1: 'value1' };
149-
const updatedSignals = { key: 'value', key1: 'value2' };
150+
const storedCustomSignals = await storage.getCustomSignals();
150151

151-
await storage.setCustomSignals(customSignals);
152+
expect(storedCustomSignals).to.deep.eq(customSignalsInStorage);
153+
});
152154

153-
await storage.setCustomSignals({ key1: 'value2' });
155+
it(`upserts custom signals when key is present in storage`, async () => {
156+
const customSignals = { key: 'value', key1: 'value1' };
157+
const updatedSignals = { key: 'value', key1: 'value2' };
154158

155-
const storedCustomSignals = await storage.getCustomSignals();
159+
await storage.setCustomSignals(customSignals);
156160

157-
expect(storedCustomSignals).to.deep.eq(updatedSignals);
158-
});
161+
await storage.setCustomSignals({ key1: 'value2' });
159162

160-
it('deletes custom signal when value supplied is null', async () => {
161-
const customSignals = { key: 'value', key1: 'value1' };
162-
const updatedSignals = { key: 'value' };
163+
const storedCustomSignals = await storage.getCustomSignals();
163164

164-
await storage.setCustomSignals(customSignals);
165+
expect(storedCustomSignals).to.deep.eq(updatedSignals);
166+
});
165167

166-
await storage.setCustomSignals({ key1: null });
168+
it(`deletes custom signal when value supplied is null`, async () => {
169+
const customSignals = { key: 'value', key1: 'value1' };
170+
const updatedSignals = { key: 'value' };
167171

168-
const storedCustomSignals = await storage.getCustomSignals();
172+
await storage.setCustomSignals(customSignals);
169173

170-
expect(storedCustomSignals).to.deep.eq(updatedSignals);
171-
});
174+
await storage.setCustomSignals({ key1: null });
175+
176+
const storedCustomSignals = await storage.getCustomSignals();
177+
178+
expect(storedCustomSignals).to.deep.eq(updatedSignals);
179+
});
180+
181+
it(`throws an error when supplied with excess custom signals`, async () => {
182+
const customSignals: { [key: string]: string } = {};
183+
for (let i = 0; i < 101; i++) {
184+
customSignals[`key${i}`] = `value${i}`;
185+
}
172186

173-
it('throws an error when supplied with excess custom signals', async () => {
174-
const customSignals: { [key: string]: string } = {};
175-
for (let i = 0; i < 101; i++) {
176-
customSignals[`key${i}`] = `value${i}`;
177-
}
178-
179-
await expect(
180-
storage.setCustomSignals(customSignals)
181-
).to.eventually.be.rejectedWith(
182-
'Remote Config: Setting more than 100 custom signals is not supported.'
183-
);
187+
await expect(
188+
storage.setCustomSignals(customSignals)
189+
).to.eventually.be.rejectedWith(
190+
'Remote Config: Setting more than 100 custom signals is not supported.'
191+
);
192+
});
184193
});
185194
}
186195
});

0 commit comments

Comments
 (0)