From: Robert Rothenberg Date: Fri, 4 Oct 2013 13:24:12 +0000 (+0100) Subject: Fixed bug with updating session when expiry_threshold is set X-Git-Tag: 0.40~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Plugin-Session.git;a=commitdiff_plain;h=3d03a2ba93787698b5006a2c6ab6e3e296b679d5 Fixed bug with updating session when expiry_threshold is set --- diff --git a/lib/Catalyst/Plugin/Session.pm b/lib/Catalyst/Plugin/Session.pm index a2c962f..e5967ba 100644 --- a/lib/Catalyst/Plugin/Session.pm +++ b/lib/Catalyst/Plugin/Session.pm @@ -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 ); diff --git a/t/lib/SessionExpiry/Controller/Root.pm b/t/lib/SessionExpiry/Controller/Root.pm index 1e4b676..e23cd12 100644 --- a/t/lib/SessionExpiry/Controller/Root.pm +++ b/t/lib/SessionExpiry/Controller/Root.pm @@ -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}); +} diff --git a/t/live_expiry_threshold.t b/t/live_expiry_threshold.t index ea3f83f..75beb64 100644 --- a/t/live_expiry_threshold.t +++ b/t/live_expiry_threshold.t @@ -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");