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.9 by root, Sat May 17 21:08:05 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;
6use AnyEvent; 7use AnyEvent::Util;
7use AnyEvent::Socket;
8 8
9my $lbytes; 9my $lbytes;
10my $rbytes; 10my $rbytes;
11 11
12print "1..2\n";
13
12my $cv = AnyEvent->condvar; 14my $cv = AnyEvent->condvar;
13 15
14my $lsock = 16my $hdl;
15 AnyEvent::Socket->new ( 17my $port;
16 Listen => 1,
17 LocalPort => 32391,
18 ReuseAddr => 1,
19 );
20 18
21my $ae_sock = 19my $w = AnyEvent::Util::tcp_server undef, undef,
22 AnyEvent::Socket->new ( 20 sub {
23 PeerAddr => "127.0.0.1:32391", 21 my ($fh, $host, $port) = @_;
24 on_connect => sub {
25 my ($ae_sock, $error) = @_;
26 if ($error) { diag "connection failed: $!"; $cv->broadcast; return }
27 22
28 print "connected to ".$ae_sock->fh->peerhost.":".$ae_sock->fh->peerport."\n"; 23 $hdl = AnyEvent::Handle->new (fh => $fh, on_eof => sub { $cv->broadcast });
29 24
30 $ae_sock->on_read (sub { 25 $hdl->push_read_chunk (6, sub {
31 my ($ae_sock) = @_; 26 my ($hdl, $data) = @_;
32 $rbytes = $ae_sock->rbuf;
33 });
34 27
35 $ae_sock->write ("TEST\015\012"); 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
47 $clhdl = AnyEvent::Handle->new (fh => $fh, on_eof => sub { $cv->broadcast });
48
49 $clhdl->push_write ("TEST\015\012");
50 $clhdl->push_read_line (sub {
51 my ($clhdl, $line) = @_;
52
53 if ($line eq 'BLABLABLA') {
54 print "ok 2 - client received response\n";
55 } else {
56 print "not ok 2 - client received bad response\n";
36 } 57 }
37 );
38 58
39$ae_sock->on_eof (sub { $cv->broadcast }); 59 $cv->broadcast;
40
41$lsock->on_accept (sub {
42 my ($lsock, $cl, $paddr) = @_;
43
44 unless (defined $cl) {
45 diag "accept failed: $!";
46 return;
47 }
48
49 $cl->read (6, sub {
50 my ($cl, $data) = @_;
51 $lbytes = $data;
52 $cl->write ("BLABLABLA\015\012");
53 }); 60 });
54}); 61};
55 62
56$cv->wait; 63$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