| 1 | package Test::AnyEvent::Time; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use base ('Exporter'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use AnyEvent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Test::Builder; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | our @EXPORT = qw(time_within_ok time_cmp_ok time_between_ok elapsed_time); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | my $Tester = Test::Builder->new(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | ## ** borrowed from Test::Exception | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 3 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | ▶ | if( @_ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | my $package = caller; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | $Tester->exported_to($package); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | $Tester->plan( @_ ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 3 | $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 | 3 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 43 | ▶ | sub elapsed_time { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
elapsed_time: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 43 | my ($cb, $timeout) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 43 | ▶ | if(!defined($cb)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 42 | ▶ | if(ref($cb) ne 'CODE') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 41 | my $cv = AE::cv; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 41 | my $w; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 41 | my $timed_out = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 41 | ▶ | if(defined($timeout)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 23 | ▶ | $w = AE::timer $timeout, 0, sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 23 | undef $w; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 23 | $timed_out = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 23 | $cv->send(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 25 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 41 | my $before = AE::now; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 41 | $cb->($cv); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 41 | $cv->recv(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 41 | ▶ | if($timed_out) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 13 | return -1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 28 | return (AE::now - $before); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 40 | ▶ | sub time_cmp_ok { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
time_cmp_ok: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 40 | my ($cb, $op, $cmp_time, $timeout, $desc) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 40 | ▶ | if(!defined($desc) && defined($timeout) && !looks_like_number($timeout)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not defined $desc and defined $timeout and not &looks_like_number($timeout)
not defined $desc and defined $timeout
MC/DC100%3/3
not defined $desc and defined $timeout and not &looks_like_number($timeout)
not defined $desc
defined $timeout
not &looks_like_number($timeout)
Truth table100%4/4
not(A) and B and not(C)
A= not defined $descB= defined $timeoutC= not &looks_like_number($timeout)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 5 | $desc = $timeout; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 5 | $timeout = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 40 | ▶ | if(!defined($op) || !defined($cb) || !defined($cmp_time) || ref($cb) ne "CODE") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
not defined $op && defined $cb && defined $cmp_time or ref $cb ne "CODE"
defined $op && defined $cb && defined $cmp_time
defined $op and defined $cb
MC/DC100%4/4
not defined $op && defined $cb && defined $cmp_time or ref $cb ne "CODE"
defined $op
defined $cb
defined $cmp_time
ref $cb ne "CODE"
Truth table100%5/5
not(A and B && C) or D
A= defined $opB= defined $cbC= defined $cmp_timeD= ref $cb ne "CODE"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 10 | $Tester->ok(0, $desc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 10 | $Tester->diag("Invalid arguments."); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 10 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 30 | my $time = elapsed_time($cb, $timeout); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 30 | ▶ | if(!defined($time)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | $Tester->ok(0, $desc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | $Tester->diag("Invalid arguments."); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | }elsif($time < 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 8 | $Tester->ok(0, $desc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 8 | $Tester->diag("Timeout ($timeout sec)"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 8 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | }else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 22 | return $Tester->cmp_ok($time, $op, $cmp_time, $desc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 6 | ▶ | sub time_between_ok { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
time_between_ok: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 6 | my ($cb, $min_time, $max_time, $desc) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 6 | local $Test::Builder::Level = $Test::Builder::Level + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 6 | return time_cmp_ok($cb, '>', $min_time, $max_time, $desc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 7 | ▶ | sub time_within_ok { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
time_within_ok: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 7 | my ($cb, $time, $desc) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 7 | local $Test::Builder::Level = $Test::Builder::Level + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 7 | return time_cmp_ok($cb, '<', $time, $time, $desc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||