rez-next-cache 0.3.0

Intelligent caching system for rez-next with multi-level cache and predictive preheating
Documentation
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
//! Tests for the intelligent cache system

#[cfg(test)]
#[allow(clippy::module_inception)]
mod tests {
    use crate::{
        IntelligentCacheManager, L1CacheConfig, MonitoringConfig, PreheatingConfig, TuningConfig,
        UnifiedCache, UnifiedCacheConfig, CACHE_VERSION, DEFAULT_L1_CAPACITY, DEFAULT_L2_CAPACITY,
        DEFAULT_MEMORY_LIMIT_MB, DEFAULT_TTL_SECONDS,
    };
    use std::time::Duration;
    use tokio::time::sleep;

    #[test]
    #[allow(clippy::const_is_empty)]
    fn test_version_info() {
        // CACHE_VERSION is set at compile time via env!("CARGO_PKG_VERSION")
        // Verify it matches the expected semantic version pattern
        assert!(
            CACHE_VERSION.contains('.'),
            "CACHE_VERSION should be a semver string, got: {}",
            CACHE_VERSION,
        );
    }

    #[test]
    fn test_default_constants() {
        const _: () = {
            assert!(DEFAULT_L1_CAPACITY > 0);
            assert!(DEFAULT_L2_CAPACITY > DEFAULT_L1_CAPACITY);
            assert!(DEFAULT_TTL_SECONDS > 0);
            assert!(DEFAULT_MEMORY_LIMIT_MB > 0);
        };
    }

    #[tokio::test]
    async fn test_basic_cache_operations() {
        let config = UnifiedCacheConfig::default();
        let cache = IntelligentCacheManager::<String, String>::new(config);

        // Test put and get
        cache
            .put("key1".to_string(), "value1".to_string())
            .await
            .unwrap();
        let result = cache.get(&"key1".to_string()).await;
        assert_eq!(result, Some("value1".to_string()));

        // Test cache miss
        let result = cache.get(&"nonexistent".to_string()).await;
        assert_eq!(result, None);

        // Test contains_key
        assert!(cache.contains_key(&"key1".to_string()).await);
        assert!(!cache.contains_key(&"nonexistent".to_string()).await);

        // Test remove
        assert!(cache.remove(&"key1".to_string()).await);
        assert!(!cache.contains_key(&"key1".to_string()).await);
    }

    #[tokio::test]
    async fn test_multilevel_caching() {
        let config = UnifiedCacheConfig {
            l1_config: L1CacheConfig {
                max_entries: 3, // Small L1 to trigger L2 usage
                promotion_threshold: 2,
                ..Default::default()
            },
            ..Default::default()
        };

        let cache = IntelligentCacheManager::<String, String>::new(config);

        // Fill L1 beyond capacity
        for i in 0..5 {
            let key = format!("key_{}", i);
            let value = format!("value_{}", i);
            cache.put(key, value).await.unwrap();
        }

        let stats = cache.get_stats().await;
        assert!(stats.l1_stats.entries <= 3);
        // L2 may or may not have entries depending on implementation
        // Just verify we can get stats without error

        // Access an item multiple times to trigger promotion
        for _ in 0..3 {
            let _ = cache.get(&"key_1".to_string()).await;
        }

        let _stats_after = cache.get_stats().await;
        // Promotions may or may not occur depending on implementation
        // Just verify we can get stats without error
    }

    #[tokio::test]
    async fn test_predictive_preheating() {
        let config = UnifiedCacheConfig {
            preheating_config: PreheatingConfig {
                enable_predictive_preheating: true,
                enable_pattern_learning: true,
                min_confidence_threshold: 0.3,
                ..Default::default()
            },
            ..Default::default()
        };

        let cache = IntelligentCacheManager::<String, String>::new(config);

        // Create access patterns
        for cycle in 0..3 {
            for i in 0..5 {
                if i % 2 == cycle % 2 {
                    let key = format!("pattern_key_{}", i);
                    cache.preheater().record_access(&key).await;
                }
            }
            sleep(Duration::from_millis(10)).await;
        }

        let stats = cache.preheater().get_stats();
        assert!(stats.patterns_learned > 0);

        // Test prediction
        let _recommendations = cache.preheater().get_preheat_recommendations().await;
        // Should have some recommendations based on patterns (may be 0 if confidence is too low)
        // Just verify we can get recommendations without error
    }

    #[tokio::test]
    async fn test_adaptive_tuning() {
        let config = UnifiedCacheConfig {
            tuning_config: TuningConfig {
                enable_adaptive_tuning: true,
                min_samples_for_tuning: 3,
                target_hit_rate: 0.9,
                ..Default::default()
            },
            ..Default::default()
        };

        let cache = IntelligentCacheManager::<String, String>::new(config);

        // Simulate workload
        for i in 0..10 {
            let key = format!("key_{}", i);
            let value = format!("value_{}", i);

            let _ = cache.get(&key).await; // Miss
            cache.put(key, value).await.unwrap(); // Put
        }

        // Record performance
        let stats = cache.get_stats().await;
        cache.tuner().record_performance(&stats).await;

        // Trigger tuning
        let _recommendations = cache.tuner().analyze_and_tune().await;

        // Should generate some recommendations (may be 0)
        // Just verify we can get recommendations without error

        let _tuning_stats = cache.tuner().get_stats();
        // Just verify we can get tuning stats without error
    }

    #[tokio::test]
    async fn test_performance_monitoring() {
        let config = UnifiedCacheConfig {
            monitoring_config: MonitoringConfig {
                enable_detailed_stats: true,
                enable_performance_metrics: true,
                enable_event_logging: true,
                ..Default::default()
            },
            ..Default::default()
        };

        let cache = IntelligentCacheManager::<String, Vec<u8>>::new(config);
        let monitor = cache.monitor();

        // Perform operations
        for i in 0..10 {
            let key = format!("key_{}", i);
            let value = vec![i as u8; 100];

            cache.put(key.clone(), value).await.unwrap();
            let _ = cache.get(&key).await;
        }

        // Check metrics
        let metrics = monitor.get_performance_metrics().await;
        assert!(metrics.avg_get_latency_us >= 0.0);
        assert!(metrics.avg_put_latency_us >= 0.0);

        // Check events
        let events = monitor.get_recent_events(5).await;
        assert!(!events.is_empty());
    }

    #[tokio::test]
    async fn test_cache_statistics() {
        let config = UnifiedCacheConfig::default();
        let cache = IntelligentCacheManager::<String, String>::new(config);

        // Initially empty
        let stats = cache.get_stats().await;
        assert_eq!(stats.l1_stats.entries, 0);
        assert_eq!(stats.l2_stats.entries, 0);
        assert_eq!(stats.overall_stats.total_entries, 0);

        // Add some data
        for i in 0..5 {
            let key = format!("key_{}", i);
            let value = format!("value_{}", i);
            cache.put(key, value).await.unwrap();
        }

        let stats = cache.get_stats().await;
        assert_eq!(stats.overall_stats.total_entries, 5);

        // Test some gets to generate hit/miss stats
        for i in 0..5 {
            let key = format!("key_{}", i);
            let _ = cache.get(&key).await;
        }

        let stats = cache.get_stats().await;
        assert!(stats.l1_stats.hits > 0);
    }

    #[tokio::test]
    async fn test_cache_capacity_limits() {
        let config = UnifiedCacheConfig {
            l1_config: L1CacheConfig {
                max_entries: 2,
                ..Default::default()
            },
            ..Default::default()
        };

        let cache = IntelligentCacheManager::<String, String>::new(config);

        // Fill beyond L1 capacity
        for i in 0..5 {
            let key = format!("key_{}", i);
            let value = format!("value_{}", i);
            cache.put(key, value).await.unwrap();
        }

        let stats = cache.get_stats().await;
        assert!(stats.l1_stats.entries <= 2);
        // Total entries may be less than 5 due to eviction policies
        assert!(stats.overall_stats.total_entries <= 5);
    }

    #[tokio::test]
    async fn test_cache_clear() {
        let config = UnifiedCacheConfig::default();
        let cache = IntelligentCacheManager::<String, String>::new(config);

        // Add data
        for i in 0..3 {
            let key = format!("key_{}", i);
            let value = format!("value_{}", i);
            cache.put(key, value).await.unwrap();
        }

        assert!(!cache.is_empty().await);

        // Clear cache
        cache.clear().await.unwrap();

        assert!(cache.is_empty().await);
        let stats = cache.get_stats().await;
        assert_eq!(stats.overall_stats.total_entries, 0);
    }

    #[tokio::test]
    async fn test_concurrent_access() {
        let config = UnifiedCacheConfig::default();
        let cache = std::sync::Arc::new(IntelligentCacheManager::<String, String>::new(config));

        let mut handles = Vec::new();

        // Spawn multiple tasks
        for worker_id in 0..4 {
            let cache = std::sync::Arc::clone(&cache);
            let handle = tokio::spawn(async move {
                for i in 0..10 {
                    let key = format!("worker_{}_key_{}", worker_id, i);
                    let value = format!("worker_{}_value_{}", worker_id, i);

                    cache.put(key.clone(), value.clone()).await.unwrap();
                    let result = cache.get(&key).await;
                    assert_eq!(result, Some(value));
                }
            });
            handles.push(handle);
        }

        // Wait for all tasks
        for handle in handles {
            handle.await.unwrap();
        }

        let stats = cache.get_stats().await;
        assert_eq!(stats.overall_stats.total_entries, 40);
    }

    #[tokio::test]
    async fn test_configuration_validation() {
        // Valid configuration
        let config = UnifiedCacheConfig::default();
        assert!(config.validate().is_ok());

        // Invalid L1 configuration
        let mut config = UnifiedCacheConfig::default();
        config.l1_config.max_entries = 0;
        assert!(config.validate().is_err());

        // Invalid preheating configuration
        let mut config = UnifiedCacheConfig::default();
        config.preheating_config.min_confidence_threshold = 1.5;
        assert!(config.validate().is_err());
    }

    // ── Phase 80: Concurrent package version query tests ─────────────────────

    /// Simulate concurrent package version lookups (like rez-next solver querying multiple packages)
    #[tokio::test]
    async fn test_concurrent_package_version_queries() {
        use std::sync::Arc;
        let config = UnifiedCacheConfig::default();
        let cache = Arc::new(IntelligentCacheManager::<String, Vec<String>>::new(config));

        // Pre-populate with package versions
        let packages = ["python", "maya", "houdini", "nuke", "hiero"];
        for pkg in &packages {
            let versions = (0..10)
                .map(|i| format!("{}.{}.0", i / 5 + 1, i % 5))
                .collect();
            cache.put(pkg.to_string(), versions).await.unwrap();
        }

        // Spawn concurrent readers (simulating solver querying versions)
        let mut handles = Vec::new();
        for worker in 0..8 {
            let cache = Arc::clone(&cache);
            let pkgs: Vec<String> = packages.iter().map(|s| s.to_string()).collect();
            let handle = tokio::spawn(async move {
                let mut hits = 0usize;
                for pkg in &pkgs {
                    if cache.get(pkg).await.is_some() {
                        hits += 1;
                    }
                }
                assert_eq!(hits, 5, "worker {} should find all 5 packages", worker);
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.await.unwrap();
        }

        let stats = cache.get_stats().await;
        // All 5 packages should still be cached
        assert!(stats.overall_stats.total_entries >= 1);
    }

    /// Test that concurrent writes don't corrupt cache state
    #[tokio::test]
    async fn test_concurrent_writes_no_corruption() {
        use std::sync::Arc;
        let config = UnifiedCacheConfig::default();
        let cache = Arc::new(IntelligentCacheManager::<String, String>::new(config));

        let mut handles = Vec::new();
        for writer in 0..5 {
            let cache = Arc::clone(&cache);
            let handle = tokio::spawn(async move {
                for i in 0..20 {
                    let key = format!("shared_key_{}", i % 5); // shared keys to test concurrent overwrite
                    let value = format!("writer_{}_value_{}", writer, i);
                    cache.put(key.clone(), value).await.unwrap();
                    // Read back and verify it's a valid value (any writer's value is ok)
                    let got = cache.get(&key).await;
                    assert!(got.is_some(), "key {} should exist after write", key);
                }
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.await.unwrap();
        }

        // All shared keys should exist
        for i in 0..5 {
            let key = format!("shared_key_{}", i);
            assert!(
                cache.contains_key(&key).await,
                "shared_key_{} should exist",
                i
            );
        }
    }

    /// Test cache eviction doesn't break concurrent access
    #[tokio::test]
    async fn test_cache_eviction_under_load() {
        use std::sync::Arc;
        let config = UnifiedCacheConfig {
            l1_config: L1CacheConfig {
                max_entries: 5, // Small capacity to force eviction
                ..Default::default()
            },
            ..Default::default()
        };
        let cache = Arc::new(IntelligentCacheManager::<String, String>::new(config));

        // Insert 20 entries into a size-5 cache
        for i in 0..20 {
            let key = format!("evict_key_{}", i);
            let value = format!("value_{}", i);
            cache.put(key, value).await.unwrap();
        }

        // L1 should not exceed max_entries
        let stats = cache.get_stats().await;
        assert!(
            stats.l1_stats.entries <= 5,
            "L1 should not exceed capacity 5, got {}",
            stats.l1_stats.entries
        );
    }

    /// Test batch put and get
    #[tokio::test]
    async fn test_batch_operations() {
        let config = UnifiedCacheConfig::default();
        let cache = IntelligentCacheManager::<u64, String>::new(config);

        // Batch insert
        let batch: Vec<(u64, String)> = (0u64..50).map(|i| (i, format!("pkg_v{}.0", i))).collect();
        for (k, v) in batch {
            cache.put(k, v).await.unwrap();
        }

        let stats = cache.get_stats().await;
        assert_eq!(stats.overall_stats.total_entries, 50);

        // Verify random sample
        let result = cache.get(&25u64).await;
        assert_eq!(result, Some("pkg_v25.0".to_string()));

        let result = cache.get(&0u64).await;
        assert_eq!(result, Some("pkg_v0.0".to_string()));
    }

    /// Test cache clear while concurrent access
    #[tokio::test]
    async fn test_clear_is_safe_under_concurrent_access() {
        use std::sync::Arc;
        let config = UnifiedCacheConfig::default();
        let cache = Arc::new(IntelligentCacheManager::<String, String>::new(config));

        // Populate
        for i in 0..20 {
            cache
                .put(format!("key_{}", i), format!("val_{}", i))
                .await
                .unwrap();
        }

        // Clear while spawning readers
        let cache2 = Arc::clone(&cache);
        let reader = tokio::spawn(async move {
            for i in 0..20 {
                let _ = cache2.get(&format!("key_{}", i)).await;
            }
        });

        cache.clear().await.unwrap();
        reader.await.unwrap();

        // After clear, cache should be empty
        assert!(cache.is_empty().await);
    }

    // ── Phase 103: Cache TTL and eviction strategy tests ─────────────────────

    /// Test default TTL config is applied correctly
    #[test]
    fn test_default_ttl_config() {
        let config = L1CacheConfig::default();
        assert_eq!(config.default_ttl, DEFAULT_TTL_SECONDS);
        assert!(config.default_ttl > 0);
    }

    /// Test cache config serialization roundtrip
    #[test]
    fn test_cache_config_serialization() {
        let config = UnifiedCacheConfig::default();
        let json = serde_json::to_string(&config).expect("serialize config");
        let restored: UnifiedCacheConfig = serde_json::from_str(&json).expect("deserialize config");
        assert_eq!(restored.l1_config.max_entries, config.l1_config.max_entries);
        assert_eq!(restored.l1_config.default_ttl, config.l1_config.default_ttl);
    }

    /// Test LRU eviction strategy via small cache pressure
    #[tokio::test]
    async fn test_lru_eviction_strategy() {
        let config = UnifiedCacheConfig {
            l1_config: L1CacheConfig {
                max_entries: 3,
                eviction_strategy: crate::EvictionStrategy::LRU,
                ..Default::default()
            },
            ..Default::default()
        };
        let cache = IntelligentCacheManager::<String, String>::new(config);

        // Insert 3 entries (fills L1 exactly)
        cache.put("a".to_string(), "1".to_string()).await.unwrap();
        cache.put("b".to_string(), "2".to_string()).await.unwrap();
        cache.put("c".to_string(), "3".to_string()).await.unwrap();

        // Insert a 4th: should evict LRU (oldest unaccessed)
        cache.put("d".to_string(), "4".to_string()).await.unwrap();

        let stats = cache.get_stats().await;
        assert!(stats.l1_stats.entries <= 3, "L1 should not exceed capacity");
    }

    /// Test FIFO eviction strategy config serialization
    #[test]
    fn test_eviction_strategy_serialization() {
        let strategies = [
            crate::EvictionStrategy::LRU,
            crate::EvictionStrategy::LFU,
            crate::EvictionStrategy::FIFO,
            crate::EvictionStrategy::TTL,
        ];
        for strategy in &strategies {
            let json = serde_json::to_string(strategy).unwrap();
            let restored: crate::EvictionStrategy = serde_json::from_str(&json).unwrap();
            assert_eq!(restored, *strategy);
        }
    }

    /// Test cache with TTL eviction strategy type
    #[tokio::test]
    async fn test_ttl_strategy_config() {
        let config = UnifiedCacheConfig {
            l1_config: L1CacheConfig {
                eviction_strategy: crate::EvictionStrategy::TTL,
                default_ttl: 5, // 5 second TTL
                max_entries: 100,
                ..Default::default()
            },
            ..Default::default()
        };
        let cache = IntelligentCacheManager::<String, String>::new(config);
        cache
            .put("ttl_key".to_string(), "ttl_val".to_string())
            .await
            .unwrap();
        // Entry should exist immediately after insert
        assert!(cache.contains_key(&"ttl_key".to_string()).await);
    }

    /// Test cache size stays consistent after repeated operations
    #[tokio::test]
    async fn test_cache_size_consistency() {
        let config = UnifiedCacheConfig::default();
        let cache = IntelligentCacheManager::<u32, String>::new(config);

        for i in 0..10u32 {
            cache.put(i, format!("v{}", i)).await.unwrap();
        }
        assert_eq!(cache.get_stats().await.overall_stats.total_entries, 10);

        // Remove 5 entries
        for i in 0..5u32 {
            cache.remove(&i).await;
        }
        let stats = cache.get_stats().await;
        assert!(
            stats.overall_stats.total_entries <= 10,
            "Entries should decrease after remove"
        );
    }

    /// Test cache clear empties all entries
    #[tokio::test]
    async fn test_cache_clear_removes_all_entries() {
        let config = UnifiedCacheConfig::default();
        let cache = IntelligentCacheManager::<String, String>::new(config);

        cache.put("a".to_string(), "1".to_string()).await.unwrap();
        cache.put("b".to_string(), "2".to_string()).await.unwrap();
        assert!(!cache.is_empty().await);

        cache.clear().await.unwrap();
        assert!(cache.is_empty().await, "Cache should be empty after clear");
        assert_eq!(cache.size().await, 0, "size() should be 0 after clear");
    }

    /// Test cache type identifier
    #[tokio::test]
    async fn test_cache_type_identifier() {
        let config = UnifiedCacheConfig::default();
        let cache = IntelligentCacheManager::<String, String>::new(config);
        let type_id = cache.cache_type();
        assert!(
            !type_id.is_empty(),
            "cache_type() should return a non-empty string"
        );
    }

    /// Test summary_report contains expected fields
    #[test]
    fn test_unified_cache_stats_summary_report() {
        use crate::UnifiedCacheStats;
        let mut stats = UnifiedCacheStats::new();
        stats.l1_stats.hits = 70;
        stats.l1_stats.misses = 30;
        stats.update_overall_stats();
        let report = stats.summary_report();
        assert!(
            report.contains("Hit Rate"),
            "Report should contain 'Hit Rate'"
        );
        assert!(
            report.contains("Entries"),
            "Report should contain 'Entries'"
        );
    }

    /// Test CacheLevelStats hit_rate calculation
    #[test]
    fn test_cache_level_stats_hit_rate_precision() {
        use crate::CacheLevelStats;
        let mut stats = CacheLevelStats {
            hits: 3,
            misses: 1,
            capacity: 10,
            entries: 5,
            ..Default::default()
        };
        stats.update_calculated_fields();
        assert!(
            (stats.hit_rate - 0.75).abs() < 1e-10,
            "Hit rate should be exactly 0.75 for 3 hits / 4 total"
        );
        assert!(
            (stats.load_factor - 0.5).abs() < 1e-10,
            "Load factor should be 0.5 for 5 entries / 10 capacity"
        );
    }
}