ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/t/handle/04_listen.t
(Generate patch)

Comparing AnyEvent/t/handle/04_listen.t (file contents):
Revision 1.6 by elmex, Fri May 2 09:15:34 2008 UTC vs.
Revision 1.11 by root, Wed May 21 14:37:55 2008 UTC

1#!/opt/perl/bin/perl 1#!/opt/perl/bin/perl
2 2
3use strict; 3use strict;
4use Test::More tests => 2; 4
5use AnyEvent::Impl::Perl; 5use AnyEvent::Impl::Perl;
6use AnyEvent::Handle;
7use AnyEvent::Util;
6use AnyEvent; 8use AnyEvent;
7use AnyEvent::Socket;
8 9
9my $lbytes; 10my $lbytes;
10my $rbytes; 11my $rbytes;
11 12
13print "1..2\n";
14
12my $cv = AnyEvent->condvar; 15my $cv = AnyEvent->condvar;
13 16
14my $lsock = 17my $hdl;
15 AnyEvent::Socket->new ( 18my $port;
16 Listen => 1,
17 ReuseAddr => 1,
18 );
19 19
20$lsock->on_accept (sub { 20my $w = AnyEvent::Util::tcp_server undef, undef,
21 my ($lsock, $cl, $paddr) = @_; 21 sub {
22 my ($fh, $host, $port) = @_;
22 23
23 unless (defined $cl) { 24 $hdl = AnyEvent::Handle->new (fh => $fh, on_eof => sub { $cv->broadcast });
24 diag "accept failed: $!";
25 return;
26 }
27 25
28 $cl->read (6, sub { 26 $hdl->push_read_chunk (6, sub {
29 my ($cl, $data) = @_; 27 my ($hdl, $data) = @_;
30 $lbytes = $data; 28
29 if ($data eq "TEST\015\012") {
30 print "ok 1 - server received client data\n";
31 } else {
32 print "not ok 1 - server received bad client data\n";
33 }
34
31 $cl->write ("BLABLABLA\015\012"); 35 $hdl->push_write ("BLABLABLA\015\012");
36 });
37 }, sub {
38 ($port) = Socket::unpack_sockaddr_in getsockname $_[0];
39
40 0
41 };
42
43
44my $clhdl;
45my $wc = AnyEvent::Util::tcp_connect localhost => $port, sub {
46 my ($fh) = @_
47 or die "connect: $!";
48
49 $clhdl = AnyEvent::Handle->new (fh => $fh, on_eof => sub { $cv->broadcast });
50
51 $clhdl->push_write ("TEST\015\012");
52 $clhdl->push_read_line (sub {
53 my ($clhdl, $line) = @_;
54
55 if ($line eq 'BLABLABLA') {
56 print "ok 2 - client received response\n";
57 } else {
58 print "not ok 2 - client received bad response\n";
59 }
60
61 $cv->broadcast;
32 }); 62 });
33}); 63};
34
35my $ae_sock =
36 AnyEvent::Socket->new (
37 PeerAddr => "127.0.0.1:" . $lsock->fh->sockport,
38 on_connect => sub {
39 my ($ae_sock, $error) = @_;
40 if ($error) { diag "connection failed: $!"; $cv->broadcast; return }
41
42 print "connected to ".$ae_sock->fh->peerhost.":".$ae_sock->fh->peerport."\n";
43
44 $ae_sock->on_read (sub {
45 my ($ae_sock) = @_;
46 $rbytes = $ae_sock->rbuf;
47 });
48
49 $ae_sock->write ("TEST\015\012");
50 }
51 );
52
53$ae_sock->on_eof (sub { $cv->broadcast });
54 64
55$cv->wait; 65$cv->wait;
56
57is ($lbytes, "TEST\015\012", 'listening end received data');
58is ($rbytes, "BLABLABLA\015\012", 'connecting received response');
59

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines