()
| 55 | |
| 56 | #[test] |
| 57 | fn loop_interrupt_from_afar() -> wasmtime::Result<()> { |
| 58 | // Create an instance which calls an imported function on each iteration of |
| 59 | // the loop so we can count the number of loop iterations we've executed so |
| 60 | // far. |
| 61 | static HITS: AtomicUsize = AtomicUsize::new(0); |
| 62 | static STOP: AtomicBool = AtomicBool::new(false); |
| 63 | let mut store = interruptible_store(); |
| 64 | let module = Module::new( |
| 65 | store.engine(), |
| 66 | r#" |
| 67 | (import "" "" (func)) |
| 68 | |
| 69 | (func (export "loop") |
| 70 | (loop |
| 71 | call 0 |
| 72 | br 0) |
| 73 | ) |
| 74 | "#, |
| 75 | )?; |
| 76 | let func = Func::wrap(&mut store, || { |
| 77 | HITS.fetch_add(1, SeqCst); |
| 78 | }); |
| 79 | let instance = Instance::new(&mut store, &module, &[func.into()])?; |
| 80 | |
| 81 | // Use the engine to wait for it to enter the loop long enough and then we |
| 82 | // signal an interrupt happens. |
| 83 | let engine = store.engine().clone(); |
| 84 | let thread = std::thread::spawn(move || { |
| 85 | while HITS.load(SeqCst) <= NUM_HITS && !STOP.load(SeqCst) { |
| 86 | // continue ... |
| 87 | } |
| 88 | println!("interrupting"); |
| 89 | engine.increment_epoch(); |
| 90 | }); |
| 91 | |
| 92 | // Enter the infinitely looping function and assert that our interrupt |
| 93 | // handle does indeed actually interrupt the function. |
| 94 | let iloop = instance.get_typed_func::<(), ()>(&mut store, "loop")?; |
| 95 | let trap = iloop.call(&mut store, ()).unwrap_err().downcast::<Trap>()?; |
| 96 | STOP.store(true, SeqCst); |
| 97 | thread.join().unwrap(); |
| 98 | assert!(HITS.load(SeqCst) > NUM_HITS); |
| 99 | assert_eq!(trap, Trap::Interrupt); |
| 100 | Ok(()) |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn function_interrupt_from_afar() -> wasmtime::Result<()> { |
nothing calls this directly
no test coverage detected