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.4 by root, Fri May 2 09:06:56 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
2
3use strict; 2use strict;
4use Test::More tests => 2;
5use AnyEvent::Impl::Perl; 3use AnyEvent::Impl::Perl;
4use AnyEvent::Handle;
6use AnyEvent; 5use AnyEvent::Util;
7use AnyEvent::Socket; 6use IO::Socket::INET;
8 7
9my $lbytes; 8my $lbytes;
10my $rbytes; 9my $rbytes;
11 10
11print "1..2\n";
12
12my $cv = AnyEvent->condvar; 13my $cv = AnyEvent->condvar;
13 14
14my $lsock = 15my $sock = IO::Socket::INET->new (
15 AnyEvent::Socket->new ( 16 Listen => 5, ReuseAddr => 1, LocalAddr => 'localhost',
16 Listen => 1, 17) or die "Couldn't make socket: $!\n";
17 LocalPort => 32391,
18 ReuseAddr => 1,
19 );
20 18
21my $ae_sock = 19my $hdl;
22 AnyEvent::Socket->new (
23 PeerAddr => "127.0.0.1:32391",
24 on_connect => sub {
25 my ($ae_sock, $error) = @_;
26 if ($error) { diag "connection failed: $!"; $cv->broadcast; return }
27 20
28 print "connected to ".$ae_sock->fh->peerhost.":".$ae_sock->fh->peerport."\n"; 21my $w = AnyEvent::Util::listen ($sock, sub {
22 my ($cl, $claddr) = @_;
23 $hdl = AnyEvent::Handle->new (fh => $cl, 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";
36 } 32 }
37 );
38 33
39$ae_sock->on_eof (sub { $cv->broadcast }); 34 $hdl->push_write ("BLABLABLA\015\012");
35 });
40 36
41$lsock->on_accept (sub { 37}, sub {
42 my ($lsock, $cl, $paddr) = @_; 38 warn "error on accept: $!";
43 39 $cv->broadcast;
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 });
54}); 40});
55 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
56$cv->wait; 64$cv->wait;
57
58is ($lbytes, "TEST\015\012", 'listening end received data');
59is ($rbytes, "BLABLABLA\015\012", 'connecting received response');

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines