File Coverage

lib/Acme/Time/Constant.pm
Criterion Covered Total %
statement 32 32 100.0
branch 2 2 100.0
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 45 45 100.0


line stmt bran cond sub pod time code
1 3     3   39335 use 5.006; # Our
  3         10  
  3         104  
2 3     3   14 use strict;
  3         3  
  3         100  
3 3     3   23 use warnings;
  3         3  
  3         216  
4              
5             package Acme::Time::Constant;
6              
7             our $VERSION = '0.001004';
8              
9             # ABSTRACT: Run any code in constant time.
10              
11             our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12              
13 3     3   1424 use Sub::Exporter::Progressive -setup => { exports => [qw( constant_time )] };
  3         2711  
  3         26  
14              
15 3     3   2605 use Time::HiRes qw( gettimeofday );
  3         4368  
  3         13  
16 3     3   2016 use Time::Warp qw( to time );
  3         1611  
  3         290  
17              
18             my $old_import;
19 3     3   67 BEGIN { $old_import = \&import }
20              
21             {
22             ## no critic (TestingAndDebugging::ProhibitNoWarnings)
23 3     3   19 no warnings 'redefine';
  3         5  
  3         481  
24              
25             sub import {
26 2     2   43 *CORE::GLOBAL::time = *Time::Warp::time;
27 2         11 goto $old_import;
28             }
29             }
30              
31              
32              
33              
34              
35              
36              
37              
38             sub constant_time {
39 3     3 1 1366 my $nargs = ( my ( $time, $callback ) = @_ );
40              
41 3 100       10 if ( $nargs < 2 ) {
42 1         2 $callback = $time;
43 1         1 $time = 1;
44             }
45 3         7 my $now = time;
46 3         6 $callback->();
47 3         1095612 to( $now + $time );
48 3         8 return;
49             }
50              
51             1;
52              
53             __END__