Skip to content

Instantly share code, notes, and snippets.

@KES777
Last active November 3, 2017 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KES777/1f5fafe6b4c61091e18d4d4b8e90cd2b to your computer and use it in GitHub Desktop.
Save KES777/1f5fafe6b4c61091e18d4d4b8e90cd2b to your computer and use it in GitHub Desktop.
use Mojolicious::Lite;
use Test::More;
use Test::Mojo;
my $r = app->routes;
$r->any('/l1')->detour( 'Lite' );
$r->any('/l2')->detour( 'Lite' );
$r->any('/f1')->detour( 'Full' );
$r->any('/f2')->detour( 'Full' );
my $t = Test::Mojo->new;
# Mojolicous reuse lite application instance
my $addr1 = $t->get_ok('/l1/addr')->tx->res->body;
my $addr2 = $t->get_ok('/l2/addr')->tx->res->body;
ok $addr1 ne $addr2, "Different instances";
# But create two instances when application is full
$addr1 = $t->get_ok('/f1/addr')->tx->res->body;
$addr2 = $t->get_ok('/f2/addr')->tx->res->body;
ok $addr1 ne $addr2, "Different instances";
done_testing;
# The sub which returns application address
sub get_app_addr {
my $c = shift;
$c->render( text => $c->app."" );
}
# The Lite application
package Lite;
use Mojolicious::Lite;
get '/addr' => \&main::get_app_addr;
__END__
# The Full application
package Full;
use Mojo::Base 'Mojolicious';
sub startup { shift->routes->get( '/addr', \&main::get_app_addr ) };
$ perl -Ilib t/mojolicious/lite_vs_full.t
ok 1 - GET /l1/addr
ok 2 - GET /l2/addr
ok 3 - Different instances
ok 4 - GET /f1/addr
ok 5 - GET /f2/addr
ok 6 - Different instances
1..6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment