Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FnMut debug handler #1696

Merged
merged 4 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼 Nice tests.

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