On Thu, 1 Mar 2007 14:53:50 +0100
"Jonas B. Nielsen" <[EMAIL PROTECTED]> wrote:

>       time_ok( sub { sleep(1); }, 5, 'Passing test' );
> 
>       time_ok( sub { sleep(10); }, 5, 'Non-passing test' );
...
> So my question is: is this module already on CPAN, or something  
> similar (better), or should I put mine there?

That looks almost exactly like something I wanted, and ended up hacking
up using Time::HiRes:

$now = time;
$set->loop_once( 2 );
$took = time - $now;

cmp_ok( $took, '>', 1.9, 'loop_once(2) while waiting takes at least 1.9 
seconds' );
cmp_ok( $took, '<', 2.1, 'loop_once(2) while waiting takes no more than 2.1 
seconds' );

It would be nice to have a native Test:: primitive for that.

Consider also

     time_atleast( sub { sleep(5); }, 3, 'sleep(5) takes at least 3
                                          seconds' );

     time_atmost( sub { sleep(5); }, 6,  'sleep(5) takes no more than
                                          6 seconds' );

Consider also whether to handle $SIG{ALRM} and call alarm( $timeout +
2 ), say... so you could throw a wobbly if the test times out
completely. Though beware that sleep() might be implemented using
alarm() on some platforms. Perhaps make it a tunable variable:

  $Test::Time::ALRM = 1;
  time_ok( sub { while(1) { } }, 1, 'while loops stop' );

I would hope that will complain after 2 seconds or so.

Be slightly careful of timing imprecision though - I decided to pull in
Time::HiRes for my specific test, but I don't know whether a generic
"Test::Time" module could justify that.

-- 
Paul "LeoNerd" Evans

[EMAIL PROTECTED]
ICQ# 4135350       |  Registered Linux# 179460
http://www.leonerd.org.uk/

Reply via email to