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.6 by elmex, Fri May 2 09:15:34 2008 UTC vs.
Revision 1.7 by elmex, Thu May 15 13:32:19 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
15my $sock = IO::Socket::INET->new (
16 Listen => 5, ReuseAddr => 1, LocalAddr => 'localhost',
17) or die "Couldn't make socket: $!\n";
18
19my $hdl;
20
21my $w = AnyEvent::Util::listen ($sock, sub {
22 my ($cl, $claddr) = @_;
23 $hdl = AnyEvent::Handle->new (fh => $cl, on_eof => sub { $cv->broadcast });
24
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";
32 }
33
34 $hdl->push_write ("BLABLABLA\015\012");
35 });
36
37}, sub {
38 warn "error on accept: $!";
39 $cv->broadcast;
40});
41
14my $lsock = 42my $clsock =
15 AnyEvent::Socket->new ( 43 IO::Socket::INET->new (
16 Listen => 1, 44 PeerHost => $sock->sockhost,
17 ReuseAddr => 1, 45 PeerPort => $sock->sockport,
46 Blocking => 0,
18 ); 47 );
19 48
20$lsock->on_accept (sub { 49my $clhdl;
21 my ($lsock, $cl, $paddr) = @_; 50my $wc = AnyEvent::Util::connect ($clsock, sub {
51 my ($clsock) = @_;
52 $clhdl = AnyEvent::Handle->new (fh => $clsock, on_eof => sub { $cv->broadcast });
22 53
23 unless (defined $cl) { 54 $clhdl->push_write ("TEST\015\012");
24 diag "accept failed: $!"; 55 $clhdl->push_read_line (sub {
25 return; 56 my ($clhdl, $line) = @_;
26 }
27 57
28 $cl->read (6, sub { 58 if ($line eq 'BLABLABLA') {
29 my ($cl, $data) = @_; 59 print "ok 2 - client received response\n";
30 $lbytes = $data; 60 } else {
31 $cl->write ("BLABLABLA\015\012"); 61 print "not ok 2 - client received bad response\n";
62 }
63
64 $cv->broadcast;
32 }); 65 });
33}); 66}, sub {
34 67 warn "couldn't connect: $!";
35my $ae_sock = 68 $cv->broadcast;
36 AnyEvent::Socket->new ( 69}, 10);
37 PeerAddr => "127.0.0.1:" . $lsock->fh->sockport,
38 on_connect => sub {
39 my ($ae_sock, $error) = @_;
40 if ($error) { diag "connection failed: $!"; $cv->broadcast; return }
41
42 print "connected to ".$ae_sock->fh->peerhost.":".$ae_sock->fh->peerport."\n";
43
44 $ae_sock->on_read (sub {
45 my ($ae_sock) = @_;
46 $rbytes = $ae_sock->rbuf;
47 });
48
49 $ae_sock->write ("TEST\015\012");
50 }
51 );
52
53$ae_sock->on_eof (sub { $cv->broadcast });
54 70
55$cv->wait; 71$cv->wait;
56
57is ($lbytes, "TEST\015\012", 'listening end received data');
58is ($rbytes, "BLABLABLA\015\012", 'connecting received response');
59

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines