Skip to content

Commit

Permalink
added experimental is_running method to Mojo::IOWatcher and Mojo::IOW…
Browse files Browse the repository at this point in the history
…atcher::EV
  • Loading branch information
kraih committed Feb 17, 2012
1 parent f249868 commit be86dfc
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 13 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,6 +1,8 @@
This file documents the revision history for Perl extension Mojolicious.

2.50 2012-02-17 00:00:00
- Added EXPERIMENTAL is_running method to Mojo::IOWatcher and
Mojo::IOWatcher::EV.
- Improved Mojo::IOLoop to be controllable from foreign event loops.
- Improved resilience of Mojo::IOLoop::Client.
- Improved documentation.
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -99,8 +99,7 @@ sub generate_port { Mojo::IOLoop::Server->generate_port }

sub is_running {
my $self = shift;
$self = $self->singleton unless ref $self;
return $self->{running};
return (ref $self ? $self : $self->singleton)->iowatcher->is_running;
}

sub one_tick {
Expand Down
8 changes: 8 additions & 0 deletions lib/Mojo/IOWatcher.pm
Expand Up @@ -45,6 +45,8 @@ sub is_readable {
return !!$result;
}

sub is_running { shift->{running} }

# "This was such a pleasant St. Patrick's Day until Irish people showed up."
sub recurring { shift->_timer(pop, after => pop, recurring => time) }

Expand Down Expand Up @@ -206,6 +208,12 @@ readable or writable.
Quick check if a handle is readable, useful for identifying tainted
sockets.
=head2 C<is_running>
my $success = $watcher->is_running;
Check if watcher is running.
=head2 C<recurring>
my $id = $watcher->recurring(3 => sub {...});
Expand Down
8 changes: 8 additions & 0 deletions lib/Mojo/IOWatcher/EV.pm
Expand Up @@ -11,6 +11,8 @@ sub DESTROY { undef $EV }
# We have to fall back to Mojo::IOWatcher, since EV is unique
sub new { $EV++ ? Mojo::IOWatcher->new : shift->SUPER::new }

sub is_running {EV::depth}

sub recurring { shift->_timer(shift, 1, @_) }

# "Wow, Barney. You brought a whole beer keg.
Expand Down Expand Up @@ -103,6 +105,12 @@ implements the following new ones.
Construct a new L<Mojo::IOWatcher::EV> object.
=head2 C<is_running>
my $success = $watcher->is_running;
Check if watcher is running.
=head2 C<recurring>
my $id = $watcher->recurring(3 => sub {...});
Expand Down
17 changes: 12 additions & 5 deletions t/mojo/iowatcher.t
Expand Up @@ -6,7 +6,7 @@ BEGIN {
$ENV{MOJO_IOWATCHER} = 'Mojo::IOWatcher';
}

use Test::More tests => 60;
use Test::More tests => 64;

# "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 @@ -185,25 +185,32 @@ package main;
is(Mojo::IOWatcher->detect, 'Mojo::IOWatcher::Test', 'right class');

# Watcher in control
undef $ENV{MOJO_IOWATCHER};
isa_ok(Mojo::IOLoop->singleton->iowatcher, 'Mojo::IOWatcher', 'right object');
$ENV{MOJO_IOWATCHER} = 'Mojo::IOWatcher';
is ref Mojo::IOLoop->singleton->iowatcher, 'Mojo::IOWatcher', 'right object';
ok !Mojo::IOLoop->is_running, 'loop is not running';
$port = Mojo::IOLoop->generate_port;
my ($server_running, $client_running);
($server, $client) = '';
Mojo::IOLoop->server(
{port => $port} => sub {
my ($loop, $stream) = @_;
$stream->write('test', sub { shift->write('321') });
$stream->on(read => sub { $server .= pop });
$server_running = Mojo::IOLoop->is_running;
}
);
Mojo::IOLoop->client(
{port => $port} => sub {
my ($loop, $err, $stream) = @_;
$stream->write('tset', sub { shift->write('123') });
$stream->on(read => sub { $client .= pop });
$client_running = Mojo::IOLoop->is_running;
}
);
Mojo::IOLoop->timer(1 => sub { Mojo::IOLoop->singleton->iowatcher->stop });
Mojo::IOLoop->singleton->iowatcher->start;
is $server, 'tset123', 'right content';
is $client, 'test321', 'right content';
ok !Mojo::IOLoop->is_running, 'loop is not running';
ok $server_running, 'loop is running';
ok $client_running, 'loop is running';
is $server, 'tset123', 'right content';
is $client, 'test321', 'right content';
19 changes: 13 additions & 6 deletions t/mojo/iowatcher_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 => 60;
plan tests => 64;

use IO::Socket::INET;
use Mojo::IOLoop;
Expand Down Expand Up @@ -186,26 +186,33 @@ package main;
is(Mojo::IOWatcher->detect, 'Mojo::IOWatcher::Test', 'right class');

# EV in control
undef $ENV{MOJO_IOWATCHER};
isa_ok(Mojo::IOLoop->singleton->iowatcher,
'Mojo::IOWatcher::EV', 'right object');
$ENV{MOJO_IOWATCHER} = 'Mojo::IOWatcher::EV';
is ref Mojo::IOLoop->singleton->iowatcher, 'Mojo::IOWatcher::EV',
'right object';
ok !Mojo::IOLoop->is_running, 'loop is not running';
$port = Mojo::IOLoop->generate_port;
my ($server_running, $client_running);
($server, $client) = '';
Mojo::IOLoop->server(
{port => $port} => sub {
my ($loop, $stream) = @_;
$stream->write('test', sub { shift->write('321') });
$stream->on(read => sub { $server .= pop });
$server_running = Mojo::IOLoop->is_running;
}
);
Mojo::IOLoop->client(
{port => $port} => sub {
my ($loop, $err, $stream) = @_;
$stream->write('tset', sub { shift->write('123') });
$stream->on(read => sub { $client .= pop });
$client_running = Mojo::IOLoop->is_running;
}
);
Mojo::IOLoop->timer(1 => sub { EV::break(EV::BREAK_ONE()) });
EV::run();
is $server, 'tset123', 'right content';
is $client, 'test321', 'right content';
ok !Mojo::IOLoop->is_running, 'loop is not running';
ok $server_running, 'loop is running';
ok $client_running, 'loop is running';
is $server, 'tset123', 'right content';
is $client, 'test321', 'right content';

0 comments on commit be86dfc

Please sign in to comment.