1# -*- perl -*-
2#
3#   $Id: fork.t,v 1.2 1999/08/12 14:28:59 joe Exp $
4#
5
6require 5.004;
7use strict;
8use IO::Socket ();
9use Config ();
10use Net::Daemon::Test ();
11
12my $ok;
13eval {
14  if ($^O ne "MSWin32") {
15    my $pid = fork();
16    if (defined($pid)) {
17      if (!$pid) { exit 0; } # Child
18    }
19    $ok = 1;
20  }
21};
22if (!$ok) {
23  print "1..0\n";
24  exit 0;
25}
26
27
28my $numTests = 5;
29
30
31my($handle, $port);
32if (@ARGV) {
33    $port = shift @ARGV;
34} else {
35    ($handle, $port) = Net::Daemon::Test->Child($numTests,
36						$^X, '-Iblib/lib',
37						'-Iblib/arch',
38						't/server', '--mode=fork',
39						'--debug', '--timeout', 60);
40}
41
42print "Making first connection to port $port...\n";
43my $fh = IO::Socket::INET->new('PeerAddr' => '127.0.0.1',
44			       'PeerPort' => $port);
45printf("%s 1\n", $fh ? "ok" : "not ok");
46printf("%s 2\n", $fh->close() ? "ok" : "not ok");
47print "Making second connection to port $port...\n";
48$fh = IO::Socket::INET->new('PeerAddr' => '127.0.0.1',
49			    'PeerPort' => $port);
50printf("%s 3\n", $fh ? "ok" : "not ok");
51eval {
52    for (my $i = 0;  $i < 20;  $i++) {
53	print "Writing number: $i\n";
54	if (!$fh->print("$i\n")  ||  !$fh->flush()) {
55	    die "Client: Error while writing number $i: " . $fh->error()
56		. " ($!)";
57	}
58	print "Written.\n";
59	my($line) = $fh->getline();
60	if (!defined($line)) {
61	    die "Client: Error while reading number $i: " . $fh->error()
62		. " ($!)";
63	}
64	if ($line !~ /(\d+)/  ||  $1 != $i*2) {
65	    die "Wrong response, exptected " . ($i*2) . ", got $line";
66	}
67    }
68};
69if ($@) {
70    print STDERR "$@\n";
71    print "not ok 4\n";
72} else {
73    print "ok 4\n";
74}
75printf("%s 5\n", $fh->close() ? "ok" : "not ok");
76
77END {
78    if ($handle) { $handle->Terminate() }
79    if (-f "ndtest.prt") { unlink "ndtest.prt" }
80    exit 0;
81}
82