1# -*- perl -*-
2#
3#   $Id: loop.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
13my $numTests = 6;
14
15
16my($handle, $port) = Net::Daemon::Test->Child($numTests,
17					      $^X, '-Iblib/lib', '-Iblib/arch',
18					      't/server', '--mode=single',
19					      '--loop-timeout=2',
20					      '--timeout', 60);
21
22print "Making first connection to port $port...\n";
23my $fh = IO::Socket::INET->new('PeerAddr' => '127.0.0.1',
24			       'PeerPort' => $port);
25printf("%s 1\n", $fh ? "ok" : "not ok");
26printf("%s 2\n", $fh->close() ? "ok" : "not ok");
27print "Making second connection to port $port...\n";
28$fh = IO::Socket::INET->new('PeerAddr' => '127.0.0.1',
29			    'PeerPort' => $port);
30printf("%s 3\n", $fh ? "ok" : "not ok");
31my($ok) = $fh ? 1 : 0;
32for (my $i = 0;  $ok  &&  $i < 20;  $i++) {
33    print "Writing number: $i\n";
34    if (!$fh->print("$i\n")  ||  !$fh->flush()) { $ok = 0; last; }
35    print "Written.\n";
36    my($line) = $fh->getline();
37    print "line = ", (defined($line) ? $line : "undef"), "\n";
38    if (!defined($line)) { $ok = 0;  last; }
39    if ($line !~ /(\d+)/  ||  $1 != $i*2) { $ok = 0;  last; }
40}
41printf("%s 4\n", $ok ? "ok" : "not ok");
42printf("%s 5\n", $fh->close() ? "ok" : "not ok");
43
44$ok = 0;
45for (my $i = 0;  $i < 30;  $i++) {
46    my $num;
47    if (open(CNT, "<ndtest.cnt") and
48	defined($num = <CNT>)  and
49	$num eq "10\n") {
50	$ok = 1;
51	last;
52    }
53    sleep 1;
54}
55printf("%s 6\n", $ok ? "ok" : "not ok");
56
57END {
58    if ($handle) { $handle->Terminate() }
59    unlink "ndtest.prt", "ndtest.cnt";
60}
61