]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
t/sigfd: reduce getpid() calls and hash lookups
authorEric Wong <e@80x24.org>
Tue, 20 Aug 2024 18:40:59 +0000 (18:40 +0000)
committerEric Wong <e@80x24.org>
Wed, 21 Aug 2024 18:16:27 +0000 (18:16 +0000)
getpid() is no longer cached by glibc, syscalls are more
expensive nowadays, so only call it once per test.  The
additional hash table depth is no longer necessary since there's
no longer a difference between signal dispatch methods now that
Sigfd uses the global %SIG.

t/sigfd.t

index eab85ed748109e665b2b52adb2b34e89f529c47e..91d1103485fe3c98132283ce2050ea6bb4996a87 100644 (file)
--- a/t/sigfd.t
+++ b/t/sigfd.t
@@ -18,15 +18,16 @@ SKIP: {
        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}++ };
+       local $SIG{USR2} = sub { $hit->{USR2}++ };
+       local $SIG{HUP} = sub { $hit->{HUP}++ };
+       local $SIG{TERM} = sub { $hit->{TERM}++ };
+       local $SIG{INT} = sub { $hit->{INT}++ };
+       local $SIG{WINCH} = sub { $hit->{WINCH}++ };
        for my $s (qw(USR2 HUP TERM INT WINCH)) {
                $sig->{$s} = sub { die "SHOULD NOT BE CALLED ($s)" }
        }
-       kill 'USR2', $$ or die "kill $!";
+       my $PID = $$;
+       kill 'USR2', $PID;
        ok(!defined($hit->{USR2}), 'no USR2 yet') or diag explain($hit);
        PublicInbox::DS->Reset;
        ok($PublicInbox::Syscall::SIGNUM{WINCH}, 'SIGWINCH number defined');
@@ -35,9 +36,9 @@ SKIP: {
                $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 $!";
+               kill 'HUP', $PID;
+               kill 'INT', $PID;
+               kill 'WINCH', $PID;
                my $fd = fileno($sigfd->{sock});
                ok($fd >= 0, 'fileno(Sigfd->{sock}) works');
                my $rvec = '';
@@ -45,12 +46,12 @@ SKIP: {
                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}->{normal}, 1, "sigfd fired $s");
+                       is $hit->{$s}, 1, "sigfd fired $s";
                }
                SKIP: {
                        skip 'Linux sigfd-only behavior', 1 if !$linux_sigfd;
-                       is($hit->{USR2}->{normal}, 1,
-                               'USR2 sent before signalfd created received');
+                       is $hit->{USR2}, 1,
+                               'USR2 sent before signalfd created received';
                }
                PublicInbox::DS->Reset;
                $sigfd = undef;
@@ -59,38 +60,38 @@ SKIP: {
                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 $!";
+               kill 'HUP', $PID;
                local @PublicInbox::DS::post_loop_do = (sub {}); # loop once
                PublicInbox::DS::event_loop();
-               is($hit->{HUP}->{normal}, 2, 'HUP sigfd fired in event loop') or
+               is $hit->{HUP}, 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 $!";
+               kill 'TERM', $PID;
+               kill 'HUP', $PID;
                PublicInbox::DS::event_loop();
                PublicInbox::DS->Reset;
-               is($hit->{TERM}->{normal}, 1, 'TERM sigfd fired in event loop');
-               is($hit->{HUP}->{normal}, 3, 'HUP sigfd fired in event loop');
-               ok($hit->{WINCH}->{normal}, 'WINCH sigfd fired in event loop');
+               is $hit->{TERM}, 1, 'TERM sigfd fired in event loop';
+               is $hit->{HUP}, 3, 'HUP sigfd fired in event loop';
+               ok $hit->{WINCH}, 'WINCH sigfd fired in event loop';
 
                my $restore = PublicInbox::DS::allow_sigs 'HUP';
-               kill 'HUP', $$;
+               kill 'HUP', $PID;
                select undef, undef, undef, 0;
-               is $hit->{HUP}->{normal}, 4, 'HUP sigfd fired after allow_sigs';
+               is $hit->{HUP}, 4, 'HUP sigfd fired after allow_sigs';
 
                undef $restore;
-               kill 'HUP', $$;
+               kill 'HUP', $PID;
                vec($rvec = '', fileno($nbsig->{sock}), 1) = 1;
                ok select($rvec, undef, undef, 1),
                        'select reports sigfd readiness';
-               is $hit->{HUP}->{normal}, 4, 'HUP not fired when sigs blocked';
+               is $hit->{HUP}, 4, 'HUP not fired when sigs blocked';
                $nbsig->event_step;
-               is $hit->{HUP}->{normal}, 5, 'HUP fires only on ->event_step';
+               is $hit->{HUP}, 5, 'HUP fires only on ->event_step';
 
-               kill 'HUP', $$;
-               is $hit->{HUP}->{normal}, 5, 'HUP not fired, yet';
+               kill 'HUP', $PID;
+               is $hit->{HUP}, 5, 'HUP not fired, yet';
                $restore = PublicInbox::DS::allow_sigs 'HUP';
                select(undef, undef, undef, 0);
-               is $hit->{HUP}->{normal}, 6, 'HUP fires from allow_sigs';
+               is $hit->{HUP}, 6, 'HUP fires from allow_sigs';
        } else {
                skip('signalfd disabled?', 10);
        }