File: http-server.t

package info (click to toggle)
libcatalyst-perl 5.80025-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,104 kB
  • ctags: 1,183
  • sloc: perl: 15,616; makefile: 7
file content (96 lines) | stat: -rw-r--r-- 2,214 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use strict;
use warnings;

use Test::More tests => 1;

use File::Path;
use FindBin;
use IPC::Open3;
use IO::Socket;

use Catalyst::Devel 1.0;
use File::Copy::Recursive;

# Run a single test by providing it as the first arg
my $single_test = shift;

my $tmpdir = "$FindBin::Bin/../../t/tmp";

# clean up
rmtree $tmpdir if -d $tmpdir;

# create a TestApp and copy the test libs into it
mkdir $tmpdir;
chdir $tmpdir;
system( $^X, "-I$FindBin::Bin/../../lib", "$FindBin::Bin/../../script/catalyst.pl", 'TestApp' );
chdir "$FindBin::Bin/..";
File::Copy::Recursive::dircopy( '../t/lib', '../t/tmp/TestApp/lib' ) or die;

# remove TestApp's tests
rmtree '../t/tmp/TestApp/t' or die;

# spawn the standalone HTTP server
my $port = 30000 + int rand(1 + 10000);
my @cmd = ($^X, "-I$FindBin::Bin/../../lib",
  "$FindBin::Bin/../../t/tmp/TestApp/script/testapp_server.pl", '--port', $port );
my $pid = open3( undef, my $server, undef, @cmd)
    or die "Unable to spawn standalone HTTP server: $!";

# wait for it to start
print "Waiting for server to start...\n";
my $timeout = 30;
my $count = 0;
while ( check_port( 'localhost', $port ) != 1 ) {
    sleep 1;
    die("Server did not start within $timeout seconds: " . join(' ', @cmd))
        if $count++ > $timeout;
}

# run the testsuite against the HTTP server
$ENV{CATALYST_SERVER} = "http://localhost:$port";

my $return;
if ( $single_test ) {
    $return = system( "$^X -I../lib/ $single_test" );
}
else {
    $return = prove( '-r', '-I../lib/', glob('../t/aggregate/live_*.t') );
}

# shut it down
kill 'INT', $pid;
close $server;

# clean up
rmtree "$FindBin::Bin/../../t/tmp" if -d "$FindBin::Bin/../../t/tmp";

is( $return, 0, 'live tests' );

sub check_port {
    my ( $host, $port ) = @_;

    my $remote = IO::Socket::INET->new(
        Proto    => "tcp",
        PeerAddr => $host,
        PeerPort => $port
    );
    if ($remote) {
        close $remote;
        return 1;
    }
    else {
        return 0;
    }
}

sub prove {
    if (!(my $pid = fork)) {
        require App::Prove;
        my $prove = App::Prove->new;
        $prove->process_args(@_);
        exit( $prove->run ? 0 : 1 );
    } else {
        waitpid $pid, 0;
        return $?;
    }
}