Files
wasmtime/crates/cache/src/tests.rs
Peter Huene c8871ee1e6 Allow instance allocators control over module compilation.
This commit introduces two new methods on `InstanceAllocator`:

* `validate_module` - this method is used to validate a module after
  translation but before compilation. It will be used for the upcoming pooling
  allocator to ensure a module being compiled adheres to the limits of the
  allocator.

* `adjust_tunables` - this method is used to adjust the `Tunables` given the
  JIT compiler.  The pooling allocator will use this to force all memories to
  be static during compilation.
2021-03-04 18:18:50 -08:00

133 lines
3.9 KiB
Rust

use super::config::tests::test_prolog;
use super::*;
use std::fs;
// Since cache system is a global thing, each test needs to be run in seperate process.
// So, init() tests are run as integration tests.
// However, caching is a private thing, an implementation detail, and needs to be tested
// from the inside of the module.
// We test init() in exactly one test, rest of the tests doesn't rely on it.
#[test]
fn test_cache_init() {
let (_tempdir, cache_dir, config_path) = test_prolog();
let baseline_compression_level = 4;
let config_content = format!(
"[cache]\n\
enabled = true\n\
directory = {}\n\
baseline-compression-level = {}\n",
toml::to_string_pretty(&format!("{}", cache_dir.display())).unwrap(),
baseline_compression_level,
);
fs::write(&config_path, config_content).expect("Failed to write test config file");
let cache_config = CacheConfig::from_file(Some(&config_path)).unwrap();
// test if we can use config
assert!(cache_config.enabled());
// assumption: config init creates cache directory and returns canonicalized path
assert_eq!(
*cache_config.directory(),
fs::canonicalize(cache_dir).unwrap()
);
assert_eq!(
cache_config.baseline_compression_level(),
baseline_compression_level
);
// test if we can use worker
cache_config.worker().on_cache_update_async(config_path);
}
#[test]
fn test_write_read_cache() {
let (_tempdir, cache_dir, config_path) = test_prolog();
let cache_config = load_config!(
config_path,
"[cache]\n\
enabled = true\n\
directory = {cache_dir}\n\
baseline-compression-level = 3\n",
cache_dir
);
assert!(cache_config.enabled());
// assumption: config load creates cache directory and returns canonicalized path
assert_eq!(
*cache_config.directory(),
fs::canonicalize(cache_dir).unwrap()
);
let compiler1 = "test-1";
let compiler2 = "test-2";
let entry1 = ModuleCacheEntry::from_inner(ModuleCacheEntryInner::new(compiler1, &cache_config));
let entry2 = ModuleCacheEntry::from_inner(ModuleCacheEntryInner::new(compiler2, &cache_config));
entry1
.get_data(1, |_| -> Result<i32, ()> { Ok(100) })
.unwrap();
entry1
.get_data(1, |_| -> Result<i32, ()> { panic!() })
.unwrap();
entry1
.get_data(2, |_| -> Result<i32, ()> { Ok(100) })
.unwrap();
entry1
.get_data(1, |_| -> Result<i32, ()> { panic!() })
.unwrap();
entry1
.get_data(2, |_| -> Result<i32, ()> { panic!() })
.unwrap();
entry1
.get_data(3, |_| -> Result<i32, ()> { Ok(100) })
.unwrap();
entry1
.get_data(1, |_| -> Result<i32, ()> { panic!() })
.unwrap();
entry1
.get_data(2, |_| -> Result<i32, ()> { panic!() })
.unwrap();
entry1
.get_data(3, |_| -> Result<i32, ()> { panic!() })
.unwrap();
entry1
.get_data(4, |_| -> Result<i32, ()> { Ok(100) })
.unwrap();
entry1
.get_data(1, |_| -> Result<i32, ()> { panic!() })
.unwrap();
entry1
.get_data(2, |_| -> Result<i32, ()> { panic!() })
.unwrap();
entry1
.get_data(3, |_| -> Result<i32, ()> { panic!() })
.unwrap();
entry1
.get_data(4, |_| -> Result<i32, ()> { panic!() })
.unwrap();
entry2
.get_data(1, |_| -> Result<i32, ()> { Ok(100) })
.unwrap();
entry1
.get_data(1, |_| -> Result<i32, ()> { panic!() })
.unwrap();
entry1
.get_data(2, |_| -> Result<i32, ()> { panic!() })
.unwrap();
entry1
.get_data(3, |_| -> Result<i32, ()> { panic!() })
.unwrap();
entry1
.get_data(4, |_| -> Result<i32, ()> { panic!() })
.unwrap();
entry2
.get_data(1, |_| -> Result<i32, ()> { panic!() })
.unwrap();
}