Skip to content

Instantly share code, notes, and snippets.

@ian-kent
Created July 13, 2013 22:03
Show Gist options
  • Save ian-kent/5992399 to your computer and use it in GitHub Desktop.
Save ian-kent/5992399 to your computer and use it in GitHub Desktop.
use Mojolicious::Lite;
use POSIX qw( strftime );
use Test::Mojo;
use Data::Dumper;
use Test::More;
hook after_build_tx => sub {
# print "AFTER BUILD TX\n";
};
get '/' => sub {
my $self = shift;
# print "START\n";
Mojo::IOLoop->timer(5 => sub {
$self->render(text => strftime('%H:%M:%S', localtime));
# print "END\n";
});
};
get '/other' => sub {
my $self = shift;
# print "START OTHER\n";
Mojo::IOLoop->timer(5 => sub {
$self->render(text => strftime('%H:%M:%S', localtime));
# print "END OTHER\n";
});
};
#app->start;
#exit;
my $t = Test::Mojo->new;
my $ua = Mojo::UserAgent->new;
my $delay = Mojo::IOLoop->delay(sub {});
my @results = ();
for my $url ('/', '/other') {
my $end = $delay->begin(0);
$ua->get($url => sub {
my ($ua, $tx) = @_;
push @results, $tx->res->content->asset->{content};
$end->();
});
}
$delay->wait;
#print Dumper \@results;
is $results[0], $results[1], 'Calling different routes with different URLs';
# Both using the same route and same URL, first request blocks the second
@results = ();
for my $url ('/', '/') {
my $end = $delay->begin(0);
$ua->get($url => sub {
my ($ua, $tx) = @_;
push @results, $tx->res->content->asset->{content};
$end->();
});
}
$delay->wait;
#print Dumper \@results;
is $results[0], $results[1], 'Calling same route and same URL';
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment