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.8 by elmex, Thu May 15 13:50:23 2008 UTC

1#!/opt/perl/bin/perl 1#!/opt/perl/bin/perl
2use strict; 2use strict;
3use Test::More tests => 2; 3use AnyEvent::Impl::Perl;
4use AnyEvent::Handle;
4use AnyEvent; 5use AnyEvent::Util;
5use AnyEvent::Socket; 6use IO::Socket::INET;
6 7
7my $lbytes; 8my $lbytes;
8my $rbytes; 9my $rbytes;
9 10
11print "1..2\n";
12
10my $cv = AnyEvent->condvar; 13my $cv = AnyEvent->condvar;
11 14
12my $lsock = 15my $sock = IO::Socket::INET->new (
13 AnyEvent::Socket->new ( 16 Listen => 5, ReuseAddr => 1, LocalAddr => 'localhost',
14 Listen => 1, 17) or die "Couldn't make socket: $!\n";
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 $hdl;
26 20
27 $ae_sock->on_read (sub { 21my $w = AnyEvent::Util::listen ($sock, sub {
28 my ($ae_sock) = @_; 22 my ($cl, $claddr) = @_;
29 $rbytes = $ae_sock->rbuf; 23 $hdl = AnyEvent::Handle->new (fh => $cl, on_eof => sub { $cv->broadcast });
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";
33 } 32 }
34 );
35 33
36$ae_sock->on_eof (sub { $cv->broadcast }); 34 $hdl->push_write ("BLABLABLA\015\012");
35 });
37 36
38$lsock->on_accept (sub { 37}, sub {
39 my ($lsock, $cl, $paddr) = @_; 38 warn "error on accept: $!";
40 39 $cv->broadcast;
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 });
51}); 40});
52 41
42my $clhdl;
43my $wc = AnyEvent::Util::tcp_connect ($sock->sockhost, $sock->sockport, sub {
44 my ($clsock) = @_;
45 $clhdl = AnyEvent::Handle->new (fh => $clsock, on_eof => sub { $cv->broadcast });
46
47 $clhdl->push_write ("TEST\015\012");
48 $clhdl->push_read_line (sub {
49 my ($clhdl, $line) = @_;
50
51 if ($line eq 'BLABLABLA') {
52 print "ok 2 - client received response\n";
53 } else {
54 print "not ok 2 - client received bad response\n";
55 }
56
57 $cv->broadcast;
58 });
59}, sub {
60 warn "couldn't connect: $!";
61 $cv->broadcast;
62}, 10);
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