File: mod.rs

package info (click to toggle)
rust-ctrlc 3.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 208 kB
  • sloc: makefile: 2
file content (46 lines) | stat: -rw-r--r-- 1,117 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// 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};

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);
}

fn main() {
    run_harness(tests);
}