Skip to content

Commit

Permalink
fixed Mojo::IOLoop to clean up after itself
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 29, 2012
1 parent 83fdc8d commit 949e0da
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -5,6 +5,7 @@ This file documents the revision history for Perl extension Mojolicious.
- Fixed small inconsistency between Mojo::IOWatcher and
Mojo::IOWatcher::EV where Mojo::IOWatcher would not stop
automatically without watchers.
- Fixed Mojo::IOLoop to clean up after itself.

2.55 2012-02-28 00:00:00
- Combined WebSocket timeout with normal inactivity timeout.
Expand Down
2 changes: 2 additions & 0 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -227,6 +227,8 @@ sub _cleaner {
}

# Graceful shutdown
$self->_drop(delete $self->{cleaner})
unless keys(%$connections) || keys(%{$self->{servers}});
$self->stop if $self->max_connections == 0 && keys %$connections == 0;
}
);
Expand Down
15 changes: 11 additions & 4 deletions t/mojo/ioloop.t
Expand Up @@ -86,38 +86,45 @@ ok $ticks > 2, 'more than two ticks';
# Run again without first tick event handler
my $before = $ticks;
my $after = 0;
$loop->recurring(0 => sub { $after++ });
my $id2 = $loop->recurring(0 => sub { $after++ });
$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 $ticks, $before, 'no additional ticks';

# Recurring timer
my $count = 0;
$loop->recurring(0.5 => sub { $count++ });
$id = $loop->recurring(0.5 => sub { $count++ });
$loop->timer(3 => sub { shift->stop });
$loop->start;
$loop->one_tick;
$loop->drop($id);
ok $count > 1, 'more than one recurring event';
ok $count < 10, 'less than ten recurring events';

# Handle
my $port = Mojo::IOLoop->generate_port;
my $handle;
$loop->server(
$id = $loop->server(
port => $port,
sub {
my ($loop, $stream) = @_;
$handle = $stream->handle;
$loop->stop;
}
);
$loop->client((address => 'localhost', port => $port) => sub { });
$id2 = $loop->client((address => 'localhost', port => $port) => sub { });
$loop->start;
$loop->drop($id);
$loop->drop($id2);
isa_ok $handle, 'IO::Socket', 'right reference';

# Make sure it stops automatically when no events are being watched
$loop->start;

# Stream
$port = Mojo::IOLoop->generate_port;
my $buffer = '';
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/iowatcher.t
Expand Up @@ -24,7 +24,7 @@ require Mojo::IOLoop;
$watcher = Mojo::IOLoop->singleton->iowatcher;
is ref $watcher, 'Mojo::IOWatcher', 'right object';

# Make sure it stops automatically without watchers
# Make sure it stops automatically when no events are being watched
Mojo::IOLoop->start;

# Listen
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/iowatcher_ev.t
Expand Up @@ -25,7 +25,7 @@ require Mojo::IOLoop;
$watcher = Mojo::IOLoop->singleton->iowatcher;
is ref $watcher, 'Mojo::IOWatcher::EV', 'right object';

# Make sure it stops automatically without watchers
# Make sure it stops automatically when no events are being watched
Mojo::IOLoop->start;

# Listen
Expand Down

0 comments on commit 949e0da

Please sign in to comment.