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 use File::Copy::Recursive;
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 my $helper = Catalyst::Helper->new(
41 $helper->mk_app('TestApp');
43 chdir "$FindBin::Bin/..";
47 # remove TestApp's tests
48 rmtree "$tmpdir/TestApp/t";
50 # spawn the standalone HTTP server
51 my $port = 30000 + int rand( 1 + 10000 );
53 my ( $pid, $server ) = start_server($port);
55 # change various files
57 "$tmpdir/TestApp/lib/TestApp.pm",
58 "$tmpdir/TestApp/lib/TestApp/Controller/Foo.pm",
59 "$tmpdir/TestApp/lib/TestApp/Controller/Root.pm",
62 # change some files and make sure the server restarts itself
65 my $index = rand @files;
66 open my $pm, '>>', $files[$index]
67 or die "Unable to open $files[$index] for writing: $!";
71 if ( ! look_for_restart() ) {
74 skip "Server did not restart, no sense in checking further", 1;
76 next NON_ERROR_RESTART;
79 my $response = get("http://localhost:$port/");
80 like( $response, qr/Welcome to the world of Catalyst/,
81 'Non-error restart, request OK' );
84 # add errors to the file and make sure server does die
87 my $index = rand @files;
88 open my $pm, '>>', $files[$index]
89 or die "Unable to open $files[$index] for writing: $!";
93 if ( ! look_for_death() ) {
96 skip "Server restarted, no sense in checking further", 2;
102 if ( ! look_for_restart() ) {
105 skip "Server did not restart, no sense in checking further", 1;
110 my $response = get("http://localhost:$port/");
111 like( $response, qr/Welcome to the world of Catalyst/,
112 'Non-error restart after death, request OK' );
115 # multiple restart directories
117 # we need different options so we have to rebuild most
118 # of the testing environment
120 kill 9, $pid or die "Cannot send kill signal to $pid: $!";
121 close $server or die "Cannot close handle to server process: $!";
124 # pick next port because the last one might still be blocked from
125 # previous server. This might fail if this port is unavailable
126 # but picking the first one has the same problem so this is acceptable
133 "$tmpdir/TestApp/lib/TestApp/Controller/Subdir1/Foo.pm",
134 "$tmpdir/TestApp/lib/TestApp/Controller/Subdir2/Foo.pm",
137 my $app_root = "$tmpdir/TestApp";
138 my $restartdirs = join ' ', map{
139 "-restartdirectory $app_root/lib/TestApp/Controller/Subdir$_"
142 ( $pid, $server ) = start_server($port);
146 my $index = rand @files;
147 open my $pm, '>>', $files[$index]
148 or die "Unable to open $files[$index] for writing: $!";
152 if ( ! look_for_restart() ) {
155 skip "Server did not restart, no sense in checking further", 1;
157 next MULTI_DIR_RESTART;
160 my $response = get("http://localhost:$port/");
161 like( $response, qr/Welcome to the world of Catalyst/,
162 'Non-error restart with multiple watched dirs' );
169 rmtree $tmpdir if -d $tmpdir;
172 { no warnings 'once'; $File::Copy::Recursive::RMTrgFil = 1; }
173 File::Copy::Recursive::dircopy( 't/lib/TestApp', "$tmpdir/TestApp/lib/TestApp" );
181 undef, $server, undef,
182 $^X, "-I$FindBin::Bin/../lib",
183 "$tmpdir/TestApp/script/testapp_server.pl", '--port',
185 ) or die "Unable to spawn standalone HTTP server: $!";
187 # switch to non-blocking reads so we can fail gracefully instead
188 # of just hanging forever
189 $server->blocking(0);
193 diag('Waiting for server to start...');
194 while ( check_port( 'localhost', $port ) != 1 ) {
198 if ( $waited >= 10 ) {
199 BAIL_OUT('Waited 10 seconds for server to start, to no avail');
203 return ($pid, $server);
207 my ( $host, $port ) = @_;
209 my $remote = IO::Socket::INET->new(
223 sub look_for_restart {
224 # give the server time to notice the change and restart
228 while ( ( $line || '' ) !~ /can connect/ ) {
229 $line = $server->getline;
231 if ( $count++ > 300 ) {
232 fail "Server restarted";
237 pass "Server restarted";
243 # give the server time to notice the change and restart
247 while ( ( $line || '' ) !~ /failed/ ) {
248 $line = $server->getline;
250 if ( $count++ > 300 ) {