|
| 1 | +/** |
| 2 | + * Copyright 2025, Optimizely |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import { vi, describe, it, expect, beforeEach } from 'vitest'; |
| 17 | +import { getMockAsyncCache, getMockSyncCache } from '../tests/mock/mock_cache'; |
| 18 | +import { SyncStore } from '../utils/cache/store'; |
| 19 | +import { EventWithId } from './batch_event_processor'; |
| 20 | +import { EventStore, StoredEvent } from './event_store'; |
| 21 | +import { createImpressionEvent } from '../tests/mock/create_event'; |
| 22 | + |
| 23 | +import { DEFAULT_MAX_EVENTS_IN_STORE } from './event_store'; |
| 24 | +import { exhaustMicrotasks } from '../tests/testUtils'; |
| 25 | + |
| 26 | +type TestStoreConfig = { |
| 27 | + maxSize?: number; |
| 28 | + ttl?: number; |
| 29 | +} |
| 30 | + |
| 31 | +const getEventStore = (config: TestStoreConfig = {}) => { |
| 32 | + const mockStore = getMockAsyncCache<StoredEvent>(); |
| 33 | + const store = new EventStore({...config, store: mockStore }); |
| 34 | + return { mockStore, store } |
| 35 | +} |
| 36 | + |
| 37 | +describe('EventStore', () => { |
| 38 | + it('should be able to get the stored event correctly', async () => { |
| 39 | + const { store } = getEventStore(); |
| 40 | + const event: EventWithId = { |
| 41 | + id: '1', |
| 42 | + event: createImpressionEvent('test'), |
| 43 | + } |
| 44 | + |
| 45 | + await store.set('test', event); |
| 46 | + const savedEvent = await store.get('test'); |
| 47 | + expect(savedEvent).toEqual(expect.objectContaining(event)); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should remove a key correctly', async () => { |
| 51 | + const { store } = getEventStore(); |
| 52 | + const event: EventWithId = { |
| 53 | + id: '1', |
| 54 | + event: createImpressionEvent('test'), |
| 55 | + } |
| 56 | + |
| 57 | + await store.set('test-1', event); |
| 58 | + let saved1 = await store.get('test-1'); |
| 59 | + expect(saved1).toEqual(expect.objectContaining(event)); |
| 60 | + |
| 61 | + await store.set('test-2', event); |
| 62 | + |
| 63 | + await store.remove('test-1'); |
| 64 | + saved1 = await store.get('test-1'); |
| 65 | + expect(saved1).toBeUndefined() |
| 66 | + |
| 67 | + const saved2 = await store.get('test-2'); |
| 68 | + expect(saved2).toEqual(expect.objectContaining(event)); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should return all keys from getKeys()', async () => { |
| 72 | + const { store } = getEventStore(); |
| 73 | + const event: EventWithId = { |
| 74 | + id: '1', |
| 75 | + event: createImpressionEvent('test'), |
| 76 | + }; |
| 77 | + |
| 78 | + const keys = []; |
| 79 | + |
| 80 | + for(let i = 0; i < 10; i++) { |
| 81 | + keys.push(`test-${i}`) |
| 82 | + await store.set(`test-${i}`, event); |
| 83 | + } |
| 84 | + |
| 85 | + const savedKeys = await store.getKeys(); |
| 86 | + savedKeys.sort(); |
| 87 | + |
| 88 | + expect(savedKeys).toEqual(keys); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should limit the number of saved keys', async () => { |
| 92 | + const { store } = getEventStore(); |
| 93 | + |
| 94 | + const event: EventWithId = { |
| 95 | + id: '1', |
| 96 | + event: createImpressionEvent('test'), |
| 97 | + } |
| 98 | + |
| 99 | + const keys = []; |
| 100 | + const promises = []; |
| 101 | + |
| 102 | + for(let i = 0; i < DEFAULT_MAX_EVENTS_IN_STORE + 10; i++) { |
| 103 | + keys.push(i + '') |
| 104 | + const res = store.set(i + '', event); |
| 105 | + res.catch(() => {}); |
| 106 | + promises.push(res); |
| 107 | + } |
| 108 | + |
| 109 | + for(let i = 0; i < DEFAULT_MAX_EVENTS_IN_STORE; i++) { |
| 110 | + await expect(promises[i]).resolves.not.toThrow(); |
| 111 | + } |
| 112 | + |
| 113 | + for(let i = 0; i < 10; i++) { |
| 114 | + await expect(promises[DEFAULT_MAX_EVENTS_IN_STORE + i]).rejects.toThrow(); |
| 115 | + } |
| 116 | + |
| 117 | + const savedKeys = await store.getKeys(); |
| 118 | + savedKeys.sort((a, b) => Number(a) - Number(b)); |
| 119 | + |
| 120 | + expect(savedKeys).toEqual(keys.slice(0, DEFAULT_MAX_EVENTS_IN_STORE)); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should save keys again when the number of stored events drops below maxSize', async () => { |
| 124 | + const { store } = getEventStore(); |
| 125 | + |
| 126 | + const event: EventWithId = { |
| 127 | + id: '1', |
| 128 | + event: createImpressionEvent('test'), |
| 129 | + } |
| 130 | + |
| 131 | + const keys = [] |
| 132 | + for(let i = 0; i < DEFAULT_MAX_EVENTS_IN_STORE + 10; i++) { |
| 133 | + keys.push(i + ''); |
| 134 | + await store.set(i + '', event).catch(() => {}); |
| 135 | + } |
| 136 | + |
| 137 | + const savedKeys = await store.getKeys(); |
| 138 | + savedKeys.sort((a, b) => Number(a) - Number(b)); |
| 139 | + |
| 140 | + expect(savedKeys).toEqual(keys.slice(0, DEFAULT_MAX_EVENTS_IN_STORE)); |
| 141 | + |
| 142 | + for(let i = 0; i < 10; i++) { |
| 143 | + const keyToRemove = i + ''; |
| 144 | + await store.remove(keyToRemove); |
| 145 | + } |
| 146 | + |
| 147 | + for(let i = DEFAULT_MAX_EVENTS_IN_STORE; i < DEFAULT_MAX_EVENTS_IN_STORE + 10; i++) { |
| 148 | + const keyToAdd = i + ''; |
| 149 | + await expect(store.set(keyToAdd, event)).resolves.not.toThrow(); |
| 150 | + } |
| 151 | + |
| 152 | + const finalKeys = await store.getKeys(); |
| 153 | + finalKeys.sort((a, b) => Number(a) - Number(b)); |
| 154 | + |
| 155 | + const expectedKeys = []; |
| 156 | + for(let i = 10; i < DEFAULT_MAX_EVENTS_IN_STORE + 10; i++) { |
| 157 | + expectedKeys.push(i + ''); |
| 158 | + } |
| 159 | + |
| 160 | + expect(finalKeys).toEqual(expectedKeys); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should expire events after ttl', async () => { |
| 164 | + const ttl = 100; // 100 ms |
| 165 | + const { store } = getEventStore({ ttl }); |
| 166 | + |
| 167 | + const event: EventWithId = { |
| 168 | + id: '1', |
| 169 | + event: createImpressionEvent('test'), |
| 170 | + } |
| 171 | + |
| 172 | + await store.set('test', event); |
| 173 | + const savedEvent = await store.get('test'); |
| 174 | + expect(savedEvent).toEqual(expect.objectContaining(event)); |
| 175 | + |
| 176 | + // wait for ttl to expire |
| 177 | + await new Promise(resolve => setTimeout(resolve, ttl + 50)); |
| 178 | + |
| 179 | + const expiredEvent = await store.get('test'); |
| 180 | + expect(expiredEvent).toBeUndefined(); |
| 181 | + |
| 182 | + expect(await store.getKeys()).toEqual([]); |
| 183 | + }); |
| 184 | + |
| 185 | + it('should resave events without expireAt on get', async () => { |
| 186 | + const ttl = 120_000; |
| 187 | + const { mockStore, store } = getEventStore({ ttl }); |
| 188 | + |
| 189 | + const event: EventWithId = { |
| 190 | + id: '1', |
| 191 | + event: createImpressionEvent('test'), |
| 192 | + } |
| 193 | + |
| 194 | + |
| 195 | + const originalSet = mockStore.set.bind(mockStore); |
| 196 | + |
| 197 | + let call = 0; |
| 198 | + const setSpy = vi.spyOn(mockStore, 'set').mockImplementation(async (key: string, value: StoredEvent) => { |
| 199 | + if (call++ > 0) { |
| 200 | + return originalSet(key, value); |
| 201 | + } |
| 202 | + |
| 203 | + // Simulate old stored event without expireAt |
| 204 | + const eventWithoutExpireAt: StoredEvent = { |
| 205 | + id: value.id, |
| 206 | + event: value.event, |
| 207 | + }; |
| 208 | + return originalSet(key, eventWithoutExpireAt); |
| 209 | + }); |
| 210 | + |
| 211 | + await store.set('test', event); |
| 212 | + |
| 213 | + const savedEvent = await store.get('test'); |
| 214 | + expect(savedEvent).toEqual(expect.objectContaining(event)); |
| 215 | + |
| 216 | + |
| 217 | + await exhaustMicrotasks(); |
| 218 | + expect(setSpy).toHaveBeenCalledTimes(2); |
| 219 | + |
| 220 | + const secondCall = setSpy.mock.calls[1]; |
| 221 | + |
| 222 | + expect(secondCall[1].expiresAt).toBeDefined(); |
| 223 | + expect(secondCall[1].expiresAt!).toBeGreaterThanOrEqual(Date.now() + ttl - 10); |
| 224 | + }); |
| 225 | +}); |
0 commit comments