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

Add panicking::panicking() function #33

Merged
merged 1 commit into from
Jan 25, 2023
Merged
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
60 changes: 54 additions & 6 deletions panic/src/panicking.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
// Copyright (c) 2023 The MobileCoin Foundation

#[allow(dead_code)]
//! Common backend panic implementation
//!
//! This is a subset of the functionality available in Rust's std
//! [panicking.rs](https://github.com/rust-lang/rust/blob/master/library/std/src/panicking.rs)
//! module.

/// Common backend panic implementation
///
/// This is a subset of the functionality available in Rust's std
/// [panicking.rs](https://github.com/rust-lang/rust/blob/master/library/std/src/panicking.rs)
/// module.
#![allow(dead_code)]

/// Determines whether the current thread is unwinding because of panic.
pub(crate) fn panicking() -> bool {
!panic_count::count_is_zero()
}

pub(crate) mod panic_count {
//! Number of panics that are currently being handled on the current thread
Expand Down Expand Up @@ -115,3 +120,46 @@ pub(crate) mod panic_count {
}
}
}

#[cfg(test)]
mod test {
use super::*;

/// Sets panic count is 0
nick-mobilecoin marked this conversation as resolved.
Show resolved Hide resolved
/// Similar to the tests in mod `panic_count` tests need to call this prior
nick-mobilecoin marked this conversation as resolved.
Show resolved Hide resolved
/// to testing to ensure correct behavior
fn clear_panic_count() {
while panic_count::get_count() != 0 {
panic_count::decrease();
}
}

#[test]
fn is_panicking_false_when_panic_count_zero() {
clear_panic_count();
assert!(!panicking());
}

#[test]
fn is_panicking_true_when_panic_count_gt_zero() {
clear_panic_count();

// First panic
panic_count::increase();
assert!(panicking());

// Second panic
panic_count::increase();
assert!(panicking());

// finished second panic
panic_count::decrease();
assert!(panicking());

// finished first panic
// NB that this one is *not* panicking since we've decreased back down
// to zero
panic_count::decrease();
assert!(!panicking());
}
}