File Coverage

blib/lib/Test/MockTime/HiRes.pm
Criterion Covered Total %
statement 65 74 87.8
branch 11 18 61.1
condition n/a
subroutine 17 21 80.9
pod 0 3 0.0
total 93 116 80.1


line stmt bran cond sub pod time code
1             package Test::MockTime::HiRes;
2 3     3   71746 use strict;
  3         5  
  3         67  
3 3     3   8 use warnings;
  3         3  
  3         53  
4              
5             # cpan
6 3     3   7 use Test::More;
  3         6  
  3         11  
7 3     3   1554 use Test::MockTime qw(:all);
  3         1638  
  3         299  
8 3     3   1294 use Time::HiRes;
  3         2749  
  3         9  
9              
10             # core
11 3     3   221 use Exporter qw(import);
  3         3  
  3         158  
12             our @EXPORT = qw(
13             set_relative_time
14             set_absolute_time
15             set_fixed_time
16             restore_time
17             mock_time
18             );
19              
20             our $VERSION = '0.06';
21              
22             my $datetime_was_loaded;
23              
24             BEGIN {
25 3     3   12 no warnings 'redefine';
  3         3  
  3         1103  
26 3     3   6 my $_time_original = \&Test::MockTime::_time;
27             *Test::MockTime::_time = sub {
28 0     0   0 my ($time, $spec) = @_;
29 0         0 my $usec = 0;
30 0 0       0 ($time, $usec) = ($1, $2) if $time =~ /\A(\d+)[.](\d+)\z/;
31 0         0 $time = $_time_original->($time, $spec);
32 0 0       0 $time = "$time.$usec" if $usec;
33 0         0 return $time;
34 3         16 };
35 3         3 my $time_original = \&Test::MockTime::time;
36             *Test::MockTime::time = sub () {
37 6     6   2268 return int($time_original->());
38 3         13 };
39 3         6 *CORE::GLOBAL::time = \&Test::MockTime::time;
40              
41             *CORE::GLOBAL::sleep = sub ($) {
42 3     2   371 return int(Test::MockTime::HiRes::_sleep($_[0], sub {CORE::sleep $_[0]}));
  1         9  
43 3         10 };
44 3         14 my $hires_clock_gettime = \&Time::HiRes::clock_gettime;
45 3         5 my $hires_time = \&Time::HiRes::time;
46 3         3 my $hires_gettimeofday = \&Time::HiRes::gettimeofday;
47 3         643 my $hires_sleep = \&Time::HiRes::sleep;
48 3         47 my $hires_usleep = \&Time::HiRes::usleep;
49 3         4 my $hires_nanosleep = \&Time::HiRes::nanosleep;
50             *Time::HiRes::clock_gettime = sub (;$) {
51 0     0   0 return Test::MockTime::HiRes::time($hires_clock_gettime, @_);
52 3         10 };
53             *Time::HiRes::time = sub () {
54 8     8   7718 return Test::MockTime::HiRes::time($hires_time);
55 3         6 };
56             *Time::HiRes::gettimeofday = sub () {
57 4     4   28 return Test::MockTime::HiRes::gettimeofday($hires_gettimeofday);
58 3         7 };
59             *Time::HiRes::sleep = sub (;@) {
60 3     3   20 return Test::MockTime::HiRes::_sleep($_[0], $hires_sleep);
61 3         6 };
62             *Time::HiRes::usleep = sub ($) {
63 0     0   0 return Test::MockTime::HiRes::_sleep($_[0], $hires_usleep, 1000);
64 3         6 };
65             *Time::HiRes::nanosleep = sub ($) {
66 0     0   0 return Test::MockTime::HiRes::_sleep($_[0], $hires_nanosleep, 1000_000);
67 3         6 };
68              
69 3 50       591 $datetime_was_loaded = 1 if $INC{'DateTime.pm'};
70             }
71              
72             sub time (&;@) {
73 8     8 0 8 my $original = shift;
74 8 100       59 defined $Test::MockTime::fixed ? $Test::MockTime::fixed : $original->(@_) + $Test::MockTime::offset;
75             }
76              
77             sub gettimeofday() {
78 4     4 0 7 my $original = shift;
79 4 100       11 if (defined $Test::MockTime::fixed) {
80 2 100       5 return wantarray ? do {
81 1         2 my $int_part = int($Test::MockTime::fixed);
82 1         4 ($int_part, 1_000_000 * ($Test::MockTime::fixed - $int_part))
83             }: $Test::MockTime::fixed;
84             } else {
85 2         15 return $original->(@_);
86             }
87             };
88              
89             sub _sleep ($&;$) {
90 6     6   9 my ($sec, $original, $resolution) = @_;
91 6 100       13 if (defined $Test::MockTime::fixed) {
92 3 50       10 $sec /= $resolution if $resolution;
93 3         5 $Test::MockTime::fixed += $sec;
94 3         11 note "sleep $sec";
95 3         109 return $sec;
96             } else {
97 3         200299 return $original->($sec);
98             }
99             }
100              
101             sub mock_time (&$) {
102 3     3 0 26 my ($code, $time) = @_;
103              
104 3 50       8 warn sprintf(
105             '%s does not affect DateTime->now since %s is loaded after DateTime',
106             'mock_time',
107             __PACKAGE__,
108             ) if $datetime_was_loaded;
109              
110 3         5 local $Test::MockTime::fixed = $time;
111 3         19 return $code->();
112             }
113              
114             1;
115             __END__