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