Fixed bug with updating session when expiry_threshold is set
Robert Rothenberg [Fri, 4 Oct 2013 13:24:12 +0000 (14:24 +0100)]
lib/Catalyst/Plugin/Session.pm
t/lib/SessionExpiry/Controller/Root.pm
t/live_expiry_threshold.t

index a2c962f..e5967ba 100644 (file)
@@ -138,6 +138,27 @@ sub finalize_session {
     $c->_clear_session_instance_data;
 }
 
+sub _session_updated {
+    my $c = shift;
+
+    if ( my $session_data = $c->_session ) {
+
+        no warnings 'uninitialized';
+        if ( Object::Signature::signature($session_data) ne
+            $c->_session_data_sig )
+        {
+            return $session_data;
+        } else {
+            return;
+        }
+
+    } else {
+
+        return;
+
+    }
+}
+
 sub _save_session_id {
     my $c = shift;
 
@@ -165,16 +186,11 @@ sub _save_session_expires {
 sub _save_session {
     my $c = shift;
 
-    if ( my $session_data = $c->_session ) {
+    if ( my $session_data = $c->_session_updated ) {
 
-        no warnings 'uninitialized';
-        if ( Object::Signature::signature($session_data) ne
-            $c->_session_data_sig )
-        {
-            $session_data->{__updated} = time();
-            my $sid = $c->sessionid;
-            $c->store_session_data( "session:$sid" => $session_data );
-        }
+        $session_data->{__updated} = time();
+        my $sid = $c->sessionid;
+        $c->store_session_data( "session:$sid" => $session_data );
     }
 }
 
@@ -376,7 +392,7 @@ sub extend_session_expires {
         my $expires = $c->_get_stored_session_expires;
         my $cutoff  = $expires - $threshold;
 
-        if (!$threshold || $cutoff <= time) {
+        if (!$threshold || $cutoff <= time || $c->_session_updated) {
 
             $c->_extended_session_expires( my $updated = $c->calculate_initial_session_expires() );
             $c->extend_session_id( $sid, $updated );
index 1e4b676..e23cd12 100644 (file)
@@ -20,3 +20,9 @@ sub session_expires : Global {
     $c->session;
     $c->res->output($c->session_expires);
 }
+
+sub update_session : Global {
+    my ($self, $c) = @_;
+    $c->session->{foo} ++;
+    $c->res->output($c->session->{foo});
+}
index ea3f83f..75beb64 100644 (file)
@@ -43,7 +43,10 @@ $res = $ua->get( "http://localhost/session_expires" );
 ok($res->is_success, "session_expires");
 is($res->decoded_content, $expiry, "session_expires == session_data_expires");
 
-sleep(10);
+#
+
+$res = $ua->get( "http://localhost/update_session" );
+ok($res->is_success, "update_session");
 
 $res = $ua->get( "http://localhost/session_data_expires" );
 ok($res->is_success, "session_data_expires");
@@ -51,6 +54,25 @@ ok($res->is_success, "session_data_expires");
 my $updated = $res->decoded_content + 0;
 ok($updated > $expiry, "expiration updated");
 
+$expiry = $updated;
+
+$res = $ua->get( "http://localhost/session_data_expires" );
+ok($res->is_success, "session_data_expires");
+
+is($res->decoded_content, $expiry, "expiration not updated");
+
+$res = $ua->get( "http://localhost/session_expires" );
+ok($res->is_success, "session_expires");
+is($res->decoded_content, $expiry, "session_expires == session_data_expires");
+
+sleep(10);
+
+$res = $ua->get( "http://localhost/session_data_expires" );
+ok($res->is_success, "session_data_expires");
+
+$updated = $res->decoded_content + 0;
+ok($updated > $expiry, "expiration updated");
+
 $res = $ua->get( "http://localhost/session_expires" );
 ok($res->is_success, "session_expires");
 is($res->decoded_content, $updated, "session_expires == session_data_expires");