Skip to content

Instantly share code, notes, and snippets.

/is_active.diff Secret

Created March 20, 2015 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/9c214442dc86e3e6333f to your computer and use it in GitHub Desktop.
Save anonymous/9c214442dc86e3e6333f to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojo/Reactor/Poll.pm b/lib/Mojo/Reactor/Poll.pm
index f1e3533..f5e2fff 100644
--- a/lib/Mojo/Reactor/Poll.pm
+++ b/lib/Mojo/Reactor/Poll.pm
@@ -17,6 +17,12 @@ sub io {
return $self->watch($handle, 1, 1);
}
+sub is_active {
+ my ($self, $active) = @_;
+ return exists $self->{io}{fileno $active} if ref $active;
+ return exists $self->{timers}{$active};
+}
+
sub is_running { !!shift->{running} }
sub one_tick {
@@ -195,6 +201,13 @@ Restart active timer.
Watch handle for I/O events, invoking the callback whenever handle becomes
readable or writable.
+=head2 is_active
+
+ my $bool = $reactor->is_active($handle);
+ my $bool = $reactor->is_active($id);
+
+Check if handle or timer has an active event watcher.
+
=head2 is_running
my $bool = $reactor->is_running;
diff --git a/t/mojo/reactor_ev.t b/t/mojo/reactor_ev.t
index 30917d6..8f1537b 100644
--- a/t/mojo/reactor_ev.t
+++ b/t/mojo/reactor_ev.t
@@ -49,7 +49,9 @@ ok !$writable, 'handle is not writable';
# Accept
my $server = $listen->accept;
+ok $reactor->is_active($listen), 'watching';
$reactor->remove($listen);
+ok !$reactor->is_active($listen), 'not watching';
($readable, $writable) = ();
$reactor->io($client => sub { pop() ? $writable++ : $readable++ });
$reactor->again($reactor->timer(0.025 => sub { shift->stop }));
@@ -58,7 +60,9 @@ ok !$readable, 'handle is not readable';
ok $writable, 'handle is writable';
print $client "hello!\n";
sleep 1;
+ok $reactor->is_active($client), 'watching';
$reactor->remove($client);
+ok !$reactor->is_active($client), 'not watching';
($readable, $writable) = ();
$reactor->io($server => sub { pop() ? $writable++ : $readable++ });
$reactor->watch($server, 1, 0);
@@ -118,7 +122,9 @@ ok $readable, 'handle is readable again';
ok $writable, 'handle is writable again';
ok !$timer, 'timer was not triggered';
ok $recurring, 'recurring was triggered again';
+ok $reactor->is_active($id), 'watching';
$reactor->remove($id);
+ok !$reactor->is_active($id), 'not watching';
($readable, $writable, $timer, $recurring) = ();
$reactor->timer(0.025 => sub { shift->stop });
$reactor->start;
diff --git a/t/mojo/reactor_poll.t b/t/mojo/reactor_poll.t
index 22a0bf1..aafe60f 100644
--- a/t/mojo/reactor_poll.t
+++ b/t/mojo/reactor_poll.t
@@ -46,7 +46,9 @@ ok !$writable, 'handle is not writable';
# Accept
my $server = $listen->accept;
+ok $reactor->is_active($listen), 'watching';
$reactor->remove($listen);
+ok !$reactor->is_active($listen), 'not watching';
($readable, $writable) = ();
$reactor->io($client => sub { pop() ? $writable++ : $readable++ });
$reactor->again($reactor->timer(0.025 => sub { shift->stop }));
@@ -55,7 +57,9 @@ ok !$readable, 'handle is not readable';
ok $writable, 'handle is writable';
print $client "hello!\n";
sleep 1;
+ok $reactor->is_active($client), 'watching';
$reactor->remove($client);
+ok !$reactor->is_active($client), 'not watching';
($readable, $writable) = ();
$reactor->io($server => sub { pop() ? $writable++ : $readable++ });
$reactor->watch($server, 1, 0);
@@ -115,7 +119,9 @@ ok $readable, 'handle is readable again';
ok $writable, 'handle is writable again';
ok !$timer, 'timer was not triggered';
ok $recurring, 'recurring was triggered again';
+ok $reactor->is_active($id), 'watching';
$reactor->remove($id);
+ok !$reactor->is_active($id), 'not watching';
($readable, $writable, $timer, $recurring) = ();
$reactor->timer(0.025 => sub { shift->stop });
$reactor->start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment