Skip to content

Commit

Permalink
Merge pull request #1696 from CosmWasm/1673-mut-debughandler
Browse files Browse the repository at this point in the history
FnMut debug handler
  • Loading branch information
webmaster128 authored May 30, 2023
2 parents 5220ed7 + 66c3395 commit 6a5c3bc
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 20 deletions.
179 changes: 172 additions & 7 deletions contracts/cyberpunk/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions contracts/cyberpunk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ thiserror = "1.0.26"

[dev-dependencies]
cosmwasm-vm = { path = "../../packages/vm", default-features = false }
tempfile = "3.1.0"
52 changes: 52 additions & 0 deletions contracts/cyberpunk/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use cosmwasm_std::{from_binary, Empty, Env, Response};
use cosmwasm_vm::testing::{
execute, instantiate, mock_env, mock_info, mock_instance, mock_instance_with_gas_limit, query,
};
use std::io::Write;
use std::time::SystemTime;
use tempfile::NamedTempFile;

use cyberpunk::msg::{ExecuteMsg, QueryMsg};

Expand Down Expand Up @@ -83,6 +85,56 @@ fn debug_works() {
let _res: Response = execute(&mut deps, mock_env(), mock_info("caller", &[]), msg).unwrap();
}

// Test with
// cargo integration-test debug_timing -- --nocapture
#[test]
fn debug_timing() {
let mut deps = mock_instance_with_gas_limit(WASM, 100_000_000_000_000);

let _res: Response =
instantiate(&mut deps, mock_env(), mock_info("admin", &[]), Empty {}).unwrap();

let mut last_time = None;
deps.set_debug_handler(move |msg, _info| {
if let Some(last_time) = last_time {
let diff = SystemTime::now()
.duration_since(last_time)
.unwrap_or_default()
.as_micros();
eprintln!("{msg} (time since last debug: {diff}µs)");
} else {
eprintln!("{msg}");
}

last_time = Some(SystemTime::now());
});

let msg = ExecuteMsg::Debug {};
let _res: Response = execute(&mut deps, mock_env(), mock_info("caller", &[]), msg).unwrap();
}

#[test]
fn debug_file() {
let mut deps = mock_instance_with_gas_limit(WASM, 100_000_000_000_000);

let _res: Response =
instantiate(&mut deps, mock_env(), mock_info("admin", &[]), Empty {}).unwrap();

let temp_file = NamedTempFile::new().unwrap();
let (mut temp_file, temp_path) = temp_file.into_parts();

deps.set_debug_handler(move |msg, _info| {
writeln!(temp_file, "{msg}").unwrap();
});

let msg = ExecuteMsg::Debug {};
let _res: Response = execute(&mut deps, mock_env(), mock_info("caller", &[]), msg).unwrap();

// check if file contains the expected output
let file_content = std::fs::read_to_string(temp_path).unwrap();
assert!(file_content.contains("Round 9 done"));
}

#[test]
fn test_env() {
let mut deps = mock_instance(WASM, &[]);
Expand Down
Loading

0 comments on commit 6a5c3bc

Please sign in to comment.