From 595f5bb472b9f5a135fe4e2d406072d3a0934ec0 Mon Sep 17 00:00:00 2001 From: Syrus Date: Thu, 16 Apr 2020 12:14:12 -0700 Subject: [PATCH] Moved cache testing to the general tests --- lib/runtime/src/cache.rs | 50 ----------------------------------- tests/cache.rs | 56 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 50 deletions(-) create mode 100644 tests/cache.rs diff --git a/lib/runtime/src/cache.rs b/lib/runtime/src/cache.rs index 02bcc0f91..83a524984 100644 --- a/lib/runtime/src/cache.rs +++ b/lib/runtime/src/cache.rs @@ -145,53 +145,3 @@ impl Cache for FileSystemCache { Ok(()) } } - -#[cfg(test)] -mod tests { - - use super::*; - use std::env; - - #[test] - fn test_file_system_cache_run() { - use crate::{compile, imports, Func}; - use wabt::wat2wasm; - - static WAT: &'static str = r#" - (module - (type $t0 (func (param i32) (result i32))) - (func $add_one (export "add_one") (type $t0) (param $p0 i32) (result i32) - get_local $p0 - i32.const 1 - i32.add)) - "#; - - let wasm = wat2wasm(WAT).unwrap(); - - let module = compile(&wasm).unwrap(); - - let cache_dir = env::temp_dir(); - println!("test temp_dir {:?}", cache_dir); - - let mut fs_cache = unsafe { - FileSystemCache::new(cache_dir) - .map_err(|e| format!("Cache error: {:?}", e)) - .unwrap() - }; - // store module - let key = WasmHash::generate(&wasm); - fs_cache.store(key, module.clone()).unwrap(); - - // load module - let cached_module = fs_cache.load(key).unwrap(); - - let import_object = imports! {}; - let instance = cached_module.instantiate(&import_object).unwrap(); - let add_one: Func = instance.exports.get("add_one").unwrap(); - - let value = add_one.call(42).unwrap(); - - // verify it works - assert_eq!(value, 43); - } -} diff --git a/tests/cache.rs b/tests/cache.rs new file mode 100644 index 000000000..3648496f9 --- /dev/null +++ b/tests/cache.rs @@ -0,0 +1,56 @@ +#[macro_use] +mod utils; + +wasmer_backends! { + use std::env; + use wasmer::{compiler::compile_with, imports, Func}; + use wasmer_runtime::Backend as BaseBackend; + use wasmer_runtime::cache::{FileSystemCache, Cache, WasmHash}; + use wabt::wat2wasm; + use std::str::FromStr; + + #[test] + fn test_file_system_cache_run() { + static WAT: &'static str = r#" + (module + (type $t0 (func (param i32) (result i32))) + (func $add_one (export "add_one") (type $t0) (param $p0 i32) (result i32) + get_local $p0 + i32.const 1 + i32.add)) + "#; + + let wasm = wat2wasm(WAT).unwrap(); + + let module = compile_with(&wasm, &*get_compiler()).unwrap(); + + let cache_dir = env::temp_dir(); + println!("test temp_dir {:?}", cache_dir); + + let mut fs_cache = unsafe { + FileSystemCache::new(cache_dir) + .map_err(|e| format!("Cache error: {:?}", e)) + .unwrap() + }; + // store module + let key = WasmHash::generate(&wasm); + fs_cache.store(key, module.clone()).unwrap(); + + // We need to transform the `wasmer::Backend` into + // `wasmer_runtime::Backend` as that's something that the + // cache loader can understand. + let backend = BaseBackend::from_str(get_backend().to_string()).expect("Can't transform wasmer backend into wasmer_runtime backend"); + + // load module + let cached_module = fs_cache.load_with_backend(key, backend).unwrap(); + + let import_object = imports! {}; + let instance = cached_module.instantiate(&import_object).unwrap(); + let add_one: Func = instance.exports.get("add_one").unwrap(); + + let value = add_one.call(42).unwrap(); + + // verify it works + assert_eq!(value, 43); + } +}