Merge pull request #170 from perl-catalyst/haarg/no-dev-circ-deps
[catagits/Catalyst-Runtime.git] / t / optional_http-server-restart.t
CommitLineData
c7ded7aa 1# This test tests the standalone server's auto-restart feature.
2
3use strict;
4use warnings;
5
4853fb50 6use Test::More;
7BEGIN {
8 plan skip_all => 'set TEST_HTTP to enable this test' unless $ENV{TEST_HTTP};
9}
10
c7ded7aa 11use File::Path;
12use FindBin;
13use LWP::Simple;
14use IO::Socket;
2f381252 15use IPC::Open3;
c7ded7aa 16use Time::HiRes qw/sleep/;
c7ded7aa 17
8ae46e98 18BEGIN {
19 eval "use File::Copy::Recursive";
20 plan skip_all => 'File::Copy::Recursive required' if $@;
21}
2f381252 22
8ae46e98 23use lib 't/lib';
24use MakeTestApp;
c7ded7aa 25
8ae46e98 26make_test_app;
c7ded7aa 27
28# spawn the standalone HTTP server
29my $port = 30000 + int rand( 1 + 10000 );
9c71d51d 30
2f381252 31my( $server, $pid );
81f25ce6 32my @cmd = ($^X, "-I$FindBin::Bin/../lib", "-I$FindBin::Bin/lib",
201464db 33 "$FindBin::Bin/../t/tmp/TestApp/script/testapp_server.pl", '--port',
34 $port, '--restart');
81f25ce6 35
36$pid = open3( undef, $server, undef, @cmd )
2f381252 37 or die "Unable to spawn standalone HTTP server: $!";
c7ded7aa 38
9c71d51d 39# switch to non-blocking reads so we can fail
40# gracefully instead of just hanging forever
41
42$server->blocking( 0 );
43
c7ded7aa 44# wait for it to start
45print "Waiting for server to start...\n";
46while ( check_port( 'localhost', $port ) != 1 ) {
47 sleep 1;
48}
49
50# change various files
51my @files = (
52 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp.pm",
53 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp/Controller/Action/Begin.pm",
7d9921b1 54 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp/Controller/Immutable.pm",
81f25ce6 55 "$FindBin::Bin/../t/tmp/TestApp/lib/TestApp/Controller/Immutable/HardToReload.pm",
c7ded7aa 56);
57
58# change some files and make sure the server restarts itself
9c71d51d 59NON_ERROR_RESTART:
c7ded7aa 60for ( 1 .. 20 ) {
61 my $index = rand @files;
62 open my $pm, '>>', $files[$index]
63 or die "Unable to open $files[$index] for writing: $!";
64 print $pm "\n";
65 close $pm;
66
67 # give the server time to notice the change and restart
68 my $count = 0;
9c71d51d 69 my $line;
201464db 70 while ( ( $line || '' ) !~ /ttempting to restart the server/ ) {
9c71d51d 71 # wait for restart message
72 $line = $server->getline;
73 sleep 0.1;
74 if ( $count++ > 100 ) {
75 fail "Server restarted";
76 SKIP: {
77 skip "Server didn't restart, no sense in checking response", 1;
78 }
79 next NON_ERROR_RESTART;
80 }
81 };
82 pass "Server restarted";
83
84 $count = 0;
85 while ( check_port( 'localhost', $port ) != 1 ) {
c7ded7aa 86 # wait for it to restart
87 sleep 0.1;
9c71d51d 88 die "Server appears to have died" if $count++ > 100;
c7ded7aa 89 }
90 my $response = get("http://localhost:$port/action/default");
91 like( $response, qr/Catalyst::Request/, 'Non-error restart, request OK' );
92
9c71d51d 93 # give the server some time to reindex its files
94 sleep 1;
c7ded7aa 95}
96
9c71d51d 97# multiple restart directories
98
99# we need different options so we have to rebuild most
100# of the testing environment
101
102kill 'KILL', $pid;
103close $server;
104
c7ded7aa 105# clean up
106rmtree "$FindBin::Bin/../t/tmp" if -d "$FindBin::Bin/../t/tmp";
107
201464db 108done_testing;
109
c7ded7aa 110sub check_port {
111 my ( $host, $port ) = @_;
112
113 my $remote = IO::Socket::INET->new(
114 Proto => "tcp",
115 PeerAddr => $host,
116 PeerPort => $port
117 );
118 if ($remote) {
119 close $remote;
120 return 1;
121 }
122 else {
123 return 0;
124 }
125}