testguts-fork.pl

filter: Statementstmt 86.5% 58 / 67 Branchbran 60.0% 12 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 82.1% 78 / 95 CC 6 SCAR 8.1
CC
6
Cov
80%
__ANON__
4.0
END
2.1
__ANON__
2.0
CRAP 2.3
« blib/lib/NetServer/Generic.pm ↑ Summary testguts-prefork.pl »
1 # Before `make install' is performed this script should be runnable with
2 # `make test'. After `make install' it should work as `perl test.pl'
3  
4 ######################### We start with some black magic to print on failure.
5  
6 # Change 1..1 below to 1..last_test_to_print .
7 # (It may become useful if the test is moved to ./t subdirectory.)
8  
9 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 $| = 1;
11 9 print "1..6\n"; # 5 tests in total
12 }
13 9 ▶END {
Subroutine END: called CC=2 SCAR=7.6
14 9 ▶ print "not ok 1\n" unless $loaded; # we couldn't create an object
Branch: 1 branch
BranchTrueFalseTotal
unless $loaded 0 9 9
15 }
16 9 ▶use NetServer::Generic;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # first test; can we create a new NetServer::Generic?
20  
21 9 my $dummy = new NetServer::Generic();
22 9 ▶if (ref($dummy) =~ /NetServer::Generic/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($dummy) =~ /NetServer::Generic/i) 9 0 9
23 9 $loaded = 1;
24 9 print "ok 1\n";
25 }
26  
27 # next tests: first, we fork.
28 # The parent spawns a new NetServer::Generic server. It prints whatever
29 # comes to it on $out (which is aliased to the original STDOUT).
30 # The child spawns a NetServer::Generic client() server, with a trigger
31 # that fires three times. It prints "ok .. $n" each time, and this is picked
32 # up by the parent (if successful).
33 #
34 # The parent prints ok .. 5 when it exits.
35 #
36 # Test meanings are:
37 #
38 # 1 we could create a NetServer::Generic
39 # 2 - 5 inclusive: the client repeatedly forked and sent these strings
40 # to the server, which echoed them to STDOUT
41 #
42 # NOTE: as yet, no testing for the non-forking server is available.
43 #
44  
45 9 my $port = 10101;
46 9 my $host = localhost;
47  
48 9 ▶no strict "vars";
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 9 $j = 1;
51  
52 9 $out = *STDOUT;
53 9 select STDERR; $| = 1;
54 9 select STDOUT; $| = 1;
55 #select STDIN;
56  
57 9 eval {
58 {
59 9 ▶ if ($pid = fork) {
Branch: 3 branches
BranchTrueFalseTotal
if ($pid = fork) { } 4 5 9
elsif (defined $pid) { } 5 0 5
elsif ($! =~ /no more processes/i) { } 0 0 0
60 # parent
61 4 my ($foo) = new NetServer::Generic();
62 4 $foo->port($port);
63 4 $foo->hostname($host);
64 #$foo->allowed([ "localhost"]);
65 4 ▶ my $server = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
66 4 my $self = shift;
67 4 ▶ while (defined($tmp = <STDIN>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($tmp = readline STDIN)) 8 0 8
68 8 ▶ if ($tmp !~ /exit/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $tmp =~ /exit/) { } 4 4 8
69 4 print $out "ok $tmp";
70 4 $val = $tmp; chomp $val;
71 } else {
72 4 ▶ if ($val !~ /5/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $val =~ /5/) { } 3 1 4
73 3 exit;
74 } else {
75 1 $self->quit();
76 }
77 }
78 }
79 4 };
80 4 $foo->callback($server);
81 4 $foo->mode(forking);
82 4 $foo->run();
83 0 exit;
84 } elsif (defined $pid) {
85 # child
86 5 my ($foo) = new NetServer::Generic();
87 5 $foo->port($port);
88 5 $foo->hostname($host);
89 4 ▶ my $client = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
90 4 my $self = shift;
91 4 print STDOUT "$j\nexit\n";
92 4 exit 0;
93 5 };
94 5 $foo->callback($client);
95 15 ▶ my $trigger = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
96 15 $j++;
97 15 ▶ return(($j > 5) ? 0 : 1 ); #make three connections
Branch: 1 branch
BranchTrueFalseTotal
$j > 5 ? : 1 14 15
98 5 };
99 5 $foo->trigger($trigger);
100 5 $foo->mode(client);
101 5 $foo->run();
102 1 exit;
103 } elsif ($! =~ /no more processes/i) {
104 0 print $out "not ok 1\n";
105 0 print STDERR "Are you running a Win32 system? If so, this \n",
106 "test script will not and cannot work\n";
107 0 exit 0;
108 } else {
109 0 die "Can't fork: $!\n";
110 0 exit 0;
111 }
112 }
113 }; # end eval
114  
115 0 ▶if ($@ ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ ne "") 0 0 0
116 0 print "eval(): $@\n";
117 }
118  
119 0 exit 0;
« blib/lib/NetServer/Generic.pm ↑ Summary testguts-prefork.pl »