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