xref: /wasmtime-44.0.1/crates/cache/src/tests.rs (revision 61a371ac)
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 separate 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]
test_cache_init()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          directory = '{}'\n\
17          baseline-compression-level = {}\n",
18         cache_dir.display(),
19         baseline_compression_level,
20     );
21     fs::write(&config_path, config_content).expect("Failed to write test config file");
22 
23     let cache_config = CacheConfig::from_file(Some(&config_path)).unwrap();
24 
25     // assumption: config init creates cache directory and returns canonicalized path
26     assert_eq!(
27         cache_config.directory(),
28         Some(&fs::canonicalize(cache_dir).unwrap())
29     );
30     assert_eq!(
31         cache_config.baseline_compression_level(),
32         baseline_compression_level
33     );
34 
35     // test if we can use worker
36     Cache::new(cache_config)
37         .unwrap()
38         .worker()
39         .on_cache_update_async(config_path);
40 }
41 
42 #[test]
test_write_read_cache()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          directory = '{cache_dir}'\n\
49          baseline-compression-level = 3\n",
50         cache_dir
51     );
52     let cache = Cache::new(cache_config.clone()).unwrap();
53 
54     // assumption: config load creates cache directory and returns canonicalized path
55     assert_eq!(
56         cache_config.directory(),
57         Some(&fs::canonicalize(cache_dir).unwrap())
58     );
59 
60     let compiler1 = "test-1";
61     let compiler2 = "test-2";
62 
63     let entry1 = ModuleCacheEntry::from_inner(ModuleCacheEntryInner::new(compiler1, &cache));
64     let entry2 = ModuleCacheEntry::from_inner(ModuleCacheEntryInner::new(compiler2, &cache));
65 
66     entry1.get_data::<_, i32, i32>(1, |_| Ok(100)).unwrap();
67     entry1.get_data::<_, i32, i32>(1, |_| panic!()).unwrap();
68 
69     entry1.get_data::<_, i32, i32>(2, |_| Ok(100)).unwrap();
70     entry1.get_data::<_, i32, i32>(1, |_| panic!()).unwrap();
71     entry1.get_data::<_, i32, i32>(2, |_| panic!()).unwrap();
72 
73     entry1.get_data::<_, i32, i32>(3, |_| Ok(100)).unwrap();
74     entry1.get_data::<_, i32, i32>(1, |_| panic!()).unwrap();
75     entry1.get_data::<_, i32, i32>(2, |_| panic!()).unwrap();
76     entry1.get_data::<_, i32, i32>(3, |_| panic!()).unwrap();
77 
78     entry1.get_data::<_, i32, i32>(4, |_| Ok(100)).unwrap();
79     entry1.get_data::<_, i32, i32>(1, |_| panic!()).unwrap();
80     entry1.get_data::<_, i32, i32>(2, |_| panic!()).unwrap();
81     entry1.get_data::<_, i32, i32>(3, |_| panic!()).unwrap();
82     entry1.get_data::<_, i32, i32>(4, |_| panic!()).unwrap();
83 
84     entry2.get_data::<_, i32, i32>(1, |_| Ok(100)).unwrap();
85     entry1.get_data::<_, i32, i32>(1, |_| panic!()).unwrap();
86     entry1.get_data::<_, i32, i32>(2, |_| panic!()).unwrap();
87     entry1.get_data::<_, i32, i32>(3, |_| panic!()).unwrap();
88     entry1.get_data::<_, i32, i32>(4, |_| panic!()).unwrap();
89     entry2.get_data::<_, i32, i32>(1, |_| panic!()).unwrap();
90 }
91