Skip to content

Commit

Permalink
Add panicking::panicking() function (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-mobilecoin authored Jan 25, 2023
2 parents 0fee18b + f941f1c commit 86b7054
Showing 1 changed file with 54 additions and 6 deletions.
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
/// Similar to the tests in mod `panic_count` tests need to call this prior
/// 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());
}
}

0 comments on commit 86b7054

Please sign in to comment.