File Coverage

blib/lib/Test/MockTime/HiRes.pm
Criterion Covered Total %
statement 72 73 98.6
branch 16 18 88.8
condition n/a
subroutine 20 21 95.2
pod 0 3 0.0
total 108 115 93.9


line stmt bran cond sub pod time code
1             package Test::MockTime::HiRes;
2 4     4   256050 use strict;
  4         35  
  4         117  
3 4     4   18 use warnings;
  4         4  
  4         82  
4              
5             # cpan
6 4     4   15 use Test::More;
  4         8  
  4         29  
7 4     4   2194 use Test::MockTime qw(:all);
  4         2837  
  4         537  
8 4     4   1713 use Time::HiRes;
  4         4761  
  4         19  
9              
10             # core
11 4     4   397 use Exporter qw(import);
  4         9  
  4         334  
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.08';
21              
22             my $datetime_was_loaded;
23              
24             BEGIN {
25 4     4   26 no warnings 'redefine';
  4         6  
  4         1998  
26 4     4   22 my $_time_original = \&Test::MockTime::_time;
27             *Test::MockTime::_time = sub {
28 100     100   1063 my ($time, $spec) = @_;
29 100         122 my $usec = 0;
30 100 100       788 ($time, $usec) = ($1, $2) if $time =~ /\A(\d+)[.](\d+)\z/;
31 100         217 $time = $_time_original->($time, $spec);
32 100 100       912 $time = "$time.$usec" if $usec;
33 100         194 return $time;
34 4         24 };
35              
36             *CORE::GLOBAL::sleep = sub ($) {
37 3     1   118 return int(Test::MockTime::HiRes::_sleep($_[0], sub {CORE::sleep $_[0]}));
  1         93  
38 4         15 };
39 4         7 my $hires_clock_gettime = \&Time::HiRes::clock_gettime;
40 4         13 my $hires_time = \&Time::HiRes::time;
41 4         7 my $hires_gettimeofday = \&Time::HiRes::gettimeofday;
42 4         8 my $hires_sleep = \&Time::HiRes::sleep;
43 4         7 my $hires_usleep = \&Time::HiRes::usleep;
44 4         6 my $hires_nanosleep = \&Time::HiRes::nanosleep;
45              
46             *Test::MockTime::time = sub () {
47 207     207   46343 return int(Test::MockTime::HiRes::time($hires_time));
48 4         14 };
49 4         43 *CORE::GLOBAL::time = \&Test::MockTime::time;
50              
51             *Time::HiRes::clock_gettime = sub (;$) {
52 0     0   0 return Test::MockTime::HiRes::time($hires_clock_gettime, @_);
53 4         20 };
54             *Time::HiRes::time = sub () {
55 210     210   8867 return Test::MockTime::HiRes::time($hires_time);
56 4         12 };
57             *Time::HiRes::gettimeofday = sub () {
58 6     6   42 return Test::MockTime::HiRes::gettimeofday($hires_gettimeofday);
59 4         15 };
60             *Time::HiRes::sleep = sub (;@) {
61 5     5   5036 return Test::MockTime::HiRes::_sleep($_[0], $hires_sleep);
62 4         12 };
63             *Time::HiRes::usleep = sub ($) {
64 1     1   4 return Test::MockTime::HiRes::_sleep($_[0], $hires_usleep, 1000_000);
65 4         13 };
66             *Time::HiRes::nanosleep = sub ($) {
67 1     1   4 return Test::MockTime::HiRes::_sleep($_[0], $hires_nanosleep, 1000_000_000);
68 4         12 };
69              
70 4 50       1131 $datetime_was_loaded = 1 if $INC{'DateTime.pm'};
71             }
72              
73             sub time ($;@) {
74 417     417 0 523 my $original = shift;
75 417 100       1522 defined $Test::MockTime::fixed ? $Test::MockTime::fixed : $original->(@_) + $Test::MockTime::offset;
76             }
77              
78             sub gettimeofday() {
79 6     6 0 11 my $original = shift;
80 6 100       24 if (defined $Test::MockTime::fixed) {
81 3 100       8 return wantarray ? do {
82 2         4 my $int_part = int($Test::MockTime::fixed);
83 2         22 ($int_part, 1_000_000 * sprintf('%.6f', ($Test::MockTime::fixed - $int_part)))
84             }: $Test::MockTime::fixed;
85             } else {
86 3         32 return $original->(@_);
87             }
88             };
89              
90             sub _sleep ($&;$) {
91 10     10   25 my ($sec, $original, $resolution) = @_;
92 10 100       30 if (defined $Test::MockTime::fixed) {
93 6 100       17 $sec /= $resolution if $resolution;
94 6         22 $Test::MockTime::fixed += $sec;
95 6         33 note "sleep $sec";
96 6         1738 return $sec;
97             } else {
98 4         500331 return $original->($sec);
99             }
100             }
101              
102             sub mock_time (&$) {
103 3     3 0 27 my ($code, $time) = @_;
104              
105 3 50       11 warn sprintf(
106             '%s does not affect DateTime->now since %s is loaded after DateTime',
107             'mock_time',
108             __PACKAGE__,
109             ) if $datetime_was_loaded;
110              
111 3         6 local $Test::MockTime::fixed = $time;
112 3         7 return $code->();
113             }
114              
115             1;
116             __END__