On 28 Dec 2005, at 16:36, Javier Amor Garcia wrote:

Hello,
  i am testing a  module for a web application and i need to test the
expiration of sessions. The problem is that i can not modify the
expiration time and i not want to make sleep the test for the full
length of expiration time (a hour).
[snip]
Can anyone give me pointers or advice about how to perform this type of
tests?

What I would do would be to isolate all the bits of code that poke at real time functions. For example I could imagine the only place where I actually call time being:

sub is_expired {
    my $self = shift;
    return ( $self->time_created + $self->session_length ) <= time;
}

then I can test everything except is_expired() by simple symbol table munging:

{
        local *MySessionClass::is_expired = sub { return 1 };
        ... test stuff that assumes session expired ...
}

{
        local *MySessionClass::is_expired = sub { return };
        ... test stuff that assumes session valid ...
}

and I can test is_expired() by overwriting time explicitly - with the rather useful Test::MockTime.

use Test::More tests => 4;
use Test::MockTime;

BEGIN { use_ok 'MySessionClass' };

my $s = MySessionClass->new( session_length => 10, time_created => 1234 );

Test::MockTime::set_fixed_time( 1234 );
ok( ! $s->is_expired, 'not expired at creation time' );

Test::MockTime::set_fixed_time( 1243 );
ok( ! $s->is_expired, 'not expired on session_length seconds' );

Test::MockTime::set_fixed_time( 1244 );
ok( $s->is_expired, 'is expired after session_length seconds' );

Hope this helps.

Adrian

Reply via email to