public-inbox.git  about / heads / tags
an "archives first" approach to mailing lists
blob 9a7b947db06fcd66cccf86de9aa4c05fa9522390 3030 bytes (raw)
$ git show HEAD:t/sigfd.t	# shows this blob on the CLI

 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
 
#!perl -w
# Copyright (C) all contributors <meta@public-inbox.org>
use v5.12;
use Test::More;
use IO::Handle;
use POSIX qw(:signal_h);
use Errno qw(ENOSYS);
require_ok 'PublicInbox::Sigfd';
use PublicInbox::DS;
my ($linux_sigfd, $has_sigfd);

SKIP: {
	if ($^O ne 'linux' && !eval { require IO::KQueue }) {
		skip 'signalfd requires Linux or IO::KQueue to emulate', 10;
	}

	my $old = PublicInbox::DS::block_signals();
	my $hit = {};
	my $sig = {};
	local $SIG{USR2} = sub { $hit->{USR2}->{normal}++ };
	local $SIG{HUP} = sub { $hit->{HUP}->{normal}++ };
	local $SIG{TERM} = sub { $hit->{TERM}->{normal}++ };
	local $SIG{INT} = sub { $hit->{INT}->{normal}++ };
	local $SIG{WINCH} = sub { $hit->{WINCH}->{normal}++ };
	for my $s (qw(USR2 HUP TERM INT WINCH)) {
		$sig->{$s} = sub { $hit->{$s}->{sigfd}++ };
	}
	kill 'USR2', $$ or die "kill $!";
	ok(!defined($hit->{USR2}), 'no USR2 yet') or diag explain($hit);
	PublicInbox::DS->Reset;
	ok($PublicInbox::Syscall::SIGNUM{WINCH}, 'SIGWINCH number defined');
	my $sigfd = PublicInbox::Sigfd->new($sig);
	if ($sigfd) {
		$linux_sigfd = 1 if $^O eq 'linux';
		$has_sigfd = 1;
		ok($sigfd, 'Sigfd->new works');
		kill('HUP', $$) or die "kill $!";
		kill('INT', $$) or die "kill $!";
		kill('WINCH', $$) or die "kill $!";
		my $fd = fileno($sigfd->{sock});
		ok($fd >= 0, 'fileno(Sigfd->{sock}) works');
		my $rvec = '';
		vec($rvec, $fd, 1) = 1;
		is(select($rvec, undef, undef, undef), 1, 'select() works');
		ok($sigfd->wait_once, 'wait_once reported success');
		for my $s (qw(HUP INT)) {
			is($hit->{$s}->{sigfd}, 1, "sigfd fired $s");
			is($hit->{$s}->{normal}, undef,
				"normal \$SIG{$s} not fired");
		}
		SKIP: {
			skip 'Linux sigfd-only behavior', 1 if !$linux_sigfd;
			is($hit->{USR2}->{sigfd}, 1,
				'USR2 sent before signalfd created received');
		}
		ok(!$hit->{USR2}->{normal}, 'USR2 not fired normally');
		PublicInbox::DS->Reset;
		$sigfd = undef;

		my $nbsig = PublicInbox::Sigfd->new($sig);
		ok($nbsig, 'Sigfd->new SFD_NONBLOCK works');
		is($nbsig->wait_once, undef, 'nonblocking ->wait_once');
		ok($! == Errno::EAGAIN, 'got EAGAIN');
		kill('HUP', $$) or die "kill $!";
		local @PublicInbox::DS::post_loop_do = (sub {}); # loop once
		PublicInbox::DS::event_loop();
		is($hit->{HUP}->{sigfd}, 2, 'HUP sigfd fired in event loop') or
			diag explain($hit); # sometimes fails on FreeBSD 11.x
		kill('TERM', $$) or die "kill $!";
		kill('HUP', $$) or die "kill $!";
		PublicInbox::DS::event_loop();
		PublicInbox::DS->Reset;
		is($hit->{TERM}->{sigfd}, 1, 'TERM sigfd fired in event loop');
		is($hit->{HUP}->{sigfd}, 3, 'HUP sigfd fired in event loop');
		ok($hit->{WINCH}->{sigfd}, 'WINCH sigfd fired in event loop');
	} else {
		skip('signalfd disabled?', 10);
	}
	ok(!$hit->{USR2}->{normal}, 'USR2 still not fired normally');
	PublicInbox::DS::sig_setmask($old);
	SKIP: {
		($has_sigfd && !$linux_sigfd) or
			skip 'EVFILT_SIGNAL-only behavior check', 1;
		is($hit->{USR2}->{normal}, 1,
			"USR2 fired normally after unblocking on $^O");
	}
}

done_testing;

git clone https://public-inbox.org/public-inbox.git
git clone http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/public-inbox.git