Test/TCP.pm

filter: Statementstmt 100% 99 / 99 Branchbran 84.7% 39 / 46 Conditioncond 64.7% 11 / 17 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 100% 19 / 19 Podpod 100% 7 / 7 totaltotal 89.9% 179 / 199 CC 30 SCAR 17.5
CC
30
Cov
92%
stop
10.1
new
8.2
wait_port
7.0
CRAP 5.7
« Net/EmptyPort.pm ↑ Summary Test/TCP/CheckPort.pm »
1 package Test::TCP;
2 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use 5.00800;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 our $VERSION = '2.22';
6 18 ▶use base qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use Test::SharedFork 0.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use Test::More ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 18 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 18 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 18 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 18 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 18 ▶use Net::EmptyPort qw(empty_port check_port);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 18 our @EXPORT = qw/ empty_port test_tcp wait_port /;
16  
17 # process does not die when received SIGTERM, on win32.
18 18 ▶my $TERMSIG = $^O eq 'MSWin32' ? 'KILL' : 'TERM';
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 18 18
19  
20 16 ▶sub test_tcp {
Subroutine test_tcp: called CC=3 SCAR=11.0
Pod documented
21 16 my %args = @_;
22 16 for my $k (qw/client server/) {
23 31 ▶ die "missing mandatory parameter $k" unless exists $args{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{$k} 2 29 31
24 }
25 14 my $server_code = delete $args{server};
26 14 my $client_code = delete $args{client};
27  
28 14 my $server = Test::TCP->new(
29 code => $server_code,
30 %args,
31 );
32 12 $client_code->($server->port, $server->pid);
33 9 undef $server; # make sure
34 }
35  
36 16 ▶sub wait_port {
Subroutine wait_port: called CC=7 SCAR=19.5
Pod documented
37 16 my ($host, $port, $max_wait);
38 16 ▶ if (@_ && ref $_[0] eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ and ref $_[0] eq 'HASH') { } 13 3 16
elsif (@_ == 3) { } 1 2 3
Condition100%3/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
1213
MC/DC100%2/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
39 13 $host = $_[0]->{host};
40 13 $port = $_[0]->{port};
41 13 $max_wait = $_[0]->{max_wait};
42 } elsif (@_ == 3) {
43 # backward compat
44 1 ($port, (my $sleep), (my $retry)) = @_;
45 1 $max_wait = $sleep * $retry;
46 } else {
47 2 ($port, $max_wait) = @_;
48 }
49 16 ▶ $host = '127.0.0.1'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 3 13 16
50 unless defined $host;
51 16 ▶ $max_wait ||= 10;
Condition100%2/2
$max_wait ||= 10
l!l
151
MC/DC100%1/1
$max_wait ||= 10
$max_wait
Truth table100%2/2
A
A= $max_wait
Aresult
11
00
52  
53 16 ▶ Net::EmptyPort::wait_port({ host => $host, port => $port, max_wait => $max_wait })
Branch: 1 branch
BranchTrueFalseTotal
Net::EmptyPort::wait_port({"host", $host, "port", $port, "max_wait", $max_wait}) or die "cannot open port: ${host}:$port" 16 0 16
54 or die "cannot open port: $host:$port";
55 }
56  
57 # -------------------------------------------------------------------------
58 # OO-ish interface
59  
60 20 ▶sub new {
Subroutine new: called CC=8 SCAR=21.1
Pod documented
61 20 my $class = shift;
62 20 ▶ my %args = @_==1 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 1 19 20
63 20 ▶ Carp::croak("missing mandatory parameter 'code'") unless exists $args{code};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"code"} 1 19 20
64 19 my $self = bless {
65 auto_start => 1,
66 max_wait => 10,
67 host => '127.0.0.1',
68 _my_pid => $$,
69 %args,
70 }, $class;
71 19 ▶ if ($self->{listen}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'listen'}) { } 1 18 19
72 $self->{socket} ||= Net::EmptyPort::listen_socket({
73 host => $self->{host},
74 proto => $self->{proto},
75 1 ▶ }) or die "Cannot listen: $!";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"socket"} ||= Net::EmptyPort::listen_socket({"host", $self->{"host"}, "proto", $self->{"proto"}}) or die "Cannot listen: $!" 1 0 1
Condition33%1/3
$self->{"socket"} ||= Net::EmptyPort::listen_socket({"host", $self->{"host"}, "proto", $self->{"proto"}})
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"socket"} ||= Net::EmptyPort::listen_socket({"host", $self->{"host"}, "proto", $self->{"proto"}})
$self->{"socket"} Net::EmptyPort::listen_socket({"host", $self->{"host"}, "proto", $self->{"proto"}})
Truth table33%1/3
A ||= B
A= $self->{"socket"}B= Net::EmptyPort::listen_socket({"host", $self->{"host"}, "proto", $self->{"proto"}})
ABresult
1X1
011
000
76 1 $self->{port} = $self->{socket}->sockport;
77 }
78 else {
79 18 ▶ $self->{port} ||= empty_port({ host => $self->{host} });
Condition66%2/3
$self->{"port"} ||= empty_port({"host", $self->{"host"}})
l!l&&r!l&&!r
2160
MC/DC0%0/2
$self->{"port"} ||= empty_port({"host", $self->{"host"}})
$self->{"port"} empty_port({"host", $self->{"host"}})
Truth table66%2/3
A ||= B
A= $self->{"port"}B= empty_port({"host", $self->{"host"}})
ABresult
1X1
011
000
80 }
81 $self->start()
82 19 ▶ if $self->{auto_start};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"auto_start"} 17 2 19
83 16 return $self;
84 }
85  
86 13 ▶sub pid { $_[0]->{pid} }
Subroutine pid: called CC=1 SCAR=0.0
Pod documented
87 42 ▶sub port { $_[0]->{port} }
Subroutine port: called CC=1 SCAR=0.0
Pod documented
88  
89 17 ▶sub start {
Subroutine start: called CC=6 SCAR=18.1
Pod documented
90 17 my $self = shift;
91 17 my $pid = fork();
92 17 ▶ die "fork() failed: $!" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 17 17
93  
94 17 ▶ if ( $pid ) { # parent process.
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 14 3 17
95 14 $self->{pid} = $pid;
96 Test::TCP::wait_port({ host => $self->{host}, port => $self->port, max_wait => $self->{max_wait} })
97 14 ▶ unless $self->{socket};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"socket"} 13 1 14
98 14 return;
99 } else { # child process
100 3 ▶ $self->{code}->($self->{socket} || $self->port);
Condition33%1/3
$self->{'socket'} || $self->port
l!l&&r!l&&!r
030
MC/DC0%0/2
$self->{'socket'} || $self->port
$self->{'socket'} $self->port
Truth table33%1/3
A || B
A= $self->{'socket'}B= $self->port
ABresult
1X1
011
000
101 # should not reach here
102 2 ▶ if (kill 0, $self->{_my_pid}) { # warn only parent process still exists
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $self->{"_my_pid"}) 1 1 2
103 1 warn("[Test::TCP] Child process does not block(PID: $$, PPID: $self->{_my_pid})");
104 }
105 2 exit 0;
106 }
107 }
108  
109 19 ▶sub stop {
Subroutine stop: called CC=10 SCAR=23.2
Pod documented
110 19 my $self = shift;
111  
112 19 ▶ return unless defined $self->{pid};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"pid"} 5 14 19
113 14 ▶ return unless $self->{_my_pid} == $$;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_my_pid"} == $$ 1 13 14
114  
115 # This is a workaround for win32 fork emulation's bug.
116 #
117 # kill is inherently unsafe for pseudo-processes in Windows
118 # and the process calling kill(9, $pid) may be destabilized
119 # The call to Sleep will decrease the frequency of this problems
120 #
121 # SEE ALSO:
122 # http://www.gossamer-threads.com/lists/perl/porters/261805
123 # https://rt.cpan.org/Ticket/Display.html?id=67292
124 13 ▶ Win32::Sleep(0) if $^O eq "MSWin32"; # will relinquish the remainder of its time slice
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 13 13
125  
126 13 kill $TERMSIG => $self->{pid};
127  
128 13 ▶ Win32::Sleep(0) if $^O eq "MSWin32"; # will relinquish the remainder of its time slice
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 13 13
129  
130  
131 13 local $?; # waitpid modifies original $?.
132 13 LOOP: while (1) {
133 25 my $kid = waitpid( $self->{pid}, 0 );
134 25 ▶ if ($^O ne 'MSWin32') { # i'm not in hell
Branch: 1 branch
BranchTrueFalseTotal
if ($^O ne "MSWin32") 25 0 25
135 25 ▶ if (POSIX::WIFSIGNALED($?)) {
Branch: 1 branch
BranchTrueFalseTotal
if (POSIX::WIFSIGNALED($?)) 11 14 25
136 11 my $signame = (split(' ', $Config{sig_name}))[POSIX::WTERMSIG($?)];
137 11 ▶ if ($signame =~ /^(ABRT|PIPE)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signame =~ /^(ABRT|PIPE)$/) 1 10 11
138 1 Test::More::diag("your server received SIG$signame");
139 }
140 }
141 }
142 25 ▶ if ($kid == 0 || $kid == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kid == 0 or $kid == -1) 13 12 25
Condition66%2/3
$kid == 0 or $kid == -1
l!l&&r!l&&!r
01312
MC/DC50%1/2
$kid == 0 or $kid == -1
$kid == 0 $kid == -1
Truth table66%2/3
A or B
A= $kid == 0B= $kid == -1
ABresult
1X1
011
000
143 13 last LOOP;
144 }
145 }
146 13 undef $self->{pid};
147 }
148  
149 19 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
150 19 my $self = shift;
151 19 local $@;
152 19 $self->stop();
153 }
154  
155 18 1;
156 __END__
« Net/EmptyPort.pm ↑ Summary Test/TCP/CheckPort.pm »