Fixed http-server tests so it reads test results
[catagits/Catalyst-Runtime.git] / t / optional / http-server-restart.t
1 #!perl\r
2 \r
3 # This test tests the standalone server's auto-restart feature.\r
4 \r
5 use strict;\r
6 use warnings;\r
7 \r
8 use File::Path;\r
9 use FindBin;\r
10 use LWP::Simple;\r
11 use IO::Socket;\r
12 use Test::More;\r
13 use Time::HiRes qw/sleep/;\r
14 eval "use File::Copy::Recursive";\r
15 \r
16 plan skip_all => 'set TEST_HTTP to enable this test' unless $ENV{TEST_HTTP};\r
17 plan skip_all => 'File::Copy::Recursive required' if $@;\r
18 \r
19 plan tests => 40;\r
20 \r
21 # clean up\r
22 rmtree "$FindBin::Bin/../../t/var" if -d "$FindBin::Bin/../../t/var";\r
23 \r
24 # create a TestApp and copy the test libs into it\r
25 mkdir "$FindBin::Bin/../../t/var";\r
26 chdir "$FindBin::Bin/../../t/var";\r
27 system "$FindBin::Bin/../../script/catalyst.pl TestApp";\r
28 chdir "$FindBin::Bin/../..";\r
29 File::Copy::Recursive::dircopy( 't/live/lib', 't/var/TestApp/lib' );\r
30 \r
31 # spawn the standalone HTTP server\r
32 my $port = 30000 + int rand(1 + 10000);\r
33 my $pid = open my $server, \r
34     "$FindBin::Bin/../../t/var/TestApp/script/testapp_server.pl -port $port -restart 2>&1 |"\r
35     or die "Unable to spawn standalone HTTP server: $!";\r
36 \r
37 # wait for it to start\r
38 print "Waiting for server to start...\n";\r
39 while ( check_port( 'localhost', $port ) != 1 ) {\r
40     sleep 1;\r
41 }\r
42 \r
43 # change various files\r
44 my @files = (\r
45     "$FindBin::Bin/../../t/var/TestApp/lib/TestApp.pm",\r
46     "$FindBin::Bin/../../t/var/TestApp/lib/TestApp/Controller/Action/Begin.pm",\r
47     "$FindBin::Bin/../../t/var/TestApp/lib/TestApp/Controller/Engine/Request/URI.pm",\r
48 );\r
49 \r
50 # change some files and make sure the server restarts itself\r
51 for ( 1..20 ) {\r
52     my $index = rand @files;\r
53     open my $pm, '>>', $files[$index]\r
54         or die "Unable to open $files[$index] for writing: $!";\r
55     print $pm "\n";\r
56     close $pm;\r
57     \r
58     # give the server time to notice the change and restart\r
59     my $count = 0;\r
60     sleep 1;\r
61     while ( check_port( 'localhost', $port ) != 1 ) {\r
62         # wait for it to restart\r
63         sleep 0.1;\r
64         die "Server appears to have died" if $count++ > 50;\r
65     }\r
66     my $response = get("http://localhost:$port/action/default");\r
67     like( $response, qr/Catalyst::Request/, 'Request OK' );\r
68     \r
69     #print $server->getline;\r
70 }\r
71 \r
72 # add errors to the file and make sure server does not die or restart\r
73 for ( 1..20 ) {\r
74     my $index = rand @files;\r
75     open my $pm, '>>', $files[$index]\r
76         or die "Unable to open $files[$index] for writing: $!";\r
77     print $pm "bleh";\r
78     close $pm;\r
79     \r
80     # give the server time to notice the change\r
81     sleep 1;\r
82     if ( check_port( 'localhost', $port ) != 1 ) {\r
83         die "Server appears to have died";\r
84     }\r
85     my $response = get("http://localhost:$port/action/default");\r
86     like( $response, qr/Catalyst::Request/, 'Request OK' );\r
87     \r
88     #print $server->getline;\r
89 }\r
90 \r
91 # shut it down\r
92 kill 2, $pid;\r
93 close $server;\r
94 \r
95 # clean up\r
96 rmtree "$FindBin::Bin/../../t/var" if -d "$FindBin::Bin/../../t/var";\r
97 \r
98 sub check_port {\r
99     my ( $host, $port ) = @_;\r
100 \r
101     my $remote = IO::Socket::INET->new(\r
102         Proto    => "tcp",\r
103         PeerAddr => $host,\r
104         PeerPort => $port\r
105     );\r
106     if ($remote) {\r
107         close $remote;\r
108         return 1;\r
109     }\r
110     else {\r
111         return 0;\r
112     }\r
113 }\r