1# -*- perl -*-
2#
3#   $Id: thread.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 = 5;
14
15
16# Check whether threads are available, otherwise skip this test.
17
18if (!eval { require threads; my $t = threads->new(sub { }) }) {
19    print "1..0\n";
20    exit 0;
21}
22
23my($handle, $port) = Net::Daemon::Test->Child
24    ($numTests, $^X, 't/server', '--timeout', 20, '--mode=ithreads');
25
26
27print "Making first connection to port $port...\n";
28my $fh = IO::Socket::INET->new('PeerAddr' => '127.0.0.1',
29			       'PeerPort' => $port);
30printf("%s 1\n", $fh ? "ok" : "not ok");
31printf("%s 2\n", $fh->close() ? "ok" : "not ok");
32print "Making second connection to port $port...\n";
33$fh = IO::Socket::INET->new('PeerAddr' => '127.0.0.1',
34			    'PeerPort' => $port);
35printf("%s 3\n", $fh ? "ok" : "not ok");
36eval {
37    for (my $i = 0;  $i < 20;  $i++) {
38	if (!$fh->print("$i\n")  ||  !$fh->flush()) {
39	    die "Error while writing $i: " . $fh->error() . " ($!)";
40	}
41	my $line = $fh->getline();
42	die "Error while reading $i: " . $fh->error() . " ($!)"
43	    unless defined($line);
44	die "Result error: Expected " . ($i*2) . ", got $line"
45	    unless ($line =~ /(\d+)/  &&  $1 == $i*2);
46    }
47};
48if ($@) {
49    print STDERR "$@\n";
50    print "not ok 4\n";
51} else {
52    print "ok 4\n";
53}
54printf("%s 5\n", $fh->close() ? "ok" : "not ok");
55
56END {
57    if ($handle) { $handle->Terminate() }
58    if (-f "ndtest.prt") { unlink "ndtest.prt" }
59}
60