test for fork also
Jonathan Rockway [Mon, 20 Nov 2006 05:17:38 +0000 (05:17 +0000)]
t/lib/TestApp/Controller/Fork.pm
t/live_fork.t

index 0f6e44c..b6992ab 100644 (file)
@@ -9,7 +9,7 @@ use warnings;
 use base 'Catalyst::Controller';
 use YAML;
 
-sub fork : Local {
+sub system : Local {
     my ($self, $c, $ls) = @_;
     my ($result, $code) = (undef, 1);
 
@@ -40,5 +40,22 @@ sub backticks : Local {
     
     $c->response->body(Dump({result => $result, code => $code}));
 }
-  
+
+sub fork : Local {
+    my ($self, $c) = @_;
+    my $pid;
+    my $x = 0;
+    
+    if($pid = fork()){
+       $x = "ok";
+    }
+    else {
+       exit(0);
+    }
+
+    waitpid $pid,0 or die;
+    
+    $c->response->body(Dump({pid => $pid, result => $x}));
+}
+
 1;
index 148a0d4..5871dcb 100644 (file)
@@ -16,11 +16,11 @@ use lib "$FindBin::Bin/lib";
 use Catalyst::Test qw(TestApp);
 
 plan 'skip_all' if !-e '/bin/ls'; # see if /bin/ls exists
-plan tests => 8; # otherwise
+plan tests => 13; # otherwise
 
 {
-  fork:
-    ok(my $result = get('/fork/fork/%2Fbin%2Fls'), 'get /fork//bin/ls');
+  system:
+    ok(my $result = get('/fork/system/%2Fbin%2Fls'), 'system');
     my @result = split /$/m, $result;
     $result = join q{}, @result[-4..-1];
     
@@ -32,7 +32,7 @@ plan tests => 8; # otherwise
 
 { 
   backticks:
-    ok(my $result = get('/fork/backticks/%2Fbin%2Fls'), 'get /fork//bin/ls');
+    ok(my $result = get('/fork/backticks/%2Fbin%2Fls'), '`backticks`');
     my @result = split /$/m, $result;
     $result = join q{}, @result[-4..-1];
     
@@ -41,3 +41,15 @@ plan tests => 8; # otherwise
     is($result_ref->{code}, 0, 'exited successfully');
     like($result_ref->{result}, qr{^/bin/ls[^:]}, 'contains ^/bin/ls$');
 }
+{ 
+  fork:
+    ok(my $result = get('/fork/fork'), 'fork');
+    my @result = split /$/m, $result;
+    $result = join q{}, @result[-4..-1];
+    
+    my $result_ref = eval { Load($result) };
+    ok($result_ref, 'is YAML');
+    isnt($result_ref->{pid}, 0, q{fork's "pid" wasn't 0});
+    isnt($result_ref->{pid}, $$, 'fork got a new pid');
+    is($result_ref->{result}, 'ok', 'fork was effective');
+}