1 use super::config::tests::test_prolog; 2 use super::*; 3 4 // Since cache system is a global thing, each test needs to be run in seperate process. 5 // So, init() tests are run as integration tests. 6 // However, caching is a private thing, an implementation detail, and needs to be tested 7 // from the inside of the module. 8 // We test init() in exactly one test, rest of the tests doesn't rely on it. 9 10 #[test] 11 fn test_cache_init() { 12 let (_tempdir, cache_dir, config_path) = test_prolog(); 13 let baseline_compression_level = 4; 14 let config_content = format!( 15 "[cache]\n\ 16 enabled = true\n\ 17 directory = '{}'\n\ 18 baseline-compression-level = {}\n", 19 cache_dir.display(), 20 baseline_compression_level, 21 ); 22 fs::write(&config_path, config_content).expect("Failed to write test config file"); 23 24 let cache_config = CacheConfig::from_file(Some(&config_path)).unwrap(); 25 26 // test if we can use config 27 assert!(cache_config.enabled()); 28 // assumption: config init creates cache directory and returns canonicalized path 29 assert_eq!( 30 *cache_config.directory(), 31 fs::canonicalize(cache_dir).unwrap() 32 ); 33 assert_eq!( 34 cache_config.baseline_compression_level(), 35 baseline_compression_level 36 ); 37 38 // test if we can use worker 39 cache_config.worker().on_cache_update_async(config_path); 40 } 41 42 #[test] 43 fn test_write_read_cache() { 44 let (_tempdir, cache_dir, config_path) = test_prolog(); 45 let cache_config = load_config!( 46 config_path, 47 "[cache]\n\ 48 enabled = true\n\ 49 directory = '{cache_dir}'\n\ 50 baseline-compression-level = 3\n", 51 cache_dir 52 ); 53 assert!(cache_config.enabled()); 54 55 // assumption: config load creates cache directory and returns canonicalized path 56 assert_eq!( 57 *cache_config.directory(), 58 fs::canonicalize(cache_dir).unwrap() 59 ); 60 61 let compiler1 = "test-1"; 62 let compiler2 = "test-2"; 63 64 let entry1 = ModuleCacheEntry::from_inner(ModuleCacheEntryInner::new(compiler1, &cache_config)); 65 let entry2 = ModuleCacheEntry::from_inner(ModuleCacheEntryInner::new(compiler2, &cache_config)); 66 67 entry1.get_data::<_, i32, i32>(1, |_| Ok(100)).unwrap(); 68 entry1.get_data::<_, i32, i32>(1, |_| panic!()).unwrap(); 69 70 entry1.get_data::<_, i32, i32>(2, |_| Ok(100)).unwrap(); 71 entry1.get_data::<_, i32, i32>(1, |_| panic!()).unwrap(); 72 entry1.get_data::<_, i32, i32>(2, |_| panic!()).unwrap(); 73 74 entry1.get_data::<_, i32, i32>(3, |_| Ok(100)).unwrap(); 75 entry1.get_data::<_, i32, i32>(1, |_| panic!()).unwrap(); 76 entry1.get_data::<_, i32, i32>(2, |_| panic!()).unwrap(); 77 entry1.get_data::<_, i32, i32>(3, |_| panic!()).unwrap(); 78 79 entry1.get_data::<_, i32, i32>(4, |_| Ok(100)).unwrap(); 80 entry1.get_data::<_, i32, i32>(1, |_| panic!()).unwrap(); 81 entry1.get_data::<_, i32, i32>(2, |_| panic!()).unwrap(); 82 entry1.get_data::<_, i32, i32>(3, |_| panic!()).unwrap(); 83 entry1.get_data::<_, i32, i32>(4, |_| panic!()).unwrap(); 84 85 entry2.get_data::<_, i32, i32>(1, |_| Ok(100)).unwrap(); 86 entry1.get_data::<_, i32, i32>(1, |_| panic!()).unwrap(); 87 entry1.get_data::<_, i32, i32>(2, |_| panic!()).unwrap(); 88 entry1.get_data::<_, i32, i32>(3, |_| panic!()).unwrap(); 89 entry1.get_data::<_, i32, i32>(4, |_| panic!()).unwrap(); 90 entry2.get_data::<_, i32, i32>(1, |_| panic!()).unwrap(); 91 } 92