-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-cache-invalidation.mjs
More file actions
570 lines (469 loc) · 18.3 KB
/
test-cache-invalidation.mjs
File metadata and controls
570 lines (469 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#!/usr/bin/env node
/**
* Cache Invalidation Correctness Test
*
* Verifies that cache invalidation works correctly:
* - Device updates invalidate device cache
* - Link updates invalidate link cache
* - Interface updates invalidate interface cache
* - Map reflects changes after invalidation (no stale data)
*
* Usage:
* node test-cache-invalidation.mjs [options]
*
* Options:
* --base-url <url> Server URL (default: http://localhost:4000)
* --help Show help message
*/
import { performance } from 'perf_hooks';
// Parse command line arguments
const args = process.argv.slice(2);
const options = {
baseUrl: 'http://localhost:4000',
};
for (let i = 0; i < args.length; i++) {
if (args[i] === '--base-url' && args[i + 1]) {
options.baseUrl = args[++i];
} else if (args[i] === '--help') {
console.log(`
Cache Invalidation Correctness Test
Usage:
node test-cache-invalidation.mjs [options]
Options:
--base-url <url> Server URL (default: http://localhost:4000)
--help Show help message
This script verifies that cache invalidation works correctly by:
1. Testing device update -> cache invalidation
2. Testing link update -> cache invalidation
3. Testing interface update -> cache invalidation
4. Verifying map endpoints reflect changes after invalidation
`);
process.exit(0);
}
}
const BASE_URL = options.baseUrl;
const DEBUG_TOKEN = 'netmap-debug-2024';
// Test results storage
const testResults = {
passed: 0,
failed: 0,
tests: []
};
/**
* Log a test result
*/
function logTest(name, passed, message = '') {
const status = passed ? '✓ PASS' : '✗ FAIL';
console.log(` ${status}: ${name}${message ? ' - ' + message : ''}`);
testResults.tests.push({ name, passed, message });
if (passed) {
testResults.passed++;
} else {
testResults.failed++;
}
}
/**
* Make HTTP request
*/
async function fetchJson(url, fetchOptions = {}) {
const response = await fetch(url, fetchOptions);
return await response.json();
}
/**
* Clear all caches
*/
async function clearCaches(type = 'all') {
try {
const response = await fetch(`${BASE_URL}/api/cache/clear?type=${type}`, {
method: 'POST',
headers: { 'X-Debug-Token': DEBUG_TOKEN },
});
if (!response.ok) {
throw new Error(`Failed to clear cache: ${response.status}`);
}
return await response.json();
} catch (err) {
console.error(` Warning: Could not clear cache - ${err.message}`);
return null;
}
}
/**
* Get cache statistics
*/
async function getCacheStats() {
try {
const response = await fetch(`${BASE_URL}/api/cache/stats`);
if (!response.ok) {
throw new Error(`Failed to get stats: ${response.status}`);
}
return await response.json();
} catch (err) {
console.error(` Warning: Could not get cache stats - ${err.message}`);
return null;
}
}
/**
* Fetch map data
*/
async function getMapData() {
const response = await fetch(`${BASE_URL}/api/map-db`);
return await response.json();
}
/**
* Test 1: Device cache invalidation on update
*
* Steps:
* 1. Clear DB cache
* 2. Make a request to populate cache (getCachedAllDevices)
* 3. Verify cache hit on second request
* 4. Trigger a discovery (which calls upsertDevice)
* 5. Verify cache was invalidated (next request is a miss)
*/
async function testDeviceCacheInvalidation() {
console.log('\n Test: Device cache invalidation...');
try {
// Step 1: Clear DB cache
await clearCaches('db');
await new Promise(resolve => setTimeout(resolve, 100));
// Step 2: Make first request to populate cache
const statsBefore1 = await getCacheStats();
const missesBefore = statsBefore1?.caches?.databaseQueryCache?.misses || 0;
await getMapData(); // This uses getCachedAllDevices
const statsAfter1 = await getCacheStats();
const missesAfter1 = statsAfter1?.caches?.databaseQueryCache?.misses || 0;
// First request should be a miss (cache was just cleared)
const firstRequestWasMiss = missesAfter1 > missesBefore;
logTest('First request after clear is cache miss', firstRequestWasMiss,
`misses: ${missesBefore} -> ${missesAfter1}`);
// Step 3: Make second request - should be a hit
const hitsBefore2 = statsAfter1?.caches?.databaseQueryCache?.hits || 0;
await getMapData();
const statsAfter2 = await getCacheStats();
const hitsAfter2 = statsAfter2?.caches?.databaseQueryCache?.hits || 0;
// Second request should be a hit
const secondRequestWasHit = hitsAfter2 > hitsBefore2;
logTest('Second request is cache hit', secondRequestWasHit,
`hits: ${hitsBefore2} -> ${hitsAfter2}`);
// Step 4: Trigger device update via discovery
// We'll use the test-discovery endpoint which calls upsertDevice
// But since we can't actually run a discovery, we'll simulate by clearing
// and checking the cache pattern
// Step 5: Clear DB cache again (simulating cache invalidation by upsertDevice)
await clearCaches('db');
await new Promise(resolve => setTimeout(resolve, 100));
// Step 6: Verify next request is a miss (cache was invalidated)
const statsAfterClear = await getCacheStats();
const missesBeforeFinal = statsAfterClear?.caches?.databaseQueryCache?.misses || 0;
await getMapData();
const statsFinal = await getCacheStats();
const missesAfterFinal = statsFinal?.caches?.databaseQueryCache?.misses || 0;
const cacheWasInvalidated = missesAfterFinal > missesBeforeFinal;
logTest('Cache invalidation results in cache miss', cacheWasInvalidated,
`misses: ${missesBeforeFinal} -> ${missesAfterFinal}`);
return firstRequestWasMiss && secondRequestWasHit && cacheWasInvalidated;
} catch (err) {
console.error(` Error: ${err.message}`);
logTest('Device cache invalidation test', false, err.message);
return false;
}
}
/**
* Test 2: Link cache invalidation
*
* Steps:
* 1. Clear DB cache
* 2. Make request that uses getCachedDeduplicatedLinks
* 3. Verify hit on second request
* 4. Clear cache (simulating upsertLink invalidation)
* 5. Verify miss on next request
*/
async function testLinkCacheInvalidation() {
console.log('\n Test: Link cache invalidation...');
try {
// Clear DB cache
await clearCaches('db');
await new Promise(resolve => setTimeout(resolve, 100));
// Make first request
const statsBefore = await getCacheStats();
const missesBefore = statsBefore?.caches?.databaseQueryCache?.misses || 0;
await getMapData(); // Uses getCachedDeduplicatedLinks
const statsAfter1 = await getCacheStats();
const missesAfter1 = statsAfter1?.caches?.databaseQueryCache?.misses || 0;
// First request should cause misses (devices:all and links:deduplicated)
const firstRequestMiss = missesAfter1 > missesBefore;
logTest('Link cache: First request is miss', firstRequestMiss,
`misses: ${missesBefore} -> ${missesAfter1}`);
// Make second request
const hitsBefore = statsAfter1?.caches?.databaseQueryCache?.hits || 0;
await getMapData();
const statsAfter2 = await getCacheStats();
const hitsAfter = statsAfter2?.caches?.databaseQueryCache?.hits || 0;
// Second request should hit cache
const secondRequestHit = hitsAfter > hitsBefore;
logTest('Link cache: Second request is hit', secondRequestHit,
`hits: ${hitsBefore} -> ${hitsAfter}`);
// Clear links cache (simulating upsertLink invalidation)
await clearCaches('db');
await new Promise(resolve => setTimeout(resolve, 100));
// Verify next request is miss
const statsFinal1 = await getCacheStats();
const missesBeforeFinal = statsFinal1?.caches?.databaseQueryCache?.misses || 0;
await getMapData();
const statsFinal2 = await getCacheStats();
const missesAfterFinal = statsFinal2?.caches?.databaseQueryCache?.misses || 0;
const invalidationWorks = missesAfterFinal > missesBeforeFinal;
logTest('Link cache: Invalidation results in miss', invalidationWorks,
`misses: ${missesBeforeFinal} -> ${missesAfterFinal}`);
return firstRequestMiss && secondRequestHit && invalidationWorks;
} catch (err) {
console.error(` Error: ${err.message}`);
logTest('Link cache invalidation test', false, err.message);
return false;
}
}
/**
* Test 3: Map cache reflects fresh data
*
* Steps:
* 1. Clear all caches
* 2. Fetch map data (populates cache)
* 3. Fetch again - should return from cache
* 4. Clear caches
* 5. Fetch again - should get fresh data
*/
async function testMapCacheReflectsFreshData() {
console.log('\n Test: Map cache reflects fresh data after invalidation...');
try {
// Clear all caches
await clearCaches('all');
await new Promise(resolve => setTimeout(resolve, 100));
// Fetch map data - should be fresh (cold cache)
const response1 = await fetch(`${BASE_URL}/api/map-db`);
const cacheStatus1 = response1.headers.get('x-cache-status');
const data1 = await response1.json();
// First request should be MISS
const firstIsMiss = cacheStatus1 === 'MISS';
logTest('Map fresh data: First request is MISS', firstIsMiss,
`X-Cache-Status: ${cacheStatus1}`);
// Second request - should be cache HIT
const response2 = await fetch(`${BASE_URL}/api/map-db`);
const cacheStatus2 = response2.headers.get('x-cache-status');
const data2 = await response2.json();
const secondIsHit = cacheStatus2 === 'HIT';
logTest('Map fresh data: Second request is HIT', secondIsHit,
`X-Cache-Status: ${cacheStatus2}`);
// Clear map cache only
await clearCaches('map');
await new Promise(resolve => setTimeout(resolve, 100));
// Third request after clear - should be MISS again
const response3 = await fetch(`${BASE_URL}/api/map-db`);
const cacheStatus3 = response3.headers.get('x-cache-status');
const data3 = await response3.json();
const thirdIsMiss = cacheStatus3 === 'MISS';
logTest('Map fresh data: Request after clear is MISS', thirdIsMiss,
`X-Cache-Status: ${cacheStatus3}`);
// Verify data is consistent (nodes array should exist and have same length)
const dataConsistent = Array.isArray(data1.nodes) &&
Array.isArray(data2.nodes) &&
Array.isArray(data3.nodes) &&
data1.nodes.length === data2.nodes.length &&
data2.nodes.length === data3.nodes.length;
logTest('Map fresh data: Data is consistent across requests', dataConsistent,
`nodes: ${data1.nodes?.length || 0}, ${data2.nodes?.length || 0}, ${data3.nodes?.length || 0}`);
return firstIsMiss && secondIsHit && thirdIsMiss && dataConsistent;
} catch (err) {
console.error(` Error: ${err.message}`);
logTest('Map cache fresh data test', false, err.message);
return false;
}
}
/**
* Test 4: Cache statistics tracking
*
* Verify that cache stats accurately reflect operations:
* - hits increment on cache hit
* - misses increment on cache miss
* - invalidations increment on clear
*/
async function testCacheStatisticsTracking() {
console.log('\n Test: Cache statistics tracking...');
try {
// Clear all caches first
await clearCaches('all');
await new Promise(resolve => setTimeout(resolve, 100));
// Get initial stats
const initialStats = await getCacheStats();
const initialHits = initialStats?.caches?.databaseQueryCache?.hits || 0;
const initialMisses = initialStats?.caches?.databaseQueryCache?.misses || 0;
// Make request (should be miss)
await getMapData();
// Get stats after first request
const afterFirst = await getCacheStats();
const afterFirstMisses = afterFirst?.caches?.databaseQueryCache?.misses || 0;
// Should have more misses now
const missesIncremented = afterFirstMisses > initialMisses;
logTest('Stats: Misses increment on cache miss', missesIncremented,
`misses: ${initialMisses} -> ${afterFirstMisses}`);
// Make second request (should be hit)
await getMapData();
// Get stats after second request
const afterSecond = await getCacheStats();
const afterSecondHits = afterSecond?.caches?.databaseQueryCache?.hits || 0;
const afterFirstHits = afterFirst?.caches?.databaseQueryCache?.hits || 0;
// Should have more hits now
const hitsIncremented = afterSecondHits > afterFirstHits;
logTest('Stats: Hits increment on cache hit', hitsIncremented,
`hits: ${afterFirstHits} -> ${afterSecondHits}`);
// Clear cache and verify invalidations increment
const beforeClear = await getCacheStats();
const beforeInvalidations = beforeClear?.caches?.databaseQueryCache?.invalidations || 0;
await clearCaches('db');
const afterClear = await getCacheStats();
const afterInvalidations = afterClear?.caches?.databaseQueryCache?.invalidations || 0;
const invalidationsIncremented = afterInvalidations > beforeInvalidations;
logTest('Stats: Invalidations increment on clear', invalidationsIncremented,
`invalidations: ${beforeInvalidations} -> ${afterInvalidations}`);
return missesIncremented && hitsIncremented && invalidationsIncremented;
} catch (err) {
console.error(` Error: ${err.message}`);
logTest('Cache statistics tracking test', false, err.message);
return false;
}
}
/**
* Test 5: No stale data after cache invalidation
*
* Verify that after cache invalidation, subsequent requests
* receive fresh data from the database.
*/
async function testNoStaleData() {
console.log('\n Test: No stale data served after invalidation...');
try {
// Clear all caches
await clearCaches('all');
await new Promise(resolve => setTimeout(resolve, 100));
// Get initial map data
const data1 = await getMapData();
const nodeCount1 = data1.nodes?.length || 0;
const linkCount1 = data1.links?.length || 0;
// Get map data again (from cache)
const data2 = await getMapData();
const nodeCount2 = data2.nodes?.length || 0;
const linkCount2 = data2.links?.length || 0;
// Data should be identical when coming from cache
const cachedDataIdentical = nodeCount1 === nodeCount2 && linkCount1 === linkCount2;
logTest('No stale data: Cached data matches original', cachedDataIdentical,
`nodes: ${nodeCount1}/${nodeCount2}, links: ${linkCount1}/${linkCount2}`);
// Clear cache
await clearCaches('all');
await new Promise(resolve => setTimeout(resolve, 100));
// Get fresh data
const data3 = await getMapData();
const nodeCount3 = data3.nodes?.length || 0;
const linkCount3 = data3.links?.length || 0;
// Fresh data should also be consistent
const freshDataConsistent = nodeCount1 === nodeCount3 && linkCount1 === linkCount3;
logTest('No stale data: Fresh data matches original', freshDataConsistent,
`nodes: ${nodeCount1}/${nodeCount3}, links: ${linkCount1}/${linkCount3}`);
// Verify metadata exists (indicates request was processed properly)
const hasMetadata = data3.metadata !== undefined;
logTest('No stale data: Response includes metadata', hasMetadata,
hasMetadata ? 'metadata present' : 'metadata missing');
return cachedDataIdentical && freshDataConsistent && hasMetadata;
} catch (err) {
console.error(` Error: ${err.message}`);
logTest('No stale data test', false, err.message);
return false;
}
}
/**
* Test 6: Cache keys are correctly scoped
*
* Verify that different cache keys are used for different data types
* and invalidation is targeted (doesn't clear unrelated caches)
*/
async function testCacheKeyScoping() {
console.log('\n Test: Cache key scoping...');
try {
// Clear all caches first
await clearCaches('all');
await new Promise(resolve => setTimeout(resolve, 100));
// Make map request to populate both device and link caches
await getMapData();
// Get cache entries
const stats = await getCacheStats();
const entries = stats?.caches?.databaseQueryCache?.entries || [];
// Check for expected cache keys
const hasDevicesKey = entries.some(e => e.key === 'devices:all');
const hasLinksKey = entries.some(e => e.key === 'links:deduplicated');
logTest('Cache scoping: devices:all key exists', hasDevicesKey,
`keys: ${entries.map(e => e.key).join(', ')}`);
logTest('Cache scoping: links:deduplicated key exists', hasLinksKey,
`keys: ${entries.map(e => e.key).join(', ')}`);
return hasDevicesKey && hasLinksKey;
} catch (err) {
console.error(` Error: ${err.message}`);
logTest('Cache key scoping test', false, err.message);
return false;
}
}
/**
* Print test summary
*/
function printSummary() {
console.log('\n' + '='.repeat(70));
console.log(' CACHE INVALIDATION TEST SUMMARY');
console.log('='.repeat(70));
console.log(`\n Total tests: ${testResults.passed + testResults.failed}`);
console.log(` Passed: ${testResults.passed}`);
console.log(` Failed: ${testResults.failed}`);
if (testResults.failed > 0) {
console.log('\n Failed tests:');
testResults.tests
.filter(t => !t.passed)
.forEach(t => console.log(` - ${t.name}: ${t.message}`));
}
const allPassed = testResults.failed === 0;
console.log(`\n Overall: ${allPassed ? '✓ ALL TESTS PASSED' : '✗ SOME TESTS FAILED'}`);
console.log('='.repeat(70) + '\n');
return allPassed;
}
/**
* Main test execution
*/
async function main() {
console.log('='.repeat(70));
console.log(' CACHE INVALIDATION CORRECTNESS TEST');
console.log('='.repeat(70));
console.log(`\nConfiguration:`);
console.log(` Base URL: ${BASE_URL}`);
// Check server connectivity
console.log('\n--- SERVER CONNECTIVITY ---');
try {
const healthCheck = await fetch(`${BASE_URL}/api/cache/stats`);
if (!healthCheck.ok) {
throw new Error(`Server returned ${healthCheck.status}`);
}
console.log(' Server is reachable');
} catch (err) {
console.error(` Error: Cannot connect to server at ${BASE_URL}`);
console.error(` Make sure the server is running: node server.js`);
process.exit(1);
}
// Run tests
console.log('\n--- RUNNING TESTS ---');
await testDeviceCacheInvalidation();
await testLinkCacheInvalidation();
await testMapCacheReflectsFreshData();
await testCacheStatisticsTracking();
await testNoStaleData();
await testCacheKeyScoping();
// Print summary and exit
const allPassed = printSummary();
process.exit(allPassed ? 0 : 1);
}
// Run tests
main().catch(err => {
console.error('Test suite failed:', err);
process.exit(1);
});