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/;
19 eval "use File::Copy::Recursive";
20 plan skip_all => 'File::Copy::Recursive required' if $@;
28 # spawn the standalone HTTP server
29 my $port = 30000 + int rand( 1 + 10000 );
32 my @cmd = ($^X, "-I$FindBin::Bin/../lib", "-I$FindBin::Bin/lib",
33 "$FindBin::Bin/../t/tmp/TestApp/script/testapp_server.pl", '--port',
36 $pid = open3( undef, $server, undef, @cmd )
37 or die "Unable to spawn standalone HTTP server: $!";
39 # switch to non-blocking reads so we can fail
40 # gracefully instead of just hanging forever
42 $server->blocking( 0 );
44 # wait for it to start
45 print "Waiting for server to start...\n";
46 while ( check_port( 'localhost', $port ) != 1 ) {
50 # change various files
52 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp.pm",
53 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp/Controller/Action/Begin.pm",
54 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp/Controller/Immutable.pm",
55 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp/Controller/Immutable/HardToReload.pm",
58 # change some files and make sure the server restarts itself
61 my $index = rand @files;
62 open my $pm, '>>', $files[$index]
63 or die "Unable to open $files[$index] for writing: $!";
67 # give the server time to notice the change and restart
70 while ( ( $line || '' ) !~ /ttempting to restart the server/ ) {
71 # wait for restart message
72 $line = $server->getline;
74 if ( $count++ > 100 ) {
75 fail "Server restarted";
77 skip "Server didn't restart, no sense in checking response", 1;
79 next NON_ERROR_RESTART;
82 pass "Server restarted";
85 while ( check_port( 'localhost', $port ) != 1 ) {
86 # wait for it to restart
88 die "Server appears to have died" if $count++ > 100;
90 my $response = get("http://localhost:$port/action/default");
91 like( $response, qr/Catalyst::Request/, 'Non-error restart, request OK' );
93 # give the server some time to reindex its files
97 # multiple restart directories
99 # we need different options so we have to rebuild most
100 # of the testing environment
106 rmtree "$FindBin::Bin/../t/tmp" if -d "$FindBin::Bin/../t/tmp";
111 my ( $host, $port ) = @_;
113 my $remote = IO::Socket::INET->new(