Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore the ability to use reals for timeout in the Socket module #26486

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions modules/packages/Socket.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ proc tcpListener.accept(in timeout: struct_timeval = indefiniteTimeout):tcpConn
}

proc tcpListener.accept(timeout: real): tcpConn throws {
return this.accept(timeout:struct_timeval);
return this.accept(realToTimeVal(timeout));
}

/*
Expand Down Expand Up @@ -884,7 +884,7 @@ proc connect(const ref address: ipAddr, in timeout = indefiniteTimeout): tcpConn
}

proc connect(const ref address: ipAddr, in timeout:real): tcpConn throws {
return connect(address, timeout:struct_timeval);
return connect(address, realToTimeVal(timeout));
}

/*
Expand Down Expand Up @@ -945,7 +945,7 @@ proc connect(in host: string, in service: string, family: IPFamily = IPFamily.IP

proc connect(in host: string, in service: string, family: IPFamily = IPFamily.IPUnspec,
timeout:real): tcpConn throws {
return connect(host, service, family, timeout:struct_timeval);
return connect(host, service, family, realToTimeVal(timeout));
}

/*
Expand Down Expand Up @@ -981,7 +981,7 @@ proc connect(in host: string, in port: uint(16), family: IPFamily = IPFamily.IPU

proc connect(in host: string, in port: uint(16), family: IPFamily = IPFamily.IPUnspec,
timeout:real): tcpConn throws {
return connect(host, port, family, timeout:struct_timeval);
return connect(host, port, family, realToTimeVal(timeout));
}

/*
Expand Down Expand Up @@ -1102,7 +1102,7 @@ proc udpSocket.recvfrom(bufferLen: int, in timeout = indefiniteTimeout,
}

proc udpSocket.recvfrom(bufferLen: int, timeout: real, flags:c_int = 0):(bytes, ipAddr) throws {
return this.recvfrom(bufferLen, timeout:struct_timeval, flags);
return this.recvfrom(bufferLen, realToTimeVal(timeout), flags);
}

/*
Expand Down Expand Up @@ -1133,7 +1133,7 @@ proc udpSocket.recv(bufferLen: int, in timeout = indefiniteTimeout) throws {
}

proc udpSocket.recv(bufferLen: int, timeout: real) throws {
return this.recv(bufferLen, timeout:struct_timeval);
return this.recv(bufferLen, realToTimeVal(timeout));
}

@chpldoc.nodoc
Expand Down Expand Up @@ -1212,7 +1212,7 @@ proc udpSocket.send(data: bytes, in address: ipAddr,
}

proc udpSocket.send(data: bytes, in address: ipAddr, timeout: real) throws {
return this.send(data, address, timeout:struct_timeval);
return this.send(data, address, realToTimeVal(timeout));
}

@chpldoc.nodoc
Expand Down Expand Up @@ -1596,4 +1596,11 @@ proc deinit() {
event_base_free(event_loop_base);
libevent_global_shutdown();
}

private proc realToTimeVal(x: real) {
const sec = x:int;
const usec = ((x-sec)*1_000_000):int;
return new struct_timeval(sec, usec);

}
}
34 changes: 34 additions & 0 deletions test/library/packages/Socket/connecttimeout.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ proc test_success_ipv4(test: borrowed Test) throws {
}
}

proc test_success_ipv4_real(test: borrowed Test) throws {
var port:uint(16) = 8811;
var host = "127.0.0.1";
var address = ipAddr.ipv4(IPv4Localhost, port);
var server = listen(address);
sync {
begin {
var conn = server.accept();
}
var conn = connect(address, 2);
test.assertEqual(conn.addr, address);
}
}

proc test_fail_backlog_ipv4(test: borrowed Test) throws {
var port:uint(16) = 7711;
var host = "127.0.0.1";
Expand All @@ -36,4 +50,24 @@ proc test_fail_backlog_ipv4(test: borrowed Test) throws {
test.assertNotEqual(failures, 0);
}

proc test_fail_backlog_ipv4_real(test: borrowed Test) throws {
var port:uint(16) = 7711;
var host = "127.0.0.1";
var address = ipAddr.ipv4(IPv4Localhost, port);
var server = listen(address, backlog = 6);

var failures = 0;
coforall x in 1..20 with (+ reduce failures) do {
try {
var conn = connect(address, 1);
conn.close();
}
catch e {
failures += 1;
}
}

test.assertNotEqual(failures, 0);
}

UnitTest.main();
8 changes: 8 additions & 0 deletions test/library/packages/Socket/connecttimeout.good
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ test_success_ipv4()
Flavour: OK
======================================================================
----------------------------------------------------------------------
test_success_ipv4_real()
Flavour: OK
======================================================================
----------------------------------------------------------------------
test_fail_backlog_ipv4()
Flavour: OK
======================================================================
----------------------------------------------------------------------
test_fail_backlog_ipv4_real()
Flavour: OK
======================================================================
----------------------------------------------------------------------
33 changes: 33 additions & 0 deletions test/library/packages/Socket/ipv6/connect_timeout_ipv6.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ proc test_success_ipv6(test: borrowed Test) throws {
}
}

proc test_success_ipv6_real(test: borrowed Test) throws {
var port:uint(16) = 8822;
var host = "::1";
var address = ipAddr.ipv6(IPv6Localhost, port);
var server = listen(address);
sync {
begin {
var conn = server.accept();
}
var conn = connect(address, 2);
test.assertEqual(conn.addr, address);
}
}

proc test_fail_backlog_ipv6(test: borrowed Test) throws {
var port:uint(16) = 7722;
var host = "::1";
Expand All @@ -35,4 +49,23 @@ proc test_fail_backlog_ipv6(test: borrowed Test) throws {
test.assertNotEqual(failures, 0);
}

proc test_fail_backlog_ipv6_real(test: borrowed Test) throws {
var port:uint(16) = 7722;
var host = "::1";
var address = ipAddr.ipv6(IPv6Localhost, port);
var server = listen(address, backlog = 6);

var failures = 0;
coforall x in 1..20 with (+ reduce failures) do {
try {
var conn = connect(address, 1);
}
catch e {
failures += 1;
}
}

test.assertNotEqual(failures, 0);
}

UnitTest.main();
8 changes: 8 additions & 0 deletions test/library/packages/Socket/ipv6/connect_timeout_ipv6.good
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ test_success_ipv6()
Flavour: OK
======================================================================
----------------------------------------------------------------------
test_success_ipv6_real()
Flavour: OK
======================================================================
----------------------------------------------------------------------
test_fail_backlog_ipv6()
Flavour: OK
======================================================================
----------------------------------------------------------------------
test_fail_backlog_ipv6_real()
Flavour: OK
======================================================================
----------------------------------------------------------------------
Loading