Skip to content

Commit a2037d3

Browse files
committed
fix cacheEvictionTest
1 parent aaf22d8 commit a2037d3

2 files changed

Lines changed: 66 additions & 4 deletions

File tree

lib/Onyx.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,13 @@ function init({
8282

8383
OnyxUtils.initStoreValues(keys, initialKeyStates, evictableKeys);
8484

85-
// Initialize all of our keys with data provided then give green light to any pending connections
86-
Promise.all([cache.addEvictableKeysToRecentlyAccessedList(OnyxUtils.isCollectionKey, OnyxUtils.getAllKeys), OnyxUtils.initializeWithDefaultKeyStates()]).then(
87-
OnyxUtils.getDeferredInitTask().resolve,
88-
);
85+
// Initialize all of our keys with data provided then give green light to any pending connections.
86+
// addEvictableKeysToRecentlyAccessedList must run after initializeWithDefaultKeyStates because
87+
// eager cache loading populates the key index (cache.setAllKeys) inside initializeWithDefaultKeyStates,
88+
// and the evictable keys list depends on that index being populated.
89+
OnyxUtils.initializeWithDefaultKeyStates()
90+
.then(() => cache.addEvictableKeysToRecentlyAccessedList(OnyxUtils.isCollectionKey, OnyxUtils.getAllKeys))
91+
.then(OnyxUtils.getDeferredInitTask().resolve);
8992
}
9093

9194
/**

tests/unit/cacheEvictionTest.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import StorageMock from '../../lib/storage';
2+
import Onyx from '../../lib';
3+
import waitForPromisesToResolve from '../utils/waitForPromisesToResolve';
4+
5+
const ONYX_KEYS = {
6+
COLLECTION: {
7+
TEST_KEY: 'test_',
8+
},
9+
};
10+
11+
test('Cache eviction', () => {
12+
const RECORD_TO_EVICT = 'evict';
13+
const RECORD_TO_ADD = 'add';
14+
const collection: Record<string, unknown> = {};
15+
16+
// Given an evictable key previously set in storage
17+
return StorageMock.setItem(`${ONYX_KEYS.COLLECTION.TEST_KEY}${RECORD_TO_EVICT}`, {test: 'evict'})
18+
.then(() => {
19+
// When we initialize Onyx and mark the set collection key as a safeEvictionKey
20+
Onyx.init({
21+
keys: ONYX_KEYS,
22+
evictableKeys: [ONYX_KEYS.COLLECTION.TEST_KEY],
23+
});
24+
25+
// And connect to this key
26+
Onyx.connect({
27+
key: ONYX_KEYS.COLLECTION.TEST_KEY,
28+
callback: (val, key) => {
29+
if (!val) {
30+
delete collection[key];
31+
} else {
32+
collection[key] = val;
33+
}
34+
},
35+
});
36+
37+
return waitForPromisesToResolve();
38+
})
39+
.then(() => {
40+
// Then it should populate our data with the key we will soon evict
41+
expect(collection[`${ONYX_KEYS.COLLECTION.TEST_KEY}${RECORD_TO_EVICT}`]).toStrictEqual({test: 'evict'});
42+
43+
// When we set a new key we want to add and force the first attempt to fail
44+
const originalSetItem = StorageMock.setItem;
45+
const setItemMock = jest.fn(originalSetItem).mockImplementationOnce(
46+
() =>
47+
new Promise((_resolve, reject) => {
48+
reject(new Error('out of memory'));
49+
}),
50+
);
51+
StorageMock.setItem = setItemMock;
52+
53+
return Onyx.set(`${ONYX_KEYS.COLLECTION.TEST_KEY}${RECORD_TO_ADD}`, {test: 'add'}).then(() => {
54+
// Then our collection should no longer contain the evictable key
55+
expect(collection[`${ONYX_KEYS.COLLECTION.TEST_KEY}${RECORD_TO_EVICT}`]).toBe(undefined);
56+
expect(collection[`${ONYX_KEYS.COLLECTION.TEST_KEY}${RECORD_TO_ADD}`]).toStrictEqual({test: 'add'});
57+
});
58+
});
59+
});

0 commit comments

Comments
 (0)