File Coverage

blib/lib/Test/MockTime/HiRes.pm
Criterion Covered Total %
statement 67 74 90.5
branch 12 18 66.6
condition n/a
subroutine 19 21 90.4
pod 0 3 0.0
total 98 116 84.4


line stmt bran cond sub pod time code
1             package Test::MockTime::HiRes;
2 3     3   71192 use strict;
  3         7  
  3         69  
3 3     3   14 use warnings;
  3         6  
  3         62  
4              
5             # cpan
6 3     3   13 use Test::More;
  3         6  
  3         15  
7 3     3   1558 use Test::MockTime qw(:all);
  3         1791  
  3         317  
8 3     3   1871 use Time::HiRes;
  3         2727  
  3         12  
9              
10             # core
11 3     3   270 use Exporter qw(import);
  3         6  
  3         166  
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.07';
21              
22             my $datetime_was_loaded;
23              
24             BEGIN {
25 3     3   16 no warnings 'redefine';
  3         6  
  3         1141  
26 3     3   13 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         22 };
35 3         8 my $time_original = \&Test::MockTime::time;
36             *Test::MockTime::time = sub () {
37 6     6   3287 return int($time_original->());
38 3         12 };
39 3         11 *CORE::GLOBAL::time = \&Test::MockTime::time;
40              
41             *CORE::GLOBAL::sleep = sub ($) {
42 3     2   586 return int(Test::MockTime::HiRes::_sleep($_[0], sub {CORE::sleep $_[0]}));
  1         92  
43 3         11 };
44 3         13 my $hires_clock_gettime = \&Time::HiRes::clock_gettime;
45 3         7 my $hires_time = \&Time::HiRes::time;
46 3         4 my $hires_gettimeofday = \&Time::HiRes::gettimeofday;
47 3         7 my $hires_sleep = \&Time::HiRes::sleep;
48 3         4 my $hires_usleep = \&Time::HiRes::usleep;
49 3         6 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 10     10   6222 return Test::MockTime::HiRes::time($hires_time);
55 3         15 };
56             *Time::HiRes::gettimeofday = sub () {
57 6     6   75 return Test::MockTime::HiRes::gettimeofday($hires_gettimeofday);
58 3         9 };
59             *Time::HiRes::sleep = sub (;@) {
60 5     5   4570 return Test::MockTime::HiRes::_sleep($_[0], $hires_sleep);
61 3         11 };
62             *Time::HiRes::usleep = sub ($) {
63 1     1   7 return Test::MockTime::HiRes::_sleep($_[0], $hires_usleep, 1000_000);
64 3         14 };
65             *Time::HiRes::nanosleep = sub ($) {
66 1     1   4 return Test::MockTime::HiRes::_sleep($_[0], $hires_nanosleep, 1000_000_000);
67 3         9 };
68              
69 3 50       633 $datetime_was_loaded = 1 if $INC{'DateTime.pm'};
70             }
71              
72             sub time (&;@) {
73 10     10 0 24 my $original = shift;
74 10 100       73 defined $Test::MockTime::fixed ? $Test::MockTime::fixed : $original->(@_) + $Test::MockTime::offset;
75             }
76              
77             sub gettimeofday() {
78 6     6 0 16 my $original = shift;
79 6 100       23 if (defined $Test::MockTime::fixed) {
80 3 100       9 return wantarray ? do {
81 2         4 my $int_part = int($Test::MockTime::fixed);
82 2         30 ($int_part, 1_000_000 * sprintf('%.6f', ($Test::MockTime::fixed - $int_part)))
83             }: $Test::MockTime::fixed;
84             } else {
85 3         44 return $original->(@_);
86             }
87             };
88              
89             sub _sleep ($&;$) {
90 10     10   32 my ($sec, $original, $resolution) = @_;
91 10 100       29 if (defined $Test::MockTime::fixed) {
92 6 100       23 $sec /= $resolution if $resolution;
93 6         14 $Test::MockTime::fixed += $sec;
94 6         36 note "sleep $sec";
95 6         349 return $sec;
96             } else {
97 4         500388 return $original->($sec);
98             }
99             }
100              
101             sub mock_time (&$) {
102 3     3 0 34 my ($code, $time) = @_;
103              
104 3 50       11 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         19 local $Test::MockTime::fixed = $time;
111 3         11 return $code->();
112             }
113              
114             1;
115             __END__