--- AnyEvent/t/handle/04_listen.t 2008/05/02 09:07:02 1.5 +++ AnyEvent/t/handle/04_listen.t 2009/07/24 22:47:04 1.16 @@ -1,60 +1,61 @@ #!/opt/perl/bin/perl use strict; -use Test::More tests => 2; + use AnyEvent::Impl::Perl; -use AnyEvent; +use AnyEvent::Handle; use AnyEvent::Socket; +use AnyEvent; 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; - }); - - $ae_sock->write ("TEST\015\012"); - } - ); - -$ae_sock->on_eof (sub { $cv->broadcast }); - -$lsock->on_accept (sub { - my ($lsock, $cl, $paddr) = @_; - - unless (defined $cl) { - diag "accept failed: $!"; - return; +my $hdl; +my $port; + +my $w = 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 = $_[2]; + + 0 + }; + +my $clhdl; $clhdl = AnyEvent::Handle->new ( + connect => [localhost => $port], + 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"; } - $cl->read (6, sub { - my ($cl, $data) = @_; - $lbytes = $data; - $cl->write ("BLABLABLA\015\012"); - }); + $cv->broadcast; }); $cv->wait; - -is ($lbytes, "TEST\015\012", 'listening end received data'); -is ($rbytes, "BLABLABLA\015\012", 'connecting received response'); -