| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
@_ and ref $_[0] eq "HASH"
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"
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 50 | unless defined $host; | ||||||||||||||||||||||||||||||||
| 51 | 16 | ▶ | $max_wait ||= 10; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$max_wait ||= 10
MC/DC100%1/1
$max_wait ||= 10
$max_wait
Truth table100%2/2
A
A= $max_wait
| |||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 16 | ▶ | Net::EmptyPort::wait_port({ host => $host, port => $port, max_wait => $max_wait }) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 63 | 20 | ▶ | Carp::croak("missing mandatory parameter 'code'") unless exists $args{code}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 72 | $self->{socket} ||= Net::EmptyPort::listen_socket({ | ||||||||||||||||||||||||||||||||
| 73 | host => $self->{host}, | ||||||||||||||||||||||||||||||||
| 74 | proto => $self->{proto}, | ||||||||||||||||||||||||||||||||
| 75 | 1 | ▶ | }) or die "Cannot listen: $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"socket"} ||= Net::EmptyPort::listen_socket({"host", $self->{"host"}, "proto", $self->{"proto"}})
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"}})
| |||||||||||||||||||||||||||||||||
| 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"}})
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"}})
| |||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | $self->start() | ||||||||||||||||||||||||||||||||
| 82 | 19 | ▶ | if $self->{auto_start}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | 17 | ▶ | if ( $pid ) { # parent process. | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 98 | 14 | return; | |||||||||||||||||||||||||||||||
| 99 | } else { # child process | ||||||||||||||||||||||||||||||||
| 100 | 3 | ▶ | $self->{code}->($self->{socket} || $self->port); | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->{'socket'} || $self->port
MC/DC0%0/2
$self->{'socket'} || $self->port
$self->{'socket'}
$self->port
Truth table33%1/3
A || B
A= $self->{'socket'}B= $self->port
| |||||||||||||||||||||||||||||||||
| 101 | # should not reach here | ||||||||||||||||||||||||||||||||
| 102 | 2 | ▶ | if (kill 0, $self->{_my_pid}) { # warn only parent process still exists | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 113 | 14 | ▶ | return unless $self->{_my_pid} == $$; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 135 | 25 | ▶ | if (POSIX::WIFSIGNALED($?)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 136 | 11 | my $signame = (split(' ', $Config{sig_name}))[POSIX::WTERMSIG($?)]; | |||||||||||||||||||||||||||||||
| 137 | 11 | ▶ | if ($signame =~ /^(ABRT|PIPE)$/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 138 | 1 | Test::More::diag("your server received SIG$signame"); | |||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||
| 142 | 25 | ▶ | if ($kid == 0 || $kid == -1) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$kid == 0 or $kid == -1
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
| |||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||