| 1 | // These tests cannot run as a regular test because cargo would spawn a thread to run it, |
| 2 | // failing the signal masking. So we make our own, non-threaded harnessing |
| 3 | |
| 4 | #[cfg(target_os = "linux")] |
| 5 | fn mainrust-analyzer cargo calloop 0.10.5 main().() { |
| 6 | for test in selfrust-analyzer cargo calloop 0.10.5 crate/::testrust-analyzer cargo calloop 0.10.5 test/::TESTSrust-analyzer cargo calloop 0.10.5 test/TESTS. { |
| 7 | test(); |
| 8 | // reset the signal mask between tests |
| 9 | selfrust-analyzer cargo calloop 0.10.5 crate/::testrust-analyzer cargo calloop 0.10.5 test/::reset_maskrust-analyzer cargo calloop 0.10.5 test/reset_mask().(); |
| 10 | } |
| 11 | } |
| 12 | |
| 13 | #[cfg(not(target_os = "linux"))] |
| 14 | fn main() {} |
| 15 | |
| 16 | #[cfg(target_os = "linux")] |
| 17 | mod testrust-analyzer cargo calloop 0.10.5 test/ { |
| 18 | extern crate callooprust-analyzer cargo calloop 0.10.5 test/calloop/; |
| 19 | extern crate nixrust-analyzer cargo calloop 0.10.5 test/nix/; |
| 20 | |
| 21 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#; |
| 22 | |
| 23 | use selfrust-analyzer cargo calloop 0.10.5 test/::callooprust-analyzer cargo calloop 0.10.5 crate/::signalsrust-analyzer cargo calloop 0.10.5 sources/signals/::{Signalrust-analyzer cargo nix 0.30.1 sys/signal/Signal#, Signalsrust-analyzer cargo calloop 0.10.5 sources/signals/Signals#}; |
| 24 | use selfrust-analyzer cargo calloop 0.10.5 test/::callooprust-analyzer cargo calloop 0.10.5 crate/::{Dispatcherrust-analyzer cargo calloop 0.10.5 sources/Dispatcher#, EventLooprust-analyzer cargo calloop 0.10.5 loop_logic/EventLoop#}; |
| 25 | |
| 26 | use selfrust-analyzer cargo calloop 0.10.5 test/::nixrust-analyzer cargo nix 0.30.1 crate/::sysrust-analyzer cargo nix 0.30.1 sys/::signalrust-analyzer cargo nix 0.30.1 sys/signal/::{killrust-analyzer cargo nix 0.30.1 sys/signal/kill()., SigSetrust-analyzer cargo nix 0.30.1 sys/signal/SigSet#}; |
| 27 | use selfrust-analyzer cargo calloop 0.10.5 test/::nixrust-analyzer cargo nix 0.30.1 crate/::unistdrust-analyzer cargo nix 0.30.1 unistd/::Pidrust-analyzer cargo nix 0.30.1 unistd/Pid#; |
| 28 | |
| 29 | pub const TESTSrust-analyzer cargo calloop 0.10.5 test/TESTS.: &[fn()] = &[single_usr1rust-analyzer cargo calloop 0.10.5 test/single_usr1()., usr2_added_afterwardsrust-analyzer cargo calloop 0.10.5 test/usr2_added_afterwards()., usr2_signal_removedrust-analyzer cargo calloop 0.10.5 test/usr2_signal_removed().]; |
| 30 | |
| 31 | pub fn reset_maskrust-analyzer cargo calloop 0.10.5 test/reset_mask().() { |
| 32 | SigSetrust-analyzer cargo nix 0.30.1 sys/signal/SigSet#::emptyrust-analyzer cargo nix 0.30.1 sys/signal/impl#[SigSet]empty().().thread_set_maskrust-analyzer cargo nix 0.30.1 sys/signal/impl#[SigSet]thread_set_mask().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 33 | } |
| 34 | |
| 35 | fn single_usr1rust-analyzer cargo calloop 0.10.5 test/single_usr1().() { |
| 36 | let mut event_loop = EventLooprust-analyzer cargo calloop 0.10.5 loop_logic/EventLoop#::try_newrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]try_new().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 37 | |
| 38 | let mut signal_received = false; |
| 39 | |
| 40 | let _signal_source = event_loop |
| 41 | .handlerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]handle().() |
| 42 | .insert_sourcerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`LoopHandle<'l, Data>`]insert_source().( |
| 43 | Signalsrust-analyzer cargo calloop 0.10.5 sources/signals/Signals#::newrust-analyzer cargo calloop 0.10.5 sources/signals/impl#[Signals]new().(&[Signalrust-analyzer cargo nix 0.30.1 sys/signal/Signal#::SIGUSR1rust-analyzer cargo nix 0.30.1 sys/signal/Signal#SIGUSR1#]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 44 | move |evt, &mut (), rcv| { |
| 45 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(evt.signalrust-analyzer cargo calloop 0.10.5 sources/signals/impl#[Event]signal().() == Signalrust-analyzer cargo nix 0.30.1 sys/signal/Signal#::SIGUSR1rust-analyzer cargo nix 0.30.1 sys/signal/Signal#SIGUSR1#); |
| 46 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().rcv = true; |
| 47 | }, |
| 48 | ) |
| 49 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 50 | |
| 51 | // send ourselves a SIGUSR1 |
| 52 | killrust-analyzer cargo nix 0.30.1 sys/signal/kill().(Pidrust-analyzer cargo nix 0.30.1 unistd/Pid#::thisrust-analyzer cargo nix 0.30.1 unistd/impl#[Pid]this().(), Signalrust-analyzer cargo nix 0.30.1 sys/signal/Signal#::SIGUSR1rust-analyzer cargo nix 0.30.1 sys/signal/Signal#SIGUSR1#).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 53 | |
| 54 | event_loop |
| 55 | .dispatchrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]dispatch().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(10)), &mut signal_received) |
| 56 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 57 | |
| 58 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(signal_received); |
| 59 | } |
| 60 | |
| 61 | fn usr2_added_afterwardsrust-analyzer cargo calloop 0.10.5 test/usr2_added_afterwards().() { |
| 62 | let mut event_loop = EventLooprust-analyzer cargo calloop 0.10.5 loop_logic/EventLoop#::try_newrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]try_new().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 63 | |
| 64 | let mut signal_received = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 65 | let dispatcher = Dispatcherrust-analyzer cargo calloop 0.10.5 sources/Dispatcher#::newrust-analyzer cargo calloop 0.10.5 sources/impl#[`Dispatcher<'a, S, Data>`]new().( |
| 66 | Signalsrust-analyzer cargo calloop 0.10.5 sources/signals/Signals#::newrust-analyzer cargo calloop 0.10.5 sources/signals/impl#[Signals]new().(&[Signalrust-analyzer cargo nix 0.30.1 sys/signal/Signal#::SIGUSR1rust-analyzer cargo nix 0.30.1 sys/signal/Signal#SIGUSR1#]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 67 | move |evt, &mut (), rcv| { |
| 68 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().rcv = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(evt.signalrust-analyzer cargo calloop 0.10.5 sources/signals/impl#[Event]signal().()); |
| 69 | }, |
| 70 | ); |
| 71 | |
| 72 | let _signal_token = event_loop |
| 73 | .handlerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]handle().() |
| 74 | .register_dispatcherrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`LoopHandle<'l, Data>`]register_dispatcher().(dispatcher.clonerust-analyzer cargo calloop 0.10.5 sources/impl#[`Dispatcher<'a, S, Data>`][Clone]clone().()) |
| 75 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 76 | dispatcher |
| 77 | .as_source_mutrust-analyzer cargo calloop 0.10.5 sources/impl#[`Dispatcher<'a, S, Data>`]as_source_mut().() |
| 78 | .add_signalsrust-analyzer cargo calloop 0.10.5 sources/signals/impl#[Signals]add_signals().(&[Signalrust-analyzer cargo nix 0.30.1 sys/signal/Signal#::SIGUSR2rust-analyzer cargo nix 0.30.1 sys/signal/Signal#SIGUSR2#]) |
| 79 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 80 | |
| 81 | // send ourselves a SIGUSR2 |
| 82 | killrust-analyzer cargo nix 0.30.1 sys/signal/kill().(Pidrust-analyzer cargo nix 0.30.1 unistd/Pid#::thisrust-analyzer cargo nix 0.30.1 unistd/impl#[Pid]this().(), Signalrust-analyzer cargo nix 0.30.1 sys/signal/Signal#::SIGUSR2rust-analyzer cargo nix 0.30.1 sys/signal/Signal#SIGUSR2#).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 83 | |
| 84 | event_loop |
| 85 | .dispatchrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]dispatch().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(10)), &mut signal_received) |
| 86 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 87 | |
| 88 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(signal_received, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Signalrust-analyzer cargo nix 0.30.1 sys/signal/Signal#::SIGUSR2rust-analyzer cargo nix 0.30.1 sys/signal/Signal#SIGUSR2#)); |
| 89 | } |
| 90 | |
| 91 | fn usr2_signal_removedrust-analyzer cargo calloop 0.10.5 test/usr2_signal_removed().() { |
| 92 | let mut event_loop = EventLooprust-analyzer cargo calloop 0.10.5 loop_logic/EventLoop#::try_newrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]try_new().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 93 | |
| 94 | let mut signal_received = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 95 | let dispatcher = Dispatcherrust-analyzer cargo calloop 0.10.5 sources/Dispatcher#::newrust-analyzer cargo calloop 0.10.5 sources/impl#[`Dispatcher<'a, S, Data>`]new().( |
| 96 | Signalsrust-analyzer cargo calloop 0.10.5 sources/signals/Signals#::newrust-analyzer cargo calloop 0.10.5 sources/signals/impl#[Signals]new().(&[Signalrust-analyzer cargo nix 0.30.1 sys/signal/Signal#::SIGUSR1rust-analyzer cargo nix 0.30.1 sys/signal/Signal#SIGUSR1#, Signalrust-analyzer cargo nix 0.30.1 sys/signal/Signal#::SIGUSR2rust-analyzer cargo nix 0.30.1 sys/signal/Signal#SIGUSR2#]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 97 | move |evt, &mut (), rcv| { |
| 98 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().rcv = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(evt.signalrust-analyzer cargo calloop 0.10.5 sources/signals/impl#[Event]signal().()); |
| 99 | }, |
| 100 | ); |
| 101 | |
| 102 | let _signal_token = event_loop |
| 103 | .handlerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]handle().() |
| 104 | .register_dispatcherrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`LoopHandle<'l, Data>`]register_dispatcher().(dispatcher.clonerust-analyzer cargo calloop 0.10.5 sources/impl#[`Dispatcher<'a, S, Data>`][Clone]clone().()) |
| 105 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 106 | dispatcher |
| 107 | .as_source_mutrust-analyzer cargo calloop 0.10.5 sources/impl#[`Dispatcher<'a, S, Data>`]as_source_mut().() |
| 108 | .remove_signalsrust-analyzer cargo calloop 0.10.5 sources/signals/impl#[Signals]remove_signals().(&[Signalrust-analyzer cargo nix 0.30.1 sys/signal/Signal#::SIGUSR2rust-analyzer cargo nix 0.30.1 sys/signal/Signal#SIGUSR2#]) |
| 109 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 110 | |
| 111 | // block sigusr2 anyway, to not be killed by it |
| 112 | let mut set = SigSetrust-analyzer cargo nix 0.30.1 sys/signal/SigSet#::emptyrust-analyzer cargo nix 0.30.1 sys/signal/impl#[SigSet]empty().(); |
| 113 | set.addrust-analyzer cargo nix 0.30.1 sys/signal/impl#[SigSet]add().(Signalrust-analyzer cargo nix 0.30.1 sys/signal/Signal#::SIGUSR2rust-analyzer cargo nix 0.30.1 sys/signal/Signal#SIGUSR2#); |
| 114 | set.thread_blockrust-analyzer cargo nix 0.30.1 sys/signal/impl#[SigSet]thread_block().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 115 | |
| 116 | // send ourselves a SIGUSR2 |
| 117 | killrust-analyzer cargo nix 0.30.1 sys/signal/kill().(Pidrust-analyzer cargo nix 0.30.1 unistd/Pid#::thisrust-analyzer cargo nix 0.30.1 unistd/impl#[Pid]this().(), Signalrust-analyzer cargo nix 0.30.1 sys/signal/Signal#::SIGUSR2rust-analyzer cargo nix 0.30.1 sys/signal/Signal#SIGUSR2#).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 118 | |
| 119 | event_loop |
| 120 | .dispatchrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]dispatch().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(10)), &mut signal_received) |
| 121 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 122 | |
| 123 | // we should not have received anything, as we don't listen to SIGUSR2 any more |
| 124 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(signal_received.is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().()); |
| 125 | |
| 126 | // swap the signals from [SIGUSR1] to [SIGUSR2] |
| 127 | dispatcher |
| 128 | .as_source_mutrust-analyzer cargo calloop 0.10.5 sources/impl#[`Dispatcher<'a, S, Data>`]as_source_mut().() |
| 129 | .set_signalsrust-analyzer cargo calloop 0.10.5 sources/signals/impl#[Signals]set_signals().(&[Signalrust-analyzer cargo nix 0.30.1 sys/signal/Signal#::SIGUSR2rust-analyzer cargo nix 0.30.1 sys/signal/Signal#SIGUSR2#]) |
| 130 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 131 | |
| 132 | event_loop |
| 133 | .dispatchrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]dispatch().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(10)), &mut signal_received) |
| 134 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 135 | |
| 136 | // we should get back the pending SIGUSR2 now |
| 137 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(signal_received, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Signalrust-analyzer cargo nix 0.30.1 sys/signal/Signal#::SIGUSR2rust-analyzer cargo nix 0.30.1 sys/signal/Signal#SIGUSR2#)); |
| 138 | } |
| 139 | } |