Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
make reactor testing a little easier
  • Loading branch information
kraih committed Sep 2, 2017
1 parent 277987e commit 0afde27
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
5 changes: 2 additions & 3 deletions lib/Mojo/Reactor.pm
Expand Up @@ -5,15 +5,14 @@ use Carp 'croak';
use Config;
use Mojo::Loader 'load_class';

my $DETECTED;
my %DETECTED;

sub again { croak 'Method "again" not implemented by subclass' }

sub detect {
my $default = 'Mojo::Reactor::' . ($Config{d_pseudofork} ? 'Poll' : 'EV');
my $try = $ENV{MOJO_REACTOR} || $default;
return $DETECTED if $DETECTED;
return $DETECTED = load_class($try) ? 'Mojo::Reactor::Poll' : $try;
return $DETECTED{$try} ||= load_class($try) ? 'Mojo::Reactor::Poll' : $try;
}

sub io { croak 'Method "io" not implemented by subclass' }
Expand Down
17 changes: 14 additions & 3 deletions t/mojo/reactor_detect.t
@@ -1,5 +1,7 @@
use Mojo::Base -strict;

BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }

use Test::More;
use Mojo::Reactor::Poll;

Expand All @@ -9,14 +11,23 @@ use Mojo::Base 'Mojo::Reactor::Poll';

package main;

# Detection (env)
# Detection (success)
{
local $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Test';
is(Mojo::Reactor->detect, 'Mojo::Reactor::Test', 'right class');
}

# Detection (fail)
{
local $ENV{MOJO_REACTOR} = 'Mojo::Reactor::DoesNotExist';
is(Mojo::Reactor->detect, 'Mojo::Reactor::Poll', 'right class');
}

# Event loop detection
require Mojo::IOLoop;
is ref Mojo::IOLoop->new->reactor, 'Mojo::Reactor::Test', 'right class';
{
local $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Test';
require Mojo::IOLoop;
is ref Mojo::IOLoop->new->reactor, 'Mojo::Reactor::Test', 'right class';
}

done_testing();

0 comments on commit 0afde27

Please sign in to comment.