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.12 by root, Fri May 23 17:47:06 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;
7use AnyEvent::Socket;
4use AnyEvent; 8use AnyEvent;
5use AnyEvent::Socket;
6 9
7my $lbytes; 10my $lbytes;
8my $rbytes; 11my $rbytes;
9 12
13print "1..2\n";
14
10my $cv = AnyEvent->condvar; 15my $cv = AnyEvent->condvar;
11 16
12my $lsock = 17my $hdl;
13 AnyEvent::Socket->new ( 18my $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 19
25 print "connected to ".$ae_sock->fh->peerhost.":".$ae_sock->fh->peerport."\n"; 20my $w = tcp_server undef, undef,
21 sub {
22 my ($fh, $host, $port) = @_;
26 23
27 $ae_sock->on_read (sub { 24 $hdl = AnyEvent::Handle->new (fh => $fh, on_eof => sub { $cv->broadcast });
28 my ($ae_sock) = @_;
29 $rbytes = $ae_sock->rbuf;
30 });
31 25
32 $ae_sock->write ("TEST\015\012"); 26 $hdl->push_read_chunk (6, sub {
27 my ($hdl, $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
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 = 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";
33 } 59 }
34 );
35 60
36$ae_sock->on_eof (sub { $cv->broadcast }); 61 $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 }); 62 });
51}); 63};
52 64
53$cv->wait; 65$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