class TestCase::IO::Poll {
use IO::Poll;
use Sys::Poll::Constant as POLL;
use Sort;
use Array;
use Sys::IO::Constant as IO;
use IO::Socket::IP;
use Sys::Socket::Constant as SOCKET;
static method new : int () {
my $poll = IO::Poll->new;
unless ($poll is_type IO::Poll) {
}
return 1;
}
static method set_mask : int () {
{
my $poll = IO::Poll->new;
$poll->set_mask(0 => POLL->POLLIN);
my $mask = $poll->mask(0);
unless ($mask == POLL->POLLIN) {
return 0;
}
}
{
my $poll = IO::Poll->new;
$poll->set_mask(0 => POLL->POLLIN);
$poll->set_mask(1 => POLL->POLLOUT);
my $mask0 = $poll->mask(0);
unless ($mask0 == POLL->POLLIN) {
return 0;
}
my $mask1 = $poll->mask(1);
unless ($mask1 == POLL->POLLOUT) {
return 0;
}
}
{
my $poll = IO::Poll->new;
$poll->set_mask(0 => POLL->POLLIN);
$poll->set_mask(0 => 0);
$poll->set_mask(1 => POLL->POLLOUT);
my $mask0 = $poll->mask(0);
unless ($mask0 == 0) {
return 0;
}
my $mask1 = $poll->mask(1);
unless ($mask1 == POLL->POLLOUT) {
return 0;
}
}
return 1;
}
static method mask : int () {
{
my $poll = IO::Poll->new;
$poll->set_mask(0 => POLL->POLLIN);
my $mask = $poll->mask(0);
unless ($mask == POLL->POLLIN) {
return 0;
}
}
{
my $poll = IO::Poll->new;
my $mask = $poll->mask(0);
unless ($mask == 0) {
return 0;
}
}
{
my $poll = IO::Poll->new;
$poll->set_mask(0 => POLL->POLLIN);
$poll->remove(0);
my $mask = $poll->mask(0);
unless ($mask == 0) {
warn $mask;
return 0;
}
}
return 1;
}
static method fds : int () {
{
my $poll = IO::Poll->new;
$poll->set_mask(1 => POLL->POLLIN);
my $fds = $poll->fds;
Sort->sort_int_asc($fds);
unless (Array->equals_int($fds, [1])) {
return 0;
}
}
{
my $poll = IO::Poll->new;
$poll->set_mask(5 => POLL->POLLIN);
$poll->set_mask(3 => POLL->POLLOUT);
my $fds = $poll->fds;
Sort->sort_int_asc($fds);
unless (Array->equals_int($fds, [3, 5])) {
return 0;
}
}
{
my $poll = IO::Poll->new;
$poll->set_mask(5 => POLL->POLLIN);
$poll->set_mask(5 => 0);
my $fds = $poll->fds;
Sort->sort_int_asc($fds);
unless (Array->equals_int($fds, new int[0])) {
warn dump $fds;
return 0;
}
}
{
my $poll = IO::Poll->new;
$poll->set_mask(5 => POLL->POLLIN);
$poll->set_mask(5 => 0);
$poll->set_mask(5 => POLL->POLLIN);
my $fds = $poll->fds;
Sort->sort_int_asc($fds);
unless (Array->equals_int($fds, [5])) {
warn dump $fds;
return 0;
}
}
{
my $poll = IO::Poll->new;
$poll->set_mask(5 => POLL->POLLIN);
$poll->set_mask(5 => 0);
$poll->set_mask(1 => POLL->POLLOUT);
$poll->set_mask(19 => POLL->POLLOUT);
$poll->set_mask(10 => POLL->POLLOUT);
$poll->set_mask(1 => 0);
$poll->set_mask(6 => POLL->POLLOUT);
my $fds = $poll->fds;
Sort->sort_int_asc($fds);
unless (Array->equals_int($fds, [6, 10, 19])) {
warn dump $fds;
return 0;
}
}
return 1;
}
static method remove : int () {
{
my $poll = IO::Poll->new;
$poll->set_mask(1 => POLL->POLLIN);
$poll->set_mask(0 => POLL->POLLIN);
$poll->remove(0);
my $mask = $poll->mask(0);
unless ($mask == 0) {
warn $mask;
return 0;
}
my $fds = $poll->fds;
Sort->sort_int_asc($fds);
unless (Array->equals_int($fds, [1])) {
warn dump $fds;
return 0;
}
}
return 1;
}
static method poll : int () {
{
my $poll = IO::Poll->new;
$poll->set_mask(IO->STDOUT_FILENO, POLL->POLLOUT);
$poll->poll;
my $events = $poll->events(IO->STDOUT_FILENO);
unless ($events & POLL->POLLOUT) {
return 0;
}
}
{
my $poll = IO::Poll->new;
$poll->set_mask(IO->STDOUT_FILENO, POLL->POLLOUT);
$poll->poll(0);
my $events = $poll->events(IO->STDOUT_FILENO);
unless ($events & POLL->POLLOUT) {
return 0;
}
}
{
my $poll = IO::Poll->new;
$poll->set_mask(IO->STDOUT_FILENO, POLL->POLLOUT);
$poll->poll(1.5);
my $events = $poll->events(IO->STDOUT_FILENO);
unless ($events & POLL->POLLOUT) {
return 0;
}
}
return 1;
}
static method events : int () {
{
my $poll = IO::Poll->new;
$poll->set_mask(IO->STDOUT_FILENO, POLL->POLLOUT);
$poll->poll;
my $events = $poll->events(IO->STDOUT_FILENO);
unless ($events & POLL->POLLOUT) {
return 0;
}
}
return 1;
}
static method poll_with_server : int ($port : int) {
# Basic
{
my $host = "127.0.0.1";
my $sockets = [
IO::Socket::IP->new({PeerAddr => $host, PeerPort => $port}),
IO::Socket::IP->new({PeerAddr => $host, PeerPort => $port}),
IO::Socket::IP->new({PeerAddr => $host, PeerPort => $port}),
];
my $socket_disable = IO::Socket::IP->new({PeerAddr => $host, PeerPort => $port});
my $poll = IO::Poll->new;
$poll->set_mask($sockets->[0]->fileno, POLL->POLLIN|POLL->POLLOUT);
$poll->set_mask($sockets->[1]->fileno, POLL->POLLIN|POLL->POLLOUT);
$poll->set_mask($sockets->[2]->fileno, POLL->POLLIN|POLL->POLLOUT);
$poll->remove($sockets->[2]->fileno);
$poll->set_mask($sockets->[2]->fileno, POLL->POLLIN|POLL->POLLOUT);
$poll->set_mask($socket_disable->fileno, POLL->POLLIN|POLL->POLLOUT);
$poll->remove($socket_disable->fileno);
$poll->poll;
my $events0 = $poll->events($sockets->[0]->fileno);
my $events1 = $poll->events($sockets->[1]->fileno);
my $events2 = $poll->events($sockets->[2]->fileno);
unless ($events0 & POLL->POLLOUT) {
warn $events0;
return 0;
}
unless ($events1 & POLL->POLLOUT) {
warn $events1;
return 0;
}
unless ($events2 & POLL->POLLOUT) {
warn $events2;
return 0;
}
for my $socket (@$sockets) {
my $send_buffer = "GET /hello HTTP/1.0\r\nHost: $host\r\n\r\n";
$socket->send($send_buffer);
$socket->shutdown(SOCKET->SHUT_WR);
while (1) {
$poll->poll;
my $events = $poll->events($socket->fileno);
if ($events & POLL->POLLIN) {
last;
}
}
my $recv_buffer = (mutable string)new_string_len 1000;
my $recv_length = $socket->recv($recv_buffer);
unless ($recv_length > 0 && $recv_length < 1000) {
warn $recv_length;
return 0;
}
Fn->shorten_null_char($recv_buffer);
my $content = $recv_buffer;
unless (Fn->contains($content, "HTTP")) {
warn dump $content;
return 0;
}
unless (Fn->contains($content, "hello")) {
warn dump $content;
return 0;
}
}
}
return 1;
}
}