Feersum

 view release on metacpan or  search on metacpan

t/core-28-eof-park-bound.t  view on Meta::CPAN

#!perl
# Input EOF on a parked streaming reply fires on_eof and reaps it after
# eof_park_timeout (0 disables the reap, not the signal); no EOF, no reap.
use strict;
use warnings;
use Test::More;
use lib 't'; use Utils;
use Feersum;
use IO::Socket::INET;
use Socket qw(SHUT_WR);
use Time::HiRes qw(time);

use constant TMULT => $ENV{PERL_TEST_TIME_OUT_FACTOR} || ($ENV{AUTOMATED_TESTING} ? 3 : 1);
my $probe = Feersum->new_instance;
my $tls_ok = $probe->has_tls && eval { require IO::Socket::SSL; 1 } && tls_client_ok();

subtest 'setter validation' => sub {
    my $f = Feersum->new_instance;
    is $f->eof_park_timeout, 60, 'default is 60';
    $f->eof_park_timeout(2.5);
    is $f->eof_park_timeout, 2.5, 'setter round-trips';
    $f->eof_park_timeout(0);
    is $f->eof_park_timeout, 0, '0 disables';
    eval { $f->eof_park_timeout(-1) };
    like $@, qr/non-negative/, 'negative croaks';
    my $inf = 9**9**9;
    eval { $f->eof_park_timeout($inf - $inf) };
    like $@, qr/non-negative/, 'NaN croaks';
    done_testing;
};

subtest 'on_eof install dance' => sub {
    my ($listen, $port) = get_listen_socket();
    my $f = Feersum->new_instance;
    $f->use_socket($listen);
    my @got;
    $f->psgi_request_handler(sub {
        return sub {
            my $w = shift->([200, ['Content-Type' => 'text/plain']]);
            push @got, 'get-undef' unless defined $w->on_eof;
            $w->on_eof(sub { push @got, 'one' });
            ($w->on_eof)->();
            $w->on_eof(sub { push @got, 'two' });
            ($w->on_eof)->();
            $w->on_eof(undef);
            push @got, 'get-undef2' unless defined $w->on_eof;
            eval { $w->on_eof("nope") };
            push @got, 'croak' if $@;
            $w->write('x');
            $w->close;
        };
    });
    run_client 'install dance', sub {
        my $sock = IO::Socket::INET->new(PeerAddr => "127.0.0.1:$port", Timeout => 3 * TMULT)
            or return 10;
        print $sock "GET / HTTP/1.0\r\n\r\n";
        my $raw = '';
        while (my $n = $sock->sysread(my $part, 65536)) { $raw .= $part }
        close $sock;
        return 20 unless $raw =~ m{\AHTTP/1\.0 200} && $raw =~ /x/;
        return 0;
    };
    is_deeply \@got, ['get-undef', 'one', 'two', 'get-undef2', 'croak'],
        'getter/install/replace/unset/croak';
    done_testing;
};

sub wait_for {
    my ($f, $cond, $secs) = @_;
    my $cv = AE::cv;
    my $deadline = time + $secs;
    my $wait = AE::timer(0, 0.01, sub { $cv->send if $cond->() || time >= $deadline });
    $cv->recv;
}

for my $api ('native', 'psgi') {
    subtest "fin reaps $api" => sub {
        my ($listen, $port) = get_listen_socket();
        my $f = Feersum->new_instance;
        $f->use_socket($listen);
        $f->eof_park_timeout(2);
        $f->linger_timeout(0);
        my ($eofs, $guards, %kept, $id) = (0, 0);
        my $start = sub {
            my $w = shift;
            my $i = ++$id;
            $kept{$i} = $w;
            $w->response_guard(guard { $guards++; delete $kept{$i} });



( run in 1.763 second using v1.01-cache-2.11-cpan-60884a8df76 )