ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/t/handle/04_listen.t
Revision: 1.8
Committed: Thu May 15 13:50:23 2008 UTC (16 years, 1 month ago) by elmex
Content type: application/x-troff
Branch: MAIN
Changes since 1.7: +1 -8 lines
Log Message:
implemented tcp_connect helper and made the listen test using it.

File Contents

# Content
1 #!/opt/perl/bin/perl
2 use strict;
3 use AnyEvent::Impl::Perl;
4 use AnyEvent::Handle;
5 use AnyEvent::Util;
6 use IO::Socket::INET;
7
8 my $lbytes;
9 my $rbytes;
10
11 print "1..2\n";
12
13 my $cv = AnyEvent->condvar;
14
15 my $sock = IO::Socket::INET->new (
16 Listen => 5, ReuseAddr => 1, LocalAddr => 'localhost',
17 ) or die "Couldn't make socket: $!\n";
18
19 my $hdl;
20
21 my $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
42 my $clhdl;
43 my $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
64 $cv->wait;