| 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 | 8 | ▶ | BEGIN { | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | 8 | $| = 1; | |||||||||||||||||
| 11 | 8 | print "7..12\n"; # 5 tests in total | |||||||||||||||||
| 12 | } | ||||||||||||||||||
| 13 | 8 | ▶ | END { | ||||||||||||||||
|
Subroutine
END: called CC=2 SCAR=7.6
| |||||||||||||||||||
| 14 | 8 | ▶ | print "not ok 7\n" unless $loaded; # we couldn't create an object | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 15 | } | ||||||||||||||||||
| 16 | 8 | ▶ | use NetServer::Generic; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 17 | 8 | ▶ | use IO::Handle; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 18 | |||||||||||||||||||
| 19 | # first test; can we create a new NetServer::Generic? | ||||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | 8 | my $dummy = new NetServer::Generic(); | |||||||||||||||||
| 22 | 8 | ▶ | if (ref($dummy) =~ /NetServer::Generic/i) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 23 | 8 | $loaded = 1; | |||||||||||||||||
| 24 | 8 | 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 | my $port = 10101; | |||||||||||||||||
| 47 | 8 | my $host = localhost; | |||||||||||||||||
| 48 | |||||||||||||||||||
| 49 | 8 | ▶ | no strict "vars"; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 50 | |||||||||||||||||||
| 51 | 8 | $j = 1; | |||||||||||||||||
| 52 | |||||||||||||||||||
| 53 | 8 | $out = *STDOUT; | |||||||||||||||||
| 54 | 8 | select STDERR; $| = 1; | |||||||||||||||||
| 55 | 8 | select STDOUT; $| = 1; | |||||||||||||||||
| 56 | #select STDIN; | ||||||||||||||||||
| 57 | 8 | eval { | |||||||||||||||||
| 58 | FORK: { | ||||||||||||||||||
| 59 | 8 | ▶ | if ($pid = fork) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 60 | # parent | ||||||||||||||||||
| 61 | 2 | my ($foo) = new NetServer::Generic(); | |||||||||||||||||
| 62 | 2 | $foo->port($port); | |||||||||||||||||
| 63 | 2 | $foo->hostname($host); | |||||||||||||||||
| 64 | 4 | ▶ | my $server = sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||
| 65 | 4 | my $self = shift; | |||||||||||||||||
| 66 | 4 | ▶ | while (defined($tmp = <STDIN>)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 67 | 8 | ▶ | if ($tmp !~ /exit/) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 68 | 4 | print $out "ok $tmp"; | |||||||||||||||||
| 69 | 4 | $val = $tmp; chomp $val; | |||||||||||||||||
| 70 | } else { | ||||||||||||||||||
| 71 | 4 | ▶ | if ($val == 12) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 72 | 1 | sleep 5; | |||||||||||||||||
| 73 | 1 | $self->quit(); | |||||||||||||||||
| 74 | } | ||||||||||||||||||
| 75 | } | ||||||||||||||||||
| 76 | } | ||||||||||||||||||
| 77 | 2 | }; | |||||||||||||||||
| 78 | 2 | $foo->callback($server); | |||||||||||||||||
| 79 | 2 | $foo->mode(prefork); | |||||||||||||||||
| 80 | 2 | $foo->start_servers(2); | |||||||||||||||||
| 81 | 2 | $foo->server_lifespan(2); | |||||||||||||||||
| 82 | 2 | $foo->max_servers(4); | |||||||||||||||||
| 83 | 2 | $foo->min_spare_servers(1); | |||||||||||||||||
| 84 | 2 | $foo->run(); | |||||||||||||||||
| 85 | 0 | exit; | |||||||||||||||||
| 86 | } elsif (defined $pid) { | ||||||||||||||||||
| 87 | # child | ||||||||||||||||||
| 88 | 6 | my ($foo) = new NetServer::Generic(); | |||||||||||||||||
| 89 | 6 | $foo->port($port); | |||||||||||||||||
| 90 | 6 | $foo->hostname($host); | |||||||||||||||||
| 91 | 5 | ▶ | my $client = sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 92 | 5 | my $self = shift; | |||||||||||||||||
| 93 | 5 | my $x = $j + 6; | |||||||||||||||||
| 94 | 5 | print STDOUT "$x\nexit\n"; | |||||||||||||||||
| 95 | 5 | exit 0; | |||||||||||||||||
| 96 | 6 | }; | |||||||||||||||||
| 97 | 6 | $foo->callback($client); | |||||||||||||||||
| 98 | 21 | ▶ | my $trigger = sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||
| 99 | 21 | $j++; | |||||||||||||||||
| 100 | 21 | ▶ | return(($j > 6) ? 0 : 1 ); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 101 | 6 | }; | |||||||||||||||||
| 102 | 6 | $foo->trigger($trigger); | |||||||||||||||||
| 103 | 6 | $foo->mode(client); | |||||||||||||||||
| 104 | 6 | $foo->run(); | |||||||||||||||||
| 105 | 1 | exit; | |||||||||||||||||
| 106 | } elsif ($! =~ /no more processes/i) { | ||||||||||||||||||
| 107 | 0 | print $out "not ok 7\n"; | |||||||||||||||||
| 108 | 0 | print STDERR "Are you running a Win32 system? If so, this \n", | |||||||||||||||||
| 109 | "test script will not and cannot work\n"; | ||||||||||||||||||
| 110 | 0 | exit 0; | |||||||||||||||||
| 111 | } else { | ||||||||||||||||||
| 112 | 0 | die "Can't fork: $!\n"; | |||||||||||||||||
| 113 | 0 | exit 0; | |||||||||||||||||
| 114 | } | ||||||||||||||||||
| 115 | } | ||||||||||||||||||
| 116 | }; # end evail | ||||||||||||||||||
| 117 | |||||||||||||||||||
| 118 | 0 | ▶ | if ($@ ne '') { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 119 | 0 | print "eval(): $@\n"; | |||||||||||||||||
| 120 | } | ||||||||||||||||||
| 121 | |||||||||||||||||||
| 122 | 0 | exit 0; | |||||||||||||||||