use std::sync::Arc;
use tensor_wasm_core::types::TenantId;
use tensor_wasm_exec::engine::{EngineConfig, TensorWasmEngine};
use tensor_wasm_exec::executor::{ExecError, SpawnConfig, TensorWasmExecutor, MAX_MODULE_BYTES};
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn spawn_rejects_module_above_default_cap() {
let engine = Arc::new(TensorWasmEngine::new().expect("engine"));
let exec = TensorWasmExecutor::new(engine);
let oversized = vec![0u8; MAX_MODULE_BYTES + 1];
let err = exec
.spawn_instance(SpawnConfig::for_tenant(TenantId(1)), &oversized)
.await
.expect_err("module above MAX_MODULE_BYTES must be rejected");
match err {
ExecError::ModuleTooLarge { len, max } => {
assert_eq!(len, MAX_MODULE_BYTES + 1);
assert_eq!(max, MAX_MODULE_BYTES);
}
other => panic!("expected ModuleTooLarge, got {other:?}"),
}
assert_eq!(
exec.instances_len(),
0,
"failed spawn must roll back its admission slot",
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn spawn_rejects_module_above_configured_cap() {
let tight_cap = 4 * 1024;
let cfg = EngineConfig {
max_module_bytes: tight_cap,
..EngineConfig::default()
};
let engine = Arc::new(TensorWasmEngine::with_config(cfg).expect("engine"));
let exec = TensorWasmExecutor::new(engine);
let oversized = vec![0u8; tight_cap + 1];
let err = exec
.spawn_instance(SpawnConfig::for_tenant(TenantId(2)), &oversized)
.await
.expect_err("module above configured cap must be rejected");
match err {
ExecError::ModuleTooLarge { len, max } => {
assert_eq!(len, tight_cap + 1);
assert_eq!(max, tight_cap);
}
other => panic!("expected ModuleTooLarge, got {other:?}"),
}
}
#[test]
fn module_too_large_maps_to_memory_exhausted() {
use tensor_wasm_core::error::TensorWasmError;
let e = ExecError::ModuleTooLarge {
len: MAX_MODULE_BYTES + 1,
max: MAX_MODULE_BYTES,
};
let b: TensorWasmError = e.into();
match b {
TensorWasmError::MemoryExhausted { requested, limit } => {
assert_eq!(requested, (MAX_MODULE_BYTES + 1) as u64);
assert_eq!(limit, MAX_MODULE_BYTES as u64);
}
other => panic!("expected MemoryExhausted, got {other:?}"),
}
}