| 1 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # Net::Daemon::Test | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # Copyright (C) 1998, Jochen Wiedmann | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # Am Eisteich 9 | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # 72555 Metzingen | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # Germany | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # Phone: +49 7123 14887 | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # Email: joe@ispsoft.de | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # This module is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||
| 15 | # it under the terms of the GNU General Public License as published by | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # the Free Software Foundation; either version 2 of the License, or | ||||||||||||||||||||||||||||||||||||||||||
| 17 | # (at your option) any later version. | ||||||||||||||||||||||||||||||||||||||||||
| 18 | # | ||||||||||||||||||||||||||||||||||||||||||
| 19 | # This module is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||||||||||||||||
| 20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||||||||||||||||
| 21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||||||||||||||||||||||||||||||
| 22 | # GNU General Public License for more details. | ||||||||||||||||||||||||||||||||||||||||||
| 23 | # | ||||||||||||||||||||||||||||||||||||||||||
| 24 | # You should have received a copy of the GNU General Public License | ||||||||||||||||||||||||||||||||||||||||||
| 25 | # along with this module; if not, write to the Free Software | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||||||||||||||||||||||||||||||||||||||||||
| 27 | # | ||||||||||||||||||||||||||||||||||||||||||
| 28 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | package Net::Daemon::Test; | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 47 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 47 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 47 | ▶ | use Net::Daemon (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 47 | ▶ | use Symbol (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 47 | ▶ | use File::Basename (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 47 | our $VERSION = '0.49'; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 47 | our @ISA = qw(Net::Daemon); | |||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | Net::Daemon::Test - support functions for testing Net::Daemon servers | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | # This is the server, stored in the file "servertask". | ||||||||||||||||||||||||||||||||||||||||||
| 50 | # | ||||||||||||||||||||||||||||||||||||||||||
| 51 | # Create a subclass of Net::Daemon::Test, which in turn is | ||||||||||||||||||||||||||||||||||||||||||
| 52 | # a subclass of Net::Daemon | ||||||||||||||||||||||||||||||||||||||||||
| 53 | use Net::Daemon::Test (); | ||||||||||||||||||||||||||||||||||||||||||
| 54 | package MyDaemon; | ||||||||||||||||||||||||||||||||||||||||||
| 55 | our @ISA = qw(Net::Daemon::Test); | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | sub Run { | ||||||||||||||||||||||||||||||||||||||||||
| 58 | # Overwrite this and other methods, as you like. | ||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | my $self = Net::Daemon->new(\%attr, \@options); | ||||||||||||||||||||||||||||||||||||||||||
| 62 | eval { $self->Bind() }; | ||||||||||||||||||||||||||||||||||||||||||
| 63 | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||
| 64 | die "Server cannot bind: $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | eval { $self->Run() }; | ||||||||||||||||||||||||||||||||||||||||||
| 67 | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||
| 68 | die "Unexpected server termination: $@"; | ||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | # This is the client, the real test script, note we call the | ||||||||||||||||||||||||||||||||||||||||||
| 73 | # "servertask" file below: | ||||||||||||||||||||||||||||||||||||||||||
| 74 | # | ||||||||||||||||||||||||||||||||||||||||||
| 75 | # Call the Child method to spawn a child. Don't forget to use | ||||||||||||||||||||||||||||||||||||||||||
| 76 | # the timeout option. | ||||||||||||||||||||||||||||||||||||||||||
| 77 | use Net::Daemon::Test (); | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | my($handle, $port) = eval { | ||||||||||||||||||||||||||||||||||||||||||
| 80 | Net::Daemon::Test->Child(5, # Number of subtests | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 'servertask', '--timeout', '20') | ||||||||||||||||||||||||||||||||||||||||||
| 82 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 83 | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||
| 84 | print "not ok 1 $@\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 85 | exit 0; | ||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | print "ok 1\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | # Real tests following here | ||||||||||||||||||||||||||||||||||||||||||
| 90 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | # Terminate the server | ||||||||||||||||||||||||||||||||||||||||||
| 93 | $handle->Terminate(); | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | This module is a frame for creating test scripts of Net::Daemon based | ||||||||||||||||||||||||||||||||||||||||||
| 99 | server packages, preferrably using Test::Harness, but that's your | ||||||||||||||||||||||||||||||||||||||||||
| 100 | choice. | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | A test consists of two parts: The client part and the server part. | ||||||||||||||||||||||||||||||||||||||||||
| 103 | The test is executed by the child part which invokes the server part, | ||||||||||||||||||||||||||||||||||||||||||
| 104 | by spawning a child process and invoking an external Perl script. | ||||||||||||||||||||||||||||||||||||||||||
| 105 | (Of course we woultn't need this external file with fork(), but that's | ||||||||||||||||||||||||||||||||||||||||||
| 106 | the best possibility to make the test scripts portable to Windows | ||||||||||||||||||||||||||||||||||||||||||
| 107 | without requiring threads in the test script.) | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | The server part is a usual Net::Daemon application, for example a script | ||||||||||||||||||||||||||||||||||||||||||
| 110 | like dbiproxy. The only difference is that it derives from | ||||||||||||||||||||||||||||||||||||||||||
| 111 | Net::Daemon::Test and not from Net::Daemon, the main difference is that | ||||||||||||||||||||||||||||||||||||||||||
| 112 | the B<Bind> method attempts to allocate a port automatically. Once a | ||||||||||||||||||||||||||||||||||||||||||
| 113 | port is allocated, the number is stored in the file "ndtest.prt". | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | After spawning the server process, the child will wait ten seconds | ||||||||||||||||||||||||||||||||||||||||||
| 116 | (hopefully sufficient) for the creation of ndtest.prt. | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | =head1 AVAILABLE METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | =head2 Server part | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | =over 8 | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | =item Options | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | Adds an option B<--timeout> to Net::Daemon: The server's Run method | ||||||||||||||||||||||||||||||||||||||||||
| 128 | will die after at most 20 seconds. | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | sub Options ($) { | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 14 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Options: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 14 | my $options = $self->SUPER::Options(); | |||||||||||||||||||||||||||||||||||||||||
| 135 | 14 | $options->{'timeout'} = { | |||||||||||||||||||||||||||||||||||||||||
| 136 | 'template' => 'timeout=i', | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 'description' => '--timeout <secs> ' . "The server will die if the test takes longer\n" . ' than this number of seconds.' | ||||||||||||||||||||||||||||||||||||||||||
| 138 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 14 | $options; | |||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | =item Bind | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | (Instance method) This is mainly the default Bind method, but it attempts | ||||||||||||||||||||||||||||||||||||||||||
| 147 | to find and allocate a free port in two ways: First of all, it tries to | ||||||||||||||||||||||||||||||||||||||||||
| 148 | call Bind with port 0, most systems will automatically choose a port in | ||||||||||||||||||||||||||||||||||||||||||
| 149 | that case. If that seems to fail, ports 30000-30049 are tried. We | ||||||||||||||||||||||||||||||||||||||||||
| 150 | hope, one of these will succeed. :-) | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | sub Bind ($) { | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | # First try: Pass unmodified options to Net::Daemon::Bind | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 14 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Bind: called CC=17 SCAR=36.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 14 | my ( $port, $socket ); | |||||||||||||||||||||||||||||||||||||||||
| 159 | 14 | ▶ | $self->{'proto'} ||= $self->{'localpath'} ? 'unix' : 'tcp'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"proto"} ||= $self->{"localpath"} ? "unix" : "tcp"
MC/DC0%0/2
$self->{"proto"} ||= $self->{"localpath"} ? "unix" : "tcp"
$self->{"proto"}
$self->{"localpath"} ? "unix" : "tcp"
Truth table33%1/3
A ||= B
A= $self->{"proto"}B= $self->{"localpath"} ? "unix" : "tcp"
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 14 | ▶ | if ( $self->{'proto'} eq 'unix' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 2 | ▶ | $port = $self->{'localpath'} || die "Missing option: localpath"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'localpath'} || die('Missing option: localpath')
MC/DC0%0/1
$self->{'localpath'} || die('Missing option: localpath')
$self->{'localpath'}
Truth table50%1/2
A
A= $self->{'localpath'}
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 2 | $socket = eval { | |||||||||||||||||||||||||||||||||||||||||
| 163 | IO::Socket::UNIX->new( | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 'Local' => $port, | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 2 | ▶ | 'Listen' => $self->{'listen'} || 10 | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'listen'} || 10
MC/DC0%0/1
$self->{'listen'} || 10
$self->{'listen'}
Truth table50%1/2
A
A= $self->{'listen'}
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 170 | my @socket_args = ( | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 'LocalAddr' => $self->{'localaddr'}, | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 'LocalPort' => $self->{'localport'}, | ||||||||||||||||||||||||||||||||||||||||||
| 173 | 'Proto' => $self->{'proto'} || 'tcp', | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 12 | ▶ | 'Listen' => $self->{'listen'} || 10, | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$self->{'proto'} || 'tcp'
$self->{'listen'} || 10
MC/DC0%0/1
$self->{'proto'} || 'tcp'
$self->{'proto'}
MC/DC0%0/1
$self->{'listen'} || 10
$self->{'listen'}
Truth table50%1/2
A
A= $self->{'proto'}
Truth table50%1/2
A
A= $self->{'listen'}
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 'Reuse' => 1 | ||||||||||||||||||||||||||||||||||||||||||
| 176 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 12 | $socket = eval { IO::Socket::INET->new(@socket_args) }; | |||||||||||||||||||||||||||||||||||||||||
| 178 | 12 | ▶ | if ($socket) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 12 | $port = $socket->sockport(); | |||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | $port = 30049; | |||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | while ( !$socket && $port++ < 30060 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $socket and $port++ < 30060
MC/DC0%0/2
not $socket and $port++ < 30060
not $socket
$port++ < 30060
Truth table0%0/3
not(A) and B
A= not $socketB= $port++ < 30060
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | $socket = eval { | |||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | IO::Socket::INET->new( | |||||||||||||||||||||||||||||||||||||||||
| 186 | @socket_args, | ||||||||||||||||||||||||||||||||||||||||||
| 187 | 'LocalPort' => $port | ||||||||||||||||||||||||||||||||||||||||||
| 188 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 189 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||
| 193 | 14 | ▶ | if ( !$socket ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | ▶ | die "Cannot create socket: " . ( $@ || $! ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$@ || $!
MC/DC0%0/2
$@ || $!
$@
$!
Truth table0%0/3
A || B
A= $@B= $!
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | # Create the "ndtest.prt" file so that the child knows to what | ||||||||||||||||||||||||||||||||||||||||||
| 198 | # port it may connect. | ||||||||||||||||||||||||||||||||||||||||||
| 199 | 14 | my $fh = Symbol::gensym(); | |||||||||||||||||||||||||||||||||||||||||
| 200 | 14 | ▶ | if ( !open( $fh, ">ndtest.prt" ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
open $fh, ">ndtest.prt" and print $fh $port and close $fh
open $fh, ">ndtest.prt" and print $fh $port
MC/DC0%0/3
open $fh, ">ndtest.prt" and print $fh $port and close $fh
open $fh, ">ndtest.prt"
print $fh $port
close $fh
Truth table25%1/4
A and B and C
A= open $fh, ">ndtest.prt"B= print $fh $portC= close $fh
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | || !( print $fh $port ) | ||||||||||||||||||||||||||||||||||||||||||
| 202 | || !close($fh) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | die "Error while creating 'ndtest.prt': $!"; | |||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||
| 205 | 14 | $self->Debug("Created ndtest.prt with port $port\n"); | |||||||||||||||||||||||||||||||||||||||||
| 206 | 14 | $self->{'socket'} = $socket; | |||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | 14 | ▶ | if ( my $timeout = $self->{'timeout'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 4 | eval { alarm $timeout }; | |||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | 14 | $self->SUPER::Bind(); | |||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | =item Run | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | (Instance method) Overwrites the Net::Daemon's method by adding a timeout. | ||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | sub Run ($) { | ||||||||||||||||||||||||||||||||||||||||||
| 224 | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 225 | $self->Run(); | ||||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | =head2 Client part | ||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | =over 8 | ||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | =item Child | ||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | (Class method) Attempts to spawn a server process. The server process is | ||||||||||||||||||||||||||||||||||||||||||
| 236 | expected to create the file 'ndtest.prt' with the port number. | ||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | The method returns a process handle and a port number. The process handle | ||||||||||||||||||||||||||||||||||||||||||
| 239 | offers a method B<Terminate> that may later be used to stop the server | ||||||||||||||||||||||||||||||||||||||||||
| 240 | process. | ||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | sub Child ($$@) { | ||||||||||||||||||||||||||||||||||||||||||
| 247 | 42 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Child: called CC=19 SCAR=44.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | 42 | my $numTests = shift; | |||||||||||||||||||||||||||||||||||||||||
| 249 | 42 | my ( $handle, $pid ); | |||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | 42 | my $args = join( " ", @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 252 | 42 | print "Starting server: $args\n"; | |||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | 42 | unlink 'ndtest.prt'; | |||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | 42 | ▶ | if ( $args =~ /\-\-mode=(?:ithread|thread|single)/ && $^O =~ /mswin32/i ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$args =~ /\-\-mode=(?:ithread|thread|single)/ and $^O =~ /mswin32/i
MC/DC0%0/2
$args =~ /\-\-mode=(?:ithread|thread|single)/ and $^O =~ /mswin32/i
$args =~ /\-\-mode=(?:ithread|thread|single)/
$^O =~ /mswin32/i
Truth table66%2/3
A and B
A= $args =~ /\-\-mode=(?:ithread|thread|single)/B= $^O =~ /mswin32/i
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | require Win32; | |||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | require Win32::Process; | |||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | my $proc = $_[0]; | |||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | # Win32::Process seems to require an absolute path; this includes | ||||||||||||||||||||||||||||||||||||||||||
| 262 | # a program extension like ".exe" | ||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | my $path; | |||||||||||||||||||||||||||||||||||||||||
| 264 | my @pdirs; | ||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | File::Basename::fileparse_set_fstype("MSWin32"); | |||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | ▶ | if ( File::Basename::basename($proc) !~ /\./ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | $proc .= ".exe"; | |||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | if ( $proc !~ /^\w\:\\/ && $proc !~ /^\\/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$proc =~ /^\w\:\\/ || $proc =~ /^\\/
MC/DC0%0/2
$proc =~ /^\w\:\\/ || $proc =~ /^\\/
$proc =~ /^\w\:\\/
$proc =~ /^\\/
Truth table0%0/3
A || B
A= $proc =~ /^\w\:\\/B= $proc =~ /^\\/
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | # Doesn't look like an absolute path | ||||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | foreach my $dir ( @pdirs = split( /;/, $ENV{'PATH'} ) ) { | |||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | ▶ | if ( -x "$dir/$proc" ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | $path = "$dir/$proc"; | |||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | ▶ | if ( !$path ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | print STDERR ( "Cannot find $proc in the following", " directories:\n" ); | |||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | foreach my $dir (@pdirs) { | |||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | print STDERR " $dir\n"; | |||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | print STDERR "Terminating.\n"; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | exit 1; | |||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||||||
| 288 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | $path = $proc; | |||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | print "Starting process: proc = $path, args = ", join( " ", @_ ), "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 293 | 0 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | !Win32::Process::Create( | ||||||||||||||||||||||||||||||||||||||||||
| 295 | $pid, $path, | ||||||||||||||||||||||||||||||||||||||||||
| 296 | join( " ", @_ ), 0, | ||||||||||||||||||||||||||||||||||||||||||
| 297 | Win32::Process::DETACHED_PROCESS(), | ||||||||||||||||||||||||||||||||||||||||||
| 298 | "." | ||||||||||||||||||||||||||||||||||||||||||
| 299 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 300 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 301 | 0 | die "Cannot create child process: " . Win32::FormatMessage( Win32::GetLastError() ); | |||||||||||||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | $handle = bless( \$pid, "Net::Daemon::Test::Win32" ); | |||||||||||||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||||||||||||
| 305 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 306 | 42 | $pid = eval { fork() }; | |||||||||||||||||||||||||||||||||||||||||
| 307 | 42 | ▶ | if ( defined($pid) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | # Aaaah, Unix! :-) | ||||||||||||||||||||||||||||||||||||||||||
| 310 | 42 | ▶ | if ( !$pid ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | # This is the child process, spawn the server. | ||||||||||||||||||||||||||||||||||||||||||
| 313 | 11 | exec @_; | |||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||
| 315 | 31 | $handle = bless( \$pid, "Net::Daemon::Test::Fork" ); | |||||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||||||
| 317 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | print "1..0\n"; | |||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | exit 0; | |||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | 31 | ▶ | print "1..$numTests\n" if defined($numTests); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 324 | 31 | ▶ | for ( my $secs = 20; $secs && !-s 'ndtest.prt'; $secs -= sleep 1 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$secs and not -s "ndtest.prt"
MC/DC50%1/2
$secs and not -s "ndtest.prt"
$secs
not -s "ndtest.prt"
Truth table66%2/3
A and not(B)
A= $secsB= not -s "ndtest.prt"
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||||||||||
| 326 | 31 | ▶ | if ( !-s 'ndtest.prt' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 327 | 0 | die "Server process didn't create a file 'ndtest.prt'."; | |||||||||||||||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | # Sleep another second in case the server is still creating the | ||||||||||||||||||||||||||||||||||||||||||
| 331 | # file with the port number ... | ||||||||||||||||||||||||||||||||||||||||||
| 332 | 31 | sleep 1; | |||||||||||||||||||||||||||||||||||||||||
| 333 | 31 | my $fh = Symbol::gensym(); | |||||||||||||||||||||||||||||||||||||||||
| 334 | 31 | my $port; | |||||||||||||||||||||||||||||||||||||||||
| 335 | 31 | ▶ | if ( !open( $fh, "<ndtest.prt" ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
open $fh, "<ndtest.prt" and defined($port = readline $fh)
MC/DC0%0/2
open $fh, "<ndtest.prt" and defined($port = readline $fh)
open $fh, "<ndtest.prt"
defined($port = readline $fh)
Truth table33%1/3
A and B
A= open $fh, "<ndtest.prt"B= defined($port = readline $fh)
| |||||||||||||||||||||||||||||||||||||||||||
| 336 | || !defined( $port = <$fh> ) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | die "Error while reading 'ndtest.prt': $!"; | |||||||||||||||||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||||||||||||||||
| 339 | 31 | ( $handle, $port ); | |||||||||||||||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | package Net::Daemon::Test::Fork; | ||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | sub Terminate ($) { | ||||||||||||||||||||||||||||||||||||||||||
| 345 | 21 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Terminate: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 346 | 21 | my $pid = $$self; | |||||||||||||||||||||||||||||||||||||||||
| 347 | 21 | kill 'TERM', $pid; | |||||||||||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | package Net::Daemon::Test::Win32; | ||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | sub Terminate ($) { | ||||||||||||||||||||||||||||||||||||||||||
| 353 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Terminate: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 354 | 0 | my $pid = $$self; | |||||||||||||||||||||||||||||||||||||||||
| 355 | 0 | $pid->Kill(0); | |||||||||||||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||
| 358 | 47 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | =head1 AUTHOR AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | Net::Daemon is Copyright (C) 1998, Jochen Wiedmann | ||||||||||||||||||||||||||||||||||||||||||
| 363 | Am Eisteich 9 | ||||||||||||||||||||||||||||||||||||||||||
| 364 | 72555 Metzingen | ||||||||||||||||||||||||||||||||||||||||||
| 365 | Germany | ||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | Phone: +49 7123 14887 | ||||||||||||||||||||||||||||||||||||||||||
| 368 | Email: joe@ispsoft.de | ||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | You may distribute under the terms of either the GNU General Public | ||||||||||||||||||||||||||||||||||||||||||
| 373 | License or the Artistic License, as specified in the Perl README file. | ||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | L<Net::Daemon(3)>, L<Test::Harness(3)> | ||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | =cut | ||||||||||||||||||||||||||||||||||||||||||