Skip to content

Commit

Permalink
Test set_remaining_points
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Dec 17, 2020
1 parent 223761b commit 96b6d34
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/middlewares/src/metering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,34 @@ mod tests {
// assert_eq!(metering.get_remaining_points(&instance), 2);
// assert_eq!(metering.get_remaining_points(&instance), 0);
}

#[test]
fn set_remaining_points_works() {
let metering = Arc::new(Metering::new(10, cost_function));
let mut compiler_config = Cranelift::default();
compiler_config.push_middleware(metering.clone());
let store = Store::new(&JIT::new(compiler_config).engine());
let module = Module::new(&store, bytecode()).unwrap();

// Instantiate
let instance = Instance::new(&module, &imports! {}).unwrap();
assert_eq!(metering.get_remaining_points(&instance), 10);
let add_one = instance
.exports
.get_function("add_one")
.unwrap()
.native::<i32, i32>()
.unwrap();

// Increase a bit to have enough for 3 calls
metering.set_remaining_points(&instance, 12);

// Ensure we can use the new points now
add_one.call(1).unwrap();
assert_eq!(metering.get_remaining_points(&instance), 8);
add_one.call(1).unwrap();
assert_eq!(metering.get_remaining_points(&instance), 4);
add_one.call(1).unwrap();
assert_eq!(metering.get_remaining_points(&instance), 0);
}
}

0 comments on commit 96b6d34

Please sign in to comment.