Created
April 12, 2010 13:58
-
-
Save clkao/363580 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
commit 25079da736b4c9d9389af5d2a367cc435456520f | |
Author: Chia-liang Kao <[email protected]> | |
Date: Mon Apr 12 21:56:27 2010 +0800 | |
Delayed loader. | |
diff --git a/lib/Plack/Loader/Delayed.pm b/lib/Plack/Loader/Delayed.pm | |
new file mode 100644 | |
index 0000000..8ab1a94 | |
--- /dev/null | |
+++ b/lib/Plack/Loader/Delayed.pm | |
@@ -0,0 +1,46 @@ | |
+package Plack::Loader::Delayed; | |
+use strict; | |
+use parent qw(Plack::Loader); | |
+ | |
+sub preload_app { | |
+ my($self, $builder) = @_; | |
+ $self->{builder} = $builder; | |
+} | |
+ | |
+sub run { | |
+ my($self, $server) = @_; | |
+ | |
+ my $compiled; | |
+ my $app = sub { | |
+ $compiled ||= $self->{builder}->(); | |
+ $compiled->(@_); | |
+ }; | |
+ | |
+ $server->{psgi_app_builder} = $self->{builder}; | |
+ $server->run($app); | |
+} | |
+ | |
+1; | |
+ | |
+__END__ | |
+ | |
+=head1 NAME | |
+ | |
+Plack::Loader::Delayed - Delay the loading of .psgi until the first run | |
+ | |
+=head1 SYNOPSIS | |
+ | |
+ starman -L Delayed | |
+ | |
+=head1 DESCRIPTIOM | |
+ | |
+=head1 AUTHOR | |
+ | |
+Tatsuhiko Miyagawa | |
+ | |
+=head1 SEE ALSO | |
+ | |
+L<plackup> | |
+ | |
+=cut | |
+ | |
diff --git a/t/Plack-Loader/delayed.t b/t/Plack-Loader/delayed.t | |
new file mode 100644 | |
index 0000000..dfc367d | |
--- /dev/null | |
+++ b/t/Plack-Loader/delayed.t | |
@@ -0,0 +1,31 @@ | |
+use strict; | |
+use Test::More; | |
+use Plack::Loader; | |
+ | |
+my $compiled; | |
+ | |
+my $builder = sub { | |
+ $compiled = 1; | |
+ my $app = sub { | |
+ return [ 200, [], [ "Hi" ] ]; | |
+ }; | |
+}; | |
+ | |
+$INC{"Plack/Handler/Twiggy.pm"} = __FILE__; | |
+sub Plack::Handler::Twiggy::new { bless {}, shift } | |
+ | |
+# The following eval might not fail if you set PLACK_SEVER | |
+delete $ENV{PLACK_SERVER}; | |
+ | |
+eval { | |
+ my $loader = Plack::Loader::Delayed->new; | |
+ $loader->preload_app($builder); | |
+ my $server = $loader->auto; | |
+ ok(!$compiled); | |
+}; | |
+ | |
+ok 1 if $@; | |
+ | |
+done_testing; | |
+ | |
+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment