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 't/tmp/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 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp.pm",
58 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp/Controller/Foo.pm",
59 "$FindBin::Bin/../t/tmp/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 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp/Controller/Subdir1/Foo.pm",
134 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp/Controller/Subdir2/Foo.pm",
137 my $app_root = "$FindBin::Bin/../t/tmp/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 "$FindBin::Bin/../t/tmp" if -d "$FindBin::Bin/../t/tmp";
172 { no warnings 'once'; $File::Copy::Recursive::RMTrgFil = 1; }
173 copy( 't/lib/TestApp.pm', 't/tmp/TestApp/lib/TestApp.pm' );
174 File::Copy::Recursive::dircopy( 't/lib/TestApp', 't/tmp/TestApp/lib/TestApp' );
182 undef, $server, undef,
183 $^X, "-I$FindBin::Bin/../lib",
184 "$FindBin::Bin/../t/tmp/TestApp/script/testapp_server.pl", '--port',
186 ) or die "Unable to spawn standalone HTTP server: $!";
188 # switch to non-blocking reads so we can fail gracefully instead
189 # of just hanging forever
190 $server->blocking(0);
194 diag('Waiting for server to start...');
195 while ( check_port( 'localhost', $port ) != 1 ) {
199 if ( $waited >= 10 ) {
200 BAIL_OUT('Waited 10 seconds for server to start, to no avail');
204 return ($pid, $server);
208 my ( $host, $port ) = @_;
210 my $remote = IO::Socket::INET->new(
224 sub look_for_restart {
225 # give the server time to notice the change and restart
229 while ( ( $line || '' ) !~ /can connect/ ) {
230 $line = $server->getline;
232 if ( $count++ > 300 ) {
233 fail "Server restarted";
238 pass "Server restarted";
244 # give the server time to notice the change and restart
248 while ( ( $line || '' ) !~ /failed/ ) {
249 $line = $server->getline;
251 if ( $count++ > 300 ) {