1 # XXX - These tests seem to be somewhat flaky and timing-dependent. I
2 # have seen them all run to completion, and I've seen them fail
3 # partway through. If someone can come up with a better way to test
4 # this stuff that'd be great.
11 plan skip_all => 'set TEST_HTTP to enable this test' unless $ENV{TEST_HTTP};
14 use File::Copy qw( copy );
20 use Time::HiRes qw/sleep/;
22 eval "use Catalyst::Devel 1.04;";
24 plan skip_all => 'Catalyst::Devel >= 1.04 required' if $@;
25 eval "use File::Copy::Recursive";
26 plan skip_all => 'File::Copy::Recursive required' if $@;
30 my $tmpdir = "$FindBin::Bin/../t/tmp";
33 rmtree $tmpdir if -d $tmpdir;
35 # create a TestApp and copy the test libs into it
39 my $helper = Catalyst::Helper->new(
45 $helper->mk_app('TestApp');
47 chdir "$FindBin::Bin/..";
51 # remove TestApp's tests
52 rmtree 't/tmp/TestApp/t';
54 # spawn the standalone HTTP server
55 my $port = 30000 + int rand( 1 + 10000 );
57 my ( $pid, $server ) = start_server($port);
59 # change various files
61 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp.pm",
62 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp/Controller/Foo.pm",
63 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp/Controller/Root.pm",
66 # change some files and make sure the server restarts itself
69 my $index = rand @files;
70 open my $pm, '>>', $files[$index]
71 or die "Unable to open $files[$index] for writing: $!";
75 if ( ! look_for_restart() ) {
78 skip "Server did not restart, no sense in checking further", 1;
80 next NON_ERROR_RESTART;
83 my $response = get("http://localhost:$port/");
84 like( $response, qr/Welcome to the world of Catalyst/,
85 'Non-error restart, request OK' );
88 # add errors to the file and make sure server does die
91 my $index = rand @files;
92 open my $pm, '>>', $files[$index]
93 or die "Unable to open $files[$index] for writing: $!";
97 if ( ! look_for_death() ) {
100 skip "Server restarted, no sense in checking further", 2;
106 if ( ! look_for_restart() ) {
109 skip "Server did not restart, no sense in checking further", 1;
114 my $response = get("http://localhost:$port/");
115 like( $response, qr/Welcome to the world of Catalyst/,
116 'Non-error restart after death, request OK' );
119 # multiple restart directories
121 # we need different options so we have to rebuild most
122 # of the testing environment
124 kill 9, $pid or die "Cannot send kill signal to $pid: $!";
125 close $server or die "Cannot close handle to server process: $!";
128 # pick next port because the last one might still be blocked from
129 # previous server. This might fail if this port is unavailable
130 # but picking the first one has the same problem so this is acceptable
137 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp/Controller/Subdir1/Foo.pm",
138 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp/Controller/Subdir2/Foo.pm",
141 my $app_root = "$FindBin::Bin/../t/tmp/TestApp";
142 my $restartdirs = join ' ', map{
143 "-restartdirectory $app_root/lib/TestApp/Controller/Subdir$_"
146 ( $pid, $server ) = start_server($port);
150 my $index = rand @files;
151 open my $pm, '>>', $files[$index]
152 or die "Unable to open $files[$index] for writing: $!";
156 if ( ! look_for_restart() ) {
159 skip "Server did not restart, no sense in checking further", 1;
161 next MULTI_DIR_RESTART;
164 my $response = get("http://localhost:$port/");
165 like( $response, qr/Welcome to the world of Catalyst/,
166 'Non-error restart with multiple watched dirs' );
173 rmtree "$FindBin::Bin/../t/tmp" if -d "$FindBin::Bin/../t/tmp";
176 { no warnings 'once'; $File::Copy::Recursive::RMTrgFil = 1; }
177 copy( 't/lib/TestApp.pm', 't/tmp/TestApp/lib/TestApp.pm' );
178 File::Copy::Recursive::dircopy( 't/lib/TestApp', 't/tmp/TestApp/lib/TestApp' );
186 undef, $server, undef,
187 $^X, "-I$FindBin::Bin/../lib",
188 "$FindBin::Bin/../t/tmp/TestApp/script/testapp_server.pl", '--port',
190 ) or die "Unable to spawn standalone HTTP server: $!";
192 # switch to non-blocking reads so we can fail gracefully instead
193 # of just hanging forever
194 $server->blocking(0);
198 diag('Waiting for server to start...');
199 while ( check_port( 'localhost', $port ) != 1 ) {
203 if ( $waited >= 10 ) {
204 BAIL_OUT('Waited 10 seconds for server to start, to no avail');
208 return ($pid, $server);
212 my ( $host, $port ) = @_;
214 my $remote = IO::Socket::INET->new(
228 sub look_for_restart {
229 # give the server time to notice the change and restart
233 while ( ( $line || '' ) !~ /can connect/ ) {
234 $line = $server->getline;
236 if ( $count++ > 300 ) {
237 fail "Server restarted";
242 pass "Server restarted";
248 # give the server time to notice the change and restart
252 while ( ( $line || '' ) !~ /failed/ ) {
253 $line = $server->getline;
255 if ( $count++ > 300 ) {