1 |
elmex |
1.1 |
#!perl |
2 |
root |
1.3 |
|
3 |
root |
1.11 |
# actually tests a few other read/write types as well |
4 |
|
|
|
5 |
elmex |
1.1 |
use strict; |
6 |
root |
1.5 |
|
7 |
root |
1.13 |
use AnyEvent; |
8 |
|
|
BEGIN { require AnyEvent::Impl::Perl unless $ENV{PERL_ANYEVENT_MODEL} } |
9 |
elmex |
1.1 |
use AnyEvent::Handle; |
10 |
root |
1.10 |
use Test::More tests => 8; |
11 |
elmex |
1.1 |
use Socket; |
12 |
root |
1.11 |
use Errno; |
13 |
elmex |
1.1 |
|
14 |
elmex |
1.2 |
{ |
15 |
|
|
my $cv = AnyEvent->condvar; |
16 |
elmex |
1.1 |
|
17 |
elmex |
1.2 |
socketpair my $rd, my $wr, AF_UNIX, SOCK_STREAM, PF_UNSPEC; |
18 |
elmex |
1.1 |
|
19 |
root |
1.4 |
my $rd_ae = AnyEvent::Handle->new ( |
20 |
root |
1.8 |
fh => $rd, |
21 |
|
|
on_error => sub { |
22 |
root |
1.11 |
ok ($! == &Errno::EPIPE); |
23 |
root |
1.12 |
$cv->broadcast; |
24 |
|
|
}, |
25 |
|
|
on_eof => sub { |
26 |
|
|
ok (0, "got eof"); |
27 |
root |
1.8 |
}, |
28 |
root |
1.4 |
); |
29 |
|
|
|
30 |
elmex |
1.2 |
my $concat; |
31 |
elmex |
1.1 |
|
32 |
root |
1.7 |
$rd_ae->push_read (line => sub { |
33 |
root |
1.4 |
is ($_[1], "A", 'A line was read correctly'); |
34 |
|
|
my $cb; $cb = sub { |
35 |
|
|
$concat .= $_[1]; |
36 |
root |
1.7 |
$_[0]->push_read (line => $cb); |
37 |
root |
1.4 |
}; |
38 |
root |
1.7 |
$_[0]->push_read (line => $cb); |
39 |
elmex |
1.2 |
}); |
40 |
|
|
|
41 |
root |
1.8 |
syswrite $wr, "A\012BC\012DEF\012G\012" . ("X" x 113) . "\012"; |
42 |
root |
1.4 |
close $wr; |
43 |
elmex |
1.2 |
|
44 |
|
|
$cv->wait; |
45 |
root |
1.8 |
is ($concat, "BCDEFG" . ("X" x 113), 'initial lines were read correctly'); |
46 |
elmex |
1.2 |
} |
47 |
|
|
|
48 |
|
|
{ |
49 |
|
|
my $cv = AnyEvent->condvar; |
50 |
|
|
|
51 |
|
|
socketpair my $rd, my $wr, AF_UNIX, SOCK_STREAM, PF_UNSPEC; |
52 |
|
|
|
53 |
|
|
my $concat; |
54 |
|
|
|
55 |
|
|
my $rd_ae = |
56 |
|
|
AnyEvent::Handle->new ( |
57 |
root |
1.4 |
fh => $rd, |
58 |
|
|
on_eof => sub { $cv->broadcast }, |
59 |
|
|
on_read => sub { |
60 |
root |
1.6 |
$_[0]->push_read (line => sub { |
61 |
root |
1.4 |
$concat .= "$_[1]:"; |
62 |
|
|
}); |
63 |
elmex |
1.2 |
} |
64 |
|
|
); |
65 |
|
|
|
66 |
root |
1.4 |
my $wr_ae = new AnyEvent::Handle fh => $wr, on_eof => sub { die }; |
67 |
|
|
|
68 |
root |
1.8 |
undef $wr; |
69 |
|
|
undef $rd; |
70 |
|
|
|
71 |
root |
1.6 |
$wr_ae->push_write (netstring => "0:xx,,"); |
72 |
root |
1.8 |
$wr_ae->push_write (netstring => ""); |
73 |
root |
1.10 |
$wr_ae->push_write (storable => [4,3,2]); |
74 |
root |
1.9 |
$wr_ae->push_write (packstring => "w", "hallole" x 99999); # try to exhaust socket buffer here |
75 |
root |
1.10 |
$wr_ae->push_write ("A\012BC\012DEF\nG\012" . ("X" x 113) . "\012"); |
76 |
root |
1.4 |
undef $wr_ae; |
77 |
elmex |
1.1 |
|
78 |
root |
1.10 |
$rd_ae->push_read (netstring => sub { is ($_[1], "0:xx,,") }); |
79 |
|
|
$rd_ae->push_read (netstring => sub { is ($_[1], "") }); |
80 |
|
|
$rd_ae->push_read (storable => "w", sub { is ("@{$_[1]}", "4 3 2") }); |
81 |
|
|
$rd_ae->push_read (packstring => "w", sub { is ($_[1], "hallole" x 99999) }); |
82 |
root |
1.6 |
|
83 |
elmex |
1.2 |
$cv->wait; |
84 |
elmex |
1.1 |
|
85 |
root |
1.10 |
is ($concat, "A:BC:DEF:G:" . ("X" x 113) . ":", 'second set of lines were read correctly'); |
86 |
elmex |
1.2 |
} |
87 |
root |
1.8 |
|