From: Vincent Pit Date: Thu, 19 Mar 2015 18:06:27 +0000 (-0300) Subject: Add usleep feature X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=4598e3669eee23f67bac4a1dc541740b1447591b;p=perl%2Fmodules%2FVPIT-TestHelpers.git Add usleep feature --- diff --git a/lib/VPIT/TestHelpers.pm b/lib/VPIT/TestHelpers.pm index f448074..68f11f0 100644 --- a/lib/VPIT/TestHelpers.pm +++ b/lib/VPIT/TestHelpers.pm @@ -23,7 +23,9 @@ my %default_exports = ( skip_all => \&skip_all, ); -my %features = (); +my %features = ( + usleep => \&init_usleep, +); sub import { shift; @@ -153,6 +155,24 @@ sub run_perl { system { $^X } $^X, '-T', map("-I$_", @INC), '-e', $code; } +sub init_usleep { + my $usleep; + + if (do { local $@; eval { require Time::HiRes; 1 } }) { + defined and diag "Using usleep() from Time::HiRes $_" + for $Time::HiRes::VERSION; + $usleep = \&Time::HiRes::usleep; + } else { + diag 'Using fallback usleep()'; + $usleep = sub { + my $s = int($_[0] / 2.5e5); + sleep $s if $s; + }; + } + + return usleep => $usleep; +} + package VPIT::TestHelpers::Guard; sub new { diff --git a/t/50-usleep.t b/t/50-usleep.t new file mode 100644 index 0000000..4ed179e --- /dev/null +++ b/t/50-usleep.t @@ -0,0 +1,14 @@ +#!perl -T + +use strict; +use warnings; + +use VPIT::TestHelpers 'usleep'; + +use Test::More tests => 2; + +pass 'before usleep()'; + +usleep 100; + +pass 'after usleep()';