blib/lib/Redis/Fast.pm

filter: Statementstmt 33.8% 159 / 470 Branchbran 19.5% 45 / 230 Conditioncond 28.5% 22 / 77 MC/DCmcdc 22.7% 10 / 44 Subroutinesub 39.4% 30 / 76 Podpod 0.0% 0 / 18 totaltotal 29.0% 266 / 915 CC 169 SCAR 46.1
CC
169
Cov
29%
new
375.6
new
375.6
__ANON__
110.0
CRAP 100.3
↑ Summary builder/MyBuilder.pm »
1 package Redis::Fast 0.41;
2  
3 42 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
4 42 ▶ use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 42 XSLoader::load __PACKAGE__, $VERSION;
6 }
7  
8 42 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 42 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 42 ▶use Carp qw/confess croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 42 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 42 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 42 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 42 ▶use Redis::Fast::Sentinel;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # small utilities for handling host and port
19 0 ▶sub _join_host_port {
Subroutine _join_host_port: not called CC=3 SCAR=24.8
20 0 ▶ my ($host, $port) = @_;
Subroutine _join_host_port: not called CC=3 SCAR=24.8
21 0 ▶ return "[$host]:$port" if $host =~ /:/ || $host =~ /%/;
Branch: 2 branches
BranchTrueFalseTotal
if $host =~ /:/ or $host =~ /%/ 0 0 0
if $host =~ /:/ or $host =~ /%/ 0 0 0
Condition0%0/6
$host =~ /:/ or $host =~ /%/
l!l&&r!l&&!r
000
$host =~ /:/ or $host =~ /%/
l!l&&r!l&&!r
000
MC/DC0%0/2
$host =~ /:/ or $host =~ /%/
$host =~ /:/ $host =~ /%/
MC/DC0%0/2
$host =~ /:/ or $host =~ /%/
$host =~ /:/ $host =~ /%/
Truth table0%0/3
A or B
A= $host =~ /:/B= $host =~ /%/
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $host =~ /:/B= $host =~ /%/
ABresult
1X1
011
000
22 0 return "$host:$port";
23 }
24 18 ▶sub _split_host_port {
Subroutine _split_host_port: called CC=2 SCAR=11.6
25 0 ▶ my $hostport = shift;
Subroutine _split_host_port: not called CC=2 SCAR=11.6
26 0 ▶ if ($hostport =~ /\A\[([^\]]+)\]:([0-9]+)\z/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hostport =~ /\A\[([^\]]+)\]:([0-9]+)\z/) 0 18 18
if ($hostport =~ /\A\[([^\]]+)\]:([0-9]+)\z/) 0 0 0
27 0 return $1, $2;
28 }
29 0 return split /:/, $hostport;
30 }
31  
32 84 ▶sub _new_on_connect_cb {
Subroutine _new_on_connect_cb: called CC=1 SCAR=4.1
33 0 ▶ my ($self, $on_conn, $password, $name) = @_;
Subroutine _new_on_connect_cb: not called CC=1 SCAR=4.1
34 0 weaken $self;
35 78 ▶ my $handler = sub {
Subroutine __ANON__: called CC=10 SCAR=41.1
36 # If we are in PubSub mode we shouldn't perform any command besides
37 # (p)(un)subscribe
38 0 ▶ if (! $self->is_subscriber) {
Subroutine __ANON__: not called CC=10 SCAR=41.1
Branch: 2 branches
BranchTrueFalseTotal
unless ($self->is_subscriber) 78 0 78
unless ($self->is_subscriber) 0 0 0
39 defined $name
40 0 ▶ and try {
Subroutine __ANON__: not called CC=3 SCAR=24.8
41 0 ▶ my $n = $name;
Subroutine __ANON__: not called CC=3 SCAR=24.8
42 0 ▶ $n = $n->($self) if ref($n) eq 'CODE';
Branch: 2 branches
BranchTrueFalseTotal
if ref $n eq "CODE" 0 0 0
if ref $n eq "CODE" 0 0 0
43 0 ▶ $self->client_setname($n) if defined $n;
Branch: 2 branches
BranchTrueFalseTotal
if defined $n 0 0 0
if defined $n 0 0 0
44 0 ▶ };
Branch: 2 branches
BranchTrueFalseTotal
defined $name and &try(sub { my $n = $name; $n = &$n($self) if ref $n eq "CODE"; $self->client_setname($n) if defined $n; } ) 0 78 78
defined $name and &try(sub { my $n = $name; $n = &$n($self) if ref $n eq "CODE"; $self->client_setname($n) if defined $n; } ) 0 0 0
45 0 my $data = $self->__get_data;
46 defined $data->{current_database}
47 0 ▶ and $self->select($data->{current_database});
Branch: 2 branches
BranchTrueFalseTotal
defined $data->{"current_database"} and $self->select($data->{"current_database"}) 0 78 78
defined $data->{"current_database"} and $self->select($data->{"current_database"}) 0 0 0
48 }
49  
50 0 my $subscribers = $self->__get_data->{subscribers};
51 0 $self->__get_data->{subscribers} = {};
52 0 $self->__get_data->{cbs} = undef;
53 0 foreach my $topic (CORE::keys(%{$subscribers})) {
54 # anchor the pattern and match newlines, so that a channel name containing
55 # "\nmessage:" is not mistaken for another channel
56 0 ▶ if ($topic =~ /\A(p?message):(.*)\z/s ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($topic =~ /\A(p?message):(.*)\z/s) 0 0 0
if ($topic =~ /\A(p?message):(.*)\z/s) 0 0 0
57 0 my ($key, $channel) = ($1, $2);
58 0 my $subs = $subscribers->{$topic};
59 0 ▶ if ($key eq 'message') {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'message') { } 0 0 0
if ($key eq 'message') { } 0 0 0
60 0 $self->__subscription_cmd('', 0, subscribe => $channel, $_) for @$subs;
61 } else {
62 0 $self->__subscription_cmd('p', 0, psubscribe => $channel, $_) for @$subs;
63 }
64 }
65 }
66  
67 0 ▶ defined $on_conn
Branch: 2 branches
BranchTrueFalseTotal
defined $on_conn and &$on_conn($self) 0 78 78
defined $on_conn and &$on_conn($self) 0 0 0
68 and $on_conn->($self);
69 0 };
70  
71 78 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=26.2
72 0 ▶ my $reconnect_stash = $self->__get_reconnect;
Subroutine __ANON__: not called CC=4 SCAR=26.2
73 0 ▶ if(defined $password) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $password) 0 78 78
if (defined $password) 0 0 0
74 0 my $err;
75 0 $self->__set_reconnect(0);
76 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
77 0 ▶ $self->auth($password);
Subroutine __ANON__: not called CC=1 SCAR=6.9
78 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
79 0 ▶ $err = $_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
80 0 };
81 0 ▶ if(defined $err) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $err) 0 0 0
if (defined $err) 0 0 0
82 0 ▶ if($err =~ /ERR invalid password|WRONGPASS invalid username-password pair/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($err =~ /ERR invalid password|WRONGPASS invalid username-password pair/) { } 0 0 0
if ($err =~ /ERR invalid password|WRONGPASS invalid username-password pair/) { } 0 0 0
83 # password setting is incorrect, no need to reconnect
84 0 die("Redis server refused password");
85 } else {
86 # it might be network error
87 # invoke reconnect
88 0 $self->__set_reconnect($reconnect_stash);
89 0 return ;
90 }
91 }
92 }
93  
94 78 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=1.2
95 # disable reconnection while executing on_connect handler
96 0 ▶ $self->__set_reconnect(0);
Subroutine __ANON__: not called CC=1 SCAR=1.2
97 0 $handler->();
98 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
99 0 ▶ $self->quit();
Subroutine __ANON__: not called CC=1 SCAR=6.9
100 78 ▶ } finally {
Subroutine __ANON__: called CC=1 SCAR=1.2
101 0 ▶ $self->__set_reconnect($reconnect_stash);
Subroutine __ANON__: not called CC=1 SCAR=1.2
102 0 };
103 0 };
104 }
105  
106 80 ▶sub _new_reconnect_on_error_cb {
Subroutine _new_reconnect_on_error_cb: called CC=2 SCAR=15.4
107 0 ▶ my ($self, $reconnect_on_error) = @_;
Subroutine _new_reconnect_on_error_cb: not called CC=2 SCAR=15.4
108 0 weaken $self;
109  
110 0 ▶ if ($reconnect_on_error) {
Branch: 2 branches
BranchTrueFalseTotal
if ($reconnect_on_error) { } 0 80 80
if ($reconnect_on_error) { } 0 0 0
111 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
112 # The unit should be second and the type should be double.
113 # -1 is a special value, it means that we do not reconnect.
114 0 ▶ my $next_reconnect_interval = $reconnect_on_error->(@_);
Subroutine __ANON__: not called CC=3 SCAR=24.8
115 0 ▶ if ($next_reconnect_interval < -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($next_reconnect_interval < -1) 0 0 0
if ($next_reconnect_interval < -1) 0 0 0
116 0 warn "reconnect_on_error must not return a number less than -1";
117  
118 # Reset a next_reconnect_interval and do not reconnect.
119 0 $next_reconnect_interval = -1;
120 }
121  
122 # Wait until next_reconnect_interval seconds elapse.
123 0 $self->__set_next_reconnect_on_error_at($next_reconnect_interval);
124  
125 0 my $need_reconnect = 0;
126 0 ▶ if (-1 < $next_reconnect_interval) {
Branch: 2 branches
BranchTrueFalseTotal
if (-1 < $next_reconnect_interval) 0 0 0
if (-1 < $next_reconnect_interval) 0 0 0
127 0 $need_reconnect = 1;
128 }
129 0 return $need_reconnect;
130 0 };
131 } else {
132 0 return;
133 }
134 }
135  
136 84 ▶sub new {
Subroutine new: called CC=27 SCAR=59.3
Pod undocumented
137 0 ▶ my $class = shift;
Subroutine new: not called CC=27 SCAR=59.3
Pod undocumented
138 0 my %args = @_;
139 0 my $self = $class->_new;
140  
141 ## Deal with REDIS_SERVER ENV
142 0 ▶ if ($ENV{REDIS_SERVER} && !$args{sock} && !$args{server}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ENV{"REDIS_SERVER"} and not $args{"sock"} and not $args{"server"}) 0 84 84
if ($ENV{"REDIS_SERVER"} and not $args{"sock"} and not $args{"server"}) 0 0 0
Condition16%2/12
$ENV{"REDIS_SERVER"} and not $args{"sock"} and not $args{"server"}
!ll&&!rl&&r
8400
$ENV{"REDIS_SERVER"} and not $args{"sock"}
!ll&&!rl&&r
8400
$ENV{"REDIS_SERVER"} and not $args{"sock"} and not $args{"server"}
!ll&&!rl&&r
000
$ENV{"REDIS_SERVER"} and not $args{"sock"}
!ll&&!rl&&r
000
MC/DC0%0/3
$ENV{"REDIS_SERVER"} and not $args{"sock"} and not $args{"server"}
$ENV{"REDIS_SERVER"} not $args{"sock"} not $args{"server"}
MC/DC0%0/3
$ENV{"REDIS_SERVER"} and not $args{"sock"} and not $args{"server"}
$ENV{"REDIS_SERVER"} not $args{"sock"} not $args{"server"}
Truth table25%1/4
A and not(B) and not(C)
A= $ENV{"REDIS_SERVER"}B= not $args{"sock"}C= not $args{"server"}
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and not(B) and not(C)
A= $ENV{"REDIS_SERVER"}B= not $args{"sock"}C= not $args{"server"}
ABCresult
0XX0
10X0
1100
1111
143 0 ▶ if ($ENV{REDIS_SERVER} =~ m!^/!) {
Branch: 6 branches
BranchTrueFalseTotal
if ($ENV{'REDIS_SERVER'} =~ m[^/]) { } 0 0 0
elsif ($ENV{'REDIS_SERVER'} =~ /^unix:(.+)/) { } 0 0 0
elsif ($ENV{'REDIS_SERVER'} =~ /^(tcp:)?(.+)/) { } 0 0 0
if ($ENV{'REDIS_SERVER'} =~ m[^/]) { } 0 0 0
elsif ($ENV{'REDIS_SERVER'} =~ /^unix:(.+)/) { } 0 0 0
elsif ($ENV{'REDIS_SERVER'} =~ /^(tcp:)?(.+)/) { } 0 0 0
144 0 $args{sock} = $ENV{REDIS_SERVER};
145 }
146 elsif ($ENV{REDIS_SERVER} =~ m!^unix:(.+)!) {
147 0 $args{sock} = $1;
148 }
149 elsif ($ENV{REDIS_SERVER} =~ m!^(tcp:)?(.+)!) {
150 0 $args{server} = $2;
151 }
152 }
153  
154 0 my $on_conn = $args{on_connect};
155 0 my $password = $args{password};
156 0 my $name = $args{name};
157 0 $self->__set_on_connect($self->_new_on_connect_cb($on_conn, $password, $name));
158 $self->__set_data({
159 subscribers => {},
160 sentinels_cnx_timeout => $args{sentinels_cnx_timeout},
161 sentinels_read_timeout => $args{sentinels_read_timeout},
162 sentinels_write_timeout => $args{sentinels_write_timeout},
163 no_sentinels_list_update => $args{no_sentinels_list_update},
164 0 });
165  
166 0 ▶ if ($args{sock}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($args{'sock'}) { } 66 18 84
elsif ($args{'sentinels'}) { } 0 18 18
if ($args{'sock'}) { } 0 0 0
elsif ($args{'sentinels'}) { } 0 0 0
167 0 $self->__connection_info_unix($args{sock});
168 } elsif ($args{sentinels}) {
169 0 my $sentinels = $args{sentinels};
170 0 ▶ ref $sentinels eq 'ARRAY'
Branch: 2 branches
BranchTrueFalseTotal
ref $sentinels eq "ARRAY" or croak("'sentinels' param must be an ArrayRef") 0 0 0
ref $sentinels eq "ARRAY" or croak("'sentinels' param must be an ArrayRef") 0 0 0
171 or croak("'sentinels' param must be an ArrayRef");
172 defined($self->__get_data->{service} = $args{service})
173 0 ▶ or croak("Need 'service' name when using 'sentinels'!");
Branch: 2 branches
BranchTrueFalseTotal
defined($self->__get_data->{"service"} = $args{"service"}) or croak("Need 'service' name when using 'sentinels'!") 0 0 0
defined($self->__get_data->{"service"} = $args{"service"}) or croak("Need 'service' name when using 'sentinels'!") 0 0 0
174 0 ▶ croak("Does not support SSL and sentinels") if exists $args{ssl} and $args{ssl};
Branch: 2 branches
BranchTrueFalseTotal
if exists $args{"ssl"} and $args{"ssl"} 0 0 0
if exists $args{"ssl"} and $args{"ssl"} 0 0 0
Condition0%0/6
exists $args{"ssl"} and $args{"ssl"}
!ll&&!rl&&r
000
exists $args{"ssl"} and $args{"ssl"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $args{"ssl"} and $args{"ssl"}
exists $args{"ssl"} $args{"ssl"}
MC/DC0%0/2
exists $args{"ssl"} and $args{"ssl"}
exists $args{"ssl"} $args{"ssl"}
Truth table0%0/3
A and B
A= exists $args{"ssl"}B= $args{"ssl"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= exists $args{"ssl"}B= $args{"ssl"}
ABresult
0X0
100
111
175 0 $self->__get_data->{sentinels} = $sentinels;
176 0 $self->__get_data->{sentinels_password} = $args{sentinels_password};
177 0 ▶ my $on_build_sock = sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
178 0 ▶ my $data = $self->__get_data;
Subroutine __ANON__: not called CC=10 SCAR=47.0
179 0 my $sentinels = $data->{sentinels};
180  
181 # try to connect to a sentinel
182 0 my $status;
183 0 foreach my $sentinel_address (@$sentinels) {
184 0 ▶ my $sentinel = eval {
Branch: 2 branches
BranchTrueFalseTotal
my $sentinel = eval { do { "Redis::Fast::Sentinel"->new("server", $sentinel_address, "password", $data->{"sentinels_password"}, "cnx_timeout", exists $data->{"sentinels_cnx_timeout"} ? $data->{"sentinels_cnx_timeout"} : "0.1", "read_timeout", exists $data->{"sentinels_read_timeout"} ? $data->{"sentinels_read_timeout"} : 1, "write_timeout", exists $data->{"sentinels_write_timeout"} ? $data->{"sentinels_write_timeout"} : 1) } } or next 0 0 0
my $sentinel = eval { do { "Redis::Fast::Sentinel"->new("server", $sentinel_address, "password", $data->{"sentinels_password"}, "cnx_timeout", exists $data->{"sentinels_cnx_timeout"} ? $data->{"sentinels_cnx_timeout"} : "0.1", "read_timeout", exists $data->{"sentinels_read_timeout"} ? $data->{"sentinels_read_timeout"} : 1, "write_timeout", exists $data->{"sentinels_write_timeout"} ? $data->{"sentinels_write_timeout"} : 1) } } or next 0 0 0
185 Redis::Fast::Sentinel->new(
186 server => $sentinel_address,
187 password => $data->{sentinels_password},
188 cnx_timeout => ( exists $data->{sentinels_cnx_timeout}
189 ? $data->{sentinels_cnx_timeout} : 0.1),
190 read_timeout => ( exists $data->{sentinels_read_timeout}
191 ? $data->{sentinels_read_timeout} : 1 ),
192 write_timeout => ( exists $data->{sentinels_write_timeout}
193 0 ▶ ? $data->{sentinels_write_timeout} : 1 ),
Branch: 6 branches
BranchTrueFalseTotal
exists $data->{'sentinels_cnx_timeout'} ? : 0 0 0
exists $data->{'sentinels_read_timeout'} ? : 0 0 0
exists $data->{'sentinels_write_timeout'} ? : 0 0 0
exists $data->{'sentinels_cnx_timeout'} ? : 0 0 0
exists $data->{'sentinels_read_timeout'} ? : 0 0 0
exists $data->{'sentinels_write_timeout'} ? : 0 0 0
194 )
195 } or next;
196 0 my $server_address = $sentinel->get_service_address($data->{service});
197 0 ▶ defined $server_address
Branch: 2 branches
BranchTrueFalseTotal
defined $server_address or $status ||= "Sentinels don't know this service", next 0 0 0
defined $server_address or $status ||= "Sentinels don't know this service", next 0 0 0
Condition0%0/4
$status ||= "Sentinels don't know this service"
l!l
00
$status ||= "Sentinels don't know this service"
l!l
00
MC/DC0%0/1
$status ||= "Sentinels don't know this service"
$status
MC/DC0%0/1
$status ||= "Sentinels don't know this service"
$status
Truth table0%0/2
A
A= $status
Aresult
11
00
Truth table0%0/2
A
A= $status
Aresult
11
00
198 or $status ||= "Sentinels don't know this service",
199 next;
200 0 ▶ $server_address eq 'IDONTKNOW'
Branch: 2 branches
BranchTrueFalseTotal
$server_address eq "IDONTKNOW" and $status = "service is configured in one Sentinel, but was never reached", next 0 0 0
$server_address eq "IDONTKNOW" and $status = "service is configured in one Sentinel, but was never reached", next 0 0 0
201 and $status = "service is configured in one Sentinel, but was never reached",
202 next;
203  
204 # we found the service, set the server
205 0 my ($server, $port) = _split_host_port $server_address;
206 0 $self->__connection_info($server, $port);
207  
208 0 ▶ if (! $data->{no_sentinels_list_update} ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($data->{"no_sentinels_list_update"}) 0 0 0
unless ($data->{"no_sentinels_list_update"}) 0 0 0
209 # move the elected sentinel at the front of the list and add
210 # additional sentinels
211 0 my $idx = 2;
212 0 my %h = ( ( map { $_ => $idx++ } @{$data->{sentinels}}),
213 $sentinel_address => 1,
214 );
215  
216 $data->{sentinels} = [
217 0 ( sort { $h{$a} <=> $h{$b} } CORE::keys(%h) ), # sorted existing sentinels,
218 0 grep { ! $h{$_}; } # list of unknown
219 map {
220 0 my $s = +{ @$_ };
221 0 _join_host_port($s->{ip}, $s->{port});
222 } # ip:port of
223 $sentinel->sentinel( # sentinels
224 sentinels => $data->{service} # for this service
225 )
226 0 ];
227 }
228 }
229 0 };
230 0 $self->__set_on_build_sock($on_build_sock);
231 } else {
232 0 ▶ my ($server, $port) = _split_host_port($args{server} || '127.0.0.1:6379');
Condition50%2/4
$args{'server'} || '127.0.0.1:6379'
l!l
144
$args{'server'} || '127.0.0.1:6379'
l!l
00
MC/DC100%1/1
$args{'server'} || '127.0.0.1:6379'
$args{'server'}
MC/DC0%0/1
$args{'server'} || '127.0.0.1:6379'
$args{'server'}
Truth table100%2/2
A
A= $args{'server'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'server'}
Aresult
11
00
233 0 $self->__connection_info($server, $port);
234 }
235  
236 #$self->{is_subscriber} = 0;
237 #$self->{subscribers} = {};
238 0 ▶ $self->__set_reconnect($args{reconnect} || 0);
Condition50%2/4
$args{'reconnect'} || 0
l!l
6618
$args{'reconnect'} || 0
l!l
00
MC/DC100%1/1
$args{'reconnect'} || 0
$args{'reconnect'}
MC/DC0%0/1
$args{'reconnect'} || 0
$args{'reconnect'}
Truth table100%2/2
A
A= $args{'reconnect'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'reconnect'}
Aresult
11
00
239 0 ▶ $self->__set_every($args{every} || 1000);
Condition50%2/4
$args{'every'} || 1000
l!l
183
$args{'every'} || 1000
l!l
00
MC/DC100%1/1
$args{'every'} || 1000
$args{'every'}
MC/DC0%0/1
$args{'every'} || 1000
$args{'every'}
Truth table100%2/2
A
A= $args{'every'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'every'}
Aresult
11
00
240 0 ▶ $self->__set_debug(($args{debug} || $ENV{REDIS_DEBUG}) ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
$args{'debug'} || $ENV{'REDIS_DEBUG'} ? : 0 84 84
$args{'debug'} || $ENV{'REDIS_DEBUG'} ? : 0 0 0
Condition16%1/6
$args{"debug"} or $ENV{"REDIS_DEBUG"}
l!l&&r!l&&!r
0084
$args{"debug"} or $ENV{"REDIS_DEBUG"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"debug"} or $ENV{"REDIS_DEBUG"}
$args{"debug"} $ENV{"REDIS_DEBUG"}
MC/DC0%0/2
$args{"debug"} or $ENV{"REDIS_DEBUG"}
$args{"debug"} $ENV{"REDIS_DEBUG"}
Truth table33%1/3
A or B
A= $args{"debug"}B= $ENV{"REDIS_DEBUG"}
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $args{"debug"}B= $ENV{"REDIS_DEBUG"}
ABresult
1X1
011
000
241 0 ▶ $self->__set_cnx_timeout($args{cnx_timeout} || -1);
Condition50%2/4
$args{'cnx_timeout'} || -1
l!l
183
$args{'cnx_timeout'} || -1
l!l
00
MC/DC100%1/1
$args{'cnx_timeout'} || -1
$args{'cnx_timeout'}
MC/DC0%0/1
$args{'cnx_timeout'} || -1
$args{'cnx_timeout'}
Truth table100%2/2
A
A= $args{'cnx_timeout'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'cnx_timeout'}
Aresult
11
00
242 0 ▶ $self->__set_read_timeout($args{read_timeout} || -1);
Condition50%2/4
$args{'read_timeout'} || -1
l!l
381
$args{'read_timeout'} || -1
l!l
00
MC/DC100%1/1
$args{'read_timeout'} || -1
$args{'read_timeout'}
MC/DC0%0/1
$args{'read_timeout'} || -1
$args{'read_timeout'}
Truth table100%2/2
A
A= $args{'read_timeout'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'read_timeout'}
Aresult
11
00
243 0 ▶ $self->__set_write_timeout($args{write_timeout} || -1);
Condition50%2/4
$args{'write_timeout'} || -1
l!l
183
$args{'write_timeout'} || -1
l!l
00
MC/DC100%1/1
$args{'write_timeout'} || -1
$args{'write_timeout'}
MC/DC0%0/1
$args{'write_timeout'} || -1
$args{'write_timeout'}
Truth table100%2/2
A
A= $args{'write_timeout'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'write_timeout'}
Aresult
11
00
244 0 ▶ $self->__set_ssl($args{ssl} || 0);
Condition50%2/4
$args{'ssl'} || 0
l!l
1272
$args{'ssl'} || 0
l!l
00
MC/DC100%1/1
$args{'ssl'} || 0
$args{'ssl'}
MC/DC0%0/1
$args{'ssl'} || 0
$args{'ssl'}
Truth table100%2/2
A
A= $args{'ssl'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'ssl'}
Aresult
11
00
245 0 ▶ if (defined(my $mode = $args{SSL_verify_mode})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $mode = $args{"SSL_verify_mode"})) 9 75 84
if (defined(my $mode = $args{"SSL_verify_mode"})) 0 0 0
246 # SSL_VERIFY_* are bit flags with the same values in IO::Socket::SSL, OpenSSL and hiredis.
247 # The default is SSL_VERIFY_PEER, so reject anything we cannot interpret instead of ignoring it.
248 0 ▶ croak("Invalid SSL_verify_mode: $mode")
Branch: 2 branches
BranchTrueFalseTotal
unless $mode =~ /\A[0-9]+\z/ and ($mode & '18446744073709551600') == 0 4 5 9
unless $mode =~ /\A[0-9]+\z/ and ($mode & '18446744073709551600') == 0 0 0 0
Condition50%3/6
$mode =~ /\A[0-9]+\z/ and ($mode & '18446744073709551600') == 0
!ll&&!rl&&r
315
$mode =~ /\A[0-9]+\z/ and ($mode & '18446744073709551600') == 0
!ll&&!rl&&r
000
MC/DC100%2/2
$mode =~ /\A[0-9]+\z/ and ($mode & '18446744073709551600') == 0
$mode =~ /\A[0-9]+\z/ ($mode & '18446744073709551600') == 0
MC/DC0%0/2
$mode =~ /\A[0-9]+\z/ and ($mode & '18446744073709551600') == 0
$mode =~ /\A[0-9]+\z/ ($mode & '18446744073709551600') == 0
Truth table100%3/3
A and B
A= $mode =~ /\A[0-9]+\z/B= ($mode & '18446744073709551600') == 0
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $mode =~ /\A[0-9]+\z/B= ($mode & '18446744073709551600') == 0
ABresult
0X0
100
111
249 unless $mode =~ /\A[0-9]+\z/ && ($mode & ~0x0f) == 0;
250 0 $self->__set_ssl_verify_mode($mode);
251 }
252  
253 0 ▶ if (my $cb = $self->_new_reconnect_on_error_cb($args{reconnect_on_error})) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $cb = $self->_new_reconnect_on_error_cb($args{"reconnect_on_error"})) 0 80 80
if (my $cb = $self->_new_reconnect_on_error_cb($args{"reconnect_on_error"})) 0 0 0
254 0 $self->__set_reconnect_on_error($cb);
255 }
256  
257 0 ▶ $self->connect unless $args{no_auto_connect_on_new};
Branch: 2 branches
BranchTrueFalseTotal
unless $args{"no_auto_connect_on_new"} 79 1 80
unless $args{"no_auto_connect_on_new"} 0 0 0
258  
259 0 return $self;
260 }
261  
262  
263  
264 ### Deal with common, general case, Redis commands
265 42 our $AUTOLOAD;
266  
267 13 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=1 SCAR=1.1
268 0 ▶ my $command = $AUTOLOAD;
Subroutine AUTOLOAD: not called CC=1 SCAR=1.1
269 0 $command =~ s/.*://;
270 0 my @command = split /_/, uc $command;
271  
272 68 ▶ my $method = sub {
Subroutine __ANON__: called CC=4 SCAR=18.8
273 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=4 SCAR=18.8
274 0 $self->__is_valid_command($command);
275 0 my ($ret, $error) = $self->__std_cmd(@command, @_);
276 0 ▶ confess "[$command] $error, " if defined $error;
Branch: 2 branches
BranchTrueFalseTotal
if defined $error 14 54 68
if defined $error 0 0 0
277 0 ▶ return (wantarray && ref $ret eq 'ARRAY') ? @$ret : $ret;
Branch: 2 branches
BranchTrueFalseTotal
wantarray && ref $ret eq 'ARRAY' ? : 9 45 54
wantarray && ref $ret eq 'ARRAY' ? : 0 0 0
Condition33%2/6
wantarray and ref $ret eq "ARRAY"
!ll&&!rl&&r
4509
wantarray and ref $ret eq "ARRAY"
!ll&&!rl&&r
000
MC/DC50%1/2
wantarray and ref $ret eq "ARRAY"
wantarray ref $ret eq "ARRAY"
MC/DC0%0/2
wantarray and ref $ret eq "ARRAY"
wantarray ref $ret eq "ARRAY"
Truth table66%2/3
A and B
A= wantarrayB= ref $ret eq "ARRAY"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= wantarrayB= ref $ret eq "ARRAY"
ABresult
0X0
100
111
278 0 };
279  
280 # Save this method for future calls
281 42 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
282 0 *$AUTOLOAD = $method;
283  
284 0 goto $method;
285 }
286  
287 0 ▶sub __with_reconnect {
Subroutine __with_reconnect: not called CC=1 SCAR=6.9
288 0 ▶ my ($self, $cb) = @_;
Subroutine __with_reconnect: not called CC=1 SCAR=6.9
289  
290 0 confess "not implemented";
291 }
292  
293  
294 ### Commands with extra logic
295  
296 0 ▶sub keys {
Subroutine keys: not called CC=3 SCAR=24.8
Pod undocumented
297 0 ▶ my $self = shift;
Subroutine keys: not called CC=3 SCAR=24.8
Pod undocumented
298 0 $self->__is_valid_command('keys');
299 0 my ($ret, $error) = $self->__keys(@_);
300 0 ▶ confess "[keys] $error, " if defined $error;
Branch: 2 branches
BranchTrueFalseTotal
if defined $error 0 0 0
if defined $error 0 0 0
301 0 ▶ return $ret unless ref $ret eq 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $ret eq "ARRAY" 0 0 0
unless ref $ret eq "ARRAY" 0 0 0
302 0 return @$ret;
303 }
304  
305 6 ▶sub ping {
Subroutine ping: called CC=2 SCAR=10.3
Pod undocumented
306 0 ▶ my $self = shift;
Subroutine ping: not called CC=2 SCAR=10.3
Pod undocumented
307 0 $self->__is_valid_command('ping');
308 0 ▶ return unless $self->__sock;
Branch: 2 branches
BranchTrueFalseTotal
unless $self->__sock 0 6 6
unless $self->__sock 0 0 0
309 6 ▶ return scalar try {
Subroutine __ANON__: called CC=2 SCAR=9.2
310 0 ▶ my ($ret, $error) = $self->__std_cmd('ping');
Subroutine __ANON__: not called CC=2 SCAR=9.2
311 0 ▶ return if defined $error;
Branch: 2 branches
BranchTrueFalseTotal
if defined $error 4 2 6
if defined $error 0 0 0
312 0 return $ret;
313 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
314 0 ▶ return ;
Subroutine __ANON__: not called CC=1 SCAR=6.9
315 0 };
316 }
317  
318 8 ▶sub info {
Subroutine info: called CC=3 SCAR=16.0
Pod undocumented
319 0 ▶ my $self = shift;
Subroutine info: not called CC=3 SCAR=16.0
Pod undocumented
320 0 $self->__is_valid_command('info');
321 0 my ($ret, $error) = $self->__info(@_);
322 0 ▶ confess "[info] $error, " if defined $error;
Branch: 2 branches
BranchTrueFalseTotal
if defined $error 3 5 8
if defined $error 0 0 0
323 0 ▶ return $ret unless ref $ret eq 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $ret eq "ARRAY" 5 0 5
unless ref $ret eq "ARRAY" 0 0 0
324 0 return @$ret;
325 }
326  
327 0 ▶sub quit {
Subroutine quit: not called CC=1 SCAR=6.9
Pod undocumented
328 0 ▶ my $self = shift;
Subroutine quit: not called CC=1 SCAR=6.9
Pod undocumented
329 0 $self->__is_valid_command('quit');
330 0 $self->__quit(@_);
331 }
332  
333 0 ▶sub shutdown {
Subroutine shutdown: not called CC=1 SCAR=6.9
Pod undocumented
334 0 ▶ my $self = shift;
Subroutine shutdown: not called CC=1 SCAR=6.9
Pod undocumented
335 0 $self->__is_valid_command('shutdown');
336 0 $self->__shutdown(@_);
337 }
338  
339 0 ▶sub select {
Subroutine select: not called CC=2 SCAR=17.9
Pod undocumented
340 0 ▶ my $self = shift;
Subroutine select: not called CC=2 SCAR=17.9
Pod undocumented
341 0 my $database = shift;
342 0 $self->__is_valid_command('select');
343 0 my ($ret, $error) = $self->__std_cmd('SELECT', $database, @_);
344 0 ▶ confess "[SELECT] $error, " if defined $error;
Branch: 2 branches
BranchTrueFalseTotal
if defined $error 0 0 0
if defined $error 0 0 0
345 0 $self->__get_data->{current_database} = $database;
346 0 return $ret;
347 }
348  
349 3 ▶sub __subscription_cmd {
Subroutine __subscription_cmd: called CC=7 SCAR=29.3
350 0 ▶ my $self = shift;
Subroutine __subscription_cmd: not called CC=7 SCAR=29.3
351 0 my $pr = shift;
352 0 my $unsub = shift;
353 0 my $command = shift;
354 0 my $cb = pop;
355 0 weaken $self;
356  
357 0 ▶ confess("Missing required callback in call to $command(), ")
Branch: 2 branches
BranchTrueFalseTotal
unless ref $cb eq "CODE" 0 3 3
unless ref $cb eq "CODE" 0 0 0
358 unless ref($cb) eq 'CODE';
359  
360 0 $self->wait_all_responses;
361  
362 0 ▶ while($self->__get_data->{cbs}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->__get_data->{"cbs"}) 0 3 3
if ($self->__get_data->{"cbs"}) 0 0 0
363 0 $self->__wait_for_event(1);
364 }
365  
366 0 my @subs = @_;
367 0 ▶ @subs = $self->__process_unsubscribe_requests($cb, $pr, @subs)
Branch: 2 branches
BranchTrueFalseTotal
if $unsub 0 3 3
if $unsub 0 0 0
368 if $unsub;
369  
370 0 ▶ if(@subs) {
Branch: 2 branches
BranchTrueFalseTotal
if (@subs) 3 0 3
if (@subs) 0 0 0
371 0 $self->__get_data->{cbs} = { map { ("${pr}message:$_" => $cb) } @subs };
372 0 for my $sub(@subs) {
373 0 $self->__send_subscription_cmd(
374 $command,
375 $sub,
376 $self->__subscription_callbak,
377 );
378 }
379 0 ▶ while($self->__get_data->{cbs}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->__get_data->{"cbs"}) 0 3 3
if ($self->__get_data->{"cbs"}) 0 0 0
380 0 $self->__wait_for_event(1);
381 }
382 }
383 }
384  
385 3 ▶sub __subscription_callbak {
Subroutine __subscription_callbak: called CC=2 SCAR=9.8
386 0 ▶ my $self = shift;
Subroutine __subscription_callbak: not called CC=2 SCAR=9.8
387 0 my $cb = $self->__get_data->{callback};
388 0 ▶ return $cb if $cb;
Branch: 2 branches
BranchTrueFalseTotal
if $cb 0 3 3
if $cb 0 0 0
389  
390 0 weaken $self;
391 12 ▶ $cb = sub {
Subroutine __ANON__: called CC=3 SCAR=15.0
392 0 ▶ my $cbs = $self->__get_data->{cbs};
Subroutine __ANON__: not called CC=3 SCAR=15.0
393 0 ▶ if($cbs) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cbs) { } 3 9 12
if ($cbs) { } 0 0 0
394 0 $self->__process_subscription_changes($cbs, @_);
395 0 ▶ unless(%$cbs) {
Branch: 2 branches
BranchTrueFalseTotal
unless (%$cbs) 3 0 3
unless (%$cbs) 0 0 0
396 0 $self->__get_data->{cbs} = undef;
397 }
398 } else {
399 0 $self->__process_pubsub_msg(@_);
400 }
401 0 };
402  
403 0 $self->__get_data->{callback} = $cb;
404 0 return $cb;
405 }
406  
407 3 ▶sub subscribe { shift->__subscription_cmd('', 0, subscribe => @_) }
Subroutine subscribe: called CC=1 SCAR=0.0
Pod undocumented
408 0 ▶sub psubscribe { shift->__subscription_cmd('p', 0, psubscribe => @_) }
Subroutine psubscribe: not called CC=1 SCAR=6.9
Pod undocumented
409 0 ▶sub unsubscribe { shift->__subscription_cmd('', 1, unsubscribe => @_) }
Subroutine unsubscribe: not called CC=1 SCAR=6.9
Pod undocumented
410 0 ▶sub punsubscribe { shift->__subscription_cmd('p', 1, punsubscribe => @_) }
Subroutine punsubscribe: not called CC=1 SCAR=6.9
Pod undocumented
411  
412 0 ▶sub __process_unsubscribe_requests {
Subroutine __process_unsubscribe_requests: not called CC=5 SCAR=34.0
413 0 my ($self, $cb, $pr, @unsubs) = @_;
414 0 my $subs = $self->__get_data->{subscribers};
415  
416 0 my @subs_to_unsubscribe;
417 0 for my $sub (@unsubs) {
418 0 my $key = "${pr}message:$sub";
419 0 ▶ next unless $subs->{$key} && @{ $subs->{$key} };
Branch: 1 branch
BranchTrueFalseTotal
unless $subs->{$key} and @{$subs->{$key};} 0 0 0
Condition0%0/3
$subs->{$key} and @{$subs->{$key};}
!ll&&!rl&&r
000
MC/DC0%0/2
$subs->{$key} and @{$subs->{$key};}
$subs->{$key} @{$subs->{$key};}
Truth table0%0/3
A and B
A= $subs->{$key}B= @{$subs->{$key};}
ABresult
0X0
100
111
420 0 my $cbs = $subs->{$key} = [grep { $_ ne $cb } @{ $subs->{$key} }];
421 0 ▶ next if @$cbs;
Branch: 1 branch
BranchTrueFalseTotal
if @$cbs 0 0 0
422  
423 0 delete $subs->{$key};
424 0 push @subs_to_unsubscribe, $sub;
425 }
426 0 return @subs_to_unsubscribe;
427 }
428  
429 3 ▶sub __process_subscription_changes {
Subroutine __process_subscription_changes: called CC=3 SCAR=11.3
430 3 my ($self, $expected, $m, $error) = @_;
431 3 my $subs = $self->__get_data->{subscribers};
432  
433 ## Deal with pending PUBLISH'ed messages
434 3 ▶ if ($m->[0] =~ /^p?message$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m->[0] =~ /^p?message$/) 0 3 3
435 0 $self->__process_pubsub_msg($m);
436 0 return ;
437 }
438  
439 3 my ($key, $unsub) = $m->[0] =~ m/^(p)?(un)?subscribe$/;
440 3 $key .= "message:$m->[1]";
441 3 my $cb = delete $expected->{$key};
442  
443 3 ▶ push @{ $subs->{$key} }, $cb unless $unsub;
Branch: 1 branch
BranchTrueFalseTotal
unless $unsub 2 1 3
444 }
445  
446 9 ▶sub __process_pubsub_msg {
Subroutine __process_pubsub_msg: called CC=4 SCAR=13.9
447 9 my ($self, $m) = @_;
448 9 my $subs = $self->__get_data->{subscribers};
449  
450 9 my $sub = $m->[1];
451 9 my $cbid = "$m->[0]:$sub";
452 9 my $data = pop @$m;
453 9 ▶ my $topic = defined $m->[2] ? $m->[2] : $sub;
Branch: 1 branch
BranchTrueFalseTotal
defined $m->[2] ? : 0 9 9
454  
455 9 ▶ if (!exists $subs->{$cbid}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $subs->{$cbid}) 5 4 9
456 5 warn "Message for topic '$topic' ($cbid) without expected callback, ";
457 5 return 0;
458 }
459  
460 4 $_->($data, $topic, $sub) for @{ $subs->{$cbid} };
461  
462 4 return 1;
463  
464 }
465  
466 82 ▶sub __is_valid_command {
Subroutine __is_valid_command: called CC=2 SCAR=7.2
467 82 my ($self, $cmd) = @_;
468  
469 82 ▶ confess("Cannot use command '$cmd' while in SUBSCRIBE mode, ")
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_subscriber 0 82 82
470 if $self->is_subscriber;
471 }
472  
473 42 1; # End of Redis.pm
474  
475 __END__
↑ Summary builder/MyBuilder.pm »