lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / ds-poll.t
blob22dbc8027edd4fda39b7f2064f08fb187b063642
1 # Copyright (C) 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 v5.12;
7 use Test::More;
8 use PublicInbox::Syscall qw(EPOLLIN EPOLLOUT EPOLLONESHOT);
9 use autodie qw(close pipe syswrite);
10 my $cls = $ENV{TEST_IOPOLLER} // 'PublicInbox::DSPoll';
11 use_ok $cls;
12 my $p = $cls->new;
14 my ($r, $w, $x, $y);
15 pipe($r, $w);
16 pipe($x, $y);
17 is($p->ep_add($r, EPOLLIN), 0, 'add EPOLLIN');
18 my $events = [];
19 $p->ep_wait(0, $events);
20 is_deeply($events, [], 'no events set');
21 is($p->ep_add($w, EPOLLOUT|EPOLLONESHOT), 0, 'add EPOLLOUT|EPOLLONESHOT');
22 $p->ep_wait(-1, $events);
23 is(scalar(@$events), 1, 'got POLLOUT event');
24 is($events->[0], fileno($w), '$w ready');
26 $p->ep_wait(0, $events);
27 is(scalar(@$events), 0, 'nothing ready after oneshot');
28 is_deeply($events, [], 'no events set after oneshot');
30 syswrite($w, '1') == 1 or die;
31 for my $t (0..1) {
32         $p->ep_wait($t, $events);
33         is($events->[0], fileno($r), "level-trigger POLLIN ready #$t");
34         is(scalar(@$events), 1, "only event ready #$t");
36 syswrite($y, '1') == 1 or die;
37 is($p->ep_add($x, EPOLLIN|EPOLLONESHOT), 0, 'EPOLLIN|EPOLLONESHOT add');
38 $p->ep_wait(-1, $events);
39 is(scalar @$events, 2, 'epoll_wait has 2 ready');
40 my @fds = sort @$events;
41 my @exp = sort((fileno($r), fileno($x)));
42 is_deeply(\@fds, \@exp, 'got both ready FDs');
44 is($p->ep_del($r, 0), 0, 'EPOLL_CTL_DEL OK');
45 $p->ep_wait(0, $events);
46 is(scalar @$events, 0, 'nothing ready after EPOLL_CTL_DEL');
48 is($p->ep_add($r, EPOLLIN), 0, 're-add');
49 SKIP: {
50         $cls =~ m!::(?:DSPoll|Select)\z! or
51                 skip 'EBADF test for select|poll only', 1;
52         my $old_fd = fileno($r);
53         close $r;
54         my @w;
55         eval {
56                 local $SIG{__WARN__} = sub { push @w, @_ };
57                 $p->ep_wait(0, $events);
58         };
59         ok($@, 'error detected from bad FD');
60         ok($!{EBADF}, 'EBADF errno set');
61         @w and ok(grep(/\bFD=$old_fd invalid/, @w), 'carps invalid FD');
64 done_testing;