]> Sergey Matveev's repositories - public-inbox.git/blob - t/ds-leak.t
treewide: run update-copyrights from gnulib for 2019
[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_ok 'PublicInbox::DS';
10
11 if ('close-on-exec for epoll and kqueue') {
12         use PublicInbox::Spawn qw(spawn);
13         my $pid;
14         my $evfd_re = qr/(?:kqueue|eventpoll)/i;
15
16         PublicInbox::DS->SetLoopTimeout(0);
17         PublicInbox::DS->SetPostLoopCallback(sub { 0 });
18
19         # make sure execve closes if we're using fork()
20         my ($r, $w);
21         pipe($r, $w) or die "pipe: $!";
22
23         PublicInbox::DS::add_timer(0, sub { $pid = spawn([qw(sleep 10)]) });
24         PublicInbox::DS->EventLoop;
25         ok($pid, 'subprocess spawned');
26
27         # wait for execve, we need to ensure lsof sees sleep(1)
28         # and not the fork of this process:
29         close $w or die "close: $!";
30         my $l = <$r>;
31         is($l, undef, 'cloexec works and sleep(1) is running');
32
33         my @of = grep(/$evfd_re/, `lsof -p $pid 2>/dev/null`);
34         my $err = $?;
35         SKIP: {
36                 skip "lsof missing? (\$?=$err)", 1 if $err;
37                 is_deeply(\@of, [], 'no FDs leaked to subprocess');
38         };
39         if (defined $pid) {
40                 kill(9, $pid);
41                 waitpid($pid, 0);
42         }
43         PublicInbox::DS->Reset;
44 }
45
46 SKIP: {
47         # not bothering with BSD::Resource
48         chomp(my $n = `/bin/sh -c 'ulimit -n'`);
49
50         # FreeBSD 11.2 with 2GB RAM gives RLIMIT_NOFILE=57987!
51         if ($n > 1024 && !$ENV{TEST_EXPENSIVE}) {
52                 skip "RLIMIT_NOFILE=$n too big w/o TEST_EXPENSIVE for $0", 1;
53         }
54         my $cb = sub {};
55         for my $i (0..$n) {
56                 PublicInbox::DS->SetLoopTimeout(0);
57                 PublicInbox::DS->SetPostLoopCallback($cb);
58                 PublicInbox::DS->EventLoop;
59                 PublicInbox::DS->Reset;
60         }
61         ok(1, "Reset works and doesn't hit RLIMIT_NOFILE ($n)");
62 };
63
64 done_testing;