blib/lib/Test/RedisServer.pm

filter: Statementstmt 75.1% 106 / 141 Branchbran 44.8% 26 / 58 Conditioncond 18.7% 3 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 85.0% 17 / 20 Podpod 100% 7 / 7 totaltotal 63.1% 159 / 252 CC 36 SCAR 24.8
CC
36
Cov
63%
stop
30.0
start
20.0
connect_info
20.0
CRAP 12.0
↑ Summary
1 package Test::RedisServer;
2 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 14 our $VERSION = '0.25';
7  
8 14 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use POSIX qw(SIGTERM WNOHANG);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use Time::HiRes qw(sleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use Errno ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 14 ▶use Redis;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 14 has auto_start => (
16 is => 'rw',
17 default => 1,
18 );
19  
20 14 has [qw/pid _owner_pid/] => (
21 is => 'rw',
22 );
23  
24 has conf => (
25 is => 'rw',
26 isa => 'HashRef',
27 14 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 14 );
29  
30 14 has timeout => (
31 is => 'rw',
32 default => 3,
33 );
34  
35 14 has tmpdir => (
36 is => 'rw',
37 lazy_build => 1,
38 );
39  
40 14 has _redis => (
41 is => 'rw',
42 isa => 'Redis',
43 );
44  
45 14 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 14 ▶sub BUILD {
Subroutine BUILD: called CC=7 SCAR=22.5
Pod documented
48 14 my ($self) = @_;
49  
50 14 $self->_owner_pid($$);
51  
52 14 my $tmpdir = $self->tmpdir;
53 14 ▶ unless (defined $self->conf->{port} or defined $self->conf->{unixsocket}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->conf->{"port"} or defined $self->conf->{"unixsocket"}) 14 0 14
Condition33%1/3
defined $self->conf->{"port"} or defined $self->conf->{"unixsocket"}
l!l&&r!l&&!r
0014
MC/DC0%0/2
defined $self->conf->{"port"} or defined $self->conf->{"unixsocket"}
defined $self->conf->{"port"} defined $self->conf->{"unixsocket"}
Truth table33%1/3
A or B
A= defined $self->conf->{"port"}B= defined $self->conf->{"unixsocket"}
ABresult
1X1
011
000
54 14 $self->conf->{unixsocket} = "$tmpdir/redis.sock";
55 14 $self->conf->{port} = '0';
56 }
57  
58 14 ▶ unless (defined $self->conf->{dir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->conf->{"dir"}) 14 0 14
59 14 $self->conf->{dir} = "$tmpdir/";
60 }
61  
62 14 ▶ if ($self->conf->{loglevel} and $self->conf->{loglevel} eq 'warning') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"loglevel"} and $self->conf->{"loglevel"} eq "warning") 0 14 14
Condition33%1/3
$self->conf->{"loglevel"} and $self->conf->{"loglevel"} eq "warning"
!ll&&!rl&&r
1400
MC/DC0%0/2
$self->conf->{"loglevel"} and $self->conf->{"loglevel"} eq "warning"
$self->conf->{"loglevel"} $self->conf->{"loglevel"} eq "warning"
Truth table33%1/3
A and B
A= $self->conf->{"loglevel"}B= $self->conf->{"loglevel"} eq "warning"
ABresult
0X0
100
111
63 0 warn "Test::RedisServer does not support \"loglevel warning\", using \"notice\" instead.\n";
64 0 $self->conf->{loglevel} = 'notice';
65 }
66  
67 14 ▶ if ($self->auto_start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auto_start) 14 0 14
68 14 $self->start;
69 }
70 }
71  
72 14 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=3 SCAR=13.1
Pod documented
73 14 my ($self) = @_;
74 14 ▶ $self->stop if defined $self->pid && $$ == $self->_owner_pid;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->pid and $$ == $self->_owner_pid 0 14 14
Condition33%1/3
defined $self->pid and $$ == $self->_owner_pid
!ll&&!rl&&r
1400
MC/DC0%0/2
defined $self->pid and $$ == $self->_owner_pid
defined $self->pid $$ == $self->_owner_pid
Truth table33%1/3
A and B
A= defined $self->pidB= $$ == $self->_owner_pid
ABresult
0X0
100
111
75 }
76  
77 14 ▶sub start {
Subroutine start: called CC=15 SCAR=30.0
Pod documented
78 14 my ($self) = @_;
79  
80 14 ▶ return if defined $self->pid;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->pid 0 14 14
81  
82 14 my $tmpdir = $self->tmpdir;
83 14 ▶ open my $logfh, '>>', "$tmpdir/redis-server.log"
Branch: 1 branch
BranchTrueFalseTotal
open my $logfh, ">>", "$tmpdir/redis-server.log" or croak("failed to create log file: $tmpdir/redis-server.log") 14 0 14
84 or croak "failed to create log file: $tmpdir/redis-server.log";
85  
86 14 my $pid = fork;
87 14 ▶ croak "fork(2) failed:$!" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 14 14
88  
89 14 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 7 7 14
90 7 ▶ open STDOUT, '>&', $logfh or croak "dup(2) failed:$!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $logfh or croak("dup(2) failed:$!") 7 0 7
91 7 ▶ open STDERR, '>&', $logfh or croak "dup(2) failed:$!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $logfh or croak("dup(2) failed:$!") 7 0 7
92 7 $self->exec;
93 }
94 7 close $logfh;
95  
96 7 my $ready;
97 7 my $elapsed = 0;
98 7 $self->pid($pid);
99  
100 7 ▶ while ($elapsed <= $self->timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elapsed <= $self->timeout) 29 0 29
101 29 ▶ if (waitpid($pid, WNOHANG) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (waitpid($pid, 1) > 0) { } 7 22 29
102 7 $self->pid(undef);
103 7 last;
104 }
105 else {
106 22 my $log = q[];
107 22 ▶ if (open $logfh, '<', "$tmpdir/redis-server.log") {
Branch: 1 branch
BranchTrueFalseTotal
if (open $logfh, "<", "$tmpdir/redis-server.log") 22 0 22
108 22 $log = do { local $/; <$logfh> };
109 22 close $logfh;
110 }
111  
112 # confirmed this message is included from v1.3.6 (older version in git repo)
113 # to current HEAD (2012-07-30)
114 # The message has changed a bit with Redis 4.x, make regexp a bit more flexible
115 22 ▶ if ( $log =~ /[Rr]eady to accept connections/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log =~ /[Rr]eady to accept connections/) 0 22 22
116 0 $ready = 1;
117 0 last;
118 }
119 }
120  
121 22 sleep($elapsed += 0.1);
122 }
123  
124 7 ▶ unless ($ready) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ready) 7 0 7
125 7 ▶ if ($self->pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pid) 0 7 7
126 0 $self->pid(undef);
127 0 kill SIGTERM, $pid;
128 0 ▶ while (waitpid($pid, WNOHANG) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (waitpid($pid, 1) >= 0) 0 0 0
129 }
130 }
131  
132 7 croak "*** failed to launch redis-server ***\n" . do {
133 7 my $log = q[];
134 7 ▶ if (open $logfh, '<', "$tmpdir/redis-server.log") {
Branch: 1 branch
BranchTrueFalseTotal
if (open $logfh, "<", "$tmpdir/redis-server.log") 7 0 7
135 7 $log = do { local $/; <$logfh> };
136 7 close $logfh;
137 }
138 7 $log;
139 };
140 }
141  
142 # This is sometimes needed to send commands to RedisServer during the stop process.
143 # Generally, we would like to generate it lazily and not have it as a property
144 # of the object. However, if you try to create the object at the stop,
145 # the object generation may fail, such as missing the socket file. Therefore,
146 # we will make the object and store it as property here.
147 0 $self->_redis( Redis->new($self->connect_info) );
148  
149 0 $self->pid($pid);
150 }
151  
152 7 ▶sub exec {
Subroutine exec: called CC=4 SCAR=14.5
Pod documented
153 7 my ($self) = @_;
154  
155 7 my $tmpdir = $self->tmpdir;
156  
157 7 ▶ open my $conffh, '>', "$tmpdir/redis.conf" or croak "cannot write conf: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $conffh, ">", "$tmpdir/redis.conf" or croak("cannot write conf: $!") 7 0 7
158 7 print $conffh $self->_conf_string;
159 7 close $conffh;
160  
161 exec 'redis-server', "$tmpdir/redis.conf"
162 7 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
CORE::exec "redis-server", "$tmpdir/redis.conf" or do { if ($! == 2) { print STDERR "exec failed: no such file or directory\n"; } else { print STDERR "exec failed: unexpected error: $!\n"; } ; exit $? } 0 7 7
163 7 ▶ if ($! == Errno::ENOENT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 2) { } 7 0 7
164 7 print STDERR "exec failed: no such file or directory\n";
165 }
166 else {
167 0 print STDERR "exec failed: unexpected error: $!\n";
168 }
169 7 exit($?);
170 };
171 }
172  
173 0 ▶sub stop {
Subroutine stop: not called CC=5 SCAR=34.0
Pod documented
174 0 my ($self, $sig) = @_;
175  
176 0 local $?; # waitpid may change this value :/
177 0 ▶ return unless defined $self->pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->pid 0 0 0
178  
179 # If the tmpdir has disappeared, clear the save config to prevent saving
180 # in the server terminating process. The newer Redis will save on stop
181 # for robustness, but will keep blocking if the directory is missing.
182 #
183 # It is unlikely that tmpdir will disappear first, but if both the RedisServer
184 # object and the tmpdir are defined globally, it may happen because the order
185 # in which they are DESTLOYed is uncertain.
186 0 ▶ if (! -d $self->tmpdir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->tmpdir) 0 0 0
187 0 $self->_redis->config_set('appendonly', 'no');
188 0 $self->_redis->config_set('save', '');
189 }
190  
191 0 ▶ $sig ||= SIGTERM;
Condition0%0/2
$sig ||= 15
l!l
00
MC/DC0%0/1
$sig ||= 15
$sig
Truth table0%0/2
A
A= $sig
Aresult
11
00
192  
193 0 kill $sig, $self->pid;
194 0 ▶ while (waitpid($self->pid, WNOHANG) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (waitpid($self->pid, 1) >= 0) 0 0 0
195 }
196  
197 0 $self->pid(undef);
198 }
199  
200 0 ▶sub wait_exit {
Subroutine wait_exit: not called CC=2 SCAR=17.9
Pod documented
201 0 my ($self) = @_;
202  
203 0 local $?;
204  
205 0 my $kid;
206 0 my $pid = $self->pid;
207 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$kid >= 0 and do { do { $kid = waitpid($pid, 1); &Test::RedisServer::sleep("0.1") } } 0 0 0
208 0 $kid = waitpid($pid, WNOHANG);
209 0 sleep 0.1;
210 } while $kid >= 0;
211  
212 0 $self->pid(undef);
213 }
214  
215 0 ▶sub connect_info {
Subroutine connect_info: not called CC=4 SCAR=30.0
Pod documented
216 0 my ($self) = @_;
217  
218 0 ▶ my $host = $self->conf->{bind} || '0.0.0.0';
Condition0%0/2
$self->conf->{'bind'} || '0.0.0.0'
l!l
00
MC/DC0%0/1
$self->conf->{'bind'} || '0.0.0.0'
$self->conf->{'bind'}
Truth table0%0/2
A
A= $self->conf->{'bind'}
Aresult
11
00
219 0 my $port = $self->conf->{port};
220 0 my $sock = $self->conf->{unixsocket};
221  
222 0 ▶ if ($port && $port > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port and $port > 0) { } 0 0 0
Condition0%0/3
$port and $port > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$port and $port > 0
$port $port > 0
Truth table0%0/3
A and B
A= $portB= $port > 0
ABresult
0X0
100
111
223 0 return (server => $host . ':' . $port);
224 }
225 else {
226 0 return (sock => $sock);
227 }
228 }
229  
230 14 ▶sub _build_tmpdir {
Subroutine _build_tmpdir: called CC=1 SCAR=0.0
231 14 File::Temp->newdir( CLEANUP => 1 );
232 }
233  
234 7 ▶sub _conf_string {
Subroutine _conf_string: called CC=3 SCAR=11.0
235 7 my ($self) = @_;
236  
237 7 my $conf = q[];
238 7 my %conf = %{ $self->conf };
239 7 ▶ while (my ($k, $v) = each %conf) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %conf) 21 7 28
240 21 ▶ next unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 21 21
241 21 $conf .= "$k $v\n";
242 }
243  
244 7 $conf;
245 }
246  
247 14 __PACKAGE__->meta->make_immutable;
248  
249 __END__
↑ Summary