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