Permalink
Browse files

[socket] adapt to more p5 like IO::Socket::INET implementation

  • Loading branch information...
1 parent 3777a9c commit 15c1b74c8f950d5db304a735cdb5bc476a1f136a @moritz moritz committed Mar 17, 2011
Showing with 16 additions and 36 deletions.
  1. +12 −23 S32-io/IO-Socket-INET.pl
  2. +4 −13 S32-io/IO-Socket-INET.t
View
@@ -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();
@@ -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";
@@ -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";
@@ -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";
@@ -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() {
@@ -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
@@ -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() {
@@ -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";
View
@@ -1,7 +1,7 @@
use v6;
use Test;
-plan 18;
+plan 17;
# L<S32::IO/IO::Socket::INET>
@@ -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
@@ -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};

0 comments on commit 15c1b74

Please sign in to comment.