1 # This test tests the standalone server's auto-restart feature.
8 plan skip_all => 'set TEST_HTTP to enable this test' unless $ENV{TEST_HTTP};
16 use Time::HiRes qw/sleep/;
17 eval "use Catalyst::Devel 1.0;";
19 plan skip_all => 'Catalyst::Devel required' if $@;
20 plan skip_all => 'Catalyst::Devel >= 1.04 required' if $Catalyst::Devel::VERSION <= 1.03;
21 eval "use File::Copy::Recursive";
22 plan skip_all => 'File::Copy::Recursive required' if $@;
26 my $tmpdir = "$FindBin::Bin/../t/tmp";
29 rmtree $tmpdir if -d $tmpdir;
31 # create a TestApp and copy the test libs into it
35 system( $^X, "-I$FindBin::Bin/../lib", "$FindBin::Bin/../script/catalyst.pl", 'TestApp' );
37 chdir "$FindBin::Bin/..";
38 File::Copy::Recursive::dircopy( 't/lib', 't/tmp/TestApp/lib' );
40 # remove TestApp's tests
41 rmtree 't/tmp/TestApp/t';
43 # spawn the standalone HTTP server
44 my $port = 30000 + int rand( 1 + 10000 );
47 $pid = open3( undef, $server, undef,
48 $^X, "-I$FindBin::Bin/../lib",
49 "$FindBin::Bin/../t/tmp/TestApp/script/testapp_server.pl", '-port',
51 or die "Unable to spawn standalone HTTP server: $!";
53 # switch to non-blocking reads so we can fail
54 # gracefully instead of just hanging forever
56 $server->blocking( 0 );
58 # wait for it to start
59 print "Waiting for server to start...\n";
60 while ( check_port( 'localhost', $port ) != 1 ) {
64 # change various files
66 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp.pm",
67 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp/Controller/Action/Begin.pm",
68 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp/Controller/Engine/Request/URI.pm",
71 # change some files and make sure the server restarts itself
74 my $index = rand @files;
75 open my $pm, '>>', $files[$index]
76 or die "Unable to open $files[$index] for writing: $!";
80 # give the server time to notice the change and restart
84 while ( ( $line || '' ) !~ /can connect/ ) {
85 # wait for restart message
86 $line = $server->getline;
88 if ( $count++ > 100 ) {
89 fail "Server restarted";
91 skip "Server didn't restart, no sense in checking response", 1;
93 next NON_ERROR_RESTART;
96 pass "Server restarted";
99 while ( check_port( 'localhost', $port ) != 1 ) {
100 # wait for it to restart
102 die "Server appears to have died" if $count++ > 100;
104 my $response = get("http://localhost:$port/action/default");
105 like( $response, qr/Catalyst::Request/, 'Non-error restart, request OK' );
107 # give the server some time to reindex its files
111 # add errors to the file and make sure server does not die or restart
114 my $index = rand @files;
115 open my $pm, '>>', $files[$index]
116 or die "Unable to open $files[$index] for writing: $!";
123 while ( ( $line || '' ) !~ /failed/ ) {
124 # wait for restart message
125 $line = $server->getline;
127 if ( $count++ > 100 ) {
128 fail "Server restarted";
130 skip "Server didn't restart, no sense in checking response", 1;
132 next NO_RESTART_ON_ERROR;
136 pass "Server refused to restart";
138 if ( check_port( 'localhost', $port ) != 1 ) {
139 die "Server appears to have died";
141 my $response = get("http://localhost:$port/action/default");
142 like( $response, qr/Catalyst::Request/,
143 'Syntax error, no restart, request OK' );
145 # give the server some time to reindex its files
150 # multiple restart directories
152 # we need different options so we have to rebuild most
153 # of the testing environment
158 # pick next port because the last one might still be blocked from
159 # previous server. This might fail if this port is unavailable
160 # but picking the first one has the same problem so this is acceptable
164 { no warnings 'once'; $File::Copy::Recursive::RMTrgFil = 1; }
165 File::Copy::Recursive::dircopy( 't/lib', 't/tmp/TestApp/lib' );
167 # change various files
169 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp/Controller/Action/Begin.pm",
170 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp/Controller/Engine/Request/URI.pm",
173 my $app_root = "$FindBin::Bin/../t/tmp/TestApp";
174 my $restartdirs = join ' ', map{
175 "-restartdirectory $app_root/lib/TestApp/Controller/$_"
178 $pid = open3( undef, $server, undef,
179 $^X, "-I$FindBin::Bin/../lib",
180 "$FindBin::Bin/../t/tmp/TestApp/script/testapp_server.pl", '-port',
181 $port, '-restart', $restartdirs )
182 or die "Unable to spawn standalone HTTP server: $!";
183 $server->blocking( 0 );
186 # wait for it to start
187 print "Waiting for server to start...\n";
188 while ( check_port( 'localhost', $port ) != 1 ) {
194 my $index = rand @files;
195 open my $pm, '>>', $files[$index]
196 or die "Unable to open $files[$index] for writing: $!";
200 # give the server time to notice the change and restart
204 while ( ( $line || '' ) !~ /can connect/ ) {
205 # wait for restart message
206 $line = $server->getline;
208 if ( $count++ > 100 ) {
209 fail "Server restarted";
211 skip "Server didn't restart, no sense in checking response", 1;
213 next MULTI_DIR_RESTART;
216 pass "Server restarted with multiple restartdirs";
219 while ( check_port( 'localhost', $port ) != 1 ) {
220 # wait for it to restart
222 die "Server appears to have died" if $count++ > 100;
224 my $response = get("http://localhost:$port/action/default");
225 like( $response, qr/Catalyst::Request/, 'Non-error restart, request OK' );
227 # give the server some time to reindex its files
237 rmtree "$FindBin::Bin/../t/tmp" if -d "$FindBin::Bin/../t/tmp";
240 my ( $host, $port ) = @_;
242 my $remote = IO::Socket::INET->new(