-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Error on multiple signal handlers on unix
- Loading branch information
Showing
8 changed files
with
148 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) 2023 CtrlC developers | ||
// Licensed under the Apache License, Version 2.0 | ||
// <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT | ||
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, | ||
// at your option. All files in the project carrying such | ||
// notice may not be copied, modified, or distributed except | ||
// according to those terms. | ||
|
||
#[macro_use] | ||
mod harness; | ||
use harness::{platform, run_harness}; | ||
|
||
mod test_signal_hook; | ||
use test_signal_hook::run_signal_hook; | ||
|
||
fn expect_multiple_handlers() { | ||
#[cfg(not(windows))] | ||
match ctrlc::set_handler(|| {}) { | ||
Err(ctrlc::Error::MultipleHandlers) => {} | ||
_ => panic!("Expected Error::MultipleHandlers"), | ||
} | ||
} | ||
|
||
fn tests() { | ||
run_tests!(run_signal_hook); | ||
run_tests!(expect_multiple_handlers); | ||
} | ||
|
||
fn main() { | ||
run_harness(tests); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) 2023 CtrlC developers | ||
// Licensed under the Apache License, Version 2.0 | ||
// <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT | ||
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, | ||
// at your option. All files in the project carrying such | ||
// notice may not be copied, modified, or distributed except | ||
// according to those terms. | ||
|
||
#[macro_use] | ||
mod harness; | ||
use harness::{platform, run_harness}; | ||
|
||
mod test_signal_hook; | ||
use test_signal_hook::run_signal_hook; | ||
|
||
use std::sync::{ | ||
atomic::{AtomicBool, Ordering}, | ||
Arc, | ||
}; | ||
|
||
fn test_set_handler() { | ||
let flag = Arc::new(AtomicBool::new(false)); | ||
let flag_handler = Arc::clone(&flag); | ||
ctrlc::set_handler(move || { | ||
flag_handler.store(true, Ordering::SeqCst); | ||
}) | ||
.unwrap(); | ||
|
||
unsafe { | ||
platform::raise_ctrl_c(); | ||
} | ||
|
||
std::thread::sleep(std::time::Duration::from_millis(100)); | ||
assert!(flag.load(Ordering::SeqCst)); | ||
|
||
match ctrlc::set_handler(|| {}) { | ||
Err(ctrlc::Error::MultipleHandlers) => {} | ||
ret => panic!("{:?}", ret), | ||
} | ||
} | ||
|
||
fn tests() { | ||
run_tests!(test_set_handler); | ||
run_tests!(run_signal_hook); | ||
} | ||
|
||
fn main() { | ||
run_harness(tests); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) 2023 CtrlC developers | ||
// Licensed under the Apache License, Version 2.0 | ||
// <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT | ||
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, | ||
// at your option. All files in the project carrying such | ||
// notice may not be copied, modified, or distributed except | ||
// according to those terms. | ||
|
||
use std::sync::{ | ||
atomic::{AtomicBool, Ordering}, | ||
Arc, | ||
}; | ||
|
||
pub fn run_signal_hook() { | ||
let hook = Arc::new(AtomicBool::new(false)); | ||
|
||
signal_hook::flag::register(signal_hook::consts::SIGINT, Arc::clone(&hook)).unwrap(); | ||
|
||
unsafe { | ||
super::platform::raise_ctrl_c(); | ||
} | ||
|
||
std::thread::sleep(std::time::Duration::from_millis(100)); | ||
assert!(hook.load(Ordering::SeqCst)); | ||
} |