POEx-HTTP-Server

 view release on metacpan or  search on metacpan

t/32_stream.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;
use POE;

#######################################
sub _start
{
    my( $heap, $kernel ) = @_[HEAP, KERNEL];
    $kernel->alias_set( $heap->{alias} );
    $kernel->sig_child( $heap->{pid}, 'pid' );
    ::DEBUG and warn "$heap->{alias}: _start";
    # $kernel->sig( shutdown => 'shutdown' );
}

#######################################
sub _stop 
{
    my( $heap, $kernel ) = @_[HEAP, KERNEL];
    ::DEBUG and warn "$heap->{alias}: _stop";
}

#######################################
sub shutdown
{
    my( $heap, $kernel ) = @_[HEAP, KERNEL];
    $kernel->alias_remove( $heap->{alias} );
    ::DEBUG and warn "$heap->{alias}: shutdown";
}

#######################################
sub pid
{
    my( $heap, $kernel, $pid ) = @_[HEAP, KERNEL, ARG0];
    ::DEBUG and warn "$heap->{alias}: pid=$pid";
}

#######################################
sub req
{
    my( $heap, $req, $resp ) = @_[ HEAP, ARG0, ARG1 ];
    if( $req->uri->path =~ /shutdown/ ) {
        $poe_kernel->signal( $poe_kernel, 'shutdown' );
        $resp->content( 'OK' );
        $resp->content_type( 'text/plain' );
        $resp->respond;
        $resp->done;
        return;
    }

    isa_ok( $req, 'POEx::HTTP::Server::Request' );
    isa_ok( $req->connection, 'POEx::HTTP::Server::Connection' );
    isa_ok( $resp, 'POEx::HTTP::Server::Response' );

    my $uri = $req->uri;
    isa_ok( $uri, 'URI' );
    my $N = $uri->query_param( 'N' );
    die "N must be set in ", pp $uri unless $N;
    ok( $N > 0, "$N must be greater then 0" );

    $resp->streaming( 1 );

    $heap->{N} = 0;
    $heap->{max} = $N;
    $resp->send;
}

#######################################
sub stream
{
    my( $heap, $req, $resp ) = @_[ HEAP, ARG0, ARG1 ];
    
    isa_ok( $req, 'POEx::HTTP::Server::Request' );
    isa_ok( $req->connection, 'POEx::HTTP::Server::Connection' );
    isa_ok( $resp, 'POEx::HTTP::Server::Response' );
    ::DEBUG and warn "N=$heap->{N} max=$heap->{max}";
    $heap->{N}++;
    $resp->send( "$heap->{N}/$heap->{max}\n" );
    if( $heap->{N} >= $heap->{max} ) {
        ::DEBUG and warn "DONE";
        $resp->done;
    }
}



( run in 0.916 second using v1.01-cache-2.11-cpan-39bf76dae61 )