ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/t/handle/04_listen.t
Revision: 1.6
Committed: Fri May 2 09:15:34 2008 UTC (16 years, 2 months ago) by elmex
Content type: application/x-troff
Branch: MAIN
CVS Tags: rel-3_41, rel-3_4
Changes since 1.5: +16 -17 lines
Log Message:
made the test being independend from the port

File Contents

# User Rev Content
1 elmex 1.1 #!/opt/perl/bin/perl
2 root 1.2
3 elmex 1.1 use strict;
4     use Test::More tests => 2;
5 root 1.2 use AnyEvent::Impl::Perl;
6 elmex 1.1 use AnyEvent;
7     use AnyEvent::Socket;
8    
9     my $lbytes;
10     my $rbytes;
11    
12     my $cv = AnyEvent->condvar;
13    
14     my $lsock =
15     AnyEvent::Socket->new (
16     Listen => 1,
17     ReuseAddr => 1,
18     );
19 root 1.4
20 elmex 1.6 $lsock->on_accept (sub {
21     my ($lsock, $cl, $paddr) = @_;
22    
23     unless (defined $cl) {
24     diag "accept failed: $!";
25     return;
26     }
27    
28     $cl->read (6, sub {
29     my ($cl, $data) = @_;
30     $lbytes = $data;
31     $cl->write ("BLABLABLA\015\012");
32     });
33     });
34    
35 elmex 1.1 my $ae_sock =
36     AnyEvent::Socket->new (
37 elmex 1.6 PeerAddr => "127.0.0.1:" . $lsock->fh->sockport,
38 elmex 1.1 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    
55     $cv->wait;
56    
57     is ($lbytes, "TEST\015\012", 'listening end received data');
58     is ($rbytes, "BLABLABLA\015\012", 'connecting received response');
59 root 1.5