Skip to content

Commit

Permalink
slightly better timer example
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 21, 2012
1 parent 5e3eb82 commit 0c3f2f9
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 43 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,5 +1,8 @@
This file documents the revision history for Perl extension Mojolicious.

2.65 2012-03-21 00:00:00
- Improved documentation.

2.64 2012-03-21 00:00:00
- Deprecated Mojolicious::Routes->controller_base_class in favor of
Mojolicious::Routes->base_classes.
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -102,7 +102,7 @@ sub is_running {
sub one_tick {
my $self = shift;
$self = $self->singleton unless ref $self;
$self->timer(shift // '0.025' => sub { shift->stop });
$self->timer(shift // 0.025 => sub { shift->stop });
$self->start;
}

Expand Down Expand Up @@ -208,7 +208,7 @@ sub timer {
sub _cleaner {
my $self = shift;
$self->{cleaner} ||= $self->recurring(
'0.025' => sub {
0.025 => sub {
my $self = shift;

# Manage connections
Expand Down Expand Up @@ -505,7 +505,7 @@ Check if loop is running.
Mojo::IOLoop->one_tick;
$loop->one_tick;
$loop->one_tick('0.25');
$loop->one_tick(0.25);
$loop->one_tick(0);
Run reactor for roughly one tick and try not to block longer than the given
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/IOLoop/Stream.pm
Expand Up @@ -56,7 +56,7 @@ sub start {
my $reactor = $self->reactor;
weaken $self;
$self->{timer} ||= $reactor->recurring(
'0.025' => sub {
0.025 => sub {
return unless $self && (my $t = $self->timeout);
$self->emit_safe('timeout')->close if (time - ($self->{active})) >= $t;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/Reactor.pm
Expand Up @@ -85,7 +85,7 @@ sub _one_tick {

# I/O
my $poll = $self->_poll;
$poll->poll('0.025');
$poll->poll(0.025);
$self->_sandbox('Read', $self->{io}->{fileno $_}->{cb}, 0)
for $poll->handles(POLLIN | POLLHUP | POLLERR);
$self->_sandbox('Write', $self->{io}->{fileno $_}->{cb}, 1)
Expand Down Expand Up @@ -224,7 +224,7 @@ Check if reactor is running.
=head2 C<recurring>
my $id = $reactor->recurring(3 => sub {...});
my $id = $reactor->recurring(0.25 => sub {...});
Create a new recurring timer, invoking the callback repeatedly after a given
amount of time in seconds.
Expand All @@ -244,7 +244,7 @@ Stop watching for I/O and timer events.
=head2 C<timer>
my $id = $reactor->timer(3 => sub {...});
my $id = $reactor->timer(0.5 => sub {...});
Create a new timer, invoking the callback after a given amount of time in
seconds.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Reactor/EV.pm
Expand Up @@ -112,7 +112,7 @@ Check if reactor is running.
=head2 C<recurring>
my $id = $reactor->recurring(3 => sub {...});
my $id = $reactor->recurring(0.25 => sub {...});
Create a new recurring timer, invoking the callback repeatedly after a given
amount of time in seconds.
Expand All @@ -132,7 +132,7 @@ Stop watching for I/O and timer events.
=head2 C<timer>
my $id = $reactor->timer(3 => sub {...});
my $id = $reactor->timer(0.5 => sub {...});
Create a new timer, invoking the callback after a given amount of time in
seconds.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Server/Hypnotoad.pm
Expand Up @@ -143,7 +143,7 @@ sub _config {
$c->{heartbeat_timeout} ||= 10;
$c->{lock_file} ||= catfile tmpdir, 'hypnotoad.lock';
$c->{lock_file} .= ".$$";
$c->{lock_timeout} ||= '0.5';
$c->{lock_timeout} ||= 0.5;
$c->{pid_file} ||= catfile dirname($ENV{HYPNOTOAD_APP}), 'hypnotoad.pid';
$c->{upgrade_timeout} ||= 60;
$c->{workers} ||= 4;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious.pm
Expand Up @@ -33,7 +33,7 @@ has static => sub { Mojolicious::Static->new };
has types => sub { Mojolicious::Types->new };

our $CODENAME = 'Leaf Fluttering In Wind';
our $VERSION = '2.64';
our $VERSION = '2.65';

# "These old doomsday devices are dangerously unstable.
# I'll rest easier not knowing where they are."
Expand Down
10 changes: 5 additions & 5 deletions t/mojo/ioloop.t
Expand Up @@ -163,7 +163,7 @@ $stream->on(read => sub { $buffer .= pop });
$stream->write('hello');
ok(Mojo::IOLoop->stream($id), 'stream exists');
Mojo::IOLoop->start;
Mojo::IOLoop->timer('0.25' => sub { Mojo::IOLoop->stop });
Mojo::IOLoop->timer(0.25 => sub { Mojo::IOLoop->stop });
Mojo::IOLoop->start;
ok !Mojo::IOLoop->stream($id), 'stream does not exist anymore';
is $buffer, 'acceptedhelloworld', 'right result';
Expand Down Expand Up @@ -211,7 +211,7 @@ $id = Mojo::IOLoop->client(
$loop->drop($id);
}
);
Mojo::IOLoop->timer('0.5' => sub { shift->stop });
Mojo::IOLoop->timer(0.5 => sub { shift->stop });
Mojo::IOLoop->start;
is $server_close, 1, 'server emitted close event once';
is $client_close, 1, 'client emitted close event once';
Expand All @@ -226,16 +226,16 @@ Mojo::IOLoop->server(
read => sub {
my ($stream, $chunk) = @_;
Mojo::IOLoop->timer(
'0.5' => sub {
0.5 => sub {
$server_before = $server;
$stream->stop;
$stream->write('works!');
Mojo::IOLoop->timer(
'0.5' => sub {
0.5 => sub {
$server_after = $server;
$client_after = $client;
$stream->start;
Mojo::IOLoop->timer('0.5' => sub { Mojo::IOLoop->stop });
Mojo::IOLoop->timer(0.5 => sub { Mojo::IOLoop->stop });
}
);
}
Expand Down
4 changes: 2 additions & 2 deletions t/mojo/ioloop_tls.t
Expand Up @@ -83,7 +83,7 @@ $loop->server(
$stream->on(close => sub { $server_close++ });
$stream->on(error => sub { $server_err = pop });
$stream->on(read => sub { $server .= pop });
$stream->timeout('0.5');
$stream->timeout(0.5);
}
);
$loop->client(
Expand Down Expand Up @@ -176,7 +176,7 @@ $loop->server(
$stream->on(close => sub { $server_close++ });
$stream->on(error => sub { $server_err = pop });
$stream->on(read => sub { $server .= pop });
$stream->timeout('0.5');
$stream->timeout(0.5);
}
);
$loop->client(
Expand Down
8 changes: 4 additions & 4 deletions t/mojo/user_agent.t
Expand Up @@ -202,7 +202,7 @@ $ua->once(
$tx->on(
connection => sub {
my ($tx, $connection) = @_;
Mojo::IOLoop->stream($connection)->timeout('0.5');
Mojo::IOLoop->stream($connection)->timeout(0.5);
}
);
}
Expand Down Expand Up @@ -263,7 +263,7 @@ my $drain;
$drain = sub {
my $req = shift;
return $ua->ioloop->timer(
'0.5' => sub {
0.5 => sub {
$req->write_chunk('');
$tx->resume;
$stream
Expand Down Expand Up @@ -319,12 +319,12 @@ $ua->get(
sub {
push @kept_alive, pop->kept_alive;
Mojo::IOLoop->timer(
'0.25' => sub {
0.25 => sub {
$ua->get(
'/',
sub {
push @kept_alive, pop->kept_alive;
Mojo::IOLoop->timer('0.25' => sub { Mojo::IOLoop->stop });
Mojo::IOLoop->timer(0.25 => sub { Mojo::IOLoop->stop });
}
);
}
Expand Down
4 changes: 2 additions & 2 deletions t/mojo/user_agent_online.t
Expand Up @@ -332,12 +332,12 @@ is $tx4->res->code, 200, 'right status';
like $tx2->res->content->asset->slurp, qr/Perl/i, 'right content';

# Connect timeout (non-routable address)
$tx = $ua->connect_timeout('0.5')->get('192.0.2.1');
$tx = $ua->connect_timeout(0.5)->get('192.0.2.1');
ok !$tx->is_finished, 'transaction is not finished';
is $tx->error, 'Connect timeout.', 'right error';
$ua->connect_timeout(3);

# Request timeout (non-routable address)
$tx = $ua->request_timeout('0.5')->get('192.0.2.1');
$tx = $ua->request_timeout(0.5)->get('192.0.2.1');
ok !$tx->is_finished, 'transaction is not finished';
is $tx->error, 'Request timeout.', 'right error';
8 changes: 4 additions & 4 deletions t/mojo/websocket.t
Expand Up @@ -154,7 +154,7 @@ websocket '/deadcallback' => sub {
my $timeout;
websocket '/timeout' => sub {
my $self = shift;
Mojo::IOLoop->stream($self->tx->connection)->timeout('0.5');
Mojo::IOLoop->stream($self->tx->connection)->timeout(0.5);
$self->on(finish => sub { $timeout = 'works!' });
};

Expand Down Expand Up @@ -296,7 +296,7 @@ $ua->websocket(
$tx->on(
finish => sub {
$finished += 4;
$loop->timer('0.5' => sub { shift->stop });
$loop->timer(0.5 => sub { shift->stop });
}
);
}
Expand All @@ -321,7 +321,7 @@ $ua->websocket(
my ($tx, $message) = @_;
$result .= $message;
$tx->finish and $running-- if $message eq 'test1';
$loop->timer('0.5' => sub { $loop->stop }) unless $running;
$loop->timer(0.5 => sub { $loop->stop }) unless $running;
}
);
$tx->on(finish => sub { $finished += 1 });
Expand All @@ -337,7 +337,7 @@ $ua->websocket(
my ($tx, $message) = @_;
$result2 .= $message;
$tx->finish and $running-- if $message eq 'test1';
$loop->timer('0.5' => sub { $loop->stop }) unless $running;
$loop->timer(0.5 => sub { $loop->stop }) unless $running;
}
);
$tx->on(finish => sub { $finished += 2 });
Expand Down
28 changes: 14 additions & 14 deletions t/mojolicious/longpolling_lite_app.t
Expand Up @@ -55,9 +55,9 @@ get '/longpoll' => sub {
$self->res->headers->content_type('text/plain');
$self->write_chunk('hi ');
my $id = Mojo::IOLoop->timer(
'0.5' => sub {
0.5 => sub {
$self->write_chunk('there,', sub { shift->write_chunk(' whats up?') });
shift->timer('0.5' => sub { $self->finish });
shift->timer(0.5 => sub { $self->finish });
}
);
$self->on(
Expand All @@ -77,9 +77,9 @@ get '/longpoll/nolength' => sub {
$self->res->headers->content_type('text/plain');
$self->write('hi ');
Mojo::IOLoop->timer(
'0.5' => sub {
0.5 => sub {
$self->write('there,', sub { shift->write(' what length?') });
shift->timer('0.5' => sub { $self->finish });
shift->timer(0.5 => sub { $self->finish });
}
);
};
Expand Down Expand Up @@ -108,7 +108,7 @@ get '/longpoll/plain' => sub {
$self->res->headers->content_length(25);
$self->write('hi ');
Mojo::IOLoop->timer(
'0.5' => sub {
0.5 => sub {
$self->on(finish => sub { $longpoll_plain = 'finished!' });
$self->write('there plain,', sub { shift->write(' whats up?') });
}
Expand All @@ -124,7 +124,7 @@ get '/longpoll/delayed' => sub {
$self->res->headers->content_type('text/plain');
$self->write_chunk;
Mojo::IOLoop->timer(
'0.5' => sub {
0.5 => sub {
$self->write_chunk(
sub {
my $self = shift;
Expand All @@ -146,7 +146,7 @@ get '/longpoll/plain/delayed' => sub {
$self->res->headers->content_length(12);
$self->write;
Mojo::IOLoop->timer(
'0.5' => sub {
0.5 => sub {
$self->write(
sub {
my $self = shift;
Expand All @@ -167,7 +167,7 @@ get '/longpoll/nolength/delayed' => sub {
$self->res->headers->content_type('text/plain');
$self->write;
Mojo::IOLoop->timer(
'0.5' => sub {
0.5 => sub {
$self->write(
sub {
my $self = shift;
Expand All @@ -184,7 +184,7 @@ my $longpoll_static_delayed;
get '/longpoll/static/delayed' => sub {
my $self = shift;
$self->on(finish => sub { $longpoll_static_delayed = 'finished!' });
Mojo::IOLoop->timer('0.5' => sub { $self->render_static('hello.txt') });
Mojo::IOLoop->timer(0.5 => sub { $self->render_static('hello.txt') });
};

# GET /longpoll/static/delayed_too
Expand All @@ -195,7 +195,7 @@ get '/longpoll/static/delayed_too' => sub {
$self->cookie(bar => 'baz');
$self->session(foo => 'bar');
$self->render_later;
Mojo::IOLoop->timer('0.5' => sub { $self->render_static('hello.txt') });
Mojo::IOLoop->timer(0.5 => sub { $self->render_static('hello.txt') });
} => 'delayed_too';

# GET /longpoll/dynamic/delayed
Expand All @@ -204,7 +204,7 @@ get '/longpoll/dynamic/delayed' => sub {
my $self = shift;
$self->on(finish => sub { $longpoll_dynamic_delayed = 'finished!' });
Mojo::IOLoop->timer(
'0.5' => sub {
0.5 => sub {
$self->res->code(201);
$self->cookie(baz => 'yada');
$self->res->body('Dynamic!');
Expand Down Expand Up @@ -308,7 +308,7 @@ Mojo::IOLoop->client(
read => sub {
my ($stream, $chunk) = @_;
$stream->close;
Mojo::IOLoop->timer('0.5', sub { Mojo::IOLoop->stop });
Mojo::IOLoop->timer(0.5, sub { Mojo::IOLoop->stop });
}
);
$stream->write("GET /longpoll HTTP/1.1\x0d\x0a\x0d\x0a");
Expand Down Expand Up @@ -417,7 +417,7 @@ $t->get_ok('/finish')->status_is(200)
ok !$finish, 'finish event timing is right';

# GET /too_long (request timeout)
$tx = $t->ua->request_timeout('0.5')->build_tx(GET => '/too_long');
$tx = $t->ua->request_timeout(0.5)->build_tx(GET => '/too_long');
$buffer = '';
$tx->res->body(
sub {
Expand All @@ -432,7 +432,7 @@ is $buffer, 'how', 'right content';
$t->ua->request_timeout(0);

# GET /too_long (inactivity timeout)
$tx = $t->ua->inactivity_timeout('0.5')->build_tx(GET => '/too_long');
$tx = $t->ua->inactivity_timeout(0.5)->build_tx(GET => '/too_long');
$buffer = '';
$tx->res->body(
sub {
Expand Down
2 changes: 1 addition & 1 deletion t/mojolicious/websocket_lite_app.t
Expand Up @@ -36,7 +36,7 @@ get '/plain' => {text => 'Nothing to see here!'};
# WebSocket /push
websocket '/push' => sub {
my $self = shift;
my $id = Mojo::IOLoop->recurring('0.5' => sub { $self->send('push') });
my $id = Mojo::IOLoop->recurring(0.5 => sub { $self->send('push') });
$self->on(finish => sub { Mojo::IOLoop->drop($id) });
};

Expand Down

0 comments on commit 0c3f2f9

Please sign in to comment.