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.7 by elmex, Thu May 15 13:32:19 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 }
33
34 $hdl->push_write ("BLABLABLA\015\012");
35 });
36
37}, sub {
38 warn "error on accept: $!";
39 $cv->broadcast;
40});
41
42my $clsock =
43 IO::Socket::INET->new (
44 PeerHost => $sock->sockhost,
45 PeerPort => $sock->sockport,
46 Blocking => 0,
34 ); 47 );
35 48
36$ae_sock->on_eof (sub { $cv->broadcast }); 49my $clhdl;
50my $wc = AnyEvent::Util::connect ($clsock, sub {
51 my ($clsock) = @_;
52 $clhdl = AnyEvent::Handle->new (fh => $clsock, on_eof => sub { $cv->broadcast });
37 53
38$lsock->on_accept (sub { 54 $clhdl->push_write ("TEST\015\012");
39 my ($lsock, $cl, $paddr) = @_; 55 $clhdl->push_read_line (sub {
56 my ($clhdl, $line) = @_;
40 57
41 unless (defined $cl) { 58 if ($line eq 'BLABLABLA') {
42 diag "accept failed: $!"; 59 print "ok 2 - client received response\n";
43 return; 60 } else {
44 } 61 print "not ok 2 - client received bad response\n";
62 }
45 63
46 $cl->read (6, sub { 64 $cv->broadcast;
47 my ($cl, $data) = @_;
48 $lbytes = $data;
49 $cl->write ("BLABLABLA\015\012");
50 }); 65 });
51}); 66}, sub {
67 warn "couldn't connect: $!";
68 $cv->broadcast;
69}, 10);
52 70
53$cv->wait; 71$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