|
| 1 | +import { AsyncStorageAssignmentCache } from '../cache/async-storage-assignment-cache'; |
| 2 | +import HybridAssignmentCache from '../cache/hybrid-assignment-cache'; |
| 3 | +import SimpleAssignmentCache from '../cache/simple-assignment-cache'; |
| 4 | + |
| 5 | +describe('HybridStorageAssignmentCache', () => { |
| 6 | + let asyncStorageCache: AsyncStorageAssignmentCache; |
| 7 | + let simpleCache: SimpleAssignmentCache; |
| 8 | + let hybridCache: HybridAssignmentCache; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + asyncStorageCache = new AsyncStorageAssignmentCache('test'); |
| 12 | + simpleCache = new SimpleAssignmentCache(); |
| 13 | + hybridCache = new HybridAssignmentCache(simpleCache, asyncStorageCache); |
| 14 | + }); |
| 15 | + |
| 16 | + it('has should return false if cache is empty', async () => { |
| 17 | + const cacheKey = { |
| 18 | + subjectKey: 'subject-1', |
| 19 | + flagKey: 'flag-1', |
| 20 | + allocationKey: 'allocation-1', |
| 21 | + variationKey: 'control', |
| 22 | + }; |
| 23 | + await hybridCache.init(); |
| 24 | + expect(hybridCache.has(cacheKey)).toBeFalsy(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('has should return true if cache key is present', async () => { |
| 28 | + const cacheKey = { |
| 29 | + subjectKey: 'subject-1', |
| 30 | + flagKey: 'flag-1', |
| 31 | + allocationKey: 'allocation-1', |
| 32 | + variationKey: 'control', |
| 33 | + }; |
| 34 | + await hybridCache.init(); |
| 35 | + expect(hybridCache.has(cacheKey)).toBeFalsy(); |
| 36 | + expect(simpleCache.has(cacheKey)).toBeFalsy(); |
| 37 | + hybridCache.set(cacheKey); |
| 38 | + expect(hybridCache.has(cacheKey)).toBeTruthy(); |
| 39 | + expect(simpleCache.has(cacheKey)).toBeTruthy(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should populate asyncStorageCache from simpleCache', async () => { |
| 43 | + const key1 = { |
| 44 | + subjectKey: 'subject-1', |
| 45 | + flagKey: 'flag-1', |
| 46 | + allocationKey: 'allocation-1', |
| 47 | + variationKey: 'control', |
| 48 | + }; |
| 49 | + const key2 = { |
| 50 | + subjectKey: 'subject-2', |
| 51 | + flagKey: 'flag-2', |
| 52 | + allocationKey: 'allocation-2', |
| 53 | + variationKey: 'control', |
| 54 | + }; |
| 55 | + expect(simpleCache.has(key1)).toBeFalsy(); |
| 56 | + asyncStorageCache.set(key1); |
| 57 | + asyncStorageCache.set(key2); |
| 58 | + await hybridCache.init(); |
| 59 | + expect(simpleCache.has(key1)).toBeTruthy(); |
| 60 | + expect(simpleCache.has(key2)).toBeTruthy(); |
| 61 | + expect(simpleCache.has({ ...key1, allocationKey: 'foo' })).toBeFalsy(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should handle variation changes for same subject, flag, and allocation', async () => { |
| 65 | + const baseKey = { |
| 66 | + subjectKey: 'subject-1', |
| 67 | + flagKey: 'flag-1', |
| 68 | + allocationKey: 'allocation-1', |
| 69 | + }; |
| 70 | + |
| 71 | + const originalAssignment = { |
| 72 | + ...baseKey, |
| 73 | + variationKey: 'control', |
| 74 | + }; |
| 75 | + |
| 76 | + const newAssignment = { |
| 77 | + ...baseKey, |
| 78 | + variationKey: 'treatment', |
| 79 | + }; |
| 80 | + |
| 81 | + await hybridCache.init(); |
| 82 | + hybridCache.set(originalAssignment); |
| 83 | + expect(hybridCache.has(originalAssignment)).toBeTruthy(); |
| 84 | + |
| 85 | + hybridCache.set(newAssignment); |
| 86 | + |
| 87 | + expect(hybridCache.has(originalAssignment)).toBeFalsy(); |
| 88 | + expect(hybridCache.has(newAssignment)).toBeTruthy(); |
| 89 | + }); |
| 90 | +}); |
0 commit comments