Skip to content

Commit

Permalink
made Mojo::IOLoop->start marcus-proof :)
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 4, 2011
1 parent 9b65b27 commit 80ed4b6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/Mojo/IOLoop.pm
@@ -1,6 +1,7 @@
package Mojo::IOLoop;
use Mojo::Base -base;

use Carp 'croak';
use Mojo::IOLoop::Client;
use Mojo::IOLoop::Resolver;
use Mojo::IOLoop::Server;
Expand Down Expand Up @@ -285,8 +286,7 @@ sub start {
$self = $self->singleton unless ref $self;

# Check if we are already running
return if $self->{running};
$self->{running} = 1;
croak 'Mojo::IOLoop already running' if $self->{running}++;

# Mainloop
$self->one_tick while $self->{running};
Expand Down
21 changes: 17 additions & 4 deletions t/mojo/ioloop.t
Expand Up @@ -7,7 +7,7 @@ BEGIN {
$ENV{MOJO_IOWATCHER} = 'Mojo::IOWatcher';
}

use Test::More tests => 13;
use Test::More tests => 14;

# "Marge, you being a cop makes you the man!
# Which makes me the woman, and I have no interest in that,
Expand All @@ -27,6 +27,18 @@ my $loop = Mojo::IOLoop->new;
Mojo::IOLoop->iowatcher(MyWatcher->new);
is ref $loop->iowatcher, 'MyWatcher', 'right class';

# Double start
my $error;
Mojo::IOLoop->defer(
sub {
eval { Mojo::IOLoop->start };
$error = $@;
Mojo::IOLoop->stop;
}
);
Mojo::IOLoop->start;
like $error, qr/^Mojo::IOLoop already running/, 'right error';

# Ticks
my $ticks = 0;
my $id = $loop->recurring(0 => sub { $ticks++ });
Expand Down Expand Up @@ -108,9 +120,10 @@ $loop->start;
isa_ok $handle, 'IO::Socket', 'right reference';

# Dropped listen socket
$port = Mojo::IOLoop->generate_port;
$id = $loop->listen(port => $port);
my ($connected, $error);
$port = Mojo::IOLoop->generate_port;
$id = $loop->listen(port => $port);
$error = undef;
my $connected;
$loop->connect(
address => 'localhost',
port => $port,
Expand Down

0 comments on commit 80ed4b6

Please sign in to comment.