blib/lib/IO/Socket/Timeout.pm

filter: Statementstmt 93.7% 120 / 128 Branchbran 66.6% 24 / 36 Conditioncond 58.1% 25 / 43 MC/DCmcdc 40.0% 8 / 20 Subroutinesub 86.2% 25 / 29 Podpod 100% 1 / 1 totaltotal 78.9% 203 / 257 CC 35 SCAR 14.1
CC
35
Cov
82%
enable_timeouts_on
7.7
__ANON__
6.8
_set_sock_opt
6.4
CRAP 4.1
↑ Summary
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 6 $IO::Socket::Timeout::VERSION = '0.32';
11 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 # ABSTRACT: IO::Socket with read/write timeout
18  
19  
20 5 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
21 5 shift;
22 5 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 8 ▶sub enable_timeouts_on {
Subroutine enable_timeouts_on: called CC=7 SCAR=20.4
Pod documented
30 8 my ($class, $socket) = @_;
31 8 ▶ defined $socket
Branch: 1 branch
BranchTrueFalseTotal
defined $socket or return 8 0 8
32 or return;
33 8 ▶ $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") 8 0 8
34 or croak 'make_timeouts_aware can be used only on instances that inherit from IO::Socket';
35  
36 8 my $osname = $Config{osname};
37 8 ▶ 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') { } 5 3 8
Condition44%4/9
not $ENV{"PERL_IO_SOCKET_TIMEOUT_FORCE_SELECT"} and $osname eq "darwin" || $osname eq "linux" || $osname eq "freebsd"
!ll&&!rl&&r
305
$osname eq "darwin" or $osname eq "linux" or $osname eq "freebsd"
l!l&&r!l&&!r
500
$osname eq "darwin" or $osname eq "linux"
l!l&&r!l&&!r
050
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 5 _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 8 $socket->enable_timeout;
47 8 return $socket;
48 }
49  
50 12 ▶sub _create_composed_class {
Subroutine _create_composed_class: called CC=2 SCAR=6.9
51 12 my ($class, @roles) = @_;
52 12 my $composed_class = $class . '__with__' . join('__and__', @roles);
53 12 my $path = $composed_class; $path =~ s|::|/|g; $path .= '.pm';
54 12 ▶ if ( ! exists $INC{$path}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $INC{$path}) 8 4 12
55 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
56 8 *{"${composed_class}::ISA"} = [ $class, @roles ];
57 8 $INC{$path} = __FILE__;
58 }
59 12 return $composed_class;
60 }
61  
62 8 ▶sub _compose_roles {
Subroutine _compose_roles: called CC=1 SCAR=0.0
63 8 my ($instance, @roles) = @_;
64 8 bless $instance, _create_composed_class(ref $instance, @roles);
65 }
66  
67 # sysread FILEHANDLE,SCALAR,LENGTH,OFFSET
68 6 ▶BEGIN {
Subroutine BEGIN: called CC=5 SCAR=16.5
69 6 my $osname = $Config{osname};
70 6 ▶ 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 5 6
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
105
$osname ne "darwin" and $osname ne "linux" and $osname ne "freebsd"
!ll&&!rl&&r
500
$osname ne "darwin" and $osname ne "linux"
!ll&&!rl&&r
050
MC/DC25%1/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 table40%2/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 31 ▶ *CORE::GLOBAL::sysread = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
77 31 my $args_count = scalar(@_);
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]) 16 15 31
$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 6 ▶BEGIN {
Subroutine BEGIN: called CC=5 SCAR=17.4
94 6 my $osname = $Config{osname};
95 6 ▶ 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 5 6
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
105
$osname ne "darwin" and $osname ne "linux" and $osname ne "freebsd"
!ll&&!rl&&r
500
$osname ne "darwin" and $osname ne "linux"
!ll&&!rl&&r
050
MC/DC25%1/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 table40%2/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 12 ▶ *CORE::GLOBAL::syswrite = sub {
Subroutine __ANON__: called CC=6 SCAR=19.1
102 12 my $args_count = scalar(@_);
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])) 6 6 12
$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 6 $IO::Socket::Timeout::Role::SetSockOpt::VERSION = '0.32';
120 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
121 6 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
122  
123 7 ▶sub _check_attributes {
Subroutine _check_attributes: called CC=2 SCAR=7.0
124 7 my ($self) = @_;
125 7 ▶ 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 7 7
126 and croak "if defined, 'ReadTimeout' and 'WriteTimeout' attributes should be >= 0";
127 }
128  
129 4 ▶sub read_timeout {
Subroutine read_timeout: called CC=2 SCAR=7.0
130 4 my ($self) = @_;
131 4 ▶ @_ > 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 4 0 4
132 4 ${*$self}{ReadTimeout}
133 4 }
134  
135 3 ▶sub write_timeout {
Subroutine write_timeout: called CC=2 SCAR=7.0
136 3 my ($self) = @_;
137 3 ▶ @_ > 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 3 0 3
138 3 ${*$self}{WriteTimeout}
139 3 }
140  
141 5 ▶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 5 ▶sub timeout_enabled {
Subroutine timeout_enabled: called CC=2 SCAR=7.0
144 5 my ($self) = @_;
145 5 ▶ @_ > 1 and ${*$self}{TimeoutEnabled} = !!$_[1], $self->_set_sock_opt;
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 and do { *$self }->{"TimeoutEnabled"} = !!$_[1], $self->_set_sock_opt 5 0 5
146 5 ${*$self}{TimeoutEnabled}
147 5 }
148  
149 12 ▶sub _set_sock_opt {
Subroutine _set_sock_opt: called CC=6 SCAR=18.5
150 12 my ($self) = @_;
151 12 my $read_seconds;
152 my $read_useconds;
153 12 my $write_seconds;
154 12 my $write_useconds;
155 12 ▶ if (${*$self}{TimeoutEnabled}) {
Branch: 1 branch
BranchTrueFalseTotal
if (do { *$self }->{'TimeoutEnabled'}) { } 12 0 12
156 12 ▶ my $read_timeout = ${*$self}{ReadTimeout} || 0;
Condition100%2/2
do { *$self }->{'ReadTimeout'} || 0
l!l
75
MC/DC100%1/1
do { *$self }->{'ReadTimeout'} || 0
do { *$self }->{'ReadTimeout'}
Truth table100%2/2
A
A= do { *$self }->{'ReadTimeout'}
Aresult
11
00
157 12 $read_seconds = int( $read_timeout );
158 12 $read_useconds = int( 1_000_000 * ( $read_timeout - $read_seconds ));
159 12 ▶ my $write_timeout = ${*$self}{WriteTimeout} || 0;
Condition100%2/2
do { *$self }->{'WriteTimeout'} || 0
l!l
39
MC/DC100%1/1
do { *$self }->{'WriteTimeout'} || 0
do { *$self }->{'WriteTimeout'}
Truth table100%2/2
A
A= do { *$self }->{'WriteTimeout'}
Aresult
11
00
160 12 $write_seconds = int( $write_timeout );
161 12 $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 12 my $read_struct = pack( 'l!l!', $read_seconds, $read_useconds );
167 12 my $write_struct = pack( 'l!l!', $write_seconds, $write_useconds );
168  
169 12 ▶ $self->setsockopt( SOL_SOCKET, SO_RCVTIMEO, $read_struct )
Branch: 1 branch
BranchTrueFalseTotal
$self->setsockopt(1, 20, $read_struct) or croak("setsockopt(SO_RCVTIMEO): $!") 12 0 12
170 or croak "setsockopt(SO_RCVTIMEO): $!";
171  
172 12 ▶ $self->setsockopt( SOL_SOCKET, SO_SNDTIMEO, $write_struct )
Branch: 1 branch
BranchTrueFalseTotal
$self->setsockopt(1, 21, $write_struct) or croak("setsockopt(SO_SNDTIMEO): $!") 12 0 12
173 or croak "setsockopt(SO_SNDTIMEO): $!";
174 }
175  
176 package IO::Socket::Timeout::Role::PerlIO;
177 6 $IO::Socket::Timeout::Role::PerlIO::VERSION = '0.32';
178 6 ▶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 6 1;
187  
188 __END__
↑ Summary