/
07-graceful-shutdown.t
148 lines (132 loc) · 3.53 KB
/
07-graceful-shutdown.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!perl
use warnings;
use strict;
use constant CLIENTS => 12;
use Test::More tests => 10 + 9 * CLIENTS;
use Test::Exception;
use Test::Differences;
use Scalar::Util qw/blessed/;
use lib 't'; use Utils;
BEGIN { use_ok('Feersum') };
use IO::Socket::INET;
use AnyEvent;
use AnyEvent::HTTP;
my ($socket,$port) = get_listen_socket();
ok $socket, "made listen socket";
ok $socket->fileno, "has a fileno";
my $evh = Feersum->new();
{
no warnings 'redefine';
*Feersum::DIED = sub {
my $err = shift;
fail "Died during request handler: $err";
};
}
my $cv = AE::cv;
my $started = 0;
my $finished = 0;
$evh->request_handler(sub {
my $r = shift;
isa_ok $r, 'Feersum::Connection', 'got an object!';
my $env = {};
$r->env($env);
ok $env && ref($env) eq 'HASH';
ok $env->{'psgi.streaming'}, 'got psgi.streaming';
my $cnum = $env->{HTTP_X_CLIENT};
ok $cnum, "got client number";
$cv->begin;
my $cb = $r->initiate_streaming(sub {
undef $r;
$started++;
my $start = shift;
is ref($start), 'CODE', "streaming handler got a code ref $cnum";
my $w = $start->("200 OK", ['Content-Type' => 'text/plain']);
isa_ok($w, 'Feersum::Connection::Writer', "got a writer $cnum");
isa_ok($w, 'Feersum::Connection::Handle', "... it's a handle $cnum");
my $t; $t = AE::timer 1.5+rand(0.5), 0, sub {
lives_ok {
$w->write("So graceful!\n");
$w->close();
} "wrote after waiting a little $cnum";
undef $t; # keep timer alive until it runs
undef $w;
$cv->end;
$finished++;
};
});
});
lives_ok {
$evh->use_socket($socket);
} 'assigned socket';
my @got;
sub client {
my $client_no = shift;
my $data;
$cv->begin;
my $h; $h = AnyEvent::Handle->new(
connect => ["localhost", $port],
on_connect => sub {
my $to_write = qq{GET /foo HTTP/1.1\nAccept: */*\nX-Client: $client_no\n\n};
$to_write =~ s/\n/\015\012/smg;
$h->push_write($to_write);
undef $to_write;
$h->on_read(sub {
# diag "GOT $h->{rbuf}";
$data .= delete $h->{rbuf};
});
$h->on_eof(sub {
$cv->end;
push @got, $data;
});
},
);
}
client($_) for (1..CLIENTS);
$cv->begin;
my $death;
my $grace_t = AE::timer 1.0, 0, sub {
pass "calling for shutdown";
$death = AE::timer 2.5, 0, sub {
fail "SHUTDOWN TOOK TOO LONG";
exit 1;
};
$evh->graceful_shutdown(sub {
pass "all gracefully shut down, supposedly";
undef $death;
$cv->end;
});
};
$cv->begin;
my $try_connect = AE::timer 1.4, 0, sub {
my $h; $h = AnyEvent::Handle->new(
connect => ["localhost", $port],
on_connect => sub {
fail "boo, connected when shut down";
$cv->end;
undef $h;
},
on_error => sub {
pass "cool, shouldn't be able to connect";
$cv->end;
undef $h;
}
);
};
$cv->recv;
is $started, CLIENTS, 'handlers started';
is $finished, CLIENTS, 'handlers finished';
use Test::Differences;
my $expect = join("\015\012",
"HTTP/1.1 200 OK",
"Content-Type: text/plain",
"Transfer-Encoding: chunked",
"",
"d",
"So graceful!\n",
"0"
);
$expect .= "\015\012\015\012";
for my $data (@got) {
eq_or_diff $data, $expect, "got correctly formatted chunked encoding";
}
pass "all done";