The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl -w

# Cribbed this test from the IO::Poll tests in the main Perl distribution.

BEGIN {
    unless(grep /blib/, @INC) {
        chdir 't' if -d 't';
        unshift(@INC, '../lib');
    }
}

if ($^O eq 'mpeix') {
    print "1..0 # Skip: broken on MPE/iX\n";
    exit 0;
}

select(STDERR); $| = 1;
select(STDOUT); $| = 1;

print "1..11\n";

use IO::Handle;
use IO::Epoll qw(:compat);
use Time::HiRes qw( time );

my $poll = new IO::Epoll;

my $stdout = \*STDOUT;
my $dupout = IO::Handle->new_from_fd(fileno($stdout),"w");

$poll->mask($stdout => POLLOUT);

print "not "
	unless $poll->mask($stdout) == POLLOUT;
print "ok 1\n";

$poll->mask($dupout => POLLPRI);

print "not "
	unless $poll->mask($dupout) == POLLPRI;
print "ok 2\n";

$poll->poll(0.1);

if ($^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'VMS' || $^O eq 'beos') {
print "ok 3 # skipped, doesn't work on non-socket fds\n";
print "ok 4 # skipped, doesn't work on non-socket fds\n";
}
else {
print "not "
	unless $poll->events($stdout) == POLLOUT;
print "ok 3\n";

print "not "
	if $poll->events($dupout);
print "ok 4\n";
}

my @h = $poll->handles;
print "not "
	unless @h == 2;
print "ok 5\n";

$poll->remove($stdout);

@h = $poll->handles;

print "not "
	unless @h == 1;
print "ok 6\n";

print "not "
	if $poll->mask($stdout);
print "ok 7\n";

$poll->poll(0.1);

print "not "
	if $poll->events($stdout);
print "ok 8\n";

$poll->remove($dupout);
print "not "
    if $poll->handles;
print "ok 9\n";

my $stdin = \*STDIN;
$poll->mask($stdin => POLLIN);
$poll->remove($stdin);
close STDIN;
print "not "
    if $poll->poll(0.1);
print "ok 10\n";

my $starttime = time();

$poll->poll( 1.000 );

my $duration = time() - $starttime;
if( $duration < 0.9 or $duration > 1.5 ) {
   print "not ok 11 # poll(1.000) took ";
   print "more than 1.5 seconds ($duration)\n" if $duration > 1.5;
   print "less than 0.9 seconds ($duration)\n" if $duration < 0.9;
}
else {
   print "ok 11\n";
}