line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::AnyEvent::Time; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
147131
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
113
|
|
4
|
3
|
|
|
3
|
|
16
|
use strict; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
116
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
16
|
use base ('Exporter'); |
|
3
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
351
|
|
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
1829
|
use AnyEvent; |
|
3
|
|
|
|
|
6459
|
|
|
3
|
|
|
|
|
108
|
|
9
|
3
|
|
|
3
|
|
23
|
use Scalar::Util qw(looks_like_number); |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
432
|
|
10
|
3
|
|
|
3
|
|
19
|
use Test::Builder; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
2255
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our @EXPORT = qw(time_within_ok time_cmp_ok time_between_ok elapsed_time); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my $Tester = Test::Builder->new(); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
## ** borrowed from Test::Exception |
18
|
|
|
|
|
|
|
sub import { |
19
|
3
|
|
|
3
|
|
26
|
my $self = shift; |
20
|
3
|
50
|
|
|
|
16
|
if( @_ ) { |
21
|
0
|
|
|
|
|
0
|
my $package = caller; |
22
|
0
|
|
|
|
|
0
|
$Tester->exported_to($package); |
23
|
0
|
|
|
|
|
0
|
$Tester->plan( @_ ); |
24
|
|
|
|
|
|
|
} |
25
|
3
|
|
|
|
|
972
|
$self->export_to_level( 1, $self, $_ ) foreach @EXPORT; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 NAME |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
Test::AnyEvent::Time - Time-related tests for asynchronous routines using AnyEvent |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head1 VERSION |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
Version 0.01 |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=cut |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub elapsed_time { |
46
|
43
|
|
|
43
|
1
|
13472
|
my ($cb, $timeout) = @_; |
47
|
43
|
100
|
|
|
|
152
|
if(!defined($cb)) { |
48
|
1
|
|
|
|
|
7
|
return undef; |
49
|
|
|
|
|
|
|
} |
50
|
42
|
100
|
|
|
|
162
|
if(ref($cb) ne 'CODE') { |
51
|
1
|
|
|
|
|
9
|
return undef; |
52
|
|
|
|
|
|
|
} |
53
|
41
|
|
|
|
|
1406
|
my $cv = AE::cv; |
54
|
41
|
|
|
|
|
773
|
my $w; |
55
|
41
|
|
|
|
|
82
|
my $timed_out = 0; |
56
|
41
|
100
|
|
|
|
142
|
if(defined($timeout)) { |
57
|
|
|
|
|
|
|
$w = AE::timer $timeout, 0, sub { |
58
|
23
|
|
|
23
|
|
7251852
|
undef $w; |
59
|
23
|
|
|
|
|
80
|
$timed_out = 1; |
60
|
23
|
|
|
|
|
208
|
$cv->send(); |
61
|
25
|
|
|
|
|
814
|
}; |
62
|
|
|
|
|
|
|
} |
63
|
41
|
|
|
|
|
2441
|
my $before = AE::now; |
64
|
41
|
|
|
|
|
648
|
$cb->($cv); |
65
|
41
|
|
|
|
|
9127
|
$cv->recv(); |
66
|
41
|
100
|
|
|
|
15404172
|
if($timed_out) { |
67
|
13
|
|
|
|
|
92
|
return -1; |
68
|
|
|
|
|
|
|
} |
69
|
28
|
|
|
|
|
2718
|
return (AE::now - $before); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub time_cmp_ok { |
73
|
40
|
|
|
40
|
1
|
68782
|
my ($cb, $op, $cmp_time, $timeout, $desc) = @_; |
74
|
40
|
100
|
100
|
|
|
407
|
if(!defined($desc) && defined($timeout) && !looks_like_number($timeout)) { |
|
|
|
100
|
|
|
|
|
75
|
5
|
|
|
|
|
9
|
$desc = $timeout; |
76
|
5
|
|
|
|
|
10
|
$timeout = undef; |
77
|
|
|
|
|
|
|
} |
78
|
40
|
100
|
100
|
|
|
429
|
if(!defined($op) || !defined($cb) || !defined($cmp_time) || ref($cb) ne "CODE") { |
|
|
|
100
|
|
|
|
|
79
|
10
|
|
|
|
|
38
|
$Tester->ok(0, $desc); |
80
|
10
|
|
|
|
|
5730
|
$Tester->diag("Invalid arguments."); |
81
|
10
|
|
|
|
|
880
|
return 0; |
82
|
|
|
|
|
|
|
} |
83
|
30
|
|
|
|
|
96
|
my $time = elapsed_time($cb, $timeout); |
84
|
30
|
50
|
|
|
|
754
|
if(!defined($time)) { |
|
|
100
|
|
|
|
|
|
85
|
0
|
|
|
|
|
0
|
$Tester->ok(0, $desc); |
86
|
0
|
|
|
|
|
0
|
$Tester->diag("Invalid arguments."); |
87
|
0
|
|
|
|
|
0
|
return 0; |
88
|
|
|
|
|
|
|
}elsif($time < 0) { |
89
|
8
|
|
|
|
|
80
|
$Tester->ok(0, $desc); |
90
|
8
|
|
|
|
|
6392
|
$Tester->diag("Timeout ($timeout sec)"); |
91
|
8
|
|
|
|
|
725
|
return 0; |
92
|
|
|
|
|
|
|
}else { |
93
|
22
|
|
|
|
|
191
|
return $Tester->cmp_ok($time, $op, $cmp_time, $desc); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub time_between_ok { |
98
|
6
|
|
|
6
|
1
|
16474
|
my ($cb, $min_time, $max_time, $desc) = @_; |
99
|
6
|
|
|
|
|
12
|
local $Test::Builder::Level = $Test::Builder::Level + 1; |
100
|
6
|
|
|
|
|
20
|
return time_cmp_ok($cb, '>', $min_time, $max_time, $desc); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub time_within_ok { |
104
|
7
|
|
|
7
|
1
|
16858
|
my ($cb, $time, $desc) = @_; |
105
|
7
|
|
|
|
|
19
|
local $Test::Builder::Level = $Test::Builder::Level + 1; |
106
|
7
|
|
|
|
|
26
|
return time_cmp_ok($cb, '<', $time, $time, $desc); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
1; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
__END__ |