summaryrefslogtreecommitdiff
path: root/Lib/test/test_socket.py
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2010-09-07 21:40:25 +0000
committerAntoine Pitrou <solipsis@pitrou.net>2010-09-07 21:40:25 +0000
commitc818ed4d61b12a702b2af813cd0ac4839faf497f (patch)
treeba4a83dcee870d847a7f0f8c2a91200c4b26304a /Lib/test/test_socket.py
parentd47a68716e7dfdf79a19129a9a8369b41a173f27 (diff)
downloadcpython-git-c818ed4d61b12a702b2af813cd0ac4839faf497f.tar.gz
Merged revisions 84597-84599 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r84597 | antoine.pitrou | 2010-09-07 22:42:19 +0200 (mar., 07 sept. 2010) | 5 lines Issue #8574: better implementation of test.support.transient_internet(). Original patch by Victor. ........ r84598 | antoine.pitrou | 2010-09-07 23:05:49 +0200 (mar., 07 sept. 2010) | 6 lines Issue #9792: In case of connection failure, socket.create_connection() would swallow the exception and raise a new one, making it impossible to fetch the original errno, or to filter timeout errors. Now the original error is re-raised. ........ r84599 | antoine.pitrou | 2010-09-07 23:09:09 +0200 (mar., 07 sept. 2010) | 4 lines Improve transient_internet() again to detect more network errors, and use it in test_robotparser. Fixes #8574. ........
Diffstat (limited to 'Lib/test/test_socket.py')
-rw-r--r--Lib/test/test_socket.py41
1 files changed, 36 insertions, 5 deletions
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index da8e155b0e..5b5d4fb237 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -12,6 +12,7 @@ import Queue
import sys
import os
import array
+import contextlib
from weakref import proxy
import signal
@@ -1048,12 +1049,42 @@ class BasicTCPTest2(NetworkConnectionTest, BasicTCPTest):
"""
class NetworkConnectionNoServer(unittest.TestCase):
- def testWithoutServer(self):
+ class MockSocket(socket.socket):
+ def connect(self, *args):
+ raise socket.timeout('timed out')
+
+ @contextlib.contextmanager
+ def mocked_socket_module(self):
+ """Return a socket which times out on connect"""
+ old_socket = socket.socket
+ socket.socket = self.MockSocket
+ try:
+ yield
+ finally:
+ socket.socket = old_socket
+
+ def test_connect(self):
port = test_support.find_unused_port()
- self.assertRaises(
- socket.error,
- lambda: socket.create_connection((HOST, port))
- )
+ cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ with self.assertRaises(socket.error) as cm:
+ cli.connect((HOST, port))
+ self.assertEqual(cm.exception.errno, errno.ECONNREFUSED)
+
+ def test_create_connection(self):
+ # Issue #9792: errors raised by create_connection() should have
+ # a proper errno attribute.
+ port = test_support.find_unused_port()
+ with self.assertRaises(socket.error) as cm:
+ socket.create_connection((HOST, port))
+ self.assertEqual(cm.exception.errno, errno.ECONNREFUSED)
+
+ def test_create_connection_timeout(self):
+ # Issue #9792: create_connection() should not recast timeout errors
+ # as generic socket errors.
+ with self.mocked_socket_module():
+ with self.assertRaises(socket.timeout):
+ socket.create_connection((HOST, 1234))
+
@unittest.skipUnless(thread, 'Threading required for this test.')
class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):