-
-
Save Akron/545b7263569b33093611 to your computer and use it in GitHub Desktop.
Confusion regarding Mojo::IOLoop - how to stop processing after non-blocking test?
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
#!/usr/bin/env perl | |
use Test::More; | |
use Test::Mojo; | |
use Mojolicious::Lite; | |
use Mojo::UserAgent; | |
$|++; | |
my $t = Test::Mojo->new; | |
my $app = $t->app; | |
my $c = Mojolicious::Controller->new; | |
# Simple route | |
get '/' => sub { | |
shift->render(text => 'Hello World'); | |
}; | |
# Helper with an async request inside | |
app->helper( | |
get_doc => sub { | |
my ($c, $path, $cb) = @_; | |
my $ua = Mojo::UserAgent->new; | |
my $delay = Mojo::IOLoop->delay( | |
sub { | |
my $delay = shift; | |
return $cb->(shift->success->body); | |
}); | |
$ua->get($path => $delay->begin); | |
# Wait if no IOLoop is running | |
$delay->wait unless Mojo::IOLoop->is_running; | |
return; | |
} | |
); | |
# Use async helper - works fine | |
app->get_doc( | |
'https://www.google.de/' => sub { | |
my $doc = shift; | |
ok($doc, 'This is an XRD 1'); | |
diag $doc; | |
}); | |
# Get Resource - works fine | |
$t->get_ok('/'); | |
# Use async helper again - works kind of fine | |
app->get_doc( | |
'https://e14n.com/.well-known/host-meta' => sub { | |
my $doc = shift; | |
ok($doc, 'This is an XRD 2'); | |
diag $doc; | |
}); | |
# Get resource - Not reached ever | |
$t->get_ok('/'); | |
done_testing; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment