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