Skip to content

Instantly share code, notes, and snippets.

@Akron
Created January 24, 2014 19:55
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 Akron/4c75eb00f104853cbe4f to your computer and use it in GitHub Desktop.
Save Akron/4c75eb00f104853cbe4f to your computer and use it in GitHub Desktop.
Mojo::IOLoop dies somehow ...
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;
#!/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