3 # This test tests the standalone server's auto-restart feature.
13 use Time::HiRes qw/sleep/;
14 eval "use Catalyst::Devel 1.0;";
16 plan skip_all => 'set TEST_HTTP to enable this test' unless $ENV{TEST_HTTP};
17 plan skip_all => 'Catalyst::Devel required' if $@;
18 plan skip_all => 'Catalyst::Devel >= 1.04 required' if $Catalyst::Devel::VERSION <= 1.03;
19 eval "use File::Copy::Recursive";
20 plan skip_all => 'File::Copy::Recursive required' if $@;
25 rmtree "$FindBin::Bin/../t/tmp" if -d "$FindBin::Bin/../t/tmp";
27 # create a TestApp and copy the test libs into it
28 mkdir "$FindBin::Bin/../t/tmp";
29 chdir "$FindBin::Bin/../t/tmp";
31 "perl -I$FindBin::Bin/../lib $FindBin::Bin/../script/catalyst.pl TestApp";
32 chdir "$FindBin::Bin/..";
33 File::Copy::Recursive::dircopy( 't/lib', 't/tmp/TestApp/lib' );
35 # remove TestApp's tests
36 rmtree 't/tmp/TestApp/t';
38 # spawn the standalone HTTP server
39 my $port = 30000 + int rand( 1 + 10000 );
41 my $pid = open my $server,
42 "perl -I$FindBin::Bin/../lib $FindBin::Bin/../t/tmp/TestApp/script/testapp_server.pl -port $port -restart 2>&1 |"
43 or die "Unable to spawn standalone HTTP server: $!";
45 # switch to non-blocking reads so we can fail
46 # gracefully instead of just hanging forever
48 $server->blocking( 0 );
50 # wait for it to start
51 print "Waiting for server to start...\n";
52 while ( check_port( 'localhost', $port ) != 1 ) {
56 # change various files
58 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp.pm",
59 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp/Controller/Action/Begin.pm",
60 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp/Controller/Engine/Request/URI.pm",
63 # change some files and make sure the server restarts itself
66 my $index = rand @files;
67 open my $pm, '>>', $files[$index]
68 or die "Unable to open $files[$index] for writing: $!";
72 # give the server time to notice the change and restart
76 while ( ( $line || '' ) !~ /can connect/ ) {
77 # wait for restart message
78 $line = $server->getline;
80 if ( $count++ > 100 ) {
81 fail "Server restarted";
83 skip "Server didn't restart, no sense in checking response", 1;
85 next NON_ERROR_RESTART;
88 pass "Server restarted";
91 while ( check_port( 'localhost', $port ) != 1 ) {
92 # wait for it to restart
94 die "Server appears to have died" if $count++ > 100;
96 my $response = get("http://localhost:$port/action/default");
97 like( $response, qr/Catalyst::Request/, 'Non-error restart, request OK' );
99 # give the server some time to reindex its files
103 # add errors to the file and make sure server does not die or restart
106 my $index = rand @files;
107 open my $pm, '>>', $files[$index]
108 or die "Unable to open $files[$index] for writing: $!";
115 while ( ( $line || '' ) !~ /failed/ ) {
116 # wait for restart message
117 $line = $server->getline;
119 if ( $count++ > 100 ) {
120 fail "Server restarted";
122 skip "Server didn't restart, no sense in checking response", 1;
124 next NO_RESTART_ON_ERROR;
128 pass "Server refused to restart";
130 if ( check_port( 'localhost', $port ) != 1 ) {
131 die "Server appears to have died";
133 my $response = get("http://localhost:$port/action/default");
134 like( $response, qr/Catalyst::Request/,
135 'Syntax error, no restart, request OK' );
137 # give the server some time to reindex its files
142 # multiple restart directories
144 # we need different options so we have to rebuild most
145 # of the testing environment
150 # pick next port because the last one might still be blocked from
151 # previous server. This might fail if this port is unavailable
152 # but picking the first one has the same problem so this is acceptable
156 { no warnings 'once'; $File::Copy::Recursive::RMTrgFil = 1; }
157 File::Copy::Recursive::dircopy( 't/lib', 't/tmp/TestApp/lib' );
159 # change various files
161 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp/Controller/Action/Begin.pm",
162 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp/Controller/Engine/Request/URI.pm",
165 my $app_root = "$FindBin::Bin/../t/tmp/TestApp";
166 my $restartdirs = join ' ', map{
167 "-restartdirectory $app_root/lib/TestApp/Controller/$_"
171 "perl -I$FindBin::Bin/../lib $FindBin::Bin/../t/tmp/TestApp/script/testapp_server.pl -port $port -restart $restartdirs 2>&1 |"
172 or die "Unable to spawn standalone HTTP server: $!";
173 $server->blocking( 0 );
176 # wait for it to start
177 print "Waiting for server to start...\n";
178 while ( check_port( 'localhost', $port ) != 1 ) {
184 my $index = rand @files;
185 open my $pm, '>>', $files[$index]
186 or die "Unable to open $files[$index] for writing: $!";
190 # give the server time to notice the change and restart
194 while ( ( $line || '' ) !~ /can connect/ ) {
195 # wait for restart message
196 $line = $server->getline;
198 if ( $count++ > 100 ) {
199 fail "Server restarted";
201 skip "Server didn't restart, no sense in checking response", 1;
203 next MULTI_DIR_RESTART;
206 pass "Server restarted with multiple restartdirs";
209 while ( check_port( 'localhost', $port ) != 1 ) {
210 # wait for it to restart
212 die "Server appears to have died" if $count++ > 100;
214 my $response = get("http://localhost:$port/action/default");
215 like( $response, qr/Catalyst::Request/, 'Non-error restart, request OK' );
217 # give the server some time to reindex its files
227 rmtree "$FindBin::Bin/../t/tmp" if -d "$FindBin::Bin/../t/tmp";
230 my ( $host, $port ) = @_;
232 my $remote = IO::Socket::INET->new(