line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Time::ETA::MockTime; |
2
|
|
|
|
|
|
|
$Time::ETA::MockTime::VERSION = '1.2.0'; |
3
|
|
|
|
|
|
|
# ABSTRACT: make it possible to test time |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
19706
|
use warnings; |
|
3
|
|
|
|
|
30
|
|
|
3
|
|
|
|
|
85
|
|
7
|
3
|
|
|
3
|
|
14
|
use strict; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
62
|
|
8
|
3
|
|
|
3
|
|
14
|
use Exporter qw(import); |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
96
|
|
9
|
3
|
|
|
3
|
|
14
|
use Time::HiRes qw(); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
58
|
|
10
|
3
|
|
|
3
|
|
14
|
use Carp; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
374
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
13
|
|
|
|
|
|
|
sleep |
14
|
|
|
|
|
|
|
usleep |
15
|
|
|
|
|
|
|
gettimeofday |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
our @EXPORT = @EXPORT_OK; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our @mocked_time = Time::HiRes::gettimeofday(); |
20
|
|
|
|
|
|
|
my $microseconds_in_second = 1_000_000; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
{ |
23
|
3
|
|
|
3
|
|
14
|
no strict 'refs'; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
81
|
|
24
|
3
|
|
|
3
|
|
12
|
no warnings 'redefine'; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
1246
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
my @packages_having_gettimeofday = grep {defined(&{$_ . '::gettimeofday'})} (map {s'\.pm''; s'/'::'g; $_} keys(%INC)), 'main'; |
27
|
|
|
|
|
|
|
*{$_ . '::gettimeofday'} = \&Time::ETA::MockTime::gettimeofday foreach @packages_having_gettimeofday; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub sleep { |
32
|
4
|
|
|
4
|
0
|
971
|
my ($seconds) = @_; |
33
|
|
|
|
|
|
|
|
34
|
4
|
50
|
|
|
|
28
|
croak "Incorrect seconds" if $seconds !~ /^[0-9]+$/; |
35
|
4
|
|
|
|
|
11
|
$mocked_time[0] += $seconds; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub usleep ($) { |
40
|
13
|
|
|
13
|
0
|
4190
|
my ($microseconds) = @_; |
41
|
|
|
|
|
|
|
|
42
|
13
|
50
|
|
|
|
116
|
croak "Incorrect microseconds" if $microseconds !~ /^[0-9]+$/; |
43
|
|
|
|
|
|
|
|
44
|
13
|
|
|
|
|
24
|
$mocked_time[1] += $microseconds; |
45
|
13
|
|
|
|
|
28
|
my $ms = $mocked_time[1] % $microseconds_in_second; |
46
|
13
|
|
|
|
|
26
|
my $remain = $mocked_time[1] - $ms; |
47
|
|
|
|
|
|
|
|
48
|
13
|
|
|
|
|
28
|
$mocked_time[0] += ($remain / $microseconds_in_second); |
49
|
13
|
|
|
|
|
29
|
$mocked_time[1] = $ms; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub gettimeofday () { |
54
|
129
|
50
|
|
129
|
0
|
2342
|
if (@mocked_time) { |
55
|
129
|
50
|
|
|
|
511
|
return wantarray ? @mocked_time : "$mocked_time[0].$mocked_time[1]"; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub set_mock_time { |
61
|
2
|
|
|
2
|
0
|
472
|
my ($sec, $ms) = @_; |
62
|
|
|
|
|
|
|
|
63
|
2
|
|
|
|
|
4
|
$mocked_time[0] = $sec; |
64
|
2
|
|
|
|
|
4
|
$mocked_time[1] = $ms; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
1; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
__END__ |