blib/lib/IO/Socket/Timeout.pm

filter: Statementstmt 93.7% 120 / 128 Branchbran 63.8% 23 / 36 Conditioncond 58.1% 25 / 43 MC/DCmcdc 30.0% 6 / 20 Subroutinesub 86.2% 25 / 29 Podpod 100% 1 / 1 totaltotal 77.8% 200 / 257 CC 35 SCAR 14.2
CC
35
Cov
81%
enable_timeouts_on
7.7
__ANON__
7.3
_set_sock_opt
6.4
CRAP 4.2
↑ Summary Build »
1 #
2 # This file is part of IO-Socket-Timeout
3 #
4 # This software is copyright (c) 2013 by Damien "dams" Krotkine.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 package IO::Socket::Timeout;
10 11 $IO::Socket::Timeout::VERSION = '0.29';
11 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 # ABSTRACT: IO::Socket with read/write timeout
18  
19  
20 sub import {
21 10 ▶ shift;
Subroutine import: called CC=2 SCAR=6.9
22 10 foreach (@_) {
23 2 _create_composed_class( $_, 'IO::Socket::Timeout::Role::SetSockOpt');
24 2 _create_composed_class( $_, 'IO::Socket::Timeout::Role::PerlIO');
25 }
26 }
27  
28  
29 sub enable_timeouts_on {
30 31 ▶ my ($class, $socket) = @_;
Subroutine enable_timeouts_on: called CC=7 SCAR=20.4
Pod documented
31 31 ▶ defined $socket
Branch: 1 branch
BranchTrueFalseTotal
defined $socket or return 0 31 31
32 or return;
33 31 ▶ $socket->isa('IO::Socket')
Branch: 1 branch
BranchTrueFalseTotal
$socket->isa("IO::Socket") or croak("make_timeouts_aware can be used only on instances that inherit from IO::Socket") 0 31 31
34 or croak 'make_timeouts_aware can be used only on instances that inherit from IO::Socket';
35  
36 31 my $osname = $Config{osname};
37 31 ▶ if ( ! $ENV{PERL_IO_SOCKET_TIMEOUT_FORCE_SELECT}
Branch: 1 branch
BranchTrueFalseTotal
if (not $ENV{'PERL_IO_SOCKET_TIMEOUT_FORCE_SELECT'} and $osname eq 'darwin' || $osname eq 'linux' || $osname eq 'freebsd') { } 28 3 31
Condition44%4/9
not $ENV{"PERL_IO_SOCKET_TIMEOUT_FORCE_SELECT"} and $osname eq "darwin" || $osname eq "linux" || $osname eq "freebsd"
!ll&&!rl&&r
3028
$osname eq "darwin" or $osname eq "linux" or $osname eq "freebsd"
l!l&&r!l&&!r
2800
$osname eq "darwin" or $osname eq "linux"
l!l&&r!l&&!r
0280
MC/DC25%1/4
not $ENV{"PERL_IO_SOCKET_TIMEOUT_FORCE_SELECT"} and $osname eq "darwin" || $osname eq "linux" || $osname eq "freebsd"
not $ENV{"PERL_IO_SOCKET_TIMEOUT_FORCE_SELECT"} $osname eq "darwin" $osname eq "linux" $osname eq "freebsd"
Truth table40%2/5
not(A) and B or C or D
A= not $ENV{"PERL_IO_SOCKET_TIMEOUT_FORCE_SELECT"}B= $osname eq "darwin"C= $osname eq "linux"D= $osname eq "freebsd"
ABCDresult
0XXX0
10000
11XX1
101X1
10011
38 && ( $osname eq 'darwin' || $osname eq 'linux' || $osname eq 'freebsd' ) ) {
39 28 _compose_roles($socket, 'IO::Socket::Timeout::Role::SetSockOpt');
40 } else {
41 3 require PerlIO::via::Timeout;
42 3 ▶ binmode($socket, ':via(Timeout)');
Subroutine BEGIN: called CC=1 SCAR=0.0
43 3 _compose_roles($socket, 'IO::Socket::Timeout::Role::PerlIO');
44 }
45  
46 31 $socket->enable_timeout;
47 31 return $socket;
48 }
49  
50 sub _create_composed_class {
51 35 ▶ my ($class, @roles) = @_;
Subroutine _create_composed_class: called CC=2 SCAR=6.9
52 35 my $composed_class = $class . '__with__' . join('__and__', @roles);
53 35 my $path = $composed_class; $path =~ s|::|/|g; $path .= '.pm';
54 35 ▶ if ( ! exists $INC{$path}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $INC{$path}) 13 22 35
55 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
56 13 *{"${composed_class}::ISA"} = [ $class, @roles ];
57 13 $INC{$path} = __FILE__;
58 }
59 35 return $composed_class;
60 }
61  
62 sub _compose_roles {
63 31 ▶ my ($instance, @roles) = @_;
Subroutine _compose_roles: called CC=1 SCAR=0.0
64 31 bless $instance, _create_composed_class(ref $instance, @roles);
65 }
66  
67 # sysread FILEHANDLE,SCALAR,LENGTH,OFFSET
68 BEGIN {
69 11 ▶ my $osname = $Config{osname};
Subroutine BEGIN: called CC=5 SCAR=16.5
70 11 ▶ if ( $ENV{PERL_IO_SOCKET_TIMEOUT_FORCE_SELECT} ||
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"PERL_IO_SOCKET_TIMEOUT_FORCE_SELECT"} or $osname ne "darwin" and $osname ne "linux" and $osname ne "freebsd") 1 10 11
Condition44%4/9
$ENV{"PERL_IO_SOCKET_TIMEOUT_FORCE_SELECT"} or $osname ne "darwin" and $osname ne "linux" and $osname ne "freebsd"
l!l&&r!l&&!r
1010
$osname ne "darwin" and $osname ne "linux" and $osname ne "freebsd"
!ll&&!rl&&r
1000
$osname ne "darwin" and $osname ne "linux"
!ll&&!rl&&r
0100
MC/DC0%0/4
$ENV{"PERL_IO_SOCKET_TIMEOUT_FORCE_SELECT"} or $osname ne "darwin" and $osname ne "linux" and $osname ne "freebsd"
$ENV{"PERL_IO_SOCKET_TIMEOUT_FORCE_SELECT"} $osname ne "darwin" $osname ne "linux" $osname ne "freebsd"
Truth table20%1/5
A or B and C and D
A= $ENV{"PERL_IO_SOCKET_TIMEOUT_FORCE_SELECT"}B= $osname ne "darwin"C= $osname ne "linux"D= $osname ne "freebsd"
ABCDresult
1XXX1
01111
00XX0
010X0
01100
71 $osname ne 'darwin' && $osname ne 'linux' && $osname ne 'freebsd'
72 ) {
73 # this variable avoids infinite recursion, because
74 # PerlIO::via::Timeout->READ calls sysread.
75 1 my $_prevent_deep_recursion;
76 *CORE::GLOBAL::sysread = sub {
77 31 ▶ my $args_count = scalar(@_);
Subroutine __ANON__: called CC=5 SCAR=16.1
78 31 ▶ $_prevent_deep_recursion
Branch: 2 branches
BranchTrueFalseTotal
$_prevent_deep_recursion || !PerlIO::via::Timeout::has_timeout_layer($_[0]) || !PerlIO::via::Timeout::timeout_enabled($_[0]) and return $args_count == 4 ? sysread($_[0], $_[1], $_[2], $_[3]) : sysread($_[0], $_[1], $_[2]) 2 15 17
$args_count == 4 ? : 14 2 16
Condition83%5/6
$_prevent_deep_recursion or not PerlIO::via::Timeout::has_timeout_layer($_[0]) or not PerlIO::via::Timeout::timeout_enabled($_[0])
l!l&&r!l&&!r
16015
$_prevent_deep_recursion or not PerlIO::via::Timeout::has_timeout_layer($_[0])
l!l&&r!l&&!r
14215
MC/DC66%2/3
$_prevent_deep_recursion or not PerlIO::via::Timeout::has_timeout_layer($_[0]) or not PerlIO::via::Timeout::timeout_enabled($_[0])
$_prevent_deep_recursion not PerlIO::via::Timeout::has_timeout_layer($_[0]) not PerlIO::via::Timeout::timeout_enabled($_[0])
Truth table75%3/4
A or not(B) or not(C)
A= $_prevent_deep_recursionB= not PerlIO::via::Timeout::has_timeout_layer($_[0])C= not PerlIO::via::Timeout::timeout_enabled($_[0])
ABCresult
1XX1
01X1
0011
0000
79 || ! PerlIO::via::Timeout::has_timeout_layer($_[0])
80 || ! PerlIO::via::Timeout::timeout_enabled($_[0])
81 and return ( $args_count == 4 ? CORE::sysread($_[0], $_[1], $_[2], $_[3])
82 : CORE::sysread($_[0], $_[1], $_[2])
83 );
84 15 $_prevent_deep_recursion = 1;
85 15 my $ret_val = PerlIO::via::Timeout->READ($_[1], $_[2], $_[0]);
86 15 $_prevent_deep_recursion = 0;
87 15 return $ret_val;
88 }
89 1 }
90 }
91  
92 # syswrite FILEHANDLE,SCALAR,LENGTH,OFFSET
93 BEGIN {
94 11 ▶ my $osname = $Config{osname};
Subroutine BEGIN: called CC=5 SCAR=17.8
95 11 ▶ if ( $ENV{PERL_IO_SOCKET_TIMEOUT_FORCE_SELECT} ||
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"PERL_IO_SOCKET_TIMEOUT_FORCE_SELECT"} or $osname ne "darwin" and $osname ne "linux" and $osname ne "freebsd") 1 10 11
Condition44%4/9
$ENV{"PERL_IO_SOCKET_TIMEOUT_FORCE_SELECT"} or $osname ne "darwin" and $osname ne "linux" and $osname ne "freebsd"
l!l&&r!l&&!r
1010
$osname ne "darwin" and $osname ne "linux" and $osname ne "freebsd"
!ll&&!rl&&r
1000
$osname ne "darwin" and $osname ne "linux"
!ll&&!rl&&r
0100
MC/DC0%0/4
$ENV{"PERL_IO_SOCKET_TIMEOUT_FORCE_SELECT"} or $osname ne "darwin" and $osname ne "linux" and $osname ne "freebsd"
$ENV{"PERL_IO_SOCKET_TIMEOUT_FORCE_SELECT"} $osname ne "darwin" $osname ne "linux" $osname ne "freebsd"
Truth table20%1/5
A or B and C and D
A= $ENV{"PERL_IO_SOCKET_TIMEOUT_FORCE_SELECT"}B= $osname ne "darwin"C= $osname ne "linux"D= $osname ne "freebsd"
ABCDresult
1XXX1
01111
00XX0
010X0
01100
96 $osname ne 'darwin' && $osname ne 'linux' && $osname ne 'freebsd'
97 ) {
98 # this variable avoids infinite recursion, because
99 # PerlIO::via::Timeout->WRITE calls syswrite.
100 1 my $_prevent_deep_recursion;
101 *CORE::GLOBAL::syswrite = sub {
102 12 ▶ my $args_count = scalar(@_);
Subroutine __ANON__: called CC=6 SCAR=19.9
103 12 ▶ $_prevent_deep_recursion
Branch: 3 branches
BranchTrueFalseTotal
$_prevent_deep_recursion || !PerlIO::via::Timeout::has_timeout_layer($_[0]) || !PerlIO::via::Timeout::timeout_enabled($_[0]) and return $args_count == 4 ? syswrite($_[0], $_[1], $_[2], $_[3]) : ($args_count == 3 ? syswrite($_[0], $_[1], $_[2]) : syswrite($_[0], $_[1])) 0 6 6
$args_count == 4 ? : 6 0 6
$args_count == 3 ? : 0 0 0
Condition66%4/6
$_prevent_deep_recursion or not PerlIO::via::Timeout::has_timeout_layer($_[0]) or not PerlIO::via::Timeout::timeout_enabled($_[0])
l!l&&r!l&&!r
606
$_prevent_deep_recursion or not PerlIO::via::Timeout::has_timeout_layer($_[0])
l!l&&r!l&&!r
606
MC/DC33%1/3
$_prevent_deep_recursion or not PerlIO::via::Timeout::has_timeout_layer($_[0]) or not PerlIO::via::Timeout::timeout_enabled($_[0])
$_prevent_deep_recursion not PerlIO::via::Timeout::has_timeout_layer($_[0]) not PerlIO::via::Timeout::timeout_enabled($_[0])
Truth table50%2/4
A or not(B) or not(C)
A= $_prevent_deep_recursionB= not PerlIO::via::Timeout::has_timeout_layer($_[0])C= not PerlIO::via::Timeout::timeout_enabled($_[0])
ABCresult
1XX1
01X1
0011
0000
104 || ! PerlIO::via::Timeout::has_timeout_layer($_[0])
105 || ! PerlIO::via::Timeout::timeout_enabled($_[0])
106 and return( $args_count == 4 ? CORE::syswrite($_[0], $_[1], $_[2], $_[3])
107 : $args_count == 3 ? CORE::syswrite($_[0], $_[1], $_[2])
108 : CORE::syswrite($_[0], $_[1])
109 );
110 6 $_prevent_deep_recursion = 1;
111 6 my $ret_val = PerlIO::via::Timeout->WRITE($_[1], $_[0]);
112 6 $_prevent_deep_recursion = 0;
113 6 return $ret_val;
114 }
115 1 }
116 }
117  
118 package IO::Socket::Timeout::Role::SetSockOpt;
119 11 $IO::Socket::Timeout::Role::SetSockOpt::VERSION = '0.29';
120 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
121 11 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
122  
123 sub _check_attributes {
124 41 ▶ my ($self) = @_;
Subroutine _check_attributes: called CC=2 SCAR=7.0
125 41 ▶ grep { $_ < 0 } grep { defined } map { ${*$self}{$_} } qw(ReadTimeout WriteTimeout)
Branch: 1 branch
BranchTrueFalseTotal
grep {$_ < 0;} grep({defined $_;} map({do { *$self }->{$_};} "ReadTimeout", "WriteTimeout")) and croak("if defined, 'ReadTimeout' and 'WriteTimeout' attributes should be >= 0") 0 41 41
126 and croak "if defined, 'ReadTimeout' and 'WriteTimeout' attributes should be >= 0";
127 }
128  
129 sub read_timeout {
130 21 ▶ my ($self) = @_;
Subroutine read_timeout: called CC=2 SCAR=7.0
131 21 ▶ @_ > 1 and ${*$self}{ReadTimeout} = $_[1], $self->_check_attributes, $self->_set_sock_opt;
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 and do { *$self }->{"ReadTimeout"} = $_[1], $self->_check_attributes, $self->_set_sock_opt 21 0 21
132 21 ${*$self}{ReadTimeout}
133 21 }
134  
135 sub write_timeout {
136 20 ▶ my ($self) = @_;
Subroutine write_timeout: called CC=2 SCAR=7.0
137 20 ▶ @_ > 1 and ${*$self}{WriteTimeout} = $_[1], $self->_check_attributes, $self->_set_sock_opt;
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 and do { *$self }->{"WriteTimeout"} = $_[1], $self->_check_attributes, $self->_set_sock_opt 20 0 20
138 20 ${*$self}{WriteTimeout}
139 20 }
140  
141 28 ▶sub enable_timeout { $_[0]->timeout_enabled(1) }
Subroutine enable_timeout: called CC=1 SCAR=0.0
142 0 ▶sub disable_timeout { $_[0]->timeout_enabled(0) }
Subroutine disable_timeout: not called CC=1 SCAR=6.9
143 sub timeout_enabled {
144 28 ▶ my ($self) = @_;
Subroutine timeout_enabled: called CC=2 SCAR=7.0
145 28 ▶ @_ > 1 and ${*$self}{TimeoutEnabled} = !!$_[1], $self->_set_sock_opt;
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 and do { *$self }->{"TimeoutEnabled"} = !!$_[1], $self->_set_sock_opt 28 0 28
146 28 ${*$self}{TimeoutEnabled}
147 28 }
148  
149 sub _set_sock_opt {
150 69 ▶ my ($self) = @_;
Subroutine _set_sock_opt: called CC=6 SCAR=18.5
151 69 my $read_seconds;
152 my $read_useconds;
153 69 my $write_seconds;
154 69 my $write_useconds;
155 69 ▶ if (${*$self}{TimeoutEnabled}) {
Branch: 1 branch
BranchTrueFalseTotal
if (do { *$self }->{'TimeoutEnabled'}) { } 69 0 69
156 69 ▶ my $read_timeout = ${*$self}{ReadTimeout} || 0;
Condition100%2/2
do { *$self }->{'ReadTimeout'} || 0
l!l
4128
MC/DC100%1/1
do { *$self }->{'ReadTimeout'} || 0
do { *$self }->{'ReadTimeout'}
Truth table100%2/2
A
A= do { *$self }->{'ReadTimeout'}
Aresult
11
00
157 69 $read_seconds = int( $read_timeout );
158 69 $read_useconds = int( 1_000_000 * ( $read_timeout - $read_seconds ));
159 69 ▶ my $write_timeout = ${*$self}{WriteTimeout} || 0;
Condition100%2/2
do { *$self }->{'WriteTimeout'} || 0
l!l
2049
MC/DC100%1/1
do { *$self }->{'WriteTimeout'} || 0
do { *$self }->{'WriteTimeout'}
Truth table100%2/2
A
A= do { *$self }->{'WriteTimeout'}
Aresult
11
00
160 69 $write_seconds = int( $write_timeout );
161 69 $write_useconds = int( 1_000_000 * ( $write_timeout - $write_seconds ));
162 } else {
163 0 $read_seconds = 0; $read_useconds = 0;
164 0 $write_seconds = 0; $write_useconds = 0;
165 }
166 69 my $read_struct = pack( 'l!l!', $read_seconds, $read_useconds );
167 69 my $write_struct = pack( 'l!l!', $write_seconds, $write_useconds );
168  
169 69 ▶ $self->setsockopt( SOL_SOCKET, SO_RCVTIMEO, $read_struct )
Branch: 1 branch
BranchTrueFalseTotal
$self->setsockopt(1, 20, $read_struct) or croak("setsockopt(SO_RCVTIMEO): $!") 0 69 69
170 or croak "setsockopt(SO_RCVTIMEO): $!";
171  
172 69 ▶ $self->setsockopt( SOL_SOCKET, SO_SNDTIMEO, $write_struct )
Branch: 1 branch
BranchTrueFalseTotal
$self->setsockopt(1, 21, $write_struct) or croak("setsockopt(SO_SNDTIMEO): $!") 0 69 69
173 or croak "setsockopt(SO_SNDTIMEO): $!";
174 }
175  
176 package IO::Socket::Timeout::Role::PerlIO;
177 11 $IO::Socket::Timeout::Role::PerlIO::VERSION = '0.29';
178 11 ▶use PerlIO::via::Timeout;
Subroutine BEGIN: called CC=1 SCAR=0.0
179  
180 2 ▶sub read_timeout { goto &PerlIO::via::Timeout::read_timeout }
Subroutine read_timeout: called CC=1 SCAR=0.0
181 0 ▶sub write_timeout { goto &PerlIO::via::Timeout::write_timeout }
Subroutine write_timeout: not called CC=1 SCAR=6.9
182 3 ▶sub enable_timeout { goto &PerlIO::via::Timeout::enable_timeout }
Subroutine enable_timeout: called CC=1 SCAR=0.0
183 0 ▶sub disable_timeout { goto &PerlIO::via::Timeout::disable_timeout }
Subroutine disable_timeout: not called CC=1 SCAR=6.9
184 0 ▶sub timeout_enabled { goto &PerlIO::via::Timeout::timeout_enabled }
Subroutine timeout_enabled: not called CC=1 SCAR=6.9
185  
186 11 1;
187  
188 __END__
↑ Summary Build »