Respite/Server/Test.pm

filter: Statementstmt 91.1% 82 / 90 Branchbran 50.0% 26 / 52 Conditioncond 41.1% 14 / 34 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 1 totaltotal 63.9% 131 / 205 CC 44 SCAR 41.3
CC
44
Cov
69%
setup_test_server
87.8
__ANON__
13.7
BEGIN
1.0
CRAP 61.9
« Respite/Client.pm ↑ Summary Respite/Common.pm »
1 package Net::Respite::Server::Test;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Throw qw(throw import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Time::HiRes qw(sleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use End;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 #use Data::Debug;
11  
12 5 ▶sub setup_test_server {
Subroutine setup_test_server: called CC=35 SCAR=44.8
Pod undocumented
13 5 ▶ my $args = shift || {};
Condition50%1/2
shift() || {}
l!l
50
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
14 5 ▶ my $verbose = exists($args->{'verbose'}) ? $args->{'verbose'} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'verbose'} ? : 0 5 5
15  
16 # setup some defaults
17 5 ▶ my $port = $args->{'port'} || do {
Condition33%1/3
$args->{'port'} || do { require IO::Socket; my $sock = 'IO::Socket::INET'->new; $sock->configure({'LocalPort', 0, 'Listen', 1, 'Proto', 'tcp', 'ReuseAddr', 1}) or throw('Could not create temp socket', {'msg', $!}); my $port = $sock->sockport || throw('Could not generate random usable sockport') }
l!l&&r!l&&!r
050
MC/DC0%0/2
$args->{'port'} || do { require IO::Socket; my $sock = 'IO::Socket::INET'->new; $sock->configure({'LocalPort', 0, 'Listen', 1, 'Proto', 'tcp', 'ReuseAddr', 1}) or throw('Could not create temp socket', {'msg', $!}); my $port = $sock->sockport || throw('Could not generate random usable sockport') }
$args->{'port'} do { require IO::Socket; my $sock = 'IO::Socket::INET'->new; $sock->configure({'LocalPort', 0, 'Listen', 1, 'Proto', 'tcp', 'ReuseAddr', 1}) or throw('Could not create temp socket', {'msg', $!}); my $port = $sock->sockport || throw('Could not generate random usable sockport') }
Truth table33%1/3
A || B
A= $args->{'port'}B= do { require IO::Socket; my $sock = 'IO::Socket::INET'->new; $sock->configure({'LocalPort', 0, 'Listen', 1, 'Proto', 'tcp', 'ReuseAddr', 1}) or throw('Could not create temp socket', {'msg', $!}); my $port = $sock->sockport || throw('Could not generate random usable sockport') }
ABresult
1X1
011
000
18 5 require IO::Socket;
19 5 my $sock = IO::Socket::INET->new;
20 5 ▶ $sock->configure({
Branch: 1 branch
BranchTrueFalseTotal
$sock->configure({"LocalPort", 0, "Listen", 1, "Proto", "tcp", "ReuseAddr", 1}) or throw("Could not create temp socket", {"msg", $!}) 5 0 5
21 LocalPort => 0,
22 Listen => 1,
23 Proto => 'tcp',
24 ReuseAddr => 1,
25 }) || throw "Could not create temp socket", {msg => $!};
26 5 ▶ my $port = $sock->sockport || throw "Could not generate random usable sockport";
Condition33%1/3
$sock->sockport || throw('Could not generate random usable sockport')
l!l&&r!l&&!r
500
MC/DC0%0/2
$sock->sockport || throw('Could not generate random usable sockport')
$sock->sockport throw('Could not generate random usable sockport')
Truth table33%1/3
A || B
A= $sock->sockportB= throw('Could not generate random usable sockport')
ABresult
1X1
011
000
27 };
28 5 my $tmpnam = File::Temp->new;
29 5 ▶ my $pid_file = $args->{'pid_file'} || "$tmpnam.$$.pid";
Condition50%1/2
$args->{'pid_file'} || "$tmpnam.${$}.pid"
l!l
05
MC/DC0%0/1
$args->{'pid_file'} || "$tmpnam.${$}.pid"
$args->{'pid_file'}
Truth table50%1/2
A
A= $args->{'pid_file'}
Aresult
11
00
30 5 ▶ my $access_file = $args->{'access_file'} || "$tmpnam.$$.access";
Condition50%1/2
$args->{'access_file'} || "$tmpnam.${$}.access"
l!l
05
MC/DC0%0/1
$args->{'access_file'} || "$tmpnam.${$}.access"
$args->{'access_file'}
Truth table50%1/2
A
A= $args->{'access_file'}
Aresult
11
00
31 5 ▶ my $error_file = $args->{'error_file'} || "$tmpnam.$$.error";
Condition50%1/2
$args->{'error_file'} || "$tmpnam.${$}.error"
l!l
05
MC/DC0%0/1
$args->{'error_file'} || "$tmpnam.${$}.error"
$args->{'error_file'}
Truth table50%1/2
A
A= $args->{'error_file'}
Aresult
11
00
32 5 undef $tmpnam;
33 5 ▶ my $no_brand = exists($args->{'no_brand'}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'no_brand'} ? : 0 5 5
34 5 ▶ my $no_ssl = exists($args->{'no_ssl'}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'no_ssl'} ? : 5 0 5
35 #$no_ssl = 1;
36 5 ▶ my $flat = exists($args->{'flat'}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'flat'} ? : 5 0 5
37  
38 5 ▶ my $server = $args->{'server'} || do {
Condition33%1/3
$args->{'server'} || do { my $pkg = $args->{'server_class'} || 'Net::Respite::Server'; (my $file = "$pkg.pm") =~ s[::][/]g; eval { do { require $file } } or throw('Could not require client library', {'msg', $@}); $pkg->new({'no_brand', $no_brand, $args->{'service'} ? ('server_name', $args->{'service'}) : (), $args->{'api_meta'} ? ('api_meta', $args->{'api_meta'}) : (), 'port', $port, 'server_type', $args->{'server_type'} || 'Fork', 'no_ssl', $no_ssl, 'flat', $flat, 'host', $args->{'host'} || 'localhost', 'pass', $args->{'pass'}, 'pid_file', $pid_file, 'access_log_file', $access_file, 'log_file', $error_file, 'user', defined $args->{'user'} ? $args->{'user'} : $<, 'group', defined $args->{'group'} ? $args->{'group'} : $(}) }
l!l&&r!l&&!r
050
MC/DC0%0/2
$args->{'server'} || do { my $pkg = $args->{'server_class'} || 'Net::Respite::Server'; (my $file = "$pkg.pm") =~ s[::][/]g; eval { do { require $file } } or throw('Could not require client library', {'msg', $@}); $pkg->new({'no_brand', $no_brand, $args->{'service'} ? ('server_name', $args->{'service'}) : (), $args->{'api_meta'} ? ('api_meta', $args->{'api_meta'}) : (), 'port', $port, 'server_type', $args->{'server_type'} || 'Fork', 'no_ssl', $no_ssl, 'flat', $flat, 'host', $args->{'host'} || 'localhost', 'pass', $args->{'pass'}, 'pid_file', $pid_file, 'access_log_file', $access_file, 'log_file', $error_file, 'user', defined $args->{'user'} ? $args->{'user'} : $<, 'group', defined $args->{'group'} ? $args->{'group'} : $(}) }
$args->{'server'} do { my $pkg = $args->{'server_class'} || 'Net::Respite::Server'; (my $file = "$pkg.pm") =~ s[::][/]g; eval { do { require $file } } or throw('Could not require client library', {'msg', $@}); $pkg->new({'no_brand', $no_brand, $args->{'service'} ? ('server_name', $args->{'service'}) : (), $args->{'api_meta'} ? ('api_meta', $args->{'api_meta'}) : (), 'port', $port, 'server_type', $args->{'server_type'} || 'Fork', 'no_ssl', $no_ssl, 'flat', $flat, 'host', $args->{'host'} || 'localhost', 'pass', $args->{'pass'}, 'pid_file', $pid_file, 'access_log_file', $access_file, 'log_file', $error_file, 'user', defined $args->{'user'} ? $args->{'user'} : $<, 'group', defined $args->{'group'} ? $args->{'group'} : $(}) }
Truth table33%1/3
A || B
A= $args->{'server'}B= do { my $pkg = $args->{'server_class'} || 'Net::Respite::Server'; (my $file = "$pkg.pm") =~ s[::][/]g; eval { do { require $file } } or throw('Could not require client library', {'msg', $@}); $pkg->new({'no_brand', $no_brand, $args->{'service'} ? ('server_name', $args->{'service'}) : (), $args->{'api_meta'} ? ('api_meta', $args->{'api_meta'}) : (), 'port', $port, 'server_type', $args->{'server_type'} || 'Fork', 'no_ssl', $no_ssl, 'flat', $flat, 'host', $args->{'host'} || 'localhost', 'pass', $args->{'pass'}, 'pid_file', $pid_file, 'access_log_file', $access_file, 'log_file', $error_file, 'user', defined $args->{'user'} ? $args->{'user'} : $<, 'group', defined $args->{'group'} ? $args->{'group'} : $(}) }
ABresult
1X1
011
000
39 5 ▶ my $pkg = $args->{'server_class'} || 'Net::Respite::Server';
Condition50%1/2
$args->{'server_class'} || 'Net::Respite::Server'
l!l
05
MC/DC0%0/1
$args->{'server_class'} || 'Net::Respite::Server'
$args->{'server_class'}
Truth table50%1/2
A
A= $args->{'server_class'}
Aresult
11
00
40 5 (my $file = "$pkg.pm") =~ s|::|/|g;
41 5 ▶ eval { require $file } || throw "Could not require client library", {msg => $@};
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require $file } } or throw("Could not require client library", {"msg", $@}) 5 0 5
42 $pkg->new({
43 no_brand => $no_brand,
44 ($args->{'service'} ? (server_name => $args->{'service'}) : ()),
45 ($args->{'api_meta'} ? (api_meta => $args->{'api_meta'}) : ()),
46 port => $port,
47 server_type => $args->{'server_type'} || 'Fork',
48 no_ssl => $no_ssl,
49 flat => $flat,
50 host => $args->{'host'} || 'localhost',
51 pass => $args->{'pass'},
52 pid_file => $pid_file,
53 access_log_file => $access_file,
54 log_file => $error_file,
55 user => defined($args->{'user'}) ? $args->{'user'} : $<,
56 5 ▶ group => defined($args->{'group'}) ? $args->{'group'} : $(,
Branch: 4 branches
BranchTrueFalseTotal
$args->{'service'} ? : 5 0 5
$args->{'api_meta'} ? : 5 0 5
defined $args->{'user'} ? : 0 5 5
defined $args->{'group'} ? : 0 5 5
Condition50%2/4
$args->{'server_type'} || 'Fork'
l!l
05
$args->{'host'} || 'localhost'
l!l
05
MC/DC0%0/1
$args->{'server_type'} || 'Fork'
$args->{'server_type'}
MC/DC0%0/1
$args->{'host'} || 'localhost'
$args->{'host'}
Truth table50%1/2
A
A= $args->{'server_type'}
Aresult
11
00
Truth table50%1/2
A
A= $args->{'host'}
Aresult
11
00
57 });
58 };
59 #debug $server;
60 5 my $service = $server->server_name;
61 5 $service =~ s/_server$//;
62  
63 5 ▶ my $encoded = exists($args->{'utf8_encoded'}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'utf8_encoded'} ? : 0 5 5
64 5 ▶ my $client = $args->{'client'} || do {
Condition33%1/3
$args->{'client'} || do { my $pkg = $args->{'client_class'} || 'Net::Respite::Client'; (my $file = "$pkg.pm") =~ s[::][/]g; eval { do { require $file } } or throw('Could not require client library', {'msg', $@}); $pkg->new({'no_brand', $no_brand, 'service', $service, 'port', $port, 'host', $server->{'host'}, 'pass', $args->{'pass'}, 'no_ssl', $no_ssl, 'flat', $flat, 'utf8_encoded', $encoded, $args->{'brand'} ? ('brand', $args->{'brand'}) : ()}) }
l!l&&r!l&&!r
050
MC/DC0%0/2
$args->{'client'} || do { my $pkg = $args->{'client_class'} || 'Net::Respite::Client'; (my $file = "$pkg.pm") =~ s[::][/]g; eval { do { require $file } } or throw('Could not require client library', {'msg', $@}); $pkg->new({'no_brand', $no_brand, 'service', $service, 'port', $port, 'host', $server->{'host'}, 'pass', $args->{'pass'}, 'no_ssl', $no_ssl, 'flat', $flat, 'utf8_encoded', $encoded, $args->{'brand'} ? ('brand', $args->{'brand'}) : ()}) }
$args->{'client'} do { my $pkg = $args->{'client_class'} || 'Net::Respite::Client'; (my $file = "$pkg.pm") =~ s[::][/]g; eval { do { require $file } } or throw('Could not require client library', {'msg', $@}); $pkg->new({'no_brand', $no_brand, 'service', $service, 'port', $port, 'host', $server->{'host'}, 'pass', $args->{'pass'}, 'no_ssl', $no_ssl, 'flat', $flat, 'utf8_encoded', $encoded, $args->{'brand'} ? ('brand', $args->{'brand'}) : ()}) }
Truth table33%1/3
A || B
A= $args->{'client'}B= do { my $pkg = $args->{'client_class'} || 'Net::Respite::Client'; (my $file = "$pkg.pm") =~ s[::][/]g; eval { do { require $file } } or throw('Could not require client library', {'msg', $@}); $pkg->new({'no_brand', $no_brand, 'service', $service, 'port', $port, 'host', $server->{'host'}, 'pass', $args->{'pass'}, 'no_ssl', $no_ssl, 'flat', $flat, 'utf8_encoded', $encoded, $args->{'brand'} ? ('brand', $args->{'brand'}) : ()}) }
ABresult
1X1
011
000
65 5 ▶ my $pkg = $args->{'client_class'} || 'Net::Respite::Client';
Condition50%1/2
$args->{'client_class'} || 'Net::Respite::Client'
l!l
05
MC/DC0%0/1
$args->{'client_class'} || 'Net::Respite::Client'
$args->{'client_class'}
Truth table50%1/2
A
A= $args->{'client_class'}
Aresult
11
00
66 5 (my $file = "$pkg.pm") =~ s|::|/|g;
67 5 ▶ eval { require $file } || throw "Could not require client library", {msg => $@};
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require $file } } or throw("Could not require client library", {"msg", $@}) 5 0 5
68 $pkg->new({
69 no_brand => $no_brand,
70 service => $service,
71 port => $port,
72 host => $server->{'host'},
73 pass => $args->{'pass'},
74 no_ssl => $no_ssl,
75 flat => $flat,
76 utf8_encoded => $encoded,
77 5 ▶ ($args->{'brand'} ? (brand => $args->{'brand'}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$args->{'brand'} ? : 5 0 5
78 });
79 };
80 #debug $client;
81  
82 ###----------------------------------------------------------------###
83 # start the server in a child, block the parent until ready
84  
85 5 my $pid = fork;
86 5 ▶ die "Could not fork during test\n" if ! defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 5 5
87 5 ▶ if (!$pid) { # child
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 2 3 5
88 2 local @ARGV;
89 2 $server->run_server(setsid => 0, background => 0); # allow a kill term to close the server too
90 0 exit;
91 }
92  
93 3 my $client_pid = $$;
94 3 ▶ $client->{'_test_ender'} = end {
Subroutine __ANON__: called CC=10 SCAR=26.2
95 3 diag("Client object ending: pid=[$$] port=[$port]");
96  
97 3 ▶ if ($client_pid != $$) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client_pid != $$) 0 3 3
98 0 diag("ORIGPID[$client_pid]!=CURRPID[$$] Refusing to stop server on port [$port]! Running setup_test_server() while an old client object still exists can trigger this phantom cleanup on the OLD client object when the child request from the NEW server completes, especially for the Fork personality.");
99 0 return;
100 }
101  
102 3 ▶ diag("Process list") if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 3 0 3
103 3 $server->__ps;
104  
105 3 diag("Stop server");
106 3 $server->__stop;
107 # get some info - then tear down
108  
109 3 ▶ diag("Tail of the error_log") if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 3 0 3
110 3 ▶ $server->__tail_error(1000) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 3 0 3
111  
112 3 ▶ diag("Tail of the access_log") if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 3 0 3
113 3 ▶ $server->__tail_access($server->{'tail_access'} || 20) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 3 0 3
Condition50%1/2
$server->{'tail_access'} || 20
l!l
03
MC/DC0%0/1
$server->{'tail_access'} || 20
$server->{'tail_access'}
Truth table50%1/2
A
A= $server->{'tail_access'}
Aresult
11
00
114  
115 3 ▶ diag("Shut down server") if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 3 0 3
116 3 unlink $_ for $pid_file, $access_file, $error_file; # double check
117 3 };
118  
119 # block the parent (that will run client tests) until the child running the server is fully setup
120 3 my $connected;
121 3 for (1 .. 30) {
122 3 sleep 0.1;
123 3 my $class = 'IO::Socket::INET';
124 3 ▶ if (!$no_ssl) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_ssl) 0 3 3
125 0 require IO::Socket::SSL;
126 0 $class = 'IO::Socket::SSL';
127 }
128 3 ▶ my $sock = $class->new(PeerHost => "localhost", PeerPort => $port, SSL_verify_mode => 0) || next;
Condition50%1/2
$class->new('PeerHost', 'localhost', 'PeerPort', $port, 'SSL_verify_mode', 0) || (next)
l!l
30
MC/DC0%0/1
$class->new('PeerHost', 'localhost', 'PeerPort', $port, 'SSL_verify_mode', 0) || (next)
$class->new('PeerHost', 'localhost', 'PeerPort', $port, 'SSL_verify_mode', 0)
Truth table50%1/2
A
A= $class->new('PeerHost', 'localhost', 'PeerPort', $port, 'SSL_verify_mode', 0)
Aresult
11
00
129 3 print $sock "GET /waited_until_child HTTP/1.0\n\n";
130 3 $connected = 1;
131 3 last;
132 }
133 3 ▶ if (! $connected) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($connected) 0 3 3
134 0 diag("Tail of the error_log");
135 0 ▶ $server->__tail_error($server->{'tail_error'} || 20) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
Condition0%0/2
$server->{'tail_error'} || 20
l!l
00
MC/DC0%0/1
$server->{'tail_error'} || 20
$server->{'tail_error'}
Truth table0%0/2
A
A= $server->{'tail_error'}
Aresult
11
00
136 0 die "Failed to connect to the server: $!";
137 }
138  
139 3 ▶ return wantarray ? ($client, $server) : $client;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
140 }
141  
142 4 1;
143  
144 __END__
« Respite/Client.pm ↑ Summary Respite/Common.pm »