MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / test_limits_async

Function test_limits_async

tests/all/limits.rs:95–182  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

93#[tokio::test]
94#[cfg_attr(miri, ignore)]
95async fn test_limits_async() -> Result<()> {
96 let engine = Engine::default();
97 let module = Module::new(
98 &engine,
99 r#"(module (memory (export "m") 0) (table (export "t") 0 funcref))"#,
100 )?;
101
102 struct LimitsAsync {
103 memory_size: usize,
104 table_elements: usize,
105 }
106 #[async_trait::async_trait]
107 impl ResourceLimiterAsync for LimitsAsync {
108 async fn memory_growing(
109 &mut self,
110 _current: usize,
111 desired: usize,
112 _maximum: Option<usize>,
113 ) -> Result<bool> {
114 Ok(desired <= self.memory_size)
115 }
116 async fn table_growing(
117 &mut self,
118 _current: usize,
119 desired: usize,
120 _maximum: Option<usize>,
121 ) -> Result<bool> {
122 Ok(desired <= self.table_elements)
123 }
124 }
125
126 let mut store = Store::new(
127 &engine,
128 LimitsAsync {
129 memory_size: 10 * WASM_PAGE_SIZE,
130 table_elements: 5,
131 },
132 );
133
134 store.limiter_async(|s| s as &mut dyn ResourceLimiterAsync);
135
136 let instance = Instance::new_async(&mut store, &module, &[]).await?;
137
138 // Test instance exports and host objects hitting the limit
139 for memory in IntoIterator::into_iter([
140 instance.get_memory(&mut store, "m").unwrap(),
141 Memory::new_async(&mut store, MemoryType::new(0, None)).await?,
142 ]) {
143 memory.grow_async(&mut store, 3).await?;
144 memory.grow_async(&mut store, 5).await?;
145 memory.grow_async(&mut store, 2).await?;
146
147 assert_eq!(
148 memory
149 .grow_async(&mut store, 1)
150 .await
151 .map_err(|e| e.to_string())
152 .unwrap_err(),

Callers

nothing calls this directly

Calls 9

into_iterFunction · 0.85
OkFunction · 0.85
limiter_asyncMethod · 0.80
newFunction · 0.50
FuncEnum · 0.50
unwrapMethod · 0.45
get_memoryMethod · 0.45
grow_asyncMethod · 0.45
get_tableMethod · 0.45

Tested by

no test coverage detected