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 {require Catalyst::Devel; Catalyst::Devel->VERSION(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 $@;
24 my $tmpdir = "$FindBin::Bin/../t/tmp";
27 rmtree $tmpdir if -d $tmpdir;
29 # create a TestApp and copy the test libs into it
33 system( $^X, "-I$FindBin::Bin/../lib", '-MFile::Spec', '-e', "\@ARGV=('TestApp'); my \$devnull = File::Spec->devnull; open my \$fh, '>', \$devnull or die \"Cannot write to \$devnull: \$!\"; *STDOUT = \$fh; do \"$FindBin::Bin/../script/catalyst.pl\"");
35 chdir "$FindBin::Bin/..";
36 File::Copy::Recursive::dircopy( 't/lib', 't/tmp/TestApp/lib' );
38 # remove TestApp's tests
39 rmtree 't/tmp/TestApp/t';
41 # spawn the standalone HTTP server
42 my $port = 30000 + int rand( 1 + 10000 );
45 my @cmd = ($^X, "-I$FindBin::Bin/../lib", "-I$FindBin::Bin/lib",
46 "$FindBin::Bin/../t/tmp/TestApp/script/testapp_server.pl", '--port',
49 $pid = open3( undef, $server, undef, @cmd )
50 or die "Unable to spawn standalone HTTP server: $!";
52 # switch to non-blocking reads so we can fail
53 # gracefully instead of just hanging forever
55 $server->blocking( 0 );
57 # wait for it to start
58 print "Waiting for server to start...\n";
59 while ( check_port( 'localhost', $port ) != 1 ) {
63 # change various files
65 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp.pm",
66 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp/Controller/Action/Begin.pm",
67 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp/Controller/Immutable.pm",
68 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp/Controller/Immutable/HardToReload.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
83 while ( ( $line || '' ) !~ /ttempting to restart the server/ ) {
84 # wait for restart message
85 $line = $server->getline;
87 if ( $count++ > 100 ) {
88 fail "Server restarted";
90 skip "Server didn't restart, no sense in checking response", 1;
92 next NON_ERROR_RESTART;
95 pass "Server restarted";
98 while ( check_port( 'localhost', $port ) != 1 ) {
99 # wait for it to restart
101 die "Server appears to have died" if $count++ > 100;
103 my $response = get("http://localhost:$port/action/default");
104 like( $response, qr/Catalyst::Request/, 'Non-error restart, request OK' );
106 # give the server some time to reindex its files
110 # multiple restart directories
112 # we need different options so we have to rebuild most
113 # of the testing environment
119 rmtree "$FindBin::Bin/../t/tmp" if -d "$FindBin::Bin/../t/tmp";
124 my ( $host, $port ) = @_;
126 my $remote = IO::Socket::INET->new(