Hi Paul,
See below,
On 03/03/2007, at 16.40, Paul LeoNerd Evans wrote:
On Sat, 3 Mar 2007 16:17:59 +0100
"Jonas B. Nielsen" <[EMAIL PROTECTED]> wrote:
Thanks for you feedback, but you have to explain your thoughts to me
a bit more explicitly.
I am standing at the Copenhagen Perl Mongers booth at LinuxForum in
Copenhagen and yes I am a bit slow too.
As I see your two suggestions for methods, time_atmost is what would
be another name for the sub I have already implemented (time_ok and
its evil twin time_nok).
As for time_atmost, this one troubles me a bit, as I see it would be
fun to include, but it's use is somewhat vague to me :)
You mention time_atmost there twice - I think one of those is a
typo, but
I can't see which one :)
Yes a typo, the latter should read time_atleast - I did not
understand the purpose of this test to begin with, but since then I
have seen a need for it, it just needed some time to dig in.
I will add both primitives
I've had a bit more of a thought, and it now looks like this in my
head:
time_between( $code, $atleast, $atmost, $message );
# Check that $code->() takes at least $atleast seconds, and no
more
# than $atmost. Optionally install an alarm handler at, say,
# $atmost + 2 seconds (perhaps that 2 is configurable).
Then the other primitives fall out almost trivially:
time_atleast( $code, $atleast, $message ) ==
time_between( $code, $atleast, undef, $message );
time_atmost( $code, $atmost, $message ) ==
time_between( $code, 0, $atmost, $message );
time_ok( $code, $time, $message ) ==
time_between( $code, $time - $delta, $time + $delta, $message );
where $delta is some other configurable. Perhaps detect if
Time::HiRes is
loaded, and use $delta = 0.1 if it is, and 1 if it isn't..? Random
thoughts..
Well I have adapted the approach outlines by Gabor Szabo, so if the
second parameter is a reference to an array you can specify a lower
and upper threshold - so the time_between primitive is left out of
now - unless we need it as syntactic sugar - well I have added it
now, then I just need the tests...
Be careful of subtle races - remember what e.g. sleep guarantees.
sleep(5) for example, waits at least 4 seconds and no more than 5.
Currently the software is alpha and putting it to use should
visualize some of the pitfalls and caveats should show their ugly
faces when the distribution in made public.
I am currently only supporting seconds, integration with Time::Hires
or similar is on the TODO list.
Your suggestion for a use of $SIG{ALRM} is very good and will be
implemented prior to the first release, since it seems very essential
and addresses a problem I did not see coming, good thing we have this
list.
It also occurs to me, that if we can't use SIGALRM, then maybe the
following might be better:
my $pid = $$;
my $kid;
if( ( $kid = fork() ) == 0 ) {
# child
sleep( $timeout );
kill SIGTERM, $pid;
exit( 0 );
}
$code->();
kill SIGKILL, $kid;
This way, $code->() isn't affected by SIGALRM.
Currently I have implemented the use of $SIG{ALRM} as proposed by you
and it works, I have however not tested it with several alarms being
defined, I use a localized alarm and it works as expected for now.
--
Paul "LeoNerd" Evans
[EMAIL PROTECTED]
ICQ# 4135350 | Registered Linux# 179460
http://www.leonerd.org.uk/
Thanks for all the marvellous output
jonasbn