Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
test the connection limit
  • Loading branch information
kraih committed Mar 2, 2016
1 parent 2a04ea8 commit f2d44b1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

6.52 2016-03-02
- Added is_listening method to Mojo::IOLoop::Server.
- Improved responsiveness of stop_gracefully method in Mojo::IOLoop.
- Fixed a few concurrency bugs in Mojo::IOLoop.

Expand Down
13 changes: 11 additions & 2 deletions lib/Mojo/IOLoop/Server.pm
Expand Up @@ -38,6 +38,8 @@ sub generate_port {

sub handle { shift->{handle} }

sub is_listening { !!shift->{active} }

sub listen {
my ($self, $args) = (shift, ref $_[0] ? $_[0] : {@_});

Expand Down Expand Up @@ -105,10 +107,11 @@ sub port { shift->{handle}->sockport }
sub start {
my $self = shift;
weaken $self;
$self->reactor->io($self->{handle} => sub { $self->_accept });
++$self->{active}
and $self->reactor->io($self->{handle} => sub { $self->_accept });
}

sub stop { $_[0]->reactor->remove($_[0]{handle}) }
sub stop { delete($_[0]{active}) and $_[0]->reactor->remove($_[0]{handle}) }

sub _accept {
my $self = shift;
Expand Down Expand Up @@ -230,6 +233,12 @@ Find a free TCP port, primarily used for tests.
Get handle for server.
=head2 is_listening
my $bool = $server->is_listening;
Check if connections are currently being accepted.
=head2 listen
$server->listen(port => 3000);
Expand Down
25 changes: 25 additions & 0 deletions t/mojo/ioloop.t
Expand Up @@ -291,6 +291,31 @@ $loop->start;
ok !$err, 'no error';
is $loop->max_accepts, 1, 'right value';

# Connection limit
$err = '';
$loop = Mojo::IOLoop->new->max_connections(2);
my @listening;
$id = $loop->server(
{address => '127.0.0.1'} => sub {
my ($loop, $stream) = @_;

$loop->next_tick(
sub {
my $loop = shift;
push @listening, $loop->acceptor($id)->is_listening;
$loop->stop if @listening >= 2;
}
);
}
);
$port = $loop->acceptor($id)->port;
$loop->client({port => $port} => sub { }) for 1 .. 2;
$loop->timer(30 => sub { shift->stop; $err = 'failed' });
$loop->start;
ok !$err, 'no error';
ok $listening[0], 'still accepting connections';
ok !$listening[1], 'connection limit reached';

# Exception in timer
{
local *STDERR;
Expand Down

0 comments on commit f2d44b1

Please sign in to comment.