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.1 by elmex, Sun Apr 27 16:56:18 2008 UTC vs.
Revision 1.10 by root, Wed May 21 14:34:03 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines