Changeset 17851

Show
Ignore:
Timestamp:
08/18/08 22:20:57 (5 years ago)
Author:
tokuhirom
Message:

use Test::TCP

Location:
lang/perl/HTTP-Engine/trunk
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/HTTP-Engine/trunk/Makefile.PL

    r17830 r17851  
    8585build_requires 'File::Temp' => '0.20'; 
    8686build_requires 'HTTP::Request::AsCGI'; 
     87build_requires 'Test::TCP'; 
    8788 
    8889use_test_base; 
  • lang/perl/HTTP-Engine/trunk/t/020_interface/poe.t

    r17765 r17851  
    22use warnings; 
    33use Test::More; 
    4 use t::Utils; 
     4use Test::TCP; 
    55use HTTP::Engine; 
    66 
  • lang/perl/HTTP-Engine/trunk/t/020_interface/server_simple.t

    r17748 r17851  
    22use warnings; 
    33use Test::More; 
    4 use t::Utils; 
    54eval "use HTTP::Server::Simple"; 
    65plan skip_all => 'this test requires HTTP::Server::Simple' if $@; 
    76plan tests => 2; 
    87use LWP::UserAgent; 
    9 use HTTP::Request::Common qw(POST $DYNAMIC_FILE_UPLOAD); 
     8use HTTP::Request::Common qw(POST); 
    109use HTTP::Engine; 
    11 use t::Utils; 
     10use Test::TCP; 
    1211 
    13 my $port = empty_port; 
    14  
    15 &main; exit(); 
    16  
    17 sub main { 
    18     daemonize( 
    19         \&_do_request, 
    20         $port, 
    21         interface => { 
    22             module => 'ServerSimple', 
    23             args => { 
    24                 port => $port, 
     12test_tcp( 
     13    client => sub { 
     14        my $port = shift; 
     15        my $ua = LWP::UserAgent->new(timeout => 10); 
     16        my $req = POST("http://localhost:$port/", Content_Type => 'multipart/form-data;', Content => ['test' => ["README"]]); 
     17        my $res = $ua->request($req); 
     18        is $res->code, 200; 
     19        like $res->content, qr{Kazuhiro Osawa}; 
     20    }, 
     21    server => sub { 
     22        my $port = shift; 
     23        HTTP::Engine->new( 
     24            interface => { 
     25                module => 'ServerSimple', 
     26                args => { 
     27                    port => $port, 
     28                }, 
     29                request_handler => sub { 
     30                    my $req = shift; 
     31                    HTTP::Engine::Response->new(body => $req->upload("test")->slurp(), status => 200); 
     32                }, 
    2533            }, 
    26             request_handler => sub { 
    27                 my $req = shift; 
    28                 HTTP::Engine::Response->new(body => $req->upload("test")->slurp(), status => 200); 
    29             }, 
    30         }, 
    31     ); 
    32 } 
    33  
    34 sub _do_request { 
    35     my $port = shift; 
    36     my $ua = LWP::UserAgent->new(timeout => 10); 
    37     my $req = POST("http://localhost:$port/", Content_Type => 'multipart/form-data;', Content => ['test' => ["README"]]); 
    38     my $res = $ua->request($req); 
    39     is $res->code, 200; 
    40     like $res->content, qr{Kazuhiro Osawa}; 
    41 } 
    42  
     34        )->run; 
     35    }, 
     36); 
  • lang/perl/HTTP-Engine/trunk/t/FCGIUtils.pm

    r17747 r17851  
    22use strict; 
    33use warnings; 
    4 use t::Utils; 
    54use File::Temp (); 
    65use FindBin; 
     
    87use IO::Socket; 
    98use File::Spec; 
     9use Test::TCP qw/test_tcp empty_port/; 
    1010 
    1111# this file is copied from Catalyst. thanks! 
     
    1818sub test_lighty ($&) { 
    1919    my ($fcgisrc, $callback, $port) = @_; 
    20     $port ||= empty_port; 
     20    $port ||= empty_port(); 
    2121 
    2222    plan skip_all => 'set TEST_LIGHTTPD to enable this test'  
     
    3434    my $tmpdir = File::Temp::tempdir(); 
    3535 
    36     my $fcgifname = File::Spec->catfile($tmpdir, "test.fcgi"); 
    37     do { 
    38         _write_file($fcgifname => $fcgisrc); 
    39         chmod 0777, $fcgifname; 
    40         warn `perl -wc $fcgifname` if $ENV{DEBUG}; 
    41     }; 
     36    test_tcp( 
     37        client => sub { 
     38            my $port = shift; 
     39            $callback->($port); 
     40            warn `cat $tmpdir/error.log` if $ENV{DEBUG}; 
     41        }, 
     42        server => sub { 
     43            my $port = shift; 
    4244 
    43     my $conf = <<"END"; 
     45            my $fcgifname = File::Spec->catfile($tmpdir, "test.fcgi"); 
     46            do { 
     47                _write_file($fcgifname => $fcgisrc); 
     48                chmod 0777, $fcgifname; 
     49                warn `perl -wc $fcgifname` if $ENV{DEBUG}; 
     50            }; 
     51 
     52            my $conffname = File::Spec->catfile($tmpdir, "lighty.conf"); 
     53            _write_file($conffname => _render_conf($tmpdir, $port, $fcgifname)); 
     54 
     55            my $pid = open my $lighttpd, "$lighttpd_bin -D -f $conffname 2>&1 |"  
     56                or die "Unable to spawn lighttpd: $!"; 
     57            $SIG{TERM} = sub { 
     58                kill 'INT', $pid; 
     59                close $lighttpd; 
     60                exit; 
     61            }; 
     62            sleep 60; # waiting tests. 
     63            die "server timeout"; 
     64        }, 
     65        port => $port, 
     66    ); 
     67} 
     68 
     69sub _write_file { 
     70    my ($fname, $src) = @_; 
     71    open my $fh, '>', $fname or die $!; 
     72    print {$fh} $src or die $!; 
     73    close $fh; 
     74} 
     75 
     76sub _render_conf { 
     77    my ($tmpdir, $port, $fcgifname) = @_; 
     78    <<"END"; 
    4479# basic lighttpd config file for testing fcgi+HTTP::Engine 
    4580server.modules = ( 
     
    74109) 
    75110END 
    76  
    77     my $conffname = File::Spec->catfile($tmpdir, "lighty.conf"); 
    78     _write_file($conffname => $conf); 
    79  
    80     my $pid = open my $lighttpd, "$lighttpd_bin -D -f $conffname 2>&1 |"  
    81         or die "Unable to spawn lighttpd: $!"; 
    82  
    83     wait_port($port); 
    84  
    85     $callback->($port); 
    86  
    87     warn `cat $tmpdir/error.log` if $ENV{DEBUG}; 
    88  
    89     kill 'INT', $pid; 
    90     close $lighttpd; 
    91 } 
    92  
    93 sub _write_file { 
    94     my ($fname, $src) = @_; 
    95     open my $fh, '>', $fname or die $!; 
    96     print {$fh} $src or die $!; 
    97     close $fh; 
    98111} 
    99112 
  • lang/perl/HTTP-Engine/trunk/t/Utils.pm

    r17823 r17851  
    66use HTTP::Request::AsCGI; 
    77use HTTP::Engine::RequestBuilder; 
     8use Test::TCP qw/test_tcp empty_port/; 
    89 
    910use IO::Socket::INET; 
    1011 
    1112use Sub::Exporter -setup => { 
    12     exports => [qw/ empty_port daemonize daemonize_all interfaces run_engine ok_response check_port wait_port req /], 
     13    exports => [qw/ daemonize_all interfaces run_engine ok_response req /], 
    1314    groups  => { default => [':all'] } 
    1415}; 
    15  
    16 sub empty_port { 
    17     my $port = shift || 10000; 
    18     $port = 19000 unless $port =~ /^[0-9]+$/ && $port < 19000; 
    19  
    20     while ($port++ < 20000) { 
    21         my $sock = IO::Socket::INET->new( 
    22             Listen    => 5, 
    23             LocalAddr => 'localhost', 
    24             LocalPort => $port, 
    25             Proto     => 'tcp' 
    26         ); 
    27         return $port if $sock; 
    28     } 
    29     die "empty port not found"; 
    30 } 
    31  
    32 sub daemonize (&@) { goto \&_daemonize } 
    33 sub _daemonize { 
    34     my($client, $port, %args) = @_; 
    35     __daemonize($client, $port, sub { 
    36         my $poe_kernel_run = delete $args{poe_kernel_run}; 
    37         HTTP::Engine->new(%args)->run; 
    38         POE::Kernel->run() if $poe_kernel_run; 
    39     }); 
    40 } 
    41 sub __daemonize { 
    42     my($client, $port, $child) = @_; 
    43  
    44     if (my $pid = fork()) { 
    45         # parent. 
    46  
    47         wait_port($port); 
    48  
    49         $client->($port); 
    50  
    51         kill TERM => $pid; 
    52         waitpid($pid, 0); 
    53     } elsif ($pid == 0) { 
    54         # child 
    55         $child->(); 
    56     } else { 
    57         die "cannot fork"; 
    58     } 
    59 } 
    6016 
    6117my @interfaces; # memoize. 
     
    7430    my($client, $codesrc) = @_; 
    7531 
    76     my $port = empty_port; 
     32    my $port = empty_port(); 
    7733 
    7834    my $code = eval $codesrc; 
     
    10561        } elsif ($interface eq 'CGI') { 
    10662            require HTTP::Server::Simple::CGI; 
    107             __daemonize $client_cb, $port, sub { 
    108                 Moose::Meta::Class 
    109                     ->create_anon_class( 
    110                         superclasses => ['HTTP::Server::Simple::CGI'], 
    111                         methods => { 
    112                             handler => sub { 
    113                                 no warnings 'redefine'; 
    114                                 require HTTP::Engine::Interface::CGI; 
    115                                 local *HTTP::Engine::Interface::CGI::should_write_response_line = sub { 1 }; # H::S::S::CGI needs status line 
    116                                 $args{interface}->{module} = $interface; 
    117                                 HTTP::Engine->new( 
    118                                     %args 
    119                                 )->run; 
     63            test_tcp( 
     64                client => $client_cb, 
     65                server => sub { 
     66                    Moose::Meta::Class 
     67                        ->create_anon_class( 
     68                            superclasses => ['HTTP::Server::Simple::CGI'], 
     69                            methods => { 
     70                                handler => sub { 
     71                                    no warnings 'redefine'; 
     72                                    require HTTP::Engine::Interface::CGI; 
     73                                    local *HTTP::Engine::Interface::CGI::should_write_response_line = sub { 1 }; # H::S::S::CGI needs status line 
     74                                    $args{interface}->{module} = $interface; 
     75                                    HTTP::Engine->new( 
     76                                        %args 
     77                                    )->run; 
     78                                }, 
    12079                            }, 
    121                         }, 
    122                         cache => 1 
    123                     )->new_object( 
    124                     )->new( 
    125                         $port 
    126                     )->run; 
    127             }; 
     80                            cache => 1 
     81                        )->new_object( 
     82                        )->new( 
     83                            $port 
     84                        )->run; 
     85                }, 
     86                port => $port, 
     87            ); 
    12888        } else { 
    12989            $args{interface}->{module} = $interface; 
    13090            $args{poe_kernel_run} = ($interface eq 'POE') if $poe_kernel_run; 
    131             _daemonize $client_cb, $port, %args; 
     91            test_tcp( 
     92                client => $client_cb, 
     93                server => sub { 
     94                    my $poe_kernel_run = delete $args{poe_kernel_run}; 
     95                    HTTP::Engine->new(%args)->run; 
     96                    POE::Kernel->run() if $poe_kernel_run; 
     97                }, 
     98                port   => $port, 
     99            ); 
    132100        } 
    133101    } 
     
    153121} 
    154122 
    155 sub check_port { 
    156     my ( $port ) = @_; 
    157  
    158     my $remote = IO::Socket::INET->new( 
    159         Proto    => "tcp", 
    160         PeerAddr => '127.0.0.1', 
    161         PeerPort => $port 
    162     ); 
    163     if ($remote) { 
    164         close $remote; 
    165         return 1; 
    166     } 
    167     else { 
    168         return 0; 
    169     } 
    170 } 
    171  
    172 sub wait_port { 
    173     my $port = shift; 
    174  
    175     my $retry = 10; 
    176     while ($retry--) { 
    177         return if check_port($port); 
    178         sleep 1; 
    179     } 
    180     die "cannot open port: $port"; 
    181 } 
    182  
    183123sub req { 
    184124    my %args = @_;