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.5 by root, Fri May 2 09:07:02 2008 UTC vs.
Revision 1.16 by root, Fri Jul 24 22:47:04 2009 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::Socket;
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 LocalPort => 32391,
18 ReuseAddr => 1,
19 );
20 19
21my $ae_sock = 20my $w = tcp_server undef, undef,
22 AnyEvent::Socket->new ( 21 sub {
23 PeerAddr => "127.0.0.1:32391", 22 my ($fh, $host, $port) = @_;
24 on_connect => sub {
25 my ($ae_sock, $error) = @_;
26 if ($error) { diag "connection failed: $!"; $cv->broadcast; return }
27 23
28 print "connected to ".$ae_sock->fh->peerhost.":".$ae_sock->fh->peerport."\n"; 24 $hdl = AnyEvent::Handle->new (fh => $fh, on_eof => sub { $cv->broadcast });
29 25
30 $ae_sock->on_read (sub { 26 $hdl->push_read (chunk => 6, sub {
31 my ($ae_sock) = @_; 27 my ($hdl, $data) = @_;
32 $rbytes = $ae_sock->rbuf;
33 });
34 28
35 $ae_sock->write ("TEST\015\012"); 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";
36 } 33 }
37 );
38 34
39$ae_sock->on_eof (sub { $cv->broadcast }); 35 $hdl->push_write ("BLABLABLA\015\012");
36 });
37 }, sub {
38 $port = $_[2];
40 39
41$lsock->on_accept (sub { 40 0
42 my ($lsock, $cl, $paddr) = @_; 41 };
43 42
44 unless (defined $cl) { 43my $clhdl; $clhdl = AnyEvent::Handle->new (
45 diag "accept failed: $!"; 44 connect => [localhost => $port],
46 return; 45 on_eof => sub { $cv->broadcast },
46);
47
48$clhdl->push_write ("TEST\015\012");
49$clhdl->push_read (line => sub {
50 my ($clhdl, $line) = @_;
51
52 if ($line eq 'BLABLABLA') {
53 print "ok 2 - client received response\n";
54 } else {
55 print "not ok 2 - client received bad response\n";
47 } 56 }
48 57
49 $cl->read (6, sub { 58 $cv->broadcast;
50 my ($cl, $data) = @_;
51 $lbytes = $data;
52 $cl->write ("BLABLABLA\015\012");
53 });
54}); 59});
55 60
56$cv->wait; 61$cv->wait;
57
58is ($lbytes, "TEST\015\012", 'listening end received data');
59is ($rbytes, "BLABLABLA\015\012", 'connecting received response');
60

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines