Skip to content

Commit

Permalink
don't declarel $reactor globally
Browse files Browse the repository at this point in the history
  • Loading branch information
misch2 committed Aug 18, 2022
1 parent e67753f commit 6060e16
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions t/mojo/reactor_ev.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ use Mojo::Util qw(steady_time);


subtest 'With reactor' => sub {
my $reactor;

subtest 'Instantiation' => sub {
use_ok 'Mojo::Reactor::EV';
$reactor = Mojo::Reactor::EV->new;

my $reactor = Mojo::Reactor::EV->new;
is ref $reactor, 'Mojo::Reactor::EV', 'right object';
is ref Mojo::Reactor::EV->new, 'Mojo::Reactor::Poll', 'right object';

Expand All @@ -27,6 +26,8 @@ subtest 'With reactor' => sub {
};

subtest 'Make sure it stops automatically when not watching for events' => sub {
my $reactor = Mojo::IOLoop->singleton->reactor;

my $triggered;
Mojo::IOLoop->next_tick(sub { $triggered++ });
Mojo::IOLoop->start;
Expand All @@ -42,6 +43,7 @@ subtest 'With reactor' => sub {
my $listen = IO::Socket::INET->new(Listen => 5, LocalAddr => '127.0.0.1');

subtest 'Listen' => sub {
my $reactor = Mojo::IOLoop->singleton->reactor;
my ($readable, $writable);
$reactor->io($listen => sub { pop() ? $writable++ : $readable++ })->watch($listen, 0, 0)->watch($listen, 1, 1);
$reactor->timer(0.025 => sub { shift->stop });
Expand All @@ -56,6 +58,7 @@ subtest 'With reactor' => sub {

subtest 'Connect' => sub {
my ($readable, $writable);
my $reactor = Mojo::IOLoop->singleton->reactor;
$reactor->io($listen => sub { pop() ? $writable++ : $readable++ })->watch($listen, 0, 0)->watch($listen, 1, 1);
$reactor->timer(1 => sub { shift->stop });
$reactor->start;
Expand All @@ -64,12 +67,13 @@ subtest 'With reactor' => sub {
};

subtest 'With server' => sub {
my $reactor = Mojo::IOLoop->singleton->reactor;
ok $reactor->remove($listen), 'removed';
ok !$reactor->remove($listen), 'not removed again';
my $server = $listen->accept;

subtest 'Accept' => sub {

my $reactor = Mojo::IOLoop->singleton->reactor;
my ($readable, $writable);
$reactor->io($client => sub { pop() ? $writable++ : $readable++ });
$reactor->again($reactor->timer(0.025 => sub { shift->stop }));
Expand Down Expand Up @@ -119,6 +123,7 @@ subtest 'With reactor' => sub {
};

subtest 'Timers' => sub {
my $reactor = Mojo::IOLoop->singleton->reactor;
my ($timer, $recurring);
$reactor->timer(0 => sub { $timer++ });
ok $reactor->remove($reactor->timer(0 => sub { $timer++ })), 'removed';
Expand Down Expand Up @@ -175,6 +180,7 @@ subtest 'With reactor' => sub {
};

subtest 'Reset' => sub {
my $reactor = Mojo::IOLoop->singleton->reactor;
$reactor->next_tick(sub { die 'Reset failed' });
$reactor->reset;
my ($readable, $writable, $recurring);
Expand All @@ -189,6 +195,7 @@ subtest 'With reactor' => sub {
};

subtest 'Ordered next_tick' => sub {
my $reactor = Mojo::IOLoop->singleton->reactor;
my $result = [];
for my $i (1 .. 10) {
$reactor->next_tick(sub { push @$result, $i });
Expand All @@ -198,13 +205,15 @@ subtest 'With reactor' => sub {
};

subtest 'Reset while watchers are active' => sub {
my $reactor = Mojo::IOLoop->singleton->reactor;
my ($writable);
$reactor->io($_ => sub { ++$writable and shift->reset })->watch($_, 0, 1) for $client, $server;
$reactor->start;
is $writable, 1, 'only one handle was writable';
};

subtest 'Concurrent reactors' => sub {
my $reactor = Mojo::IOLoop->singleton->reactor;
my $timer = 0;
$reactor->recurring(0 => sub { $timer++ });
my $timer2;
Expand Down Expand Up @@ -237,6 +246,7 @@ subtest 'With reactor' => sub {
};

subtest 'Restart timer' => sub {
my $reactor = Mojo::IOLoop->singleton->reactor;
my ($single, $pair, $one, $two, $last);
$reactor->timer(0.025 => sub { $single++ });
$one = $reactor->timer(
Expand All @@ -260,6 +270,7 @@ subtest 'With reactor' => sub {
};

subtest 'Reset timer' => sub {
my $reactor = Mojo::IOLoop->singleton->reactor;
my $before = steady_time;
my ($after, $again);
my ($one, $two);
Expand All @@ -284,6 +295,7 @@ subtest 'With reactor' => sub {
};

subtest 'Restart inactive timer' => sub {
my $reactor = Mojo::IOLoop->singleton->reactor;
my $id = $reactor->timer(0 => sub { });
ok $reactor->remove($id), 'removed';

Expand All @@ -292,13 +304,15 @@ subtest 'With reactor' => sub {
};

subtest 'Change inactive I/O watcher' => sub {
my $reactor = Mojo::IOLoop->singleton->reactor;
ok !$reactor->remove($listen), 'not removed again';

eval { $reactor->watch($listen, 1, 1) };
like $@, qr!I/O watcher not active!, 'right error';
};

subtest 'Error' => sub {
my $reactor = Mojo::IOLoop->singleton->reactor;
my $err;
$reactor->unsubscribe('error')->on(
error => sub {
Expand All @@ -312,6 +326,7 @@ subtest 'With reactor' => sub {
};

subtest 'Reset events' => sub {
my $reactor = Mojo::IOLoop->singleton->reactor;
$reactor->on(error => sub { });
ok $reactor->has_subscribers('error'), 'has subscribers';

Expand All @@ -320,6 +335,7 @@ subtest 'With reactor' => sub {
};

subtest 'Recursion' => sub {
my $reactor = Mojo::IOLoop->singleton->reactor;
my $timer;
$reactor = $reactor->new;
$reactor->timer(0 => sub { ++$timer and shift->one_tick });
Expand Down

0 comments on commit 6060e16

Please sign in to comment.