Skip to content

Commit

Permalink
more relaxed Mojo::Reactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 23, 2012
1 parent 5c922b9 commit b6b51d3
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 126 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -3,6 +3,7 @@ This file documents the revision history for Perl extension Mojolicious.
2.66 2012-03-23
- Reformatted "Changes".
- Improved Mojo::Reactor::Poll performance.
- Improved tests.
- Fixed one_tick semantics to be equal in Mojo::Reactor::Poll and
Mojo::Reactor::EV.

Expand Down
3 changes: 3 additions & 0 deletions lib/Mojo/Reactor.pm
Expand Up @@ -169,6 +169,9 @@ Stop watching for I/O and timer events.
Create a new timer, invoking the callback after a given amount of time in
seconds.
# Invoke as soon as possible
$reactor->timer(0 => sub { say 'First tick.' });
=head2 C<watch>
$reactor = $reactor->watch($handle, $readable, $writable);
Expand Down
139 changes: 76 additions & 63 deletions t/mojo/reactor_ev.t
Expand Up @@ -38,10 +38,10 @@ my $listen = IO::Socket::INET->new(
my ($readable, $writable);
$reactor->io($listen => sub { pop() ? $writable++ : $readable++ })
->watch($listen, 0, 0)->watch($listen, 1, 1);
$reactor->timer(0 => sub { shift->stop });
$reactor->timer(0.025 => sub { shift->stop });
$reactor->start;
is $readable, undef, 'handle is not readable';
is $writable, undef, 'handle is not writable';
ok !$readable, 'handle is not readable';
ok !$writable, 'handle is not writable';
ok !$reactor->is_readable($listen), 'handle is not readable';

# Connect
Expand All @@ -58,86 +58,96 @@ my $server = $listen->accept;
$reactor->remove($listen);
($readable, $writable) = undef;
$reactor->io($client => sub { pop() ? $writable++ : $readable++ });
$reactor->timer(0 => sub { shift->stop });
$reactor->timer(0.025 => sub { shift->stop });
$reactor->start;
is $readable, undef, 'handle is not readable';
is $writable, 1, 'handle is writable';
ok !$readable, 'handle is not readable';
ok $writable, 'handle is writable';
print $client "hello!\n";
sleep 1;
$reactor->remove($client);
($readable, $writable) = undef;
$reactor->io($server => sub { pop() ? $writable++ : $readable++ });
$reactor->watch($server, 1, 0);
$reactor->timer(0 => sub { shift->stop });
$reactor->timer(0.025 => sub { shift->stop });
$reactor->start;
is $readable, 1, 'handle is readable';
is $writable, undef, 'handle is not writable';
ok $readable, 'handle is readable';
ok !$writable, 'handle is not writable';
($readable, $writable) = undef;
$reactor->watch($server, 1, 1);
$reactor->timer(0 => sub { shift->stop });
$reactor->timer(0.025 => sub { shift->stop });
$reactor->start;
is $readable, 2, 'handle is readable';
is $writable, 1, 'handle is writable';
ok $readable, 'handle is readable';
ok $writable, 'handle is writable';
($readable, $writable) = undef;
$reactor->watch($server, 0, 0);
$reactor->timer(0 => sub { shift->stop });
$reactor->timer(0.025 => sub { shift->stop });
$reactor->start;
is $readable, 2, 'handle is not readable';
is $writable, 1, 'handle is not writable';
ok !$readable, 'handle is not readable';
ok !$writable, 'handle is not writable';
($readable, $writable) = undef;
$reactor->watch($server, 1, 0);
$reactor->timer(0 => sub { shift->stop });
$reactor->timer(0.025 => sub { shift->stop });
$reactor->start;
is $readable, 3, 'handle is readable';
is $writable, 1, 'handle is not writable';
ok $readable, 'handle is readable';
ok !$writable, 'handle is not writable';
($readable, $writable) = undef;
$reactor->io($server => sub { pop() ? $writable++ : $readable++ });
$reactor->timer(0 => sub { shift->stop });
$reactor->timer(0.025 => sub { shift->stop });
$reactor->start;
is $readable, 1, 'handle is readable';
is $writable, 1, 'handle is writable';
ok $readable, 'handle is readable';
ok $writable, 'handle is writable';

# Timers
my ($timer, $recurring);
$reactor->timer(0 => sub { $timer++ });
$reactor->remove($reactor->timer(0 => sub { $timer++ }));
my $id = $reactor->recurring(0 => sub { $recurring++ });
$reactor->one_tick;
is $readable, 2, 'handle is readable again';
is $writable, 2, 'handle is writable again';
is $timer, 1, 'timer was triggered';
is $recurring, 1, 'recurring was triggered';
($readable, $writable) = undef;
$reactor->timer(0.025 => sub { shift->stop });
$reactor->start;
ok $readable, 'handle is readable again';
ok $writable, 'handle is writable again';
ok $timer, 'timer was triggered';
ok $recurring, 'recurring was triggered';
my $done = 0;
$reactor->timer(0 => sub { $done = shift->is_running });
($readable, $writable, $timer, $recurring) = undef;
$reactor->timer(0.025 => sub { $done = shift->is_running });
$reactor->one_tick while !$done;
is $readable, 3, 'handle is readable again';
is $writable, 3, 'handle is writable again';
is $timer, 1, 'timer was not triggered';
is $recurring, 2, 'recurring was triggered again';
$reactor->timer(0 => sub { shift->stop });
$reactor->start;
is $readable, 4, 'handle is readable again';
is $writable, 4, 'handle is writable again';
is $timer, 1, 'timer was not triggered';
is $recurring, 3, 'recurring was not triggered';
$reactor->timer(0 => sub { shift->stop });
$reactor->start;
is $readable, 5, 'handle is readable again';
is $writable, 5, 'handle is writable again';
is $timer, 1, 'timer was not triggered';
is $recurring, 4, 'recurring was triggered again';
ok $readable, 'handle is readable again';
ok $writable, 'handle is writable again';
ok !$timer, 'timer was not triggered';
ok $recurring, 'recurring was triggered again';
($readable, $writable, $timer, $recurring) = undef;
$reactor->timer(0.025 => sub { shift->stop });
$reactor->start;
ok $readable, 'handle is readable again';
ok $writable, 'handle is writable again';
ok !$timer, 'timer was not triggered';
ok $recurring, 'recurring was triggered again';
($readable, $writable, $timer, $recurring) = undef;
$reactor->timer(0.025 => sub { shift->stop });
$reactor->start;
ok $readable, 'handle is readable again';
ok $writable, 'handle is writable again';
ok !$timer, 'timer was not triggered';
ok $recurring, 'recurring was triggered again';
$reactor->remove($id);
$reactor->timer(0 => sub { shift->stop });
($readable, $writable, $timer, $recurring) = undef;
$reactor->timer(0.025 => sub { shift->stop });
$reactor->start;
is $readable, 6, 'handle is readable again';
is $writable, 6, 'handle is writable again';
is $timer, 1, 'timer was not triggered';
is $recurring, 4, 'recurring was not triggered again';
ok $readable, 'handle is readable again';
ok $writable, 'handle is writable again';
ok !$timer, 'timer was not triggered';
ok !$recurring, 'recurring was not triggered again';

# Reset
$reactor->remove($id);
$reactor->remove($server);
$reactor->timer(0 => sub { shift->stop });
($readable, $writable) = undef;
$reactor->timer(0.025 => sub { shift->stop });
$reactor->start;
is $readable, 6, 'io event was not triggered again';
is $writable, 6, 'io event was not triggered again';
ok !$readable, 'io event was not triggered again';
ok !$writable, 'io event was not triggered again';
my $reactor2 = Mojo::Reactor::EV->new;
is ref $reactor2, 'Mojo::Reactor::Poll', 'right object';

Expand All @@ -146,22 +156,25 @@ $timer = 0;
$reactor->recurring(0 => sub { $timer++ });
my $timer2 = 0;
$reactor2->recurring(0 => sub { $timer2++ });
$reactor->timer(0 => sub { shift->stop });
$reactor->timer(0.025 => sub { shift->stop });
$reactor->start;
is $timer, 1, 'timer was triggered';
is $timer2, 0, 'timer was not triggered';
$reactor2->timer(0 => sub { shift->stop });
ok $timer, 'timer was triggered';
ok !$timer2, 'timer was not triggered';
($timer, $timer2) = 0;
$reactor2->timer(0.025 => sub { shift->stop });
$reactor2->start;
is $timer, 1, 'timer was not triggered';
is $timer2, 1, 'timer was triggered';
$reactor->timer(0 => sub { shift->stop });
ok !$timer, 'timer was not triggered';
ok $timer2, 'timer was triggered';
($timer, $timer2) = 0;
$reactor->timer(0.025 => sub { shift->stop });
$reactor->start;
is $timer, 2, 'timer was triggered';
is $timer2, 1, 'timer was not triggered';
$reactor2->timer(0 => sub { shift->stop });
ok $timer, 'timer was triggered';
ok !$timer2, 'timer was not triggered';
($timer, $timer2) = 0;
$reactor2->timer(0.025 => sub { shift->stop });
$reactor2->start;
is $timer, 2, 'timer was not triggered';
is $timer2, 2, 'timer was triggered';
ok !$timer, 'timer was not triggered';
ok $timer2, 'timer was triggered';

# Error
my $err;
Expand Down

0 comments on commit b6b51d3

Please sign in to comment.