| 1 |
// Copyright (c) 2023 CtrlC developers |
| 2 |
// Licensed under the Apache License, Version 2.0 |
| 3 |
// <LICENSE-APACHE or |
| 4 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT |
| 5 |
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, |
| 6 |
// at your option. All files in the project carrying such |
| 7 |
// notice may not be copied, modified, or distributed except |
| 8 |
// according to those terms. |
| 9 |
|
| 10 |
#[macro_use] |
| 11 |
mod harness; |
| 12 |
use harness::{platform, run_harness}; |
| 13 |
|
| 14 |
use std::sync::{ |
| 15 |
atomic::{AtomicBool, Ordering}, |
| 16 |
Arc, |
| 17 |
}; |
| 18 |
|
| 19 |
fn test_set_handler() { |
| 20 |
let flag = Arc::new(AtomicBool::new(false)); |
| 21 |
let flag_handler = Arc::clone(&flag); |
| 22 |
ctrlc::set_handler(move || { |
| 23 |
flag_handler.store(true, Ordering::SeqCst); |
| 24 |
}) |
| 25 |
.unwrap(); |
| 26 |
|
| 27 |
unsafe { |
| 28 |
platform::raise_ctrl_c(); |
| 29 |
} |
| 30 |
|
| 31 |
std::thread::sleep(std::time::Duration::from_millis(100)); |
| 32 |
assert!(flag.load(Ordering::SeqCst)); |
| 33 |
|
| 34 |
match ctrlc::set_handler(|| {}) { |
| 35 |
Err(ctrlc::Error::MultipleHandlers) => {} |
| 36 |
ret => panic!("{:?}", ret), |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
fn tests() { |
| 41 |
run_tests!(test_set_handler); |
| 42 |
} |
| 43 |
|
| 44 |
fn main() { |
| 45 |
run_harness(tests); |
| 46 |
} |