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