]> Sergey Matveev's repositories - public-inbox.git/blob - t/ds-leak.t
testcommon: spawn-aware system() and qx[] workalikes
[public-inbox.git] / t / ds-leak.t
1 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
2 # Licensed the same as Danga::Socket (and Perl5)
3 # License: GPL-1.0+ or Artistic-1.0-Perl
4 #  <https://www.gnu.org/licenses/gpl-1.0.txt>
5 #  <https://dev.perl.org/licenses/artistic.html>
6 use strict;
7 use warnings;
8 use Test::More;
9 use PublicInbox::TestCommon;
10 use_ok 'PublicInbox::DS';
11
12 if ('close-on-exec for epoll and kqueue') {
13         use PublicInbox::Spawn qw(spawn which);
14         my $pid;
15         my $evfd_re = qr/(?:kqueue|eventpoll)/i;
16
17         PublicInbox::DS->SetLoopTimeout(0);
18         PublicInbox::DS->SetPostLoopCallback(sub { 0 });
19
20         # make sure execve closes if we're using fork()
21         my ($r, $w);
22         pipe($r, $w) or die "pipe: $!";
23
24         PublicInbox::DS::add_timer(0, sub { $pid = spawn([qw(sleep 10)]) });
25         PublicInbox::DS->EventLoop;
26         ok($pid, 'subprocess spawned');
27
28         # wait for execve, we need to ensure lsof sees sleep(1)
29         # and not the fork of this process:
30         close $w or die "close: $!";
31         my $l = <$r>;
32         is($l, undef, 'cloexec works and sleep(1) is running');
33
34         SKIP: {
35                 my $lsof = which('lsof') or skip 'lsof missing', 1;
36                 my $rdr = { 2 => \(my $null) };
37                 my @of = grep(/$evfd_re/, xqx([$lsof, '-p', $pid], {}, $rdr));
38                 my $err = $?;
39                 skip "lsof broken ? (\$?=$err)", 1 if $err;
40                 is_deeply(\@of, [], 'no FDs leaked to subprocess');
41         };
42         if (defined $pid) {
43                 kill(9, $pid);
44                 waitpid($pid, 0);
45         }
46         PublicInbox::DS->Reset;
47 }
48
49 SKIP: {
50         require_mods('BSD::Resource', 1);
51         my $rlim = BSD::Resource::RLIMIT_NOFILE();
52         my ($n,undef) = BSD::Resource::getrlimit($rlim);
53
54         # FreeBSD 11.2 with 2GB RAM gives RLIMIT_NOFILE=57987!
55         if ($n > 1024 && !$ENV{TEST_EXPENSIVE}) {
56                 skip "RLIMIT_NOFILE=$n too big w/o TEST_EXPENSIVE for $0", 1;
57         }
58         my $cb = sub {};
59         for my $i (0..$n) {
60                 PublicInbox::DS->SetLoopTimeout(0);
61                 PublicInbox::DS->SetPostLoopCallback($cb);
62                 PublicInbox::DS->EventLoop;
63                 PublicInbox::DS->Reset;
64         }
65         ok(1, "Reset works and doesn't hit RLIMIT_NOFILE ($n)");
66 };
67
68 done_testing;