Skip to content

Commit

Permalink
improved Mojo::Reactor and Mojo::IOLoop to pass timer id as argument …
Browse files Browse the repository at this point in the history
…to recurring timers
  • Loading branch information
kraih committed Mar 21, 2012
1 parent 2cd1771 commit ee250e7
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -3,6 +3,8 @@ This file documents the revision history for Perl extension Mojolicious.
2.64 2012-03-21 00:00:00
- Deprecated Mojolicious::Routes->controller_base_class in favor of
Mojolicious::Routes->base_classes.
- Improved Mojo::Reactor and Mojo::IOLoop to pass timer id as
argument to recurring timers.
- Improved documentation.

2.63 2012-03-20 00:00:00
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/IOLoop.pm
Expand Up @@ -110,7 +110,7 @@ sub recurring {
my ($self, $after, $cb) = @_;
$self = $self->singleton unless ref $self;
weaken $self;
return $self->reactor->recurring($after => sub { $self->$cb });
return $self->reactor->recurring($after => sub { shift; $self->$cb(@_) });
}

# "Fat Tony is a cancer on this fair city!
Expand Down
10 changes: 9 additions & 1 deletion lib/Mojo/Reactor.pm
Expand Up @@ -109,7 +109,7 @@ sub _one_tick {
elsif ($after && $t->{recurring}) { $t->{recurring} += $after }

# Handle timer
if (my $cb = $t->{cb}) { $self->_sandbox("Timer $id", $cb) }
if (my $cb = $t->{cb}) { $self->_sandbox("Timer $id", $cb, $id) }
}
}

Expand Down Expand Up @@ -232,6 +232,14 @@ Check if reactor is running.
Create a new recurring timer, invoking the callback repeatedly after a given
amount of time in seconds.
# Invoke callback 3 times
my $i = 1;
$reactor->recurring(0.5 => sub {
my ($reactor, $id) = @_;
say $i++;
$reactor->drop($id) if $i == 3;
});
=head2 C<start>
$reactor->start;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Reactor/EV.pm
Expand Up @@ -63,7 +63,7 @@ sub _timer {
$recurring ? $after : 0,
sub {
my $w = shift;
$self->_sandbox("Timer $id", $self->{timers}->{$id}->{cb});
$self->_sandbox("Timer $id", $self->{timers}->{$id}->{cb}, $id);
delete $self->{timers}->{$id} unless $recurring;
}
);
Expand Down
13 changes: 9 additions & 4 deletions t/mojo/ioloop.t
Expand Up @@ -86,13 +86,18 @@ ok $ticks > 2, 'more than two ticks';
# Run again without first tick event handler
my $before = $ticks;
my $after = 0;
my $id2 = $loop->recurring(0 => sub { $after++ });
$loop->recurring(
0 => sub {
my ($loop, $id) = @_;
$after++;
$loop->drop($id) if $after == 2;
}
);
$loop->drop($id);
$loop->timer(1 => sub { shift->stop });
$loop->start;
$loop->one_tick;
$loop->drop($id2);
ok $after > 1, 'more than one tick';
is $after, 2, 'exactly two tick';
is $ticks, $before, 'no additional ticks';

# Recurring timer
Expand All @@ -117,7 +122,7 @@ $id = $loop->server(
$loop->stop;
}
);
$id2 = $loop->client((address => 'localhost', port => $port) => sub { });
my $id2 = $loop->client((address => 'localhost', port => $port) => sub { });
$loop->start;
$loop->drop($id);
$loop->drop($id2);
Expand Down
18 changes: 16 additions & 2 deletions t/mojo/reactor.t
Expand Up @@ -6,7 +6,7 @@ BEGIN {
$ENV{MOJO_REACTOR} = 'Mojo::Reactor';
}

use Test::More tests => 66;
use Test::More tests => 70;

# "I don't mind being called a liar when I'm lying, or about to lie,
# or just finished lying, but NOT WHEN I'M TELLING THE TRUTH."
Expand Down Expand Up @@ -144,7 +144,13 @@ is ref $reactor2, 'Mojo::Reactor', 'right object';
$timer = 0;
$reactor->recurring(0 => sub { $timer++ });
my $timer2 = 0;
$reactor2->recurring(0 => sub { $timer2++ });
$reactor2->recurring(
0 => sub {
my ($reactor2, $id) = @_;
$timer2++;
$reactor2->drop($id) if $timer2 == 2;
}
);
$reactor->timer(0 => sub { shift->stop });
$reactor->start;
is $timer, 1, 'timer was triggered';
Expand All @@ -161,6 +167,14 @@ $reactor2->timer(0 => sub { shift->stop });
$reactor2->start;
is $timer, 2, 'timer was not triggered';
is $timer2, 2, 'timer was triggered';
$reactor2->timer(0 => sub { shift->stop });
$reactor2->start;
is $timer, 2, 'timer was not triggered';
is $timer2, 2, 'timer was triggered';
$reactor->timer(0 => sub { shift->stop });
$reactor->start;
is $timer, 3, 'timer was triggered';
is $timer2, 2, 'timer was not triggered';

# Error
my $err;
Expand Down
18 changes: 16 additions & 2 deletions t/mojo/reactor_ev.t
Expand Up @@ -8,7 +8,7 @@ use Test::More;
plan skip_all => 'set TEST_EV to enable this test (developer only!)'
unless $ENV{TEST_EV};
plan skip_all => 'EV 4.0 required for this test!' unless eval 'use EV 4.0; 1';
plan tests => 67;
plan tests => 71;

# "Oh well. At least we'll die doing what we love: inhaling molten rock."
use IO::Socket::INET;
Expand Down Expand Up @@ -145,7 +145,13 @@ is ref $reactor2, 'Mojo::Reactor', 'right object';
$timer = 0;
$reactor->recurring(0 => sub { $timer++ });
my $timer2 = 0;
$reactor2->recurring(0 => sub { $timer2++ });
$reactor2->recurring(
0 => sub {
my ($reactor2, $id) = @_;
$timer2++;
$reactor2->drop($id) if $timer2 == 2;
}
);
$reactor->timer(0 => sub { shift->stop });
$reactor->start;
is $timer, 1, 'timer was triggered';
Expand All @@ -162,6 +168,14 @@ $reactor2->timer(0 => sub { shift->stop });
$reactor2->start;
is $timer, 2, 'timer was not triggered';
is $timer2, 2, 'timer was triggered';
$reactor2->timer(0 => sub { shift->stop });
$reactor2->start;
is $timer, 2, 'timer was not triggered';
is $timer2, 2, 'timer was triggered';
$reactor->timer(0 => sub { shift->stop });
$reactor->start;
is $timer, 3, 'timer was triggered';
is $timer2, 2, 'timer was not triggered';

# Error
my $err;
Expand Down

0 comments on commit ee250e7

Please sign in to comment.