]> Sergey Matveev's repositories - public-inbox.git/blob - t/ds-leak.t
Merge remote-tracking branch 'origin/git-cleanup'
[public-inbox.git] / t / ds-leak.t
1 # Copyright (C) 2019 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 subtest('close-on-exec for epoll and kqueue' => sub {
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         PublicInbox::DS->AddTimer(0, sub { $pid = spawn([qw(sleep 10)]) });
19         PublicInbox::DS->EventLoop;
20         ok($pid, 'subprocess spawned');
21         my @of = grep(/$evfd_re/, `lsof -p $pid 2>/dev/null`);
22         my $err = $?;
23         SKIP: {
24                 skip "lsof missing? (\$?=$err)", 1 if $err;
25                 is_deeply(\@of, [], 'no FDs leaked to subprocess');
26         };
27         if (defined $pid) {
28                 kill(9, $pid);
29                 waitpid($pid, 0);
30         }
31         PublicInbox::DS->Reset;
32 });
33
34 SKIP: {
35         # not bothering with BSD::Resource
36         chomp(my $n = `/bin/sh -c 'ulimit -n'`);
37
38         # FreeBSD 11.2 with 2GB RAM gives RLIMIT_NOFILE=57987!
39         if ($n > 1024 && !$ENV{TEST_EXPENSIVE}) {
40                 skip "RLIMIT_NOFILE=$n too big w/o TEST_EXPENSIVE for $0", 1;
41         }
42         my $cb = sub {};
43         for my $i (0..$n) {
44                 PublicInbox::DS->SetLoopTimeout(0);
45                 PublicInbox::DS->SetPostLoopCallback($cb);
46                 PublicInbox::DS->EventLoop;
47                 PublicInbox::DS->Reset;
48         }
49         ok(1, "Reset works and doesn't hit RLIMIT_NOFILE ($n)");
50 };
51
52 done_testing;