Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 148 lines (133 sloc) 4.576 kB
e6266cf @stash Streaming responses!
authored
1 #!perl
2 use warnings;
3 use strict;
df99a8b @stash Trying to track down the truncated responses that ab reports
authored
4 use constant CLIENTS_11 => 25;
5 use constant CLIENTS_10 => 25;
36088da @stash Use Conn:close streaming for 1.0 clients
authored
6 use constant CLIENTS => CLIENTS_11 + CLIENTS_10;
670218f @stash Deprecate a number of methods on the Feersum API.
authored
7 use Test::More tests => 7 + 21 * CLIENTS_11 + 22 * CLIENTS_10;
e6266cf @stash Streaming responses!
authored
8 use Test::Exception;
adf03c8 @stash Refactor common test code into t/Utils.pm
authored
9 use lib 't'; use Utils;
e6266cf @stash Streaming responses!
authored
10
8b823cd @stash Rename to Feersum
authored
11 BEGIN { use_ok('Feersum') };
e6266cf @stash Streaming responses!
authored
12
adf03c8 @stash Refactor common test code into t/Utils.pm
authored
13 my ($socket,$port) = get_listen_socket();
e6266cf @stash Streaming responses!
authored
14 ok $socket, "made listen socket";
15 ok $socket->fileno, "has a fileno";
16
8b823cd @stash Rename to Feersum
authored
17 my $evh = Feersum->new();
e6266cf @stash Streaming responses!
authored
18
19 {
20 no warnings 'redefine';
8b823cd @stash Rename to Feersum
authored
21 *Feersum::DIED = sub {
e6266cf @stash Streaming responses!
authored
22 my $err = shift;
23 fail "Died during request handler: $err";
24 };
25 }
26
27 my $cv = AE::cv;
28 my $started = 0;
29 my $finished = 0;
30 $evh->request_handler(sub {
31 my $r = shift;
276260c @stash The term "Connection" is more accurate than "Client"
authored
32 isa_ok $r, 'Feersum::Connection', 'got an object!';
926ad44 @stash Make env() faster.
authored
33 my $env = $r->env();
e6266cf @stash Streaming responses!
authored
34 ok $env && ref($env) eq 'HASH';
35
36 ok $env->{'psgi.streaming'}, 'got psgi.streaming';
37 my $cnum = $env->{HTTP_X_CLIENT};
38 ok $cnum, "got client number";
39
0874283 @stash Support close() on reader/writer handles, bump ver.
authored
40 ok !$r->can('write'), "write method removed from connection object";
e6266cf @stash Streaming responses!
authored
41
42 $cv->begin;
df99a8b @stash Trying to track down the truncated responses that ab reports
authored
43 my $w = $r->start_streaming("200 OK", ['Content-Type' => 'text/plain', 'X-Client' => $cnum, 'X-Fileno' => $r->fileno ]);
670218f @stash Deprecate a number of methods on the Feersum API.
authored
44 $started++;
45 isa_ok($w, 'Feersum::Connection::Writer', "got a writer $cnum");
46 isa_ok($w, 'Feersum::Connection::Handle', "... it's a handle $cnum");
47 my $n = 0;
df99a8b @stash Trying to track down the truncated responses that ab reports
authored
48 my $wrote_third = 0;
4ee7da5 @stash Refcount the connection properly this time
authored
49 my $t; $t = AE::timer rand()/5,rand()/5, sub {
670218f @stash Deprecate a number of methods on the Feersum API.
authored
50 $n++;
51 eval {
52 if ($n == 1) {
df99a8b @stash Trying to track down the truncated responses that ab reports
authored
53 ok blessed($w), "still blessed? $cnum";
670218f @stash Deprecate a number of methods on the Feersum API.
authored
54 # cover PADTMP case
55 $w->write("$cnum Hello streaming world! chunk ".
56 ($n==1?"one":"WTF")."\n");
57 pass "wrote chunk $n $cnum";
e6266cf @stash Streaming responses!
authored
58 }
670218f @stash Deprecate a number of methods on the Feersum API.
authored
59 elsif ($n == 2) {
df99a8b @stash Trying to track down the truncated responses that ab reports
authored
60 ok blessed($w), "still blessed? $cnum";
670218f @stash Deprecate a number of methods on the Feersum API.
authored
61 # cover PADMY case
62 my $d = "$cnum Hello streaming world! chunk ".
63 ($n==1?"WTF":"'two'")."\n";
64 $w->write($d);
65 pass "wrote chunk $n $cnum";
66 }
67 elsif ($n == 3) {
df99a8b @stash Trying to track down the truncated responses that ab reports
authored
68 ok blessed($w), "still blessed? $cnum";
670218f @stash Deprecate a number of methods on the Feersum API.
authored
69 my $buf = "$cnum Hello streaming world! chunk three\n";
70 $w->poll_cb(sub {
71 my $w2 = shift;
72 isa_ok($w2, 'Feersum::Connection::Writer',
73 "got another writer $cnum");
74 $w2->write($buf);
75 $w2->poll_cb(undef); # unset
df99a8b @stash Trying to track down the truncated responses that ab reports
authored
76 $wrote_third = 1;
670218f @stash Deprecate a number of methods on the Feersum API.
authored
77 });
78 }
df99a8b @stash Trying to track down the truncated responses that ab reports
authored
79 elsif ($wrote_third) {
80 ok blessed($w), "still blessed? $cnum";
670218f @stash Deprecate a number of methods on the Feersum API.
authored
81 $w->close();
82 pass "async writer finished $cnum";
83 dies_ok {
84 $w->write("after completion");
85 } "can't write after completion $cnum";
86 $finished++;
87 $cv->end;
88 undef $t; # important ref
89 }
90 }; if ($@) {
91 warn "oshit $cnum $@";
92 }
93 };
e6266cf @stash Streaming responses!
authored
94 });
95
96 lives_ok {
97 $evh->use_socket($socket);
98 } 'assigned socket';
99
100 sub client {
27f9207 @stash Get rid of AnyEvent::HTTP dep, clean up tests.
authored
101 my $cnum = sprintf("%04d",shift);
36088da @stash Use Conn:close streaming for 1.0 clients
authored
102 my $is_chunked = shift || 0;
e6266cf @stash Streaming responses!
authored
103 $cv->begin;
27f9207 @stash Get rid of AnyEvent::HTTP dep, clean up tests.
authored
104 my $h; $h = simple_client GET => '/foo',
105 name => $cnum,
36088da @stash Use Conn:close streaming for 1.0 clients
authored
106 timeout => 15,
107 proto => $is_chunked ? '1.1' : '1.0',
27f9207 @stash Get rid of AnyEvent::HTTP dep, clean up tests.
authored
108 headers => {
109 "Accept" => "*/*",
110 'X-Client' => $cnum,
e6266cf @stash Streaming responses!
authored
111 },
27f9207 @stash Get rid of AnyEvent::HTTP dep, clean up tests.
authored
112 sub {
113 my ($body, $headers) = @_;
36088da @stash Use Conn:close streaming for 1.0 clients
authored
114 is $headers->{Status}, 200, "$cnum got 200"
115 or diag $headers->{Reason};
116 if ($is_chunked) {
117 is $headers->{HTTPVersion}, '1.1';
118 is $headers->{'transfer-encoding'}, "chunked", "$cnum got chunked!";
119 }
120 else {
121 is $headers->{HTTPVersion}, '1.0';
122 ok !exists $headers->{'transfer-encoding'}, "$cnum not chunked!";
123 is $headers->{'connection'}, 'close', "$cnum conn closed";
124 }
27f9207 @stash Get rid of AnyEvent::HTTP dep, clean up tests.
authored
125 is_deeply [split /\n/,$body], [
126 "$cnum Hello streaming world! chunk one",
127 "$cnum Hello streaming world! chunk 'two'",
128 "$cnum Hello streaming world! chunk three",
df99a8b @stash Trying to track down the truncated responses that ab reports
authored
129 ], "$cnum got all three lines"
130 or do {
131 warn "descriptor ".$headers->{'x-fileno'}." failed!";
132 exit 2;
133 };
27f9207 @stash Get rid of AnyEvent::HTTP dep, clean up tests.
authored
134 $cv->end;
135 undef $h;
136 };
e6266cf @stash Streaming responses!
authored
137 }
138
36088da @stash Use Conn:close streaming for 1.0 clients
authored
139
140 client(1000+$_,1) for (1..CLIENTS_11);
141 client(2000+$_,0) for (1..CLIENTS_10); # HTTP/1.0 style
e6266cf @stash Streaming responses!
authored
142
143 $cv->recv;
144 is $started, CLIENTS, 'handlers started';
145 is $finished, CLIENTS, 'handlers finished';
146
147 pass "all done";
Something went wrong with that request. Please try again.