1 | #!perl |
1 | #!perl |
|
|
2 | |
2 | use strict; |
3 | use strict; |
|
|
4 | |
|
|
5 | use AnyEvent::Impl::Perl; |
3 | use AnyEvent::Handle; |
6 | use AnyEvent::Handle; |
4 | use Test::More tests => 1; |
7 | use Test::More tests => 8; |
5 | use Socket; |
8 | use Socket; |
6 | |
9 | |
|
|
10 | { |
7 | my $cv = AnyEvent->condvar; |
11 | my $cv = AnyEvent->condvar; |
8 | |
12 | |
9 | socketpair my $rd, my $wr, AF_UNIX, SOCK_STREAM, PF_UNSPEC; |
13 | socketpair my $rd, my $wr, AF_UNIX, SOCK_STREAM, PF_UNSPEC; |
10 | |
14 | |
11 | my $rd_ae = AnyEvent::Handle->new (fh => $rd); |
15 | my $rd_ae = AnyEvent::Handle->new ( |
|
|
16 | fh => $rd, |
|
|
17 | on_error => sub { |
|
|
18 | ok ($! == Errno::EPIPE); |
|
|
19 | }, |
|
|
20 | on_eof => sub { $cv->broadcast }, |
|
|
21 | ); |
12 | |
22 | |
13 | my $line_cnt = 3; |
|
|
14 | my $concat; |
23 | my $concat; |
15 | |
24 | |
16 | $rd_ae->readlines (sub { |
25 | $rd_ae->push_read (line => sub { |
17 | my ($rd_ae, @lines) = @_; |
26 | is ($_[1], "A", 'A line was read correctly'); |
18 | for (@lines) { |
27 | my $cb; $cb = sub { |
19 | chomp; |
|
|
20 | $line_cnt--; |
|
|
21 | $concat .= $_; |
28 | $concat .= $_[1]; |
|
|
29 | $_[0]->push_read (line => $cb); |
|
|
30 | }; |
|
|
31 | $_[0]->push_read (line => $cb); |
22 | } |
32 | }); |
23 | if ($line_cnt <= 0) { $cv->broadcast } |
|
|
24 | }); |
|
|
25 | |
33 | |
26 | $wr->syswrite ("A\nBC\nDEF\nG\n"); |
34 | syswrite $wr, "A\012BC\012DEF\012G\012" . ("X" x 113) . "\012"; |
27 | $wr->syswrite (("X" x 113) . "\n"); |
35 | close $wr; |
28 | |
36 | |
29 | $cv->wait; |
37 | $cv->wait; |
|
|
38 | is ($concat, "BCDEFG" . ("X" x 113), 'initial lines were read correctly'); |
|
|
39 | } |
30 | |
40 | |
|
|
41 | { |
|
|
42 | my $cv = AnyEvent->condvar; |
|
|
43 | |
|
|
44 | socketpair my $rd, my $wr, AF_UNIX, SOCK_STREAM, PF_UNSPEC; |
|
|
45 | |
|
|
46 | my $concat; |
|
|
47 | |
|
|
48 | my $rd_ae = |
|
|
49 | AnyEvent::Handle->new ( |
|
|
50 | fh => $rd, |
|
|
51 | on_eof => sub { $cv->broadcast }, |
|
|
52 | on_read => sub { |
|
|
53 | $_[0]->push_read (line => sub { |
|
|
54 | $concat .= "$_[1]:"; |
|
|
55 | }); |
|
|
56 | } |
|
|
57 | ); |
|
|
58 | |
|
|
59 | my $wr_ae = new AnyEvent::Handle fh => $wr, on_eof => sub { die }; |
|
|
60 | |
|
|
61 | undef $wr; |
|
|
62 | undef $rd; |
|
|
63 | |
|
|
64 | $wr_ae->push_write (netstring => "0:xx,,"); |
|
|
65 | $wr_ae->push_write (netstring => ""); |
|
|
66 | $wr_ae->push_write (storable => [4,3,2]); |
|
|
67 | $wr_ae->push_write (packstring => "w", "hallole" x 99999); # try to exhaust socket buffer here |
|
|
68 | $wr_ae->push_write ("A\012BC\012DEF\nG\012" . ("X" x 113) . "\012"); |
|
|
69 | undef $wr_ae; |
|
|
70 | |
|
|
71 | $rd_ae->push_read (netstring => sub { is ($_[1], "0:xx,,") }); |
|
|
72 | $rd_ae->push_read (netstring => sub { is ($_[1], "") }); |
|
|
73 | $rd_ae->push_read (storable => "w", sub { is ("@{$_[1]}", "4 3 2") }); |
|
|
74 | $rd_ae->push_read (packstring => "w", sub { is ($_[1], "hallole" x 99999) }); |
|
|
75 | |
|
|
76 | $cv->wait; |
|
|
77 | |
31 | is ($concat, "ABCDEFG".("X"x113), 'lines were read correctly'); |
78 | is ($concat, "A:BC:DEF:G:" . ("X" x 113) . ":", 'second set of lines were read correctly'); |
|
|
79 | } |
|
|
80 | |