Skip to content

Commit fe1dfd4

Browse files
committed
test: test of catching rust panic
1 parent 7dfd85b commit fe1dfd4

File tree

6 files changed

+74
-1
lines changed

6 files changed

+74
-1
lines changed

examples/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ members = [
1414
"shlib/calc_lib",
1515
"shlib/calc_bin",
1616
"shlib/printer_lib",
17+
"panic",
1718
]
1819
resolver = "2"

examples/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,9 @@ Example of shared library with C interface and a consumer of this lib.
5151

5252
### Tokioticker
5353

54-
Tick 5 seconds and exit. Useful for tokio oracle testing.
54+
Tick 5 seconds and exit. Useful for tokio oracle testing.
55+
56+
### Panic
57+
58+
Program that just panics.
59+
Initiated by user or system panic (like divide by zero panic).

examples/panic/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "panic"
3+
version = "0.0.0"
4+
edition = "2021"
5+
workspace = "./.."
6+
publish = false
7+
8+
[[bin]]
9+
name = "panic"
10+
path = "src/panic.rs"
11+
12+
[dependencies]
13+
uuid = { version = "1.8.0", features = ["v4", "v7"] }

examples/panic/src/panic.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::env;
2+
3+
fn user_panic() {
4+
let a = 1;
5+
let b = a + 1;
6+
println!("b: {b}");
7+
panic!("then panic!");
8+
}
9+
10+
#[allow(unconditional_panic)]
11+
fn divided_by_zero() {
12+
println!("{}", 1 / 0)
13+
}
14+
15+
pub fn main() {
16+
let args: Vec<String> = env::args().collect();
17+
let panic_type = args[1].as_str();
18+
19+
match panic_type {
20+
"user" => user_panic(),
21+
"system" => divided_by_zero(),
22+
_ => panic!("unsupported"),
23+
}
24+
}

tests/integration/test_command.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,3 +472,26 @@ def test_source_bounds(self):
472472
self.debugger.expect_exact('4 fn main() {')
473473
self.debugger.expect_exact('9 myprint("bye!")')
474474

475+
@staticmethod
476+
def test_breakpoint_at_rust_panic():
477+
"""Set breakpoint to rust panic handler and catch panics"""
478+
debugger = pexpect.spawn(
479+
'./target/debug/bs ./examples/target/debug/panic -- user')
480+
debugger.sendline('break rust_panic')
481+
debugger.expect('New breakpoint')
482+
debugger.sendline('run')
483+
debugger.expect_exact('then panic!')
484+
debugger.sendline('bt')
485+
debugger.expect('rust_panic')
486+
debugger.expect('panic::user_panic')
487+
debugger.sendline('continue')
488+
489+
debugger = pexpect.spawn(
490+
'./target/debug/bs ./examples/target/debug/panic -- system')
491+
debugger.sendline('break rust_panic')
492+
debugger.expect('New breakpoint')
493+
debugger.sendline('run')
494+
debugger.expect('attempt to divide by zero')
495+
debugger.sendline('bt')
496+
debugger.expect('rust_panic')
497+
debugger.expect('panic::divided_by_zero')

0 commit comments

Comments
 (0)