Aggregate http-server.t tests
[catagits/Catalyst-Runtime.git] / t / author / http-server.t
CommitLineData
040850b3 1use strict;
2use warnings;
3
81f25ce6 4use Test::More tests => 1;
4853fb50 5
040850b3 6use File::Path;
7use FindBin;
2f381252 8use IPC::Open3;
896c9ed2 9use IO::Socket;
f0ee1a76 10
81f25ce6 11use Catalyst::Devel 1.0;
12use File::Copy::Recursive;
040850b3 13
547f8806 14# Run a single test by providing it as the first arg
15my $single_test = shift;
16
81f25ce6 17my $tmpdir = "$FindBin::Bin/../../t/tmp";
2f381252 18
040850b3 19# clean up
2f381252 20rmtree $tmpdir if -d $tmpdir;
040850b3 21
22# create a TestApp and copy the test libs into it
2f381252 23mkdir $tmpdir;
24chdir $tmpdir;
81f25ce6 25system( $^X, "-I$FindBin::Bin/../../lib", "$FindBin::Bin/../../script/catalyst.pl", 'TestApp' );
67c3b305 26chdir "$FindBin::Bin/..";
81f25ce6 27File::Copy::Recursive::dircopy( '../t/lib', '../t/tmp/TestApp/lib' ) or die;
10bdcbe8 28
29# remove TestApp's tests
81f25ce6 30rmtree '../t/tmp/TestApp/t' or die;
040850b3 31
32# spawn the standalone HTTP server
896c9ed2 33my $port = 30000 + int rand(1 + 10000);
81f25ce6 34my @cmd = ($^X, "-I$FindBin::Bin/../../lib",
3dcfb4c8 35 "$FindBin::Bin/../../t/tmp/TestApp/script/testapp_server.pl", '--port', $port );
81f25ce6 36my $pid = open3( undef, my $server, undef, @cmd)
040850b3 37 or die "Unable to spawn standalone HTTP server: $!";
896c9ed2 38
040850b3 39# wait for it to start
896c9ed2 40print "Waiting for server to start...\n";
81f25ce6 41my $timeout = 30;
42my $count = 0;
896c9ed2 43while ( check_port( 'localhost', $port ) != 1 ) {
44 sleep 1;
81f25ce6 45 die("Server did not start within $timeout seconds: " . join(' ', @cmd))
46 if $count++ > $timeout;
896c9ed2 47}
32e231eb 48
040850b3 49# run the testsuite against the HTTP server
896c9ed2 50$ENV{CATALYST_SERVER} = "http://localhost:$port";
547f8806 51
e8e8895a 52chdir '..';
53
2f381252 54my $return;
547f8806 55if ( $single_test ) {
e8e8895a 56 $return = system( "$^X -Ilib/ $single_test" );
547f8806 57}
58else {
e8e8895a 59 $return = prove( ['lib/'], [grep { $_ ne '..' } glob('t/aggregate/live_*.t')] );
547f8806 60}
040850b3 61
62# shut it down
e1b364f4 63kill 'INT', $pid;
896c9ed2 64close $server;
040850b3 65
66# clean up
81f25ce6 67rmtree "$FindBin::Bin/../../t/tmp" if -d "$FindBin::Bin/../../t/tmp";
896c9ed2 68
2f381252 69is( $return, 0, 'live tests' );
55ddccae 70
896c9ed2 71sub check_port {
72 my ( $host, $port ) = @_;
73
74 my $remote = IO::Socket::INET->new(
75 Proto => "tcp",
76 PeerAddr => $host,
77 PeerPort => $port
78 );
79 if ($remote) {
80 close $remote;
81 return 1;
82 }
83 else {
84 return 0;
85 }
86}
868a7cca 87
88sub prove {
641b0131 89 my ($inc, $tests) = @_;
868a7cca 90 if (!(my $pid = fork)) {
641b0131 91 unshift @INC, @{ $inc };
92
93 require TAP::Harness;
e8e8895a 94
95 my $aggr = -e '.aggregating';
96 my $harness = TAP::Harness->new({
97 ($aggr ? (test_args => $tests) : ())
98 });
99
100 my $aggregator = $aggr
101 ? $harness->runtests('t/aggregate.t')
102 : $harness->runtests(@{ $tests });
641b0131 103
104 exit $aggregator->has_errors ? 1 : 0;
868a7cca 105 } else {
106 waitpid $pid, 0;
107 return $?;
108 }
109}