From: Vincent Pit Date: Mon, 20 Apr 2015 14:27:36 +0000 (-0300) Subject: Make sure the sleep() fallback implementation sleeps long enough X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=2a6ac0f1b934d60631de30d3f14a38667b389cad;p=perl%2Fmodules%2FVPIT-TestHelpers.git Make sure the sleep() fallback implementation sleeps long enough If the users asks for 500ms, it should sleep at least one second. --- diff --git a/lib/VPIT/TestHelpers.pm b/lib/VPIT/TestHelpers.pm index bdc3a4c..b7b7635 100644 --- a/lib/VPIT/TestHelpers.pm +++ b/lib/VPIT/TestHelpers.pm @@ -740,8 +740,9 @@ sub init_usleep { 'sleep' => sub { diag 'Using sleep()-based fallback usleep()'; return sub ($) { - my $s = int($_[0] / 1e6); - my $t = sleep $s; + my $ms = int $_[0]; + my $s = int($ms / 1e6) + ($ms % 1e6 == 0 ? 0 : 1); + my $t = sleep $s; return $t * 1e6; }; }, diff --git a/t/50-usleep.t b/t/50-usleep.t index 15592ec..217231d 100644 --- a/t/50-usleep.t +++ b/t/50-usleep.t @@ -9,6 +9,8 @@ use Test::More; my @impls = qw; +my $duration = 1e5; + for my $impl (@impls) { my $desc = "$impl-based usleep()"; { @@ -26,8 +28,8 @@ for my $impl (@impls) { eval 'defined &main::usleep'; }; ok $has_usleep, "$desc was imported"; - my $ret = usleep(1e6); - cmp_ok $ret, '>=', 1e6, "$desc did sleep"; + my $ret = usleep($duration); + cmp_ok $ret, '>=', $duration, "$desc did sleep"; diag "$desc actually slept $ret microseconds"; }