Skip to content
Browse files

Calls srand() automatically in the child init hook to avoid fixing th…

…e random seed
  • Loading branch information...
1 parent 45e0b6f commit 61a823384674cadaa72d74195144c88759e788a8 @miyagawa committed Feb 23, 2011
Showing with 28 additions and 0 deletions.
  1. +1 −0 lib/Starman/Server.pm
  2. +5 −0 t/rand.psgi
  3. +22 −0 t/rand.t
View
1 lib/Starman/Server.pm
@@ -108,6 +108,7 @@ sub child_init_hook {
DEBUG && warn "[$$] Initializing the PSGI app\n";
$self->{app} = $self->{options}->{psgi_app_builder}->();
}
+ srand();
$0 = "starman worker " . join(" ", @{$self->{options}{argv} || []});
}
View
5 t/rand.psgi
@@ -0,0 +1,5 @@
+rand(); # this initializes the random seed
+
+sub {
+ return [ 200, ["Content-Type", "text/plain"], [ rand(100) ] ];
+};
View
22 t/rand.t
@@ -0,0 +1,22 @@
+use Test::TCP;
+use LWP::UserAgent;
+use FindBin;
+use Test::More;
+
+my $s = Test::TCP->new(
+ code => sub {
+ my $port = shift;
+ exec "$^X bin/starman --preload-app --port $port --max-requests=1 --workers=1 $FindBin::Bin/rand.psgi";
+ },
+);
+
+my $ua = LWP::UserAgent->new;
+
+my @res;
+for (1..2) {
+ push @res, $ua->get("http://localhost:" . $s->port);
+}
+
+isnt $res[0]->content, $res[1]->content;
+
+done_testing;

0 comments on commit 61a8233

Please sign in to comment.
Something went wrong with that request. Please try again.