--- AnyEvent/t/handle/04_listen.t 2008/05/02 09:04:57 1.3 +++ AnyEvent/t/handle/04_listen.t 2008/05/21 14:37:55 1.11 @@ -1,58 +1,65 @@ #!/opt/perl/bin/perl use strict; -use Test::More tests => 2; + use AnyEvent::Impl::Perl; +use AnyEvent::Handle; +use AnyEvent::Util; use AnyEvent; -use AnyEvent::Socket; my $lbytes; my $rbytes; +print "1..2\n"; + my $cv = AnyEvent->condvar; -my $lsock = - AnyEvent::Socket->new ( - Listen => 1, - LocalPort => 32391, - ReuseAddr => 1, - ); -my $ae_sock = - AnyEvent::Socket->new ( - PeerAddr => "127.0.0.1:32391", - on_connect => sub { - my ($ae_sock, $error) = @_; - if ($error) { diag "connection failed: $!"; $cv->broadcast; return } - - print "connected to ".$ae_sock->fh->peerhost.":".$ae_sock->fh->peerport."\n"; - - $ae_sock->on_read (sub { - my ($ae_sock) = @_; - $rbytes = $ae_sock->rbuf; - }); +my $hdl; +my $port; - $ae_sock->write ("TEST\015\012"); - } - ); +my $w = AnyEvent::Util::tcp_server undef, undef, + sub { + my ($fh, $host, $port) = @_; + + $hdl = AnyEvent::Handle->new (fh => $fh, on_eof => sub { $cv->broadcast }); + + $hdl->push_read_chunk (6, sub { + my ($hdl, $data) = @_; + + if ($data eq "TEST\015\012") { + print "ok 1 - server received client data\n"; + } else { + print "not ok 1 - server received bad client data\n"; + } + + $hdl->push_write ("BLABLABLA\015\012"); + }); + }, sub { + ($port) = Socket::unpack_sockaddr_in getsockname $_[0]; -$ae_sock->on_eof (sub { $cv->broadcast }); + 0 + }; -$lsock->on_accept (sub { - my ($lsock, $cl, $paddr) = @_; - unless (defined $cl) { - diag "accept failed: $!"; - return; - } - - $cl->read (6, sub { - my ($cl, $data) = @_; - $lbytes = $data; - $cl->write ("BLABLABLA\015\012"); +my $clhdl; +my $wc = AnyEvent::Util::tcp_connect localhost => $port, sub { + my ($fh) = @_ + or die "connect: $!"; + + $clhdl = AnyEvent::Handle->new (fh => $fh, on_eof => sub { $cv->broadcast }); + + $clhdl->push_write ("TEST\015\012"); + $clhdl->push_read_line (sub { + my ($clhdl, $line) = @_; + + if ($line eq 'BLABLABLA') { + print "ok 2 - client received response\n"; + } else { + print "not ok 2 - client received bad response\n"; + } + + $cv->broadcast; }); -}); +}; $cv->wait; - -is ($lbytes, "TEST\015\012", 'listening end received data'); -is ($rbytes, "BLABLABLA\015\012", 'connecting received response');