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