Skip to content

Commit

Permalink
Implement spec tests for .native-descriptor on async sockets
Browse files Browse the repository at this point in the history
Fixes Raku#490
  • Loading branch information
Kaiepi committed Nov 4, 2018
1 parent cc2bc13 commit 2f5a992
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
46 changes: 26 additions & 20 deletions S32-io/IO-Socket-Async-UDP.t
@@ -1,45 +1,51 @@
use v6;
use Test;

plan 3;
plan 5;

my $hostname = 'localhost';
my $port = 5001;

{
my $sock = IO::Socket::Async.bind-udp($hostname, $port);
my $s = IO::Socket::Async.bind-udp($hostname, $port);
dies-ok { IO::Socket::Async.bind-udp($hostname, $port) },
'Error on trying to re-use port with UDP bind';
$sock.close;
$s.close;
}

# Promise used to check listener received the correct data.
my $rec-prom;

# Listener
{
my $sock = IO::Socket::Async.bind-udp($hostname, $port);
my $tap = $sock.Supply.tap: -> $chars {
if $chars.chars > 0 {
$rec-prom.keep($chars);
}
my Promise $rec-prom;
my $s = IO::Socket::Async.bind-udp($hostname, $port);
my $tap = $s.Supply.tap: -> $chars {
if $chars.chars > 0 {
$rec-prom.keep($chars);
}
}

# Client print-to
{
$rec-prom = Promise.new;
my $sock = IO::Socket::Async.udp();
$sock.print-to($hostname, $port, "Unusually Dubious Protocol");
$rec-prom .= new;
my $c = IO::Socket::Async.udp();
$c.print-to($hostname, $port, "Unusually Dubious Protocol");
is $rec-prom.result, "Unusually Dubious Protocol", "Sent/received data with UDP (print)";
$sock.close;
$c.close;
}

# Client write-to
{
$rec-prom = Promise.new;
my $sock = IO::Socket::Async.udp();
$sock.write-to($hostname, $port, "Unhelpful Dataloss Protocol".encode('ascii'));
$rec-prom .= new;
my $c = IO::Socket::Async.udp();
$c.write-to($hostname, $port, "Unhelpful Dataloss Protocol".encode('ascii'));
is $rec-prom.result, "Unhelpful Dataloss Protocol", "Sent/received data with UDP (write)";
$sock.close;
$c.close;
}

#?rakudo,jvm skip 'nqp::filenofh not yet implemented on the JVM'
{
my $c = IO::Socket::Async.udp();
cmp-ok $s.native-descriptor, '>', -1, 'server file descriptor makes sense';
cmp-ok $c.native-descriptor, '>', -1, 'client file descriptor makes sense';
$c.close;
}

$s.close;
21 changes: 20 additions & 1 deletion S32-io/IO-Socket-Async.t
@@ -1,7 +1,7 @@
use v6;
use Test;

plan 38;
plan 41;

my $hostname = 'localhost';
my $port = 5000;
Expand Down Expand Up @@ -292,3 +292,22 @@ for '127.0.0.1', '::1' -> $host {
is @first-got.join(""), "hello first", "first server socket got the right message";
is @second-got.join(""), "hello second", "second server socket got the right message";
}

#?rakudo,jvm skip 'nqp::filenofh not yet implemented on the JVM'
{
my Promise $p .= new;
my $v = $p.vow;

my $s = IO::Socket::Async.listen('localhost', 0).tap(-> $c {
cmp-ok $c.native-descriptor, '>', -1, 'server peer file descriptor makes sense';
$v.keep: 1;
});
cmp-ok $s.native-descriptor, '>', -1, 'server file descriptor makes sense';

my $c = await IO::Socket::Async.connect: 'localhost', await $s.socket-port;
cmp-ok $c.native-descriptor, '>', -1, 'client file descriptor makes sense';

await $p;
$s.close;
$c.close;
}

0 comments on commit 2f5a992

Please sign in to comment.