Respite/Server/Test.pm

filter: Statementstmt 92.9% 79 / 85 Branchbran 50.0% 25 / 50 Conditioncond 41.1% 14 / 34 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 1 totaltotal 64.1% 127 / 198 CC 43 SCAR 41.1
CC
43
Cov
70%
setup_test_server
86.2
__ANON__
11.0
BEGIN
1.0
CRAP 61.2
« Respite/Client.pm ↑ Summary Respite.pm »
1 package 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.6
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'} || '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'} || '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'} || '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'} || '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'} || 'Respite::Server';
Condition50%1/2
$args->{'server_class'} || 'Respite::Server'
l!l
05
MC/DC0%0/1
$args->{'server_class'} || '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'} || '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'} || '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'} || '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'} || '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'} || 'Respite::Client';
Condition50%1/2
$args->{'client_class'} || 'Respite::Client'
l!l
05
MC/DC0%0/1
$args->{'client_class'} || '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'} ? : 0 5 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 ▶ $client->{'_test_ender'} = end {
Subroutine __ANON__: called CC=9 SCAR=24.0
94 3 ▶ diag("Process list") if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 3 0 3
95 3 $server->__ps;
96  
97 3 diag("Stop server");
98 3 $server->__stop;
99 # get some info - then tear down
100  
101 3 ▶ diag("Tail of the error_log") if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 3 0 3
102 3 ▶ $server->__tail_error(1000) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 3 0 3
103  
104 3 ▶ diag("Tail of the access_log") if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 3 0 3
105 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
106  
107 3 ▶ diag("Shut down server") if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 3 0 3
108 3 unlink $_ for $pid_file, $access_file, $error_file; # double check
109 3 };
110  
111 # block the parent (that will run client tests) until the child running the server is fully setup
112 3 my $connected;
113 3 for (1 .. 30) {
114 3 sleep 0.1;
115 3 my $class = 'IO::Socket::INET';
116 3 ▶ if (!$no_ssl) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_ssl) 0 3 3
117 0 require IO::Socket::SSL;
118 0 $class = 'IO::Socket::SSL';
119 }
120 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
121 3 print $sock "GET /waited_until_child HTTP/1.0\n\n";
122 3 $connected = 1;
123 3 last;
124 }
125 3 ▶ if (! $connected) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($connected) 0 3 3
126 0 diag("Tail of the error_log");
127 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
128 0 die "Failed to connect to the server: $!";
129 }
130  
131 3 ▶ return wantarray ? ($client, $server) : $client;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
132 }
133  
134 4 1;
135  
136 __END__
« Respite/Client.pm ↑ Summary Respite.pm »