Looping and recursion tests plus a fix
Matt S Trout [Sun, 12 Mar 2006 00:39:49 +0000 (00:39 +0000)]
lib/Catalyst.pm
t/lib/TestApp.pm
t/live_loop.t [new file with mode: 0644]
t/live_recursion.t [new file with mode: 0644]

index e093ddb..203f1c4 100644 (file)
@@ -971,19 +971,22 @@ sub execute {
     $class = $c->component($class) || $class;
     $c->state(0);
 
+    if ($c->depth >= $RECURSION) {
+        my $action = "$code";
+        $action = "/$action" unless $action =~ /\-\>/;
+        my $error = qq/Deep recursion detected calling "$action"/;
+        $c->log->error($error);
+        $c->error($error);
+        $c->state(0);
+        return $c->state;
+    }
+
+
     if ( $c->debug ) {
         my $action = "$code";
         $action = "/$action" unless $action =~ /\-\>/;
         $c->counter->{"$code"}++;
 
-        if ( $c->counter->{"$code"} > $RECURSION ) {
-            my $error = qq/Deep recursion detected in "$action"/;
-            $c->log->error($error);
-            $c->error($error);
-            $c->state(0);
-            return $c->state;
-        }
-
         # determine if the call was the result of a forward
         # this is done by walking up the call stack and looking for a calling
         # sub of Catalyst::forward before the eval
index 00cde9a..cdb9d6e 100644 (file)
@@ -55,11 +55,24 @@ sub execute {
     return $c->SUPER::execute(@_);
 }
 
-sub class_forward_test_method {
+sub class_forward_test_method :Private {
     my ( $self, $c ) = @_;
     $c->response->headers->header( 'X-Class-Forward-Test-Method' => 1 );
 }
 
+sub loop_test : Local {
+    my ( $self, $c ) = @_;
+
+    for( 1..1001 ) {
+        $c->forward( 'class_forward_test_method' );
+    }
+}
+
+sub recursion_test : Local {
+    my ( $self, $c ) = @_;
+    $c->forward( 'recursion_test' );
+}
+
 {
     no warnings 'redefine';
     sub Catalyst::Log::error { }
diff --git a/t/live_loop.t b/t/live_loop.t
new file mode 100644 (file)
index 0000000..23f2ad2
--- /dev/null
@@ -0,0 +1,14 @@
+#!perl
+
+use strict;
+use warnings;
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+use Test::More tests => 3;
+use Catalyst::Test 'TestApp';
+
+ok( my $response = request('http://localhost/loop_test'), 'Request' );
+ok( $response->is_success, 'Response Successful 2xx' );
+ok( $response->header('X-Class-Forward-Test-Method'), 'Loop OK' );
diff --git a/t/live_recursion.t b/t/live_recursion.t
new file mode 100644 (file)
index 0000000..fa8ced1
--- /dev/null
@@ -0,0 +1,14 @@
+#!perl
+
+use strict;
+use warnings;
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+use Test::More tests => 3;
+use Catalyst::Test 'TestApp';
+
+ok( my $response = request('http://localhost/recursion_test'), 'Request' );
+ok( !$response->is_success, 'Response Not Successful' );
+is( $response->header('X-Catalyst-Error'), 'Deep recursion detected calling "/recursion_test"', 'Deep Recursion Detected' );