File Coverage

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


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