-
-
Save Akron/4c75eb00f104853cbe4f to your computer and use it in GitHub Desktop.
Mojo::IOLoop dies somehow ...
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
package Mojolicious::Plugin::Random; | |
use Mojo::Base 'Mojolicious::Plugin'; | |
sub register { | |
my ($plugin, $mojo) = @_; | |
# Reseed on fork | |
Mojo::IOLoop->timer( | |
0 => sub { srand } | |
); | |
# Establish 'random' helper | |
$mojo->helper( | |
random => sub { | |
my $c = shift; | |
# Start Loop unless it is running | |
Mojo::IOLoop->start unless Mojo::IOLoop->is_running; | |
# Return random value | |
return int(rand(50)); | |
} | |
); | |
}; | |
1; |
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 Mojo::Base -strict; | |
use Mojolicious::Lite; | |
use Test::Mojo; | |
use Test::More; | |
my $t = Test::Mojo->new; | |
my $app = $t->app; | |
# Load Random string plugin | |
plugin 'Random'; | |
# Establish route | |
get '/test' => sub { shift->render(text => 'yeah') }; | |
# Get random value - fine | |
ok($app->random, 'Random value 1'); | |
# Get route - fine | |
$t->get_ok('/test')->content_is('yeah'); | |
# Get random value - never reached | |
ok($app->random, 'Random value 2'); | |
done_testing(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment