xref: /wasmtime-44.0.1/crates/cache/src/tests.rs (revision 61a371ac)
108f9eb17SAlex Crichton use super::config::tests::test_prolog;
208f9eb17SAlex Crichton use super::*;
308f9eb17SAlex Crichton 
40e9121daSFrankReh // Since cache system is a global thing, each test needs to be run in separate process.
508f9eb17SAlex Crichton // So, init() tests are run as integration tests.
608f9eb17SAlex Crichton // However, caching is a private thing, an implementation detail, and needs to be tested
708f9eb17SAlex Crichton // from the inside of the module.
808f9eb17SAlex Crichton // We test init() in exactly one test, rest of the tests doesn't rely on it.
908f9eb17SAlex Crichton 
1008f9eb17SAlex Crichton #[test]
test_cache_init()1108f9eb17SAlex Crichton fn test_cache_init() {
1208f9eb17SAlex Crichton     let (_tempdir, cache_dir, config_path) = test_prolog();
1308f9eb17SAlex Crichton     let baseline_compression_level = 4;
1408f9eb17SAlex Crichton     let config_content = format!(
1508f9eb17SAlex Crichton         "[cache]\n\
1608c7359fSAlex Crichton          directory = '{}'\n\
1708f9eb17SAlex Crichton          baseline-compression-level = {}\n",
1808c7359fSAlex Crichton         cache_dir.display(),
1908f9eb17SAlex Crichton         baseline_compression_level,
2008f9eb17SAlex Crichton     );
2108f9eb17SAlex Crichton     fs::write(&config_path, config_content).expect("Failed to write test config file");
2208f9eb17SAlex Crichton 
2308f9eb17SAlex Crichton     let cache_config = CacheConfig::from_file(Some(&config_path)).unwrap();
2408f9eb17SAlex Crichton 
2508f9eb17SAlex Crichton     // assumption: config init creates cache directory and returns canonicalized path
2608f9eb17SAlex Crichton     assert_eq!(
27*61a371acSJesse Rusak         cache_config.directory(),
28*61a371acSJesse Rusak         Some(&fs::canonicalize(cache_dir).unwrap())
2908f9eb17SAlex Crichton     );
3008f9eb17SAlex Crichton     assert_eq!(
3108f9eb17SAlex Crichton         cache_config.baseline_compression_level(),
3208f9eb17SAlex Crichton         baseline_compression_level
3308f9eb17SAlex Crichton     );
3408f9eb17SAlex Crichton 
3508f9eb17SAlex Crichton     // test if we can use worker
362cd52b76SBen Brandt     Cache::new(cache_config)
372cd52b76SBen Brandt         .unwrap()
382cd52b76SBen Brandt         .worker()
392cd52b76SBen Brandt         .on_cache_update_async(config_path);
4008f9eb17SAlex Crichton }
4108f9eb17SAlex Crichton 
4208f9eb17SAlex Crichton #[test]
test_write_read_cache()4308f9eb17SAlex Crichton fn test_write_read_cache() {
4408f9eb17SAlex Crichton     let (_tempdir, cache_dir, config_path) = test_prolog();
4508f9eb17SAlex Crichton     let cache_config = load_config!(
4608f9eb17SAlex Crichton         config_path,
4708f9eb17SAlex Crichton         "[cache]\n\
4808c7359fSAlex Crichton          directory = '{cache_dir}'\n\
4908f9eb17SAlex Crichton          baseline-compression-level = 3\n",
5008f9eb17SAlex Crichton         cache_dir
5108f9eb17SAlex Crichton     );
522cd52b76SBen Brandt     let cache = Cache::new(cache_config.clone()).unwrap();
5308f9eb17SAlex Crichton 
5408f9eb17SAlex Crichton     // assumption: config load creates cache directory and returns canonicalized path
5508f9eb17SAlex Crichton     assert_eq!(
56*61a371acSJesse Rusak         cache_config.directory(),
57*61a371acSJesse Rusak         Some(&fs::canonicalize(cache_dir).unwrap())
5808f9eb17SAlex Crichton     );
5908f9eb17SAlex Crichton 
6008f9eb17SAlex Crichton     let compiler1 = "test-1";
6108f9eb17SAlex Crichton     let compiler2 = "test-2";
6208f9eb17SAlex Crichton 
632cd52b76SBen Brandt     let entry1 = ModuleCacheEntry::from_inner(ModuleCacheEntryInner::new(compiler1, &cache));
642cd52b76SBen Brandt     let entry2 = ModuleCacheEntry::from_inner(ModuleCacheEntryInner::new(compiler2, &cache));
6508f9eb17SAlex Crichton 
6608f9eb17SAlex Crichton     entry1.get_data::<_, i32, i32>(1, |_| Ok(100)).unwrap();
6708f9eb17SAlex Crichton     entry1.get_data::<_, i32, i32>(1, |_| panic!()).unwrap();
6808f9eb17SAlex Crichton 
6908f9eb17SAlex Crichton     entry1.get_data::<_, i32, i32>(2, |_| Ok(100)).unwrap();
7008f9eb17SAlex Crichton     entry1.get_data::<_, i32, i32>(1, |_| panic!()).unwrap();
7108f9eb17SAlex Crichton     entry1.get_data::<_, i32, i32>(2, |_| panic!()).unwrap();
7208f9eb17SAlex Crichton 
7308f9eb17SAlex Crichton     entry1.get_data::<_, i32, i32>(3, |_| Ok(100)).unwrap();
7408f9eb17SAlex Crichton     entry1.get_data::<_, i32, i32>(1, |_| panic!()).unwrap();
7508f9eb17SAlex Crichton     entry1.get_data::<_, i32, i32>(2, |_| panic!()).unwrap();
7608f9eb17SAlex Crichton     entry1.get_data::<_, i32, i32>(3, |_| panic!()).unwrap();
7708f9eb17SAlex Crichton 
7808f9eb17SAlex Crichton     entry1.get_data::<_, i32, i32>(4, |_| Ok(100)).unwrap();
7908f9eb17SAlex Crichton     entry1.get_data::<_, i32, i32>(1, |_| panic!()).unwrap();
8008f9eb17SAlex Crichton     entry1.get_data::<_, i32, i32>(2, |_| panic!()).unwrap();
8108f9eb17SAlex Crichton     entry1.get_data::<_, i32, i32>(3, |_| panic!()).unwrap();
8208f9eb17SAlex Crichton     entry1.get_data::<_, i32, i32>(4, |_| panic!()).unwrap();
8308f9eb17SAlex Crichton 
8408f9eb17SAlex Crichton     entry2.get_data::<_, i32, i32>(1, |_| Ok(100)).unwrap();
8508f9eb17SAlex Crichton     entry1.get_data::<_, i32, i32>(1, |_| panic!()).unwrap();
8608f9eb17SAlex Crichton     entry1.get_data::<_, i32, i32>(2, |_| panic!()).unwrap();
8708f9eb17SAlex Crichton     entry1.get_data::<_, i32, i32>(3, |_| panic!()).unwrap();
8808f9eb17SAlex Crichton     entry1.get_data::<_, i32, i32>(4, |_| panic!()).unwrap();
8908f9eb17SAlex Crichton     entry2.get_data::<_, i32, i32>(1, |_| panic!()).unwrap();
9008f9eb17SAlex Crichton }
91