Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
improved port range and address used for testing
  • Loading branch information
kraih committed Mar 16, 2012
1 parent a05148a commit c679baa
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 36 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,6 +1,6 @@
This file documents the revision history for Perl extension Mojolicious.

2.62 2012-03-16 00:00:00
2.62 2012-03-17 00:00:00
- Added options function to Mojolicious::Lite.
- Added options method to Mojolicious::Routes.
- Added options method to Mojo::UserAgent.
Expand Down Expand Up @@ -60,6 +60,7 @@ This file documents the revision history for Perl extension Mojolicious.
Mojo::Content::Single.
- Removed experimental status from is_dynamic, is_limit_exceeded and
max_line_size methods in Mojo::Message.
- Improved port range and address used for testing.
- Improved Mojo::IOWatcher exception handling a little.
- Improved Mojolicious::Routes logging.
- Improved documentation.
Expand Down
12 changes: 5 additions & 7 deletions lib/Mojo/IOLoop/Server.pm
Expand Up @@ -146,16 +146,14 @@ sub listen {

sub generate_port {

# Try random ports
my $port = 1 . int(rand 10) . int(rand 10) . int(rand 10) . int(rand 10);
while ($port++ < 30000) {
return $port
if IO::Socket::INET->new(
# Try private ports
for my $port ((49152 + int(rand 10000)) .. 655535) {
IO::Socket::INET->new(
Listen => 5,
LocalAddr => '127.0.0.1',
LocalPort => $port,
Proto => 'tcp'
);
) and return $port;
}

return;
Expand Down Expand Up @@ -340,7 +338,7 @@ Path to the TLS key file, defaults to a built-in test key.
my $port = $server->generate_port;
Find a free TCP port.
Find a free TCP port, this is a utility function primarily used for tests.
=head2 C<start>
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -449,8 +449,8 @@ sub _server {
my $port = $self->{port} = $loop->generate_port;
die "Couldn't find a free TCP port for testing.\n" unless $port;
$self->{scheme} = $scheme ||= 'http';
$server->listen(["$scheme://*:$port"])->start;
warn "TEST SERVER STARTED ($scheme://*:$port)\n" if DEBUG;
$server->listen(["$scheme://127.0.0.1:$port"])->start;
warn "TEST SERVER STARTED ($scheme://127.0.0.1:$port)\n" if DEBUG;

return $server;
}
Expand Down
18 changes: 12 additions & 6 deletions t/mojo/hypnotoad.t
Expand Up @@ -35,8 +35,10 @@ use Mojolicious::Lite;
plugin Config => {
default => {
hypnotoad =>
{listen => ['http://*:$port1', 'http://*:$port2'], workers => 1}
hypnotoad => {
listen => ['http://127.0.0.1:$port1', 'http://127.0.0.1:$port2'],
workers => 1
}
}
};
Expand All @@ -50,10 +52,11 @@ EOF
# Start
my $prefix = "$FindBin::Bin/../../script";
open my $start, '-|', $^X, "$prefix/hypnotoad", $script;
sleep 3;
sleep 1
while !IO::Socket::INET->new(
Proto => 'tcp',
PeerAddr => 'localhost',
PeerAddr => '127.0.0.1',
PeerPort => $port2
);
my $old = _pid();
Expand Down Expand Up @@ -98,8 +101,10 @@ use Mojolicious::Lite;
plugin Config => {
default => {
hypnotoad =>
{listen => ['http://*:$port1', 'http://*:$port2'], workers => 1}
hypnotoad => {
listen => ['http://127.0.0.1:$port1', 'http://127.0.0.1:$port2'],
workers => 1
}
}
};
Expand Down Expand Up @@ -171,10 +176,11 @@ is $tx->res->body, 'Hello World!', 'right content';

# Stop
open my $stop, '-|', $^X, "$prefix/hypnotoad", $script, '-s';
sleep 3;
sleep 1
while IO::Socket::INET->new(
Proto => 'tcp',
PeerAddr => 'localhost',
PeerAddr => '127.0.0.1',
PeerPort => $port2
);

Expand Down
12 changes: 7 additions & 5 deletions t/mojo/ioloop.t
Expand Up @@ -109,7 +109,8 @@ ok $count < 10, 'less than ten recurring events';
my $port = Mojo::IOLoop->generate_port;
my $handle;
$id = $loop->server(
port => $port,
address => '127.0.0.1',
port => $port,
sub {
my ($loop, $stream) = @_;
$handle = $stream->handle;
Expand All @@ -129,7 +130,8 @@ $loop->start;
$port = Mojo::IOLoop->generate_port;
my $buffer = '';
Mojo::IOLoop->server(
port => $port,
address => '127.0.0.1',
port => $port,
sub {
my ($loop, $stream, $id) = @_;
$buffer .= 'accepted';
Expand Down Expand Up @@ -168,7 +170,7 @@ is $buffer, 'acceptedhelloworld', 'right result';

# Dropped listen socket
$port = Mojo::IOLoop->generate_port;
$id = $loop->server({port => $port} => sub { });
$id = $loop->server({address => '127.0.0.1', port => $port} => sub { });
my $connected;
$loop->client(
{port => $port} => sub {
Expand Down Expand Up @@ -197,7 +199,7 @@ ok $err, 'has error';
$port = Mojo::IOLoop->generate_port;
my ($server_close, $client_close);
Mojo::IOLoop->server(
(port => $port) => sub {
(address => '127.0.0.1', port => $port) => sub {
my ($loop, $stream) = @_;
$stream->on(close => sub { $server_close++ });
}
Expand All @@ -218,7 +220,7 @@ is $client_close, 1, 'client emitted close event once';
$port = Mojo::IOLoop->generate_port;
my ($client, $server, $client_after, $server_before, $server_after) = '';
Mojo::IOLoop->server(
{port => $port} => sub {
{address => '127.0.0.1', port => $port} => sub {
my ($loop, $stream) = @_;
$stream->timeout(0)->on(
read => sub {
Expand Down
7 changes: 6 additions & 1 deletion t/mojo/ioloop_tls.t
Expand Up @@ -44,7 +44,7 @@ my $loop = Mojo::IOLoop->new;
my $port = Mojo::IOLoop->generate_port;
my ($server, $client) = '';
$loop->server(
{port => $port, tls => 1} => sub {
{address => '127.0.0.1', port => $port, tls => 1} => sub {
my ($loop, $stream) = @_;
$stream->write('test', sub { shift->write('321') });
$stream->on(read => sub { $server .= pop });
Expand All @@ -69,6 +69,7 @@ $server = $client = '';
my ($drop, $running, $timeout, $server_err, $server_close, $client_close);
Mojo::IOLoop->drop(Mojo::IOLoop->recurring(0 => sub { $drop++ }));
$loop->server(
address => '127.0.0.1',
port => $port,
tls => 1,
tls_ca => 't/mojo/certs/ca.crt',
Expand Down Expand Up @@ -135,6 +136,7 @@ $loop = Mojo::IOLoop->new;
$port = Mojo::IOLoop->generate_port;
$server_err = $client_err = '';
$loop->server(
address => '127.0.0.1',
port => $port,
tls => 1,
tls_ca => 'no cert',
Expand All @@ -160,6 +162,7 @@ $port = Mojo::IOLoop->generate_port;
$server = $client = '';
($running, $timeout, $server_err, $server_close, $client_close) = undef;
$loop->server(
address => '127.0.0.1',
port => $port,
tls => 1,
tls_ca => 't/mojo/certs/ca.crt',
Expand Down Expand Up @@ -204,6 +207,7 @@ $loop = Mojo::IOLoop->new;
$port = Mojo::IOLoop->generate_port;
$server_err = $client_err = '';
$loop->server(
address => '127.0.0.1',
port => $port,
tls => 1,
tls_cert => 't/mojo/certs/badclient.crt',
Expand All @@ -226,6 +230,7 @@ $loop = Mojo::IOLoop->new;
$port = Mojo::IOLoop->generate_port;
$server_err = $client_err = '';
$loop->server(
address => '127.0.0.1',
port => $port,
tls => 1,
tls_cert => 't/mojo/certs/badclient.crt',
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/iowatcher.t
Expand Up @@ -195,7 +195,7 @@ $port = Mojo::IOLoop->generate_port;
my ($server_err, $server_running, $client_err, $client_running);
($server, $client) = '';
Mojo::IOLoop->server(
{port => $port} => sub {
{address => '127.0.0.1', port => $port} => sub {
my ($loop, $stream) = @_;
$stream->write('test', sub { shift->write('321') });
$stream->on(read => sub { $server .= pop });
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/iowatcher_ev.t
Expand Up @@ -197,7 +197,7 @@ $port = Mojo::IOLoop->generate_port;
my ($server_err, $server_running, $client_err, $client_running);
($server, $client) = '';
Mojo::IOLoop->server(
{port => $port} => sub {
{address => '127.0.0.1', port => $port} => sub {
my ($loop, $stream) = @_;
$stream->write('test', sub { shift->write('321') });
$stream->on(read => sub { $server .= pop });
Expand Down
11 changes: 6 additions & 5 deletions t/mojo/morbo.t
Expand Up @@ -46,11 +46,12 @@ EOF
my $port = Mojo::IOLoop->generate_port;
my $prefix = "$FindBin::Bin/../../script";
my $pid = open my $server, '-|', $^X, "$prefix/morbo", '-l',
"http://*:$port", $script;
"http://127.0.0.1:$port", $script;
sleep 3;
sleep 1
while !IO::Socket::INET->new(
Proto => 'tcp',
PeerAddr => 'localhost',
PeerAddr => '127.0.0.1',
PeerPort => $port
);

Expand Down Expand Up @@ -86,7 +87,7 @@ sleep 3;
sleep 1
while !IO::Socket::INET->new(
Proto => 'tcp',
PeerAddr => 'localhost',
PeerAddr => '127.0.0.1',
PeerPort => $port
);

Expand Down Expand Up @@ -122,7 +123,7 @@ sleep 3;
sleep 1
while !IO::Socket::INET->new(
Proto => 'tcp',
PeerAddr => 'localhost',
PeerAddr => '127.0.0.1',
PeerPort => $port
);

Expand All @@ -143,7 +144,7 @@ kill 'INT', $pid;
sleep 1
while IO::Socket::INET->new(
Proto => 'tcp',
PeerAddr => 'localhost',
PeerAddr => '127.0.0.1',
PeerPort => $port
);

Expand Down
4 changes: 2 additions & 2 deletions t/mojo/user_agent.t
Expand Up @@ -336,8 +336,8 @@ is_deeply \@kept_alive, [1, 1], 'connections kept alive';

# Premature connection close
my $port = Mojo::IOLoop->generate_port;
my $id =
Mojo::IOLoop->server({port => $port} => sub { Mojo::IOLoop->drop(pop) });
my $id = Mojo::IOLoop->server(
{address => '127.0.0.1', port => $port} => sub { Mojo::IOLoop->drop(pop) });
$tx = $ua->get("http://localhost:$port/");
ok !$tx->success, 'not successful';
is $tx->error, 'Premature connection close.', 'right error';
2 changes: 1 addition & 1 deletion t/mojo/user_agent_tls.t
Expand Up @@ -33,7 +33,7 @@ my $daemon =
Mojo::Server::Daemon->new(app => app, ioloop => Mojo::IOLoop->singleton);
my $port = Mojo::IOLoop->new->generate_port;
my $listen =
"https://*:$port"
"https://127.0.0.1:$port"
. '?cert=t/mojo/certs/server.crt'
. '&key=t/mojo/certs/server.key'
. '&ca=t/mojo/certs/ca.crt';
Expand Down
4 changes: 2 additions & 2 deletions t/mojo/websocket_proxy.t
Expand Up @@ -50,7 +50,7 @@ my $ua = Mojo::UserAgent->new;
my $daemon =
Mojo::Server::Daemon->new(app => app, ioloop => Mojo::IOLoop->singleton);
my $port = Mojo::IOLoop->new->generate_port;
$daemon->listen(["http://*:$port"])->start;
$daemon->listen(["http://127.0.0.1:$port"])->start;

# Connect proxy server for testing
my $proxy = Mojo::IOLoop->generate_port;
Expand All @@ -63,7 +63,7 @@ my $nf =
. "Connection: close\x0d\x0a\x0d\x0a";
my $ok = "HTTP/1.1 200 OK\x0d\x0aConnection: keep-alive\x0d\x0a\x0d\x0a";
Mojo::IOLoop->server(
{port => $proxy} => sub {
{address => '127.0.0.1', port => $proxy} => sub {
my ($loop, $stream, $client) = @_;
$stream->on(
read => sub {
Expand Down
4 changes: 2 additions & 2 deletions t/mojo/websocket_proxy_tls.t
Expand Up @@ -64,7 +64,7 @@ my $daemon =
Mojo::Server::Daemon->new(app => app, ioloop => Mojo::IOLoop->singleton);
my $port = Mojo::IOLoop->new->generate_port;
my $listen =
"https://*:$port"
"https://127.0.0.1:$port"
. '?cert=t/mojo/certs/server.crt'
. '&key=t/mojo/certs/server.key'
. '&ca=t/mojo/certs/ca.crt';
Expand All @@ -81,7 +81,7 @@ my $nf =
. "Connection: close\x0d\x0a\x0d\x0a";
my $ok = "HTTP/1.0 200 OK\x0d\x0aX-Something: unimportant\x0d\x0a\x0d\x0a";
Mojo::IOLoop->server(
{port => $proxy} => sub {
{address => '127.0.0.1', port => $proxy} => sub {
my ($loop, $stream, $client) = @_;
$stream->on(
read => sub {
Expand Down

0 comments on commit c679baa

Please sign in to comment.