Skip to content

Commit

Permalink
[socket] adapt to more p5 like IO::Socket::INET implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
moritz committed Mar 17, 2011
1 parent 3777a9c commit 15c1b74
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 36 deletions.
35 changes: 12 additions & 23 deletions S32-io/IO-Socket-INET.pl
Expand Up @@ -12,16 +12,15 @@
sub SOCK_STREAM { 1 } # constant SOCK_STREAM = 1; # but what name and directory?
sub TCP { 6 } # constant TCP = 6;
my ( $test, $port, $server_or_client ) = @*ARGS;
$port = $port.Int;
my $host = '127.0.0.1';

given $test {

when 2 { # test number 2 - echo protocol, RFC 862
if $server_or_client eq 'server' {
# warn "SERVER TEST=$test PORT=$port";
my $server = IO::Socket::INET.socket( PF_INET, SOCK_STREAM, TCP );
$server.bind( $host, $port.Int );
$server.listen(); # should accept max queue size parameter
my $server = IO::Socket::INET.new(:localhost($host), :localport($port), :listen);
# warn "SERVER LISTENING";
my $fd = open( 't/spec/S32-io/server-ready-flag', :w );
$fd.close();
Expand All @@ -38,9 +37,8 @@
# warn "CLIENT TEST=$test PORT=$port";
# avoid a race condition, where the client tries to
# open() before the server gets to accept().
until 't/spec/S32-io/server-ready-flag'.IO ~~ :e { sleep(1) }
my $client = IO::Socket::INET.new;
$client.open( $host, $port.Int );
until 't/spec/S32-io/server-ready-flag'.IO ~~ :e { sleep(0.1) }
my $client = IO::Socket::INET.new(:$host, :$port);
# warn "CLIENT OPENED";
$client.send( [~] '0'..'9', 'a'..'z' );
# warn "CLIENT SENT";
Expand All @@ -55,9 +53,7 @@
when 3 { # test number 3 - discard protocol, RFC 863
if $server_or_client eq 'server' {
# warn "SERVER TEST=$test PORT=$port";
my $server = IO::Socket::INET.socket( PF_INET, SOCK_STREAM, TCP );
$server.bind( $host, $port.Int );
$server.listen(); # should accept max queue size parameter
my $server = IO::Socket::INET.new(:localhost($host), :localport($port), :listen);
# warn "SERVER LISTENING";
while my $client = $server.accept() {
# warn "SERVER ACCEPTED";
Expand All @@ -71,8 +67,7 @@
# avoid a race condition, where the client tries to
# open() before the server gets to accept().
sleep 1; # crude, sorry
my $client = IO::Socket::INET.new;
$client.open( $host, $port.Int );
my $client = IO::Socket::INET.new(:$host, :$port);
# warn "CLIENT OPENED";
$client.send( [~] '0'..'9', 'a'..'z' );
# warn "CLIENT SENT";
Expand All @@ -86,9 +81,7 @@

when 4 { # test number 4 - recv with parameter
if $server_or_client eq 'server' {
my $server = IO::Socket::INET.socket(PF_INET, SOCK_STREAM, TCP);
$server.bind($host, $port.Int);
$server.listen();
my $server = IO::Socket::INET.new(:localhost($host), :localport($port), :listen);
my $fd = open( 't/spec/S32-io/server-ready-flag', :w );
$fd.close();
while my $client = $server.accept() {
Expand All @@ -99,9 +92,8 @@
}
}
else {
my $sock = IO::Socket::INET.new;
until 't/spec/S32-io/server-ready-flag'.IO ~~ :e { sleep(1) }
$sock.open($host, $port.Int);
until 't/spec/S32-io/server-ready-flag'.IO ~~ :e { sleep(0.1) }
my $sock = IO::Socket::INET.new(:$host, :$port);
# Tests that if we do not receive all the data available
# it is buffered correctly for when we do request it
say $sock.recv(7); # 0123456
Expand All @@ -124,9 +116,7 @@

when 5 { # test number 5 - get()
if $server_or_client eq 'server' {
my $server = IO::Socket::INET.socket(PF_INET, SOCK_STREAM, TCP);
$server.bind($host, $port.Int);
$server.listen();
my $server = IO::Socket::INET.new(:localhost($host), :localport($port), :listen);
my $fd = open('t/spec/S32-io/server-ready-flag', :w);
$fd.close();
while my $client = $server.accept() {
Expand All @@ -142,9 +132,8 @@
$client.close();
}
} else { # client
my $sock = IO::Socket::INET.new;
until 't/spec/S32-io/server-ready-flag'.IO ~~ :e { sleep(1) }
$sock.open($host, $port.Int);
until 't/spec/S32-io/server-ready-flag'.IO ~~ :e { sleep(0.1) }
my $sock = IO::Socket::INET.new(:$host, :$port);
say $sock.get();
say $sock.get();
$sock.input-line-separator = "\r\n";
Expand Down
17 changes: 4 additions & 13 deletions S32-io/IO-Socket-INET.t
@@ -1,7 +1,7 @@
use v6;
use Test;

plan 18;
plan 17;

# L<S32::IO/IO::Socket::INET>

Expand Down Expand Up @@ -44,22 +44,13 @@ if $received ~~ $netstat_pat { @ports = $/.list; } # development complete
# sequentially search for the first unused port
my $port = 1024;
while $port < 65535 && $port==any(@ports) { $port++; }
if $port > 65535 {
diag "no free port; abortin";
if $port >= 65535 {
diag "no free port; aborting";
skip_rest 'No port free - cannot test';
exit 0;
}
diag "Testing on port $port";

# test 1 creates a TCP socket but does not use it.
# use Perl 5 style subs for constants until 'constant' works again
sub PF_INET { 2 } # constant PF_INET = 2; # these should move into a file,
sub SOCK_STREAM { 1 } # constant SOCK_STREAM = 1; # but what name and directory?
sub TCP { 6 } # constant TCP = 6;
my $server = IO::Socket::INET.socket( PF_INET, SOCK_STREAM, TCP );
isa_ok $server, IO::Socket::INET;
# Do not bind to this socket in the parent process, that would prevent a
# child process from using it.

if $*OS eq any <linux darwin solaris MSWin32> { # please add more valid OS names

Expand Down Expand Up @@ -105,7 +96,7 @@ if $*OS eq any <linux darwin solaris MSWin32> { # please add more valid OS names
is $expected[$i++], chr(0xbabe), "combined the bytes form {chr 0xbabe}";
is $expected[$i++], 3, '... which is 3 bytes';

#?rakudo 7 skip
#?rakudo 7 skip 'NYI'
# test 5 tests get()
if $is-win {
$received = qqx{t\\spec\\S32-io\\IO-Socket-INET.bat 5 $port};
Expand Down

0 comments on commit 15c1b74

Please sign in to comment.