Light/Timeout/SetSockOpt.pm untested

filter: Statementstmt 0.0% 0 / 59 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 103 CC 5 SCAR 15.2
CC
5
Cov
0%
BUILD
12.0
sysread
6.0
syswrite
6.0
CRAP 4.6
1 #
2 # This file is part of Riak-Light
3 #
4 # This software is copyright (c) 2013 by Weborama.
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 ## no critic (RequireUseStrict, RequireUseWarnings)
10 package Riak::Light::Timeout::SetSockOpt;
11 {
12 $Riak::Light::Timeout::SetSockOpt::VERSION = '0.12';
13 }
14 ## use critic
15  
16 use POSIX qw(ETIMEDOUT ECONNRESET);
17 use Socket;
18 use IO::Select;
19 use Time::HiRes;
20 use Riak::Light::Util qw(is_netbsd is_solaris);
21 use Carp;
22 use Moo;
23 use Types::Standard -types;
24  
25 with 'Riak::Light::Timeout';
26  
27 # ABSTRACT: proxy to read/write using IO::Select as a timeout provider only for READ operations.
28  
29 has socket => ( is => 'ro', required => 1 );
30 has in_timeout => ( is => 'ro', isa => Num, default => sub {0.5} );
31 has out_timeout => ( is => 'ro', isa => Num, default => sub {0.5} );
32 has is_valid => ( is => 'rw', isa => Bool, default => sub {1} );
33  
34 sub BUILD {
35  
36 # carp "This Timeout Provider is EXPERIMENTAL!";
37  
38 croak "NetBSD no supported yet"
39 if is_netbsd();
40 ## TODO: see https://metacpan.org/source/ZWON/RedisDB-2.12/lib/RedisDB.pm#L235
41  
42 croak "Solaris is not supported"
43 if is_solaris();
44  
45 $_[0]->_set_so_rcvtimeo();
46 $_[0]->_set_so_sndtimeo();
47 }
48  
49 sub _set_so_rcvtimeo {
50 my ($self) = @_;
51 my $seconds = int( $self->in_timeout );
52 my $useconds = int( 1_000_000 * ( $self->in_timeout - $seconds ) );
53 my $timeout = pack( 'l!l!', $seconds, $useconds );
54  
55 $self->socket->setsockopt( SOL_SOCKET, SO_RCVTIMEO, $timeout )
56 or croak "setsockopt(SO_RCVTIMEO): $!";
57 }
58  
59 sub _set_so_sndtimeo {
60 my ($self) = @_;
61 my $seconds = int( $self->out_timeout );
62 my $useconds = int( 1_000_000 * ( $self->out_timeout - $seconds ) );
63 my $timeout = pack( 'l!l!', $seconds, $useconds );
64  
65 $self->socket->setsockopt( SOL_SOCKET, SO_SNDTIMEO, $timeout )
66 or croak "setsockopt(SO_SNDTIMEO): $!";
67 }
68  
69 sub clean {
70 $_[0]->socket->close();
71 $_[0]->is_valid(0);
72 $! = ETIMEDOUT; ## no critic (RequireLocalizedPunctuationVars)
73 }
74  
75 sub sysread {
76 my $self = shift;
77 $self->is_valid
78 or $! = ECONNRESET,
79 return; ## no critic (RequireLocalizedPunctuationVars)
80  
81 my $result = $self->socket->sysread(@_);
82  
83 $self->clean() unless ($result);
84  
85 $result;
86 }
87  
88 sub syswrite {
89 my $self = shift;
90 $self->is_valid
91 or $! = ECONNRESET,
92 return; ## no critic (RequireLocalizedPunctuationVars)
93  
94 my $result = $self->socket->syswrite(@_);
95  
96 $self->clean() unless ($result);
97  
98 $result;
99 }
100  
101 1;
102  
103  
104 =pod
105  
106 =head1 NAME
107  
108 Riak::Light::Timeout::SetSockOpt - proxy to read/write using IO::Select as a timeout provider only for READ operations.
109  
110 =head1 VERSION
111  
112 version 0.12
113  
114 =head1 DESCRIPTION
115  
116 Internal class
117  
118 =head1 AUTHORS
119  
120 =over 4
121  
122 =item *
123  
124 Tiago Peczenyj <tiago.peczenyj@gmail.com>
125  
126 =item *
127  
128 Damien Krotkine <dams@cpan.org>
129  
130 =back
131  
132 =head1 COPYRIGHT AND LICENSE
133  
134 This software is copyright (c) 2013 by Weborama.
135  
136 This is free software; you can redistribute it and/or modify it under
137 the same terms as the Perl 5 programming language system itself.
138  
139 =cut
140  
141  
142 __END__