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>
8 use PublicInbox::Syscall qw(EPOLLIN EPOLLOUT EPOLLONESHOT);
9 use autodie qw(close pipe syswrite);
10 my $cls = $ENV{TEST_IOPOLLER} // 'PublicInbox::DSPoll';
17 is($p->ep_add($r, EPOLLIN), 0, 'add EPOLLIN');
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;
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');
50 $cls =~ m!::(?:DSPoll|Select)\z! or
51 skip 'EBADF test for select|poll only', 1;
52 my $old_fd = fileno($r);
56 local $SIG{__WARN__} = sub { push @w, @_ };
57 $p->ep_wait(0, $events);
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');