[Dancer-users] Problems mounting multiple webapps

Takeshi OKURA okura3 at gmail.com
Tue Jun 14 08:57:22 CEST 2011


Hmm...

2011/6/14 Alex Kalderimis <alex at flymine.org>:

> I would try adding an appname to these codereferences:
>
>  set appname => 'foo'; # Or similar
>
> If I recall correctly, the current app is stored by name, and with
> these both being unnamed they will clobber each other. I have multiple
> webapps working happily on the same Starman server, and they do not
> interfere with each other.

I tried adding an appname and test it.
But mounting multiple webapps does not work.

This is my test code named test_multi_webapp.pl.
-----------------------------------------------------------------------------------------
use WWW::Mechanize;
use Test::More;

my $starman_pid     = "__starman.pid";
my $starman_workers = 2;
my $repeat_count    = 10;
my $app1_url        = "http://127.0.0.1:3000/app1";
my $app2_url        = "http://127.0.0.1:3000/app2";

unless ( my $pid = fork() ) {
  system( "plackup --pid $starman_pid --port 3000 -s Starman "
        . "--workers $starman_workers -a multi_webapp.psgi "
        . ">/dev/null 2>&1" );
  exit;
}
sleep 3;    # wait until starman up

my $mech = WWW::Mechanize->new();
$mech->get($app1_url);    # first access is /app1
like( $mech->content(), qr/App1/, "Hello App1" );

foreach my $i ( 1 .. $repeat_count ) {
  $mech->get($app2_url);    # continue access is /app2
  like( $mech->content(), qr/App2/, "Hello App2" );
}

done_testing;

kill 1, `cat $starman_pid`;
unlink $starman_pid;
wait;
exit;
-----------------------------------------------------------------------------------------

And This is a webapps code named multi_webapps.psgi.
-----------------------------------------------------------------------------------------
use Dancer ':syntax';
use Plack::Builder;

my $app1 = sub {
  my $env = shift;
  set appname => 'App1';
  get '/' => sub { return "Hello App1"; };
  my $request = Dancer::Request->new($env);
  Dancer->dance($request);
};

my $app2 = sub {
  my $env = shift;
  set appname => 'App2';
  get '/' => sub { return "Hello App2"; };
  my $request = Dancer::Request->new($env);
  Dancer->dance($request);
};

builder {
  mount "/app1" => builder {$app1};
  mount "/app2" => builder {$app2};
};
-----------------------------------------------------------------------------------------

My runtime environment is  perl 5.8.9, Dancer 1.3050 and Plack 0.998.
I execute a above test code with Starman's 2 workers and result is
below(About half access was fail).

$ perl test_multi_webapp.pl
ok 1 - Hello App1
ok 2 - Hello App2
not ok 3 - Hello App2
(snip)
ok 4 - Hello App2
not ok 5 - Hello App2
(snip)
ok 6 - Hello App2
not ok 7 - Hello App2
(snip)
ok 8 - Hello App2
not ok 9 - Hello App2
(snip)
ok 10 - Hello App2
not ok 11 - Hello App2
(snip)
1..11
# Looks like you failed 5 tests of 11.

What's wrong my webapps code ?

Sincerely Yours.
-- 
Takeshi OKURA


More information about the Dancer-users mailing list