File Coverage

testguts-prefork.pl
Criterion Covered Total %
statement 63 72 87.5
branch 11 18 61.1
condition n/a
subroutine 8 8 100.0
pod n/a
total 82 98 83.6


line stmt bran cond sub pod time code
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             BEGIN {
10 8     8   64 $| = 1;
11 8         1624 print "7..12\n"; # 5 tests in total
12             }
13             END {
14 8 50   8     print "not ok 7\n" unless $loaded; # we couldn't create an object
15             }
16 8     8   10784 use NetServer::Generic;
  8         24  
  8         336  
17 8     8   80 use IO::Handle;
  8         8  
  8         1096  
18              
19             # first test; can we create a new NetServer::Generic?
20              
21 8         112 my $dummy = new NetServer::Generic();
22 8 50       72 if (ref($dummy) =~ /NetServer::Generic/i) {
23 8         24 $loaded = 1;
24 8         296 print "ok 7\n";
25             }
26              
27             # next tests: first, we fork.
28             # The parent spawns a new NetServer::Generic server in preforked server mode.
29             # It spawns two children that re-spawn after two instances. It prints whatever
30             # comes to it on $out (which is aliased to the original STDOUT).
31             # The child spawns a NetServer::Generic client() server, with a trigger
32             # that fires three times. It prints "ok .. $n" each time, and this is picked
33             # up by the parent (if successful).
34             #
35             # The parent prints ok .. 5 when it exits.
36             #
37             # Test meanings are:
38             #
39             # 1 we could create a NetServer::Generic
40             # 2 - 5 inclusive: the client repeatedly forked and sent these strings
41             # to the server, which echoed them to STDOUT
42             #
43             # NOTE: as yet, no testing for the non-forking server is available.
44             #
45              
46 8         24 my $port = 10101;
47 8         16 my $host = localhost;
48              
49 8     8   48 no strict "vars";
  8         16  
  8         28472  
50              
51 8         16 $j = 1;
52              
53 8         40 $out = *STDOUT;
54 8         32 select STDERR; $| = 1;
  8         32  
55 8         32 select STDOUT; $| = 1;
  8         16  
56             #select STDIN;
57 8         16 eval {
58             FORK: {
59 8 100       8 if ($pid = fork) {
  8 50       13278  
    0          
60             # parent
61 2         332 my ($foo) = new NetServer::Generic();
62 2         250 $foo->port($port);
63 2         26 $foo->hostname($host);
64             my $server = sub {
65 4     4   14 my $self = shift;
66 4         69 while (defined($tmp = )) {
67 8 100       61 if ($tmp !~ /exit/) {
68 4         220 print $out "ok $tmp";
69 4         16 $val = $tmp; chomp $val;
  4         32  
70             } else {
71 4 100       1093740 if ($val == 12) {
72 1         5000250 sleep 5;
73 1         42 $self->quit();
74             }
75             }
76             }
77 2         48 };
78 2         14 $foo->callback($server);
79 2         38 $foo->mode(prefork);
80 2         36 $foo->start_servers(2);
81 2         14 $foo->server_lifespan(2);
82 2         12 $foo->max_servers(4);
83 2         12 $foo->min_spare_servers(1);
84 2         20 $foo->run();
85 0         0 exit;
86             } elsif (defined $pid) {
87             # child
88 6         1530 my ($foo) = new NetServer::Generic();
89 6         924 $foo->port($port);
90 6         138 $foo->hostname($host);
91             my $client = sub {
92 5     5   17 my $self = shift;
93 5         66 my $x = $j + 6;
94 5         360 print STDOUT "$x\nexit\n";
95 5         2577 exit 0;
96 6         288 };
97 6         114 $foo->callback($client);
98             my $trigger = sub {
99 21     21   140 $j++;
100 21 100       895 return(($j > 6) ? 0 : 1 );
101 6         132 };
102 6         216 $foo->trigger($trigger);
103 6         324 $foo->mode(client);
104 6         108 $foo->run();
105 1         457 exit;
106             } elsif ($! =~ /no more processes/i) {
107 0         0 print $out "not ok 7\n";
108 0         0 print STDERR "Are you running a Win32 system? If so, this \n",
109             "test script will not and cannot work\n";
110 0         0 exit 0;
111             } else {
112 0         0 die "Can't fork: $!\n";
113 0         0 exit 0;
114             }
115             }
116             }; # end evail
117              
118 0 0       0 if ($@ ne '') {
119 0         0 print "eval(): $@\n";
120             }
121              
122 0         0 exit 0;