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