Skip to content

Commit

Permalink
added reset methods to a few classes
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 16, 2014
1 parent eb78860 commit 80c5c6d
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 12 deletions.
5 changes: 4 additions & 1 deletion Changes
@@ -1,5 +1,8 @@

5.08 2014-06-15
5.08 2014-06-16
- Added reset method to Mojo::IOLoop.
- Added reset method to Mojo::Reactor.
- Added reset method to Mojo::Reactor::Poll.

5.07 2014-06-13
- Fixed RFC 7230 compliance bugs in Mojo::Headers.
Expand Down
15 changes: 15 additions & 0 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -98,6 +98,14 @@ sub remove {
$self->_remove($id);
}

sub reset {
my $self = _instance(shift);
$self->_remove($_)
for keys %{$self->{acceptors}}, keys %{$self->{connections}};
delete @$self{qw(accept stop)};
$self->reactor->reset;
}

sub server {
my ($self, $cb) = (_instance(shift), pop);

Expand Down Expand Up @@ -552,6 +560,13 @@ amount of time in seconds.
Remove anything with an id, connections will be dropped gracefully by allowing
them to finish writing all data in their write buffers.
=head2 reset
Mojo::IOLoop->reset;
$loop->reset;
Remove everything.
=head2 server
my $id = Mojo::IOLoop->server(port => 3000, sub {...});
Expand Down
7 changes: 7 additions & 0 deletions lib/Mojo/Reactor.pm
Expand Up @@ -33,6 +33,7 @@ sub next_tick { shift->timer(0 => @_) and return undef }
sub one_tick { croak 'Method "one_tick" not implemented by subclass' }
sub recurring { croak 'Method "recurring" not implemented by subclass' }
sub remove { croak 'Method "remove" not implemented by subclass' }
sub reset { croak 'Method "reset" not implemented by subclass' }
sub start { croak 'Method "start" not implemented by subclass' }
sub stop { croak 'Method "stop" not implemented by subclass' }
sub timer { croak 'Method "timer" not implemented by subclass' }
Expand Down Expand Up @@ -170,6 +171,12 @@ amount of time in seconds. Meant to be overloaded in a subclass.
Remove handle or timer. Meant to be overloaded in a subclass.
=head2 reset
$reactor->reset;
Remove all handles and timers. Meant to be overloaded in a subclass.
=head2 start
$reactor->start;
Expand Down
8 changes: 8 additions & 0 deletions lib/Mojo/Reactor/Poll.pm
Expand Up @@ -80,6 +80,8 @@ sub remove {
return !!delete $self->{io}{fileno $remove};
}

sub reset { delete @{shift()}{qw(io poll timers)} }

sub start {
my $self = shift;
$self->{running}++;
Expand Down Expand Up @@ -207,6 +209,12 @@ amount of time in seconds.
Remove handle or timer.
=head2 reset
$reactor->reset;
Remove all handles and timers.
=head2 start
$reactor->start;
Expand Down
11 changes: 8 additions & 3 deletions t/mojo/ioloop.t
Expand Up @@ -76,7 +76,7 @@ $loop->remove($id);
ok $count > 1, 'more than one recurring event';
ok $count < 10, 'less than ten recurring events';

# Handle
# Handle and reset
my ($handle, $handle2);
$id = Mojo::IOLoop->server(
(address => '127.0.0.1') => sub {
Expand All @@ -90,8 +90,13 @@ Mojo::IOLoop->acceptor($id)->on(accept => sub { $handle2 = pop });
$id2
= Mojo::IOLoop->client((address => 'localhost', port => $port) => sub { });
Mojo::IOLoop->start;
Mojo::IOLoop->remove($id);
Mojo::IOLoop->remove($id2);
$count = 0;
Mojo::IOLoop->recurring(0 => sub { $timer++ });
Mojo::IOLoop->reset;
Mojo::IOLoop->one_tick;
is $count, 0, 'no recurring events';
ok !Mojo::IOLoop->acceptor($id), 'acceptor has been removed';
ok !Mojo::IOLoop->stream($id2), 'stream has been removed';
is $handle, $handle2, 'handles are equal';
isa_ok $handle, 'IO::Socket', 'right reference';

Expand Down
8 changes: 4 additions & 4 deletions t/mojo/reactor_ev.t
Expand Up @@ -139,13 +139,13 @@ ok !$timer, 'timer was not triggered';
ok !$recurring, 'recurring was not triggered again';

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

Expand Down
8 changes: 4 additions & 4 deletions t/mojo/reactor_poll.t
Expand Up @@ -137,13 +137,13 @@ ok !$timer, 'timer was not triggered';
ok !$recurring, 'recurring was not triggered again';

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

Expand Down

0 comments on commit 80c5c6d

Please sign in to comment.