()
| 863 | |
| 864 | #[test] |
| 865 | fn test_memory_leak_prevention() { |
| 866 | let cache = TtlCache::new(); |
| 867 | |
| 868 | // Insert entries with explicit size |
| 869 | cache.insert_with_ttl_and_size("key1".to_string(), "value1".to_string(), Duration::from_secs(1), 100); |
| 870 | cache.insert_with_ttl_and_size("key2".to_string(), "value2".to_string(), Duration::from_secs(1), 200); |
| 871 | |
| 872 | let initial_stats = cache.stats(); |
| 873 | assert_eq!(initial_stats.total_size_bytes, 300); |
| 874 | assert_eq!(initial_stats.total_entries, 2); |
| 875 | |
| 876 | // Replace entry (should deallocate old one) |
| 877 | cache.insert_with_ttl_and_size("key1".to_string(), "new_value1".to_string(), Duration::from_secs(1), 150); |
| 878 | |
| 879 | let stats_after_replace = cache.stats(); |
| 880 | assert_eq!(stats_after_replace.total_size_bytes, 350); // 200 + 150 |
| 881 | assert_eq!(stats_after_replace.total_entries, 2); |
| 882 | |
| 883 | // Wait for expiration |
| 884 | thread::sleep(Duration::from_millis(1100)); |
| 885 | |
| 886 | // Access expired entry (should trigger deallocation) |
| 887 | cache.get(&"key1".to_string()); |
| 888 | |
| 889 | let stats_after_expired_access = cache.stats(); |
| 890 | assert_eq!(stats_after_expired_access.expired_evictions, 1); |
| 891 | |
| 892 | // Validate memory tracking consistency |
| 893 | assert!(cache.validate_memory_tracking()); |
| 894 | } |
| 895 | |
| 896 | #[test] |
| 897 | fn test_emergency_memory_eviction() { |
nothing calls this directly
no test coverage detected