From 8d417f3e25f5cf3a509465f7faffd0ff6072de32 Mon Sep 17 00:00:00 2001 From: Engin Kayraklioglu Date: Tue, 7 Jan 2025 11:59:24 -0800 Subject: [PATCH] Restore the ability to use reals for timeout in the Socket module Signed-off-by: Engin Kayraklioglu --- modules/packages/Socket.chpl | 21 ++++++++---- .../packages/Socket/connecttimeout.chpl | 34 +++++++++++++++++++ .../packages/Socket/connecttimeout.good | 8 +++++ .../Socket/ipv6/connect_timeout_ipv6.chpl | 33 ++++++++++++++++++ .../Socket/ipv6/connect_timeout_ipv6.good | 8 +++++ 5 files changed, 97 insertions(+), 7 deletions(-) diff --git a/modules/packages/Socket.chpl b/modules/packages/Socket.chpl index 9ef917600490..29e01a83c583 100644 --- a/modules/packages/Socket.chpl +++ b/modules/packages/Socket.chpl @@ -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)); } /* @@ -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)); } /* @@ -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)); } /* @@ -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)); } /* @@ -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); } /* @@ -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 @@ -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 @@ -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); + +} } diff --git a/test/library/packages/Socket/connecttimeout.chpl b/test/library/packages/Socket/connecttimeout.chpl index ef0f4244b977..562de79c3beb 100644 --- a/test/library/packages/Socket/connecttimeout.chpl +++ b/test/library/packages/Socket/connecttimeout.chpl @@ -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"; @@ -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(); diff --git a/test/library/packages/Socket/connecttimeout.good b/test/library/packages/Socket/connecttimeout.good index 38318054e31f..2a213265e819 100644 --- a/test/library/packages/Socket/connecttimeout.good +++ b/test/library/packages/Socket/connecttimeout.good @@ -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 +====================================================================== +---------------------------------------------------------------------- diff --git a/test/library/packages/Socket/ipv6/connect_timeout_ipv6.chpl b/test/library/packages/Socket/ipv6/connect_timeout_ipv6.chpl index 3caa9adb58b3..24506e6bfa8e 100644 --- a/test/library/packages/Socket/ipv6/connect_timeout_ipv6.chpl +++ b/test/library/packages/Socket/ipv6/connect_timeout_ipv6.chpl @@ -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"; @@ -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(); diff --git a/test/library/packages/Socket/ipv6/connect_timeout_ipv6.good b/test/library/packages/Socket/ipv6/connect_timeout_ipv6.good index 5f6607eacd68..7c94a17303dc 100644 --- a/test/library/packages/Socket/ipv6/connect_timeout_ipv6.good +++ b/test/library/packages/Socket/ipv6/connect_timeout_ipv6.good @@ -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 +====================================================================== +----------------------------------------------------------------------