blib/lib/Test/ttserver.pm

filter: Statementstmt 42.2% 49 / 116 Branchbran 10.7% 6 / 56 Conditioncond 6.2% 1 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 54.1% 13 / 24 Podpod 100% 12 / 12 totaltotal 34.6% 81 / 234 CC 37 SCAR 42.1
CC
37
Cov
30%
start
182.0
new
114.8
stop
20.0
CRAP 67.3
↑ Summary
1 package Test::ttserver;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use POSIX qw/SIGTERM WNOHANG :sys_wait_h/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Time::HiRes 'sleep';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Test::TCP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 our $VERSION = '0.003';
14 8 $VERSION = eval $VERSION;
15  
16 8 our $errstr;
17 8 our @SearchPaths = qw(/usr/bin /usr/local/bin);
18 8 our @BooleanArgs = qw(dmn kl ld le uas rcc);
19 8 our %Defaults = (
20 debug => 0,
21 auto_start => 1,
22 base_dir => undef,
23 bin => undef,
24 );
25  
26 4 ▶sub new {
Subroutine new: called CC=13 SCAR=47.4
Pod documented
27 4 my $class = shift;
28 4 ▶ my $dbname = shift || '';
Condition50%1/2
shift() || ''
l!l
04
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
29 my $self = bless +{
30 %Defaults,
31 0 ▶ @_ == 1 ? %{$_[0]} : @_,
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 4 4
32 }, $class;
33  
34 4 ▶ $self->{'bin'} = $self->_find_bin or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"bin"} = $self->_find_bin or return 0 4 4
35  
36 0 ▶ if ( defined $self->{'base_dir'} && $self->{'base_dir'} !~ m{^/} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'base_dir'} and not $self->{'base_dir'} =~ m[^/]) { } 0 0 0
Condition0%0/3
defined $self->{"base_dir"} and not $self->{"base_dir"} =~ m[^/]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"base_dir"} and not $self->{"base_dir"} =~ m[^/]
defined $self->{"base_dir"} not $self->{"base_dir"} =~ m[^/]
Truth table0%0/3
A and not(B)
A= defined $self->{"base_dir"}B= not $self->{"base_dir"} =~ m[^/]
ABresult
0X0
100
111
37 0 $self->{'base_dir'} = getcwd . '/' . $self->{'base_dir'}
38 } else {
39 $self->{'base_dir'} = File::Temp::tempdir(
40 0 ▶ CLEANUP => $ENV{TEST_TTSERVE_PRESERVE} ? undef : 1,
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'TEST_TTSERVE_PRESERVE'} ? : 0 0 0
41 );
42 }
43  
44 0 my %args;
45 0 for (keys %$self) {
46 0 ▶ $args{$_} = delete $self->{$_} unless exists $Defaults{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Test::ttserver::Defaults{$_} 0 0 0
47 }
48 0 $args{'pid'} = $self->{'base_dir'} . '/tmp/pid';
49 0 ▶ $args{'host'} ||= $self->host;
Condition0%0/3
$args{"host"} ||= $self->host
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"host"} ||= $self->host
$args{"host"} $self->host
Truth table0%0/3
A ||= B
A= $args{"host"}B= $self->host
ABresult
1X1
011
000
50 0 ▶ $args{'port'} ||= $self->port;
Condition0%0/3
$args{"port"} ||= $self->port
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"port"} ||= $self->port
$args{"port"} $self->port
Truth table0%0/3
A ||= B
A= $args{"port"}B= $self->port
ABresult
1X1
011
000
51 0 $self->{'args'} = \%args;
52  
53 0 ▶ $self->{'dbname'} = $dbname ? $self->{'base_dir'} .'/tmp/'. $dbname : '';
Branch: 1 branch
BranchTrueFalseTotal
$dbname ? : 0 0 0
54  
55 0 ▶ if ( $self->{'auto_start'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"auto_start"}) 0 0 0
56 0 $self->setup;
57 0 $self->start;
58 }
59  
60 0 return $self;
61 }
62  
63 4 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.2
64 4 my $self = shift;
65 4 ▶ $self->stop if defined $self->pid;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->pid 0 4 4
66 }
67  
68 0 ▶sub setup {
Subroutine setup: not called CC=1 SCAR=6.9
Pod documented
69 0 my $self = shift;
70 0 mkpath( $self->{'base_dir'} . '/tmp', { verbose => $self->{'debug'} });
71 }
72  
73 0 ▶sub start {
Subroutine start: not called CC=13 SCAR=52.0
Pod documented
74 0 my $self = shift;
75 0 ▶ return if defined $self->pid;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->pid 0 0 0
76  
77 0 my $log_file = $self->{'base_dir'} . '/tmp/ttserver.log';
78 0 ▶ my $log_fh = IO::File->new($log_file, O_WRONLY|O_APPEND|O_CREAT)
Branch: 1 branch
BranchTrueFalseTotal
my $log_fh = "IO::File"->new($log_file, 1089) or die qq[failed to create log file: $! "$log_file"] 0 0 0
79 or die qq/failed to create log file: $! "$log_file"/;
80  
81 0 my $pid = fork;
82 0 ▶ die qq/failed to fork: $!/ unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
83  
84 0 ▶ if ( $pid == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 0 0 0
85 0 my $bin = $self->{'bin'};
86 0 my $args = $self->{'args'};
87 0 my %bool; @bool{@BooleanArgs} = (1) x @BooleanArgs;
88 my @command = (
89 $self->{'bin'},
90 ( map {
91 0 ▶ $bool{$_} ? "-$_" : ("-$_" => $args->{$_})
Branch: 1 branch
BranchTrueFalseTotal
$bool{$_} ? : 0 0 0
92 } (keys %$args) ),
93 0 $self->{'dbname'},
94 );
95 0 ▶ pop @command unless @command and defined $command[-1];
Branch: 1 branch
BranchTrueFalseTotal
unless @command and defined $command[-1] 0 0 0
Condition0%0/3
@command and defined $command[-1]
!ll&&!rl&&r
000
MC/DC0%0/2
@command and defined $command[-1]
@command defined $command[-1]
Truth table0%0/3
A and B
A= @commandB= defined $command[-1]
ABresult
0X0
100
111
96 0 ▶ warn "@command\n" if $self->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
97 0 ▶ open STDOUT, '>&', $log_fh or die qq/failed to dup: $! "stdout"/;
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $log_fh or die qq[failed to dup: $! "stdout"] 0 0 0
98 0 ▶ open STDERR, '>&', $log_fh or die qq/failed to dup: $! "stderr"/;
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $log_fh or die qq[failed to dup: $! "stderr"] 0 0 0
99 0 exec @command;
100 0 die qq/failed to launch ttserver: $? "$bin"/;
101 }
102  
103 0 $log_fh->close;
104 0 ▶ while (! -e $self->{'args'}{'pid'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $self->{"args"}{"pid"}) 0 0 0
105 0 ▶ if ( 0 < waitpid $pid, WNOHANG ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < waitpid($pid, 1)) 0 0 0
106 0 die qq/*** failed to launch ttserver ***\n/ . $self->_get_log( $log_file );
107 }
108 0 sleep 0.1;
109 }
110  
111 0 $self->{'child'} = $pid;
112 }
113  
114 0 ▶sub stop {
Subroutine stop: not called CC=4 SCAR=30.0
Pod documented
115 0 my ( $self, $sig ) = @_;
116 0 ▶ return unless defined $self->pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->pid 0 0 0
117 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
118 0 kill $sig, $self->pid;
119 0 ▶ 1 while ( 0 >= waitpid $self->pid, 0 );
Branch: 1 branch
BranchTrueFalseTotal
0 >= waitpid($self->pid, 0) and do { '???' } 0 0 0
120 0 my $is_exited = WIFEXITED( $? );
121 0 delete $self->{'child'};
122 # might remain for example when sending SIGKILL
123 0 unlink $self->{'args'}{'pid'};
124 0 return $is_exited;
125 }
126  
127 0 ▶sub socket { ($_[0]->host, $_[0]->port) }
Subroutine socket: not called CC=1 SCAR=6.9
Pod documented
128 0 ▶sub host { shift->{'args'}{'host'} || '127.0.0.1' }
Subroutine host: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"args"}{"host"} or "127.0.0.1" 0 0 0
129 0 ▶sub port { shift->{'args'}{'port'} || empty_port }
Subroutine port: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"args"}{"port"} or empty_port() 0 0 0
130 4 ▶sub pid { shift->{'child'} }
Subroutine pid: called CC=1 SCAR=0.0
Pod documented
131 0 ▶sub is_up { shift->{'child'} ? 1 : 0 }
Subroutine is_up: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{'child'} ? : 0 0 0
132 0 ▶sub is_down { shift->is_up ? 0 : 1 }
Subroutine is_down: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
(shift())->is_up ? : 0 0 0
133 0 ▶sub pid_file { shift->{'args'}{'pid'} }
Subroutine pid_file: not called CC=1 SCAR=6.9
Pod documented
134 0 ▶sub args { shift->{'args'} }
Subroutine args: not called CC=1 SCAR=6.9
Pod documented
135  
136 4 ▶sub _find_bin {
Subroutine _find_bin: called CC=4 SCAR=13.9
137 4 my $self = shift;
138  
139 4 my @paths = @SearchPaths;
140 4 ▶ push @paths, split ':', $ENV{'PATH'} if defined $ENV{'PATH'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"PATH"} 3 1 4
141  
142 4 for my $path (@paths) {
143 36 my $bin = $path . '/ttserver';
144 36 ▶ return $bin if -x $bin;
Branch: 1 branch
BranchTrueFalseTotal
if -x $bin 0 36 36
145 }
146  
147 4 $errstr = "could not find ttserver, please set appropriate PATH";
148 4 return;
149 }
150  
151 0 ▶sub _get_log {
Subroutine _get_log: not called CC=2 SCAR=17.9
152 0 my ( $self, $log_file ) = @_;
153 0 my $log = '';
154 0 ▶ if ( my $log_fh = IO::File->new($log_file, O_RDONLY) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $log_fh = "IO::File"->new($log_file, 0)) 0 0 0
155 0 $log = do { local $/; <$log_fh> };
156 0 $log_fh->close;
157 }
158 0 return $log;
159 }
160  
161 8 1;
162  
163 __END__
↑ Summary