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 => 3; |
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 ( |
18 | my $rd_ae = AnyEvent::Handle->new ( |
15 | fh => $rd, |
19 | fh => $rd, |
|
|
20 | on_error => sub { |
|
|
21 | ok ($! == &Errno::EPIPE); |
|
|
22 | }, |
16 | on_eof => sub { $cv->broadcast }, |
23 | on_eof => sub { $cv->broadcast }, |
17 | ); |
24 | ); |
18 | |
25 | |
19 | my $concat; |
26 | my $concat; |
20 | |
27 | |
21 | $rd_ae->push_read_line (sub { |
28 | $rd_ae->push_read (line => sub { |
22 | is ($_[1], "A", 'A line was read correctly'); |
29 | is ($_[1], "A", 'A line was read correctly'); |
23 | my $cb; $cb = sub { |
30 | my $cb; $cb = sub { |
24 | $concat .= $_[1]; |
31 | $concat .= $_[1]; |
25 | $_[0]->push_read_line ($cb); |
32 | $_[0]->push_read (line => $cb); |
26 | }; |
33 | }; |
27 | $_[0]->push_read_line ($cb); |
34 | $_[0]->push_read (line => $cb); |
28 | }); |
35 | }); |
29 | |
36 | |
30 | syswrite $wr, "A\nBC\nDEF\nG\n" . ("X" x 113) . "\n"; |
37 | syswrite $wr, "A\012BC\012DEF\012G\012" . ("X" x 113) . "\012"; |
31 | close $wr; |
38 | close $wr; |
32 | |
39 | |
33 | $cv->wait; |
40 | $cv->wait; |
34 | is ($concat, "BCDEFG" . ("X" x 113), 'first lines were read correctly'); |
41 | is ($concat, "BCDEFG" . ("X" x 113), 'initial lines were read correctly'); |
35 | } |
42 | } |
36 | |
43 | |
37 | { |
44 | { |
38 | my $cv = AnyEvent->condvar; |
45 | my $cv = AnyEvent->condvar; |
39 | |
46 | |
… | |
… | |
44 | my $rd_ae = |
51 | my $rd_ae = |
45 | AnyEvent::Handle->new ( |
52 | AnyEvent::Handle->new ( |
46 | fh => $rd, |
53 | fh => $rd, |
47 | on_eof => sub { $cv->broadcast }, |
54 | on_eof => sub { $cv->broadcast }, |
48 | on_read => sub { |
55 | on_read => sub { |
49 | $_[0]->push_read_line (sub { |
56 | $_[0]->push_read (line => sub { |
50 | $concat .= "$_[1]:"; |
57 | $concat .= "$_[1]:"; |
51 | }); |
58 | }); |
52 | } |
59 | } |
53 | ); |
60 | ); |
54 | |
61 | |
55 | my $wr_ae = new AnyEvent::Handle fh => $wr, on_eof => sub { die }; |
62 | my $wr_ae = new AnyEvent::Handle fh => $wr, on_eof => sub { die }; |
56 | |
63 | |
57 | $wr_ae->push_write ("A\nBC\nDEF\nG\n" . ("X" x 113) . "\n"); |
|
|
58 | undef $wr; |
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"); |
59 | undef $wr_ae; |
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) }); |
60 | |
78 | |
61 | $cv->wait; |
79 | $cv->wait; |
62 | |
80 | |
63 | is ($concat, "A:BC:DEF:G:" . ("X" x 113) . ":", 'second lines were read correctly'); |
81 | is ($concat, "A:BC:DEF:G:" . ("X" x 113) . ":", 'second set of lines were read correctly'); |
64 | } |
82 | } |
|
|
83 | |