Fix waiting for the server to start in t/author/http-server.t
[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;
a526c982 8use Net::EmptyPort qw(wait_port empty_port);
29057674 9use Try::Tiny;
876db346 10use Plack::Builder;
f0ee1a76 11
86b73ee0 12eval { require Catalyst::Devel; Catalyst::Devel->VERSION(1.0); 1; } || do {
13 fail("Could not load Catalyst::Devel: $@");
14 exit 1;
15};
16
17eval { require File::Copy::Recursive; 1 } || do {
18 fail("Could not load File::Copy::Recursive: $@");
19 exit 1;
20};
040850b3 21
547f8806 22# Run a single test by providing it as the first arg
23my $single_test = shift;
24
81f25ce6 25my $tmpdir = "$FindBin::Bin/../../t/tmp";
2f381252 26
040850b3 27# clean up
2f381252 28rmtree $tmpdir if -d $tmpdir;
040850b3 29
30# create a TestApp and copy the test libs into it
2f381252 31mkdir $tmpdir;
32chdir $tmpdir;
81f25ce6 33system( $^X, "-I$FindBin::Bin/../../lib", "$FindBin::Bin/../../script/catalyst.pl", 'TestApp' );
67c3b305 34chdir "$FindBin::Bin/..";
81f25ce6 35File::Copy::Recursive::dircopy( '../t/lib', '../t/tmp/TestApp/lib' ) or die;
10bdcbe8 36
37# remove TestApp's tests
81f25ce6 38rmtree '../t/tmp/TestApp/t' or die;
040850b3 39
40# spawn the standalone HTTP server
29057674 41my $port = empty_port;
d93d402c 42
43my $pid = fork;
44if ($pid) {
45 # parent.
46 print "Waiting for server to start...\n";
29057674 47 wait_port_timeout($port, 30);
d93d402c 48} elsif ($pid == 0) {
49 # child process
50 unshift @INC, "$tmpdir/TestApp/lib", "$FindBin::Bin/../../lib";
51 require TestApp;
52
1316cc64 53 my $psgi_app = TestApp->apply_default_middlewares(TestApp->psgi_app);
876db346 54 Plack::Loader->auto(port => $port)->run(builder {
55 mount '/test_prefix' => $psgi_app;
56 mount '/' => sub {
57 return [501, ['Content-Type' => 'text/plain'], ['broken tests']];
58 };
59 });
d93d402c 60
61 exit 0;
62} else {
63 die "fork failed: $!";
896c9ed2 64}
32e231eb 65
040850b3 66# run the testsuite against the HTTP server
876db346 67$ENV{CATALYST_SERVER} = "http://localhost:$port/test_prefix";
547f8806 68
e8e8895a 69chdir '..';
70
2f381252 71my $return;
547f8806 72if ( $single_test ) {
e8e8895a 73 $return = system( "$^X -Ilib/ $single_test" );
547f8806 74}
75else {
529d5abc 76 $return = prove(grep { $_ ne '..' } glob('t/aggregate/live_*.t'));
547f8806 77}
040850b3 78
79# shut it down
e1b364f4 80kill 'INT', $pid;
040850b3 81
82# clean up
81f25ce6 83rmtree "$FindBin::Bin/../../t/tmp" if -d "$FindBin::Bin/../../t/tmp";
896c9ed2 84
2f381252 85is( $return, 0, 'live tests' );
55ddccae 86
4d7d4257 87# kill 'INT' doesn't exist in Windows, so to prevent child hanging,
88# this process will need to commit seppuku to clean up the children.
89if ($^O eq 'MSWin32') {
90 # Furthermore, it needs to do it 'politely' so that TAP doesn't
91 # smell anything 'dubious'.
92 require Win32::Process; # core in all versions of Win32 Perl
93 Win32::Process::KillProcess($$, $return);
94}
95
29057674 96sub wait_port_timeout {
97 my ($port, $timeout) = @_;
98
a526c982 99 wait_port($port, 0.1, $timeout * 10) and return;
29057674 100
101 die "Server did not start within $timeout seconds";
896c9ed2 102}
868a7cca 103
104sub prove {
529d5abc 105 my (@tests) = @_;
868a7cca 106 if (!(my $pid = fork)) {
641b0131 107 require TAP::Harness;
e8e8895a 108
109 my $aggr = -e '.aggregating';
110 my $harness = TAP::Harness->new({
529d5abc 111 ($aggr ? (test_args => \@tests) : ()),
112 lib => ['lib'],
e8e8895a 113 });
114
115 my $aggregator = $aggr
116 ? $harness->runtests('t/aggregate.t')
529d5abc 117 : $harness->runtests(@tests);
641b0131 118
119 exit $aggregator->has_errors ? 1 : 0;
868a7cca 120 } else {
121 waitpid $pid, 0;
122 return $?;
123 }
124}