Permalink
Browse files

* Implement psgix.harakiri support (with tests).

  • Loading branch information...
1 parent ffcd316 commit a9523cdd3d9ef46d1e2119dc8e4c80420ef74922 @audreyt audreyt committed Jun 13, 2011
Showing with 54 additions and 0 deletions.
  1. +12 −0 lib/Starman/Server.pm
  2. +42 −0 t/harakiri.t
View
12 lib/Starman/Server.pm
@@ -396,6 +396,11 @@ sub _prepare_env {
sub _finalize_response {
my($self, $env, $res) = @_;
+ if ($env->{'psgix.harakiri'}) {
+ $self->{client}->{keepalive} = 0;
+ $self->{client}->{harakiri} = 1;
+ }
+
my $protocol = $env->{SERVER_PROTOCOL};
my $status = $res->[0];
my $message = status_message($status);
@@ -480,4 +485,11 @@ sub _finalize_response {
}
}
+sub post_client_connection_hook {
+ my $self = shift;
+ if ($self->{client}->{harakiri}) {
+ exit;
+ }
+}
+
1;
View
42 t/harakiri.t
@@ -0,0 +1,42 @@
+use strict;
+use warnings;
+
+use HTTP::Request::Common;
+use Plack::Test;
+use Test::More;
+
+$Plack::Test::Impl = 'Server';
+$ENV{PLACK_SERVER} = 'Starman';
+
+test_psgi
+ app => sub {
+ my $env = shift;
+ return [ 200, [ 'Content-Type' => 'text/plain' ], [$$] ];
+ },
+ client => sub {
+ my %seen_pid;
+ my $cb = shift;
+ for (1..23) {
+ my $res = $cb->(GET "/");
+ $seen_pid{$res->content}++;
+ }
+ is keys(%seen_pid), 5, 'In non-harakiri mode, pid is reused';
+ };
+
+test_psgi
+ app => sub {
+ my $env = shift;
+ $env->{'psgix.harakiri'} = 1;
+ return [ 200, [ 'Content-Type' => 'text/plain' ], [$$] ];
+ },
+ client => sub {
+ my %seen_pid;
+ my $cb = shift;
+ for (1..23) {
+ my $res = $cb->(GET "/");
+ $seen_pid{$res->content}++;
+ }
+ is keys(%seen_pid), 23, 'In Harakiri mode, each pid only used once';
+ };
+
+done_testing;

0 comments on commit a9523cd

Please sign in to comment.