]> Sergey Matveev's repositories - public-inbox.git/blob - t/ds-poll.t
treewide: run update-copyrights from gnulib for 2019
[public-inbox.git] / t / ds-poll.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::Syscall qw(:epoll);
10 my $cls = $ENV{TEST_IOPOLLER} // 'PublicInbox::DSPoll';
11 use_ok $cls;
12 my $p = $cls->new;
13
14 my ($r, $w, $x, $y);
15 pipe($r, $w) or die;
16 pipe($x, $y) or die;
17 is($p->epoll_ctl(EPOLL_CTL_ADD, fileno($r), EPOLLIN), 0, 'add EPOLLIN');
18 my $events = [];
19 my $n = $p->epoll_wait(9, 0, $events);
20 is_deeply($events, [], 'no events set');
21 is($n, 0, 'nothing ready, yet');
22 is($p->epoll_ctl(EPOLL_CTL_ADD, fileno($w), EPOLLOUT|EPOLLONESHOT), 0,
23         'add EPOLLOUT|EPOLLONESHOT');
24 $n = $p->epoll_wait(9, -1, $events);
25 is($n, 1, 'got POLLOUT event');
26 is($events->[0]->[0], fileno($w), '$w ready');
27
28 $n = $p->epoll_wait(9, 0, $events);
29 is($n, 0, 'nothing ready after oneshot');
30 is_deeply($events, [], 'no events set after oneshot');
31
32 syswrite($w, '1') == 1 or die;
33 for my $t (0..1) {
34         $n = $p->epoll_wait(9, $t, $events);
35         is($events->[0]->[0], fileno($r), "level-trigger POLLIN ready #$t");
36         is($n, 1, "only event ready #$t");
37 }
38 syswrite($y, '1') == 1 or die;
39 is($p->epoll_ctl(EPOLL_CTL_ADD, fileno($x), EPOLLIN|EPOLLONESHOT), 0,
40         'EPOLLIN|EPOLLONESHOT add');
41 is($p->epoll_wait(9, -1, $events), 2, 'epoll_wait has 2 ready');
42 my @fds = sort(map { $_->[0] } @$events);
43 my @exp = sort((fileno($r), fileno($x)));
44 is_deeply(\@fds, \@exp, 'got both ready FDs');
45
46 is($p->epoll_ctl(EPOLL_CTL_DEL, fileno($r), 0), 0, 'EPOLL_CTL_DEL OK');
47 $n = $p->epoll_wait(9, 0, $events);
48 is($n, 0, 'nothing ready after EPOLL_CTL_DEL');
49
50 done_testing;