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.16 by root, Fri Jul 24 22:47:04 2009 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 {
33 } 27 my ($hdl, $data) = @_;
34 );
35 28
36$ae_sock->on_eof (sub { $cv->broadcast }); 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 }
37 34
38$lsock->on_accept (sub { 35 $hdl->push_write ("BLABLABLA\015\012");
39 my ($lsock, $cl, $paddr) = @_; 36 });
37 }, sub {
38 $port = $_[2];
40 39
41 unless (defined $cl) { 40 0
42 diag "accept failed: $!"; 41 };
43 return; 42
43my $clhdl; $clhdl = AnyEvent::Handle->new (
44 connect => [localhost => $port],
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";
44 } 56 }
45 57
46 $cl->read (6, sub { 58 $cv->broadcast;
47 my ($cl, $data) = @_;
48 $lbytes = $data;
49 $cl->write ("BLABLABLA\015\012");
50 });
51}); 59});
52 60
53$cv->wait; 61$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