1. Alex Prengère
  2. pypy

Commits

Brian Kearns  committed 1d978ce

rsocket connect() with timeout: use getsockopt() to get result rather than calling connect() a second time, enhance tests
(inspired by cpython changeset 44789:3c9ccc32da9a)

  • Participants
  • Parent commits 073896d
  • Branches default

Comments (0)

Files changed (2)

File pypy/rlib/rsocket.py

View file
             errno = _c.geterrno()
             if self.timeout > 0.0 and res < 0 and errno == _c.EINPROGRESS:
                 timeout = self._select(True)
-                errno = _c.geterrno()
                 if timeout == 0:
-                    addr = address.lock()
-                    res = _c.socketconnect(self.fd, addr, address.addrlen)
-                    address.unlock()
-                    if res < 0:
-                        errno = _c.geterrno()
-                        if errno == _c.EISCONN:
-                            res = 0
+                    res = self.getsockopt_int(_c.SOL_SOCKET, _c.SO_ERROR)
+                    if (res == _c.EISCONN):
+                        res = 0
+                    errno = res
                 elif timeout == -1:
-                    return (errno, False)
+                    return (_c.geterrno(), False)
                 else:
                     return (_c.EWOULDBLOCK, True)
 
-            if res == 0:
-                errno = 0
-            return (errno, False)
+            if res < 0:
+                res = errno
+            return (res, False)
         
     def connect(self, address):
         """Connect the socket to a remote address."""

File pypy/rlib/test/test_rsocket.py

View file
     assert addr.eq(sock.getsockname())
     sock.listen(1)
     s2 = RSocket(AF_INET, SOCK_STREAM)
+    s2.settimeout(10.0) # test one side with timeouts so select is used, shouldn't affect test
     def connecting():
         s2.connect(addr)
         lock.release()
     addr = INETAddress('127.0.0.1', port)
     assert addr.eq(s1.getsockname())
     s2 = RSocket(AF_INET, SOCK_DGRAM)
+    s2.settimeout(10.0) # test one side with timeouts so select is used, shouldn't affect test
     s2.bind(INETAddress('127.0.0.1', INADDR_ANY))
     addr2 = s2.getsockname()
 
     if errcodesok:
         assert err in (errno.ECONNREFUSED, errno.EADDRNOTAVAIL)
 
+def test_connect_with_timeout_fail():
+    s = RSocket()
+    s.settimeout(0.1)
+    with py.test.raises(SocketTimeout):
+        s.connect(INETAddress('240.240.240.240', 12345))
+
+def test_connect_with_timeout_succeed():
+    s = RSocket()
+    s.settimeout(10.0)
+    s.connect(INETAddress('python.org', 80))
+    s.close()
 
 def test_getsetsockopt():
     import struct