Skip to content

Commit

Permalink
Don't use localhost; bind to 0.0.0.0, connect to 127.0.0.1 instead
Browse files Browse the repository at this point in the history
localhost can resolve to more than one address, which causes upcoming
changes to MoarVM to break some tests when they shouldn't actually be
breaking changes.
  • Loading branch information
Kaiepi committed Aug 30, 2019
1 parent c5d724c commit 9513102
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 48 deletions.
17 changes: 11 additions & 6 deletions S17-promise/nonblocking-await.t
Expand Up @@ -221,8 +221,11 @@ PROCESS::<$SCHEDULER> := ThreadPoolScheduler.new(max_threads => 4);
{
my $kill = Promise.new;
my $started = Promise.new;
my $s-address = '0.0.0.0';
my $c-address = '127.0.0.1';
my $port = 4893;
my $ok = start react {
whenever IO::Socket::Async.listen('localhost', 4893) -> $conn {
whenever IO::Socket::Async.listen($s-address, $port) -> $conn {
whenever $conn.Supply(:bin) -> $buf {
await $conn.write: $buf;
$conn.close;
Expand All @@ -236,7 +239,7 @@ PROCESS::<$SCHEDULER> := ThreadPoolScheduler.new(max_threads => 4);
my @responses;
for ^20 {
react {
whenever IO::Socket::Async.connect('localhost', 4893) -> $client {
whenever IO::Socket::Async.connect($c-address, $port) -> $client {
await $client.write('is this thing on?'.encode('ascii'));
whenever $client.Supply(:bin) {
push @responses, .decode('ascii');
Expand All @@ -256,17 +259,17 @@ PROCESS::<$SCHEDULER> := ThreadPoolScheduler.new(max_threads => 4);
#?rakudo.jvm skip 'NullPointerException'
{
my @foo = do {
await start { do for ^2 { my uint64 @ = 9, 9; }.Slip },
start { do for ^2 { my uint64 @ = 1, 2; }.Slip };
await start { do for ^2 { my uint64 @ = 9, 9; }.Slip },
start { do for ^2 { my uint64 @ = 1, 2; }.Slip };
}
is @foo.elems, 4, "slips awaited over get flattened out";
}
#?rakudo.jvm skip 'UnwindException'
{
await start {
my @foo = do {
await start { do for ^2 { my uint64 @ = 9, 9; }.Slip },
start { do for ^2 { my uint64 @ = 1, 2; }.Slip };
await start { do for ^2 { my uint64 @ = 9, 9; }.Slip },
start { do for ^2 { my uint64 @ = 1, 2; }.Slip };
}
is @foo.elems, 4, "slips awaited over get flattened out";
}
Expand All @@ -284,3 +287,5 @@ PROCESS::<$SCHEDULER> := ThreadPoolScheduler.new(max_threads => 4);
await (((p(), (p(), (p(),))), (p(), p(), p())), (p(), p(), p()));
is-deeply $x, 9, '&await awaits in sink context, with nested iterables';
}

# vim: ft=perl6 sw=4 ts=4 sts=4 expandtab
15 changes: 8 additions & 7 deletions S32-io/IO-Socket-Async-UDP.t
Expand Up @@ -3,12 +3,13 @@ use Test;

plan 3;

my $hostname = 'localhost';
my $port = 5001;
my $s-address = '0.0.0.0';
my $c-address = '127.0.0.1';
my $port = 5001;

{
my $sock = IO::Socket::Async.bind-udp($hostname, $port);
dies-ok { IO::Socket::Async.bind-udp($hostname, $port) },
my $sock = IO::Socket::Async.bind-udp($s-address, $port);
dies-ok { IO::Socket::Async.bind-udp($s-address, $port) },
'Error on trying to re-use port with UDP bind';
$sock.close;
}
Expand All @@ -18,7 +19,7 @@ my $rec-prom;

# Listener
{
my $sock = IO::Socket::Async.bind-udp($hostname, $port);
my $sock = IO::Socket::Async.bind-udp($s-address, $port);
my $tap = $sock.Supply.tap: -> $chars {
if $chars.chars > 0 {
$rec-prom.keep($chars);
Expand All @@ -30,7 +31,7 @@ my $rec-prom;
{
$rec-prom = Promise.new;
my $sock = IO::Socket::Async.udp();
$sock.print-to($hostname, $port, "Unusually Dubious Protocol");
$sock.print-to($c-address, $port, "Unusually Dubious Protocol");
is $rec-prom.result, "Unusually Dubious Protocol", "Sent/received data with UDP (print)";
$sock.close;
}
Expand All @@ -39,7 +40,7 @@ my $rec-prom;
{
$rec-prom = Promise.new;
my $sock = IO::Socket::Async.udp();
$sock.write-to($hostname, $port, "Unhelpful Dataloss Protocol".encode('ascii'));
$sock.write-to($c-address, $port, "Unhelpful Dataloss Protocol".encode('ascii'));
is $rec-prom.result, "Unhelpful Dataloss Protocol", "Sent/received data with UDP (write)";
$sock.close;
}
33 changes: 17 additions & 16 deletions S32-io/IO-Socket-Async.t
Expand Up @@ -3,7 +3,8 @@ use Test;

plan 40;

my $hostname = 'localhost';
my $s-address = '0.0.0.0';
my $c-address = '127.0.0.1';
my $port = 5000;

try {
Expand All @@ -15,19 +16,19 @@ try {
await $sync;
}

await IO::Socket::Async.connect($hostname, $port).then(-> $sr {
await IO::Socket::Async.connect($c-address, $port).then(-> $sr {
is $sr.status, Broken, 'Async connect to unavailable server breaks promise';
});

my $server = IO::Socket::Async.listen($hostname, $port);
my $server = IO::Socket::Async.listen($s-address, $port);

my $echoTap = $server.tap(-> $c {
$c.Supply.tap(-> $chars {
$c.print($chars).then({ $c.close });
}, quit => { say $_; });
});

await IO::Socket::Async.connect($hostname, $port).then(-> $sr {
await IO::Socket::Async.connect($c-address, $port).then(-> $sr {
is $sr.status, Kept, 'Async connect to available server keeps promise';
$sr.result.close() if $sr.status == Kept;
});
Expand All @@ -36,7 +37,7 @@ multi sub client(&code) {
my $p = Promise.new;
my $v = $p.vow;

my $client = IO::Socket::Async.connect($hostname, $port).then(-> $sr {
my $client = IO::Socket::Async.connect($c-address, $port).then(-> $sr {
if $sr.status == Kept {
my $socket = $sr.result;
code($socket, $v);
Expand Down Expand Up @@ -162,7 +163,7 @@ $echoTap.close;
}

{
my $latin1server = IO::Socket::Async.listen($hostname, $port, :enc('latin-1'));
my $latin1server = IO::Socket::Async.listen($s-address, $port, :enc('latin-1'));
my $latin1Tap = $latin1server.tap(-> $c {
$c.Supply.tap(-> Str $msg { $c.print($msg).then({ $c.close }) });
});
Expand All @@ -173,7 +174,7 @@ $echoTap.close;
}

{
my $transcodeServer = IO::Socket::Async.listen($hostname, $port, :enc('utf-8'));
my $transcodeServer = IO::Socket::Async.listen($s-address, $port, :enc('utf-8'));
my $transcodeTap = $transcodeServer.tap(-> $c {
$c.Supply(:enc('latin-1')).tap(-> Str $msg { $c.print($msg).then({ $c.close }) });
});
Expand All @@ -188,7 +189,7 @@ $echoTap.close;
my $byteCountTap = $server.tap(-> $c {
$c.Supply(:bin).tap(-> Blob $b { $c.write("Öl,$b.elems()\n".encode('latin-1')).then({ $c.close }) });
});
my $latin1Client = await IO::Socket::Async.connect($hostname, $port, :enc('latin-1'));
my $latin1Client = await IO::Socket::Async.connect($c-address, $port, :enc('latin-1'));
my $received = Promise.new;
my $receivedStr = '';
$latin1Client.Supply.tap({ $receivedStr ~= $_ }, done => { $received.keep($receivedStr) });
Expand Down Expand Up @@ -235,8 +236,7 @@ $echoTap.close;
}

{
my $anotherPort = 6000;
my $badServer = IO::Socket::Async.listen($hostname, $anotherPort);
my $badServer = IO::Socket::Async.listen($s-address, 6000);
my $failed = Promise.new;
my $t1 = $badServer.tap();
my $t2 = $badServer.tap(quit => { $failed.keep });
Expand All @@ -245,8 +245,9 @@ $echoTap.close;
}

{
my $t = IO::Socket::Async.listen("localhost", 5007).tap: -> $conn { };
my $conn = await IO::Socket::Async.connect("localhost", 5007);
my $port = 5007;
my $t = IO::Socket::Async.listen($s-address, $port).tap: -> $conn { };
my $conn = await IO::Socket::Async.connect($c-address, $port);
lives-ok { for ^5 { $conn.close; sleep 0.05; } },
'Multiple close of an IO::Socket::Async silently coped with';
dies-ok { await $conn.write("42".encode("ascii")) },
Expand All @@ -257,7 +258,7 @@ $echoTap.close;
}

{
my $lis = IO::Socket::Async.listen("localhost", 0);
my $lis = IO::Socket::Async.listen($s-address, 0);

my @first-got;
my @second-got;
Expand Down Expand Up @@ -288,8 +289,8 @@ $echoTap.close;

my $fconn;
my $sconn;
lives-ok { $fconn = await IO::Socket::Async.connect("localhost", $first.socket-port.result) }, "can connect to first port on localhost";
lives-ok { $sconn = await IO::Socket::Async.connect($second.socket-host.result, $second.socket-port.result) }, "can connect to second port on given host and port";
lives-ok { $fconn = await IO::Socket::Async.connect($c-address, $first.socket-port.result) }, "can connect to first port on localhost";
lives-ok { $sconn = await IO::Socket::Async.connect($c-address, $second.socket-port.result) }, "can connect to second port on given host and port";

lives-ok { await $fconn.write("hello first".encode("ascii")) }, "send message to first connection";
lives-ok { await $sconn.write("hello second".encode("ascii")) }, "send message to second connection";
Expand All @@ -310,7 +311,7 @@ $echoTap.close;

# Rakudo Issue #2411
{
my $listen-socket = IO::Socket::Async.listen("127.0.0.1", 0);
my $listen-socket = IO::Socket::Async.listen($s-address, 0);
react {
my $listen-tap = do whenever $listen-socket -> $socket { … }
ok $listen-tap.defined, "listen tap is defined";
Expand Down
11 changes: 7 additions & 4 deletions S32-io/IO-Socket-INET.t
Expand Up @@ -3,6 +3,9 @@ use Test;

plan 28;

my $localhost = '0.0.0.0';
my $host = '127.0.0.1';

# test 2 does echo protocol - Internet RFC 862
do-test
# server
Expand Down Expand Up @@ -292,7 +295,7 @@ do-test
my $sync := Channel.new;
start {
my $server := IO::Socket::INET.new:
:localhost<localhost>, :0localport, :listen;
:$localhost, :0localport, :listen;
$sync.send: $server.localport;
my $client := $server.accept;
$client.print: "Test passed\n";
Expand All @@ -301,7 +304,7 @@ do-test
}
is IO::Socket::INET.new(
:!listen, # <-- testing on purpose with :!listen set
:host<localhost>, :port($sync.receive)
:$host, :port($sync.receive)
).get, 'Test passed', 'can connect as client when :$listen is set to False';
}

Expand All @@ -319,15 +322,15 @@ sub do-test(Block $b-server, Block $b-client) {
my $sync = Channel.new;
my $thread = Thread.start(
{
my $server = IO::Socket::INET.new(:localhost<localhost>, :localport(0), :listen);
my $server = IO::Socket::INET.new(:$localhost, :localport(0), :listen);

$sync.send($server.localport);

$b-server($server);
}
);

my $client = IO::Socket::INET.new(:host<localhost>, :port($sync.receive));
my $client = IO::Socket::INET.new(:$host, :port($sync.receive));
$b-client($client);
$thread.finish;
}
Expand Down
10 changes: 5 additions & 5 deletions S32-io/socket-fail-invalid-values.t
Expand Up @@ -2,7 +2,7 @@ use Test;

# RT #130473, #130475

constant HOST = 'localhost';
my $localhost = '0.0.0.0';

constant FAMILY_VALUE_TOO_LOW = -1;
constant FAMILY_VALUE_TOO_HIGH = 9999999;
Expand All @@ -26,23 +26,23 @@ done-testing;
sub port-too-low() {
my $listen = IO::Socket::INET.new(
:listen,
:localhost(HOST),
:$localhost,
:localport(PORT_VALUE_TOO_LOW),
);
}

sub port-too-high() {
my $listen = IO::Socket::INET.new(
:listen,
:localhost(HOST),
:$localhost,
:localport(PORT_VALUE_TOO_HIGH),
);
}

sub family-too-low() {
my $listen = IO::Socket::INET.new(
:listen,
:localhost(HOST),
:$localhost,
:localport(PORT_VALUE_VALID),
:family(FAMILY_VALUE_TOO_LOW),
);
Expand All @@ -51,7 +51,7 @@ sub family-too-low() {
sub family-too-high() {
my $listen = IO::Socket::INET.new(
:listen,
:localhost(HOST),
:$localhost,
:localport(PORT_VALUE_VALID),
:family(FAMILY_VALUE_TOO_HIGH),
);
Expand Down
23 changes: 13 additions & 10 deletions S32-io/socket-host-port-split.t
Expand Up @@ -3,28 +3,31 @@ use Test;

# RT #130474

constant HOST_PORT_IPV4 = '127.0.0.1:5014';
constant HOST_PORT_IPV6 = '[::1]:5016';

plan 2;

split-host-port :uri(HOST_PORT_IPV4), :family(PF_INET);
{
my $localhost = '0.0.0.0:5014';
my $host = '127.0.0.1:5014';
my $family = PF_INET;
split-host-port :$localhost, :$host, :$family;
}

#?rakudo skip 'Hangs on boxes without IPv6 support'
#?DOES 1
{
split-host-port :uri(HOST_PORT_IPV6), :family(PF_INET6);
my $localhost = '[::]:5014';
my $host = '[::1]:5014';
my $family = PF_INET6;
split-host-port :$localhost, :$host, :$family;
}

done-testing;

sub split-host-port(:$uri, :$family) {
sub split-host-port(:$localhost, :$host, :$family) {
my $c = Channel.new;
my $ready = Promise.new;

start {
my $listen = IO::Socket::INET.new(
:localhost($uri),
:$localhost,
:listen,
:$family,
);
Expand All @@ -47,7 +50,7 @@ sub split-host-port(:$uri, :$family) {
await $ready;

my $connection = IO::Socket::INET.new(
:host($uri),
:$host,
:$family,
);

Expand Down

0 comments on commit 9513102

Please sign in to comment.