This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Refactor t/thread_it.pl to use test.pl's skip_all_*() functions.
[perl5.git] / t / op / sigdispatch.t
CommitLineData
8f3964af
NC
1#!perl -w
2
3# We assume that TestInit has been used.
4
5BEGIN {
6 require './test.pl';
7}
8
9use strict;
0c1bf4c7 10use Config;
8f3964af 11
b3dbdd48 12plan tests => 15;
8f3964af
NC
13
14watchdog(10);
15
16$SIG{ALRM} = sub {
17 die "Alarm!\n";
18};
19
20pass('before the first loop');
21
22alarm 2;
23
24eval {
25 1 while 1;
26};
27
28is($@, "Alarm!\n", 'after the first loop');
29
30pass('before the second loop');
31
32alarm 2;
33
34eval {
35 while (1) {
36 }
37};
38
39is($@, "Alarm!\n", 'after the second loop');
0c1bf4c7
LT
40
41SKIP: {
b3dbdd48 42 skip('We can\'t test blocking without sigprocmask', 11) if $ENV{PERL_CORE_MINITEST} || !$Config{d_sigprocmask};
0c1bf4c7
LT
43
44 require POSIX;
45 my $new = POSIX::SigSet->new(&POSIX::SIGUSR1);
46 POSIX::sigprocmask(&POSIX::SIG_BLOCK, $new);
47
48 my $gotit = 0;
49 $SIG{USR1} = sub { $gotit++ };
50 kill SIGUSR1, $$;
7fe50b8b 51 is $gotit, 0, 'Haven\'t received third signal yet';
0c1bf4c7
LT
52
53 my $old = POSIX::SigSet->new();
54 POSIX::sigsuspend($old);
55 is $gotit, 1, 'Received third signal';
56
7fe50b8b
LT
57 {
58 kill SIGUSR1, $$;
59 local $SIG{USR1} = sub { die "FAIL\n" };
60 POSIX::sigprocmask(&POSIX::SIG_BLOCK, undef, $old);
61 ok $old->ismember(&POSIX::SIGUSR1), 'SIGUSR1 is blocked';
62 eval { POSIX::sigsuspend(POSIX::SigSet->new) };
63 is $@, "FAIL\n", 'Exception is thrown, so received fourth signal';
64 POSIX::sigprocmask(&POSIX::SIG_BLOCK, undef, $old);
65 ok $old->ismember(&POSIX::SIGUSR1), 'SIGUSR1 is still blocked';
66 }
67
0c1bf4c7 68 kill SIGUSR1, $$;
7fe50b8b 69 is $gotit, 1, 'Haven\'t received fifth signal yet';
0c1bf4c7
LT
70 POSIX::sigprocmask(&POSIX::SIG_UNBLOCK, $new, $old);
71 ok $old->ismember(&POSIX::SIGUSR1), 'SIGUSR1 was still blocked';
7fe50b8b 72 is $gotit, 2, 'Received fifth signal';
c22d665b
LT
73
74 # test unsafe signal handlers in combination with exceptions
75 my $action = POSIX::SigAction->new(sub { $gotit--, die }, POSIX::SigSet->new, 0);
28fcd422 76 POSIX::sigaction(&POSIX::SIGALRM, $action);
b3dbdd48
LT
77 eval {
78 alarm 1;
79 my $set = POSIX::SigSet->new;
80 POSIX::sigprocmask(&POSIX::SIG_BLOCK, undef, $set);
81 is $set->ismember(&POSIX::SIGALRM), 0, "SIGALRM is not blocked on attempt $_";
82 POSIX::sigsuspend($set);
83 } for 1..2;
c22d665b 84 is $gotit, 0, 'Received both signals';
0c1bf4c7 85}