Index: Lib/test/test_timeout.py =================================================================== --- Lib/test/test_timeout.py (revision 54211) +++ Lib/test/test_timeout.py (arbetskopia) @@ -6,6 +6,7 @@ # This requires the 'network' resource as given on the regrtest command line. skip_expected = not test_support.is_resource_enabled('network') +import errno import time import socket @@ -100,96 +101,90 @@ def setUp(self): self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.addr_remote = ('www.python.org.', 80) - self.addr_local = ('127.0.0.1', 25339) + self.cli_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.addr_local = '127.0.0.1', 0 def tearDown(self): self.sock.close() + self.cli_sock.close() - def testConnectTimeout(self): - # Test connect() timeout - _timeout = 0.001 - self.sock.settimeout(_timeout) + def sockOperation(self, count, timeout, method, *args): + """ + Test the specified socket method. - # If we are too close to www.python.org, this test will fail. - # Pick a host that should be farther away. - if (socket.getfqdn().split('.')[-2:] == ['python', 'org'] or - socket.getfqdn().split('.')[-2:-1] == ['xs4all']): - self.addr_remote = ('tut.fi', 80) + The method is run count times and must raise a socket.error + within timeout + self.fuzz seconds, preferably with the + correct errno, otherwise test fails. + """ + t1 = time.time() + # FIXME: 0 raised on Win (see BUG #708927) + good_errnos = (0, errno.EAGAIN, errno.EWOULDBLOCK, errno.EALREADY) + for i in range(count): + try: + method(*args) + except socket.error, args: + if isinstance(args[0], basestring) or args[0] in good_errnos: + break + raise + else: + self.fail('Expected exception was not raised.') + t2 = time.time() + delta = t2 - t1 + fmt = "Timeout (%g) is more than %g seconds more than expected (%g" + str = fmt % (delta, self.fuzz, timeout) + self.assert_(delta < timeout + self.fuzz, str) + - _t1 = time.time() - self.failUnlessRaises(socket.error, self.sock.connect, - self.addr_remote) - _t2 = time.time() + def testConnectTimeout(self): + self.sock.settimeout(0.001) + # Must not use port 80 due to proxies. + remote_addr = 'www.google.com', 21 + self.sockOperation(1, 0.001, self.sock.connect, remote_addr) - _delta = abs(_t1 - _t2) - self.assert_(_delta < _timeout + self.fuzz, - "timeout (%g) is more than %g seconds more than expected (%g)" - %(_delta, self.fuzz, _timeout)) - def testRecvTimeout(self): - # Test recv() timeout - _timeout = 0.02 - self.sock.connect(self.addr_remote) - self.sock.settimeout(_timeout) + # Use a local UDP socket. Only thing we can be sure of that we + # can connect to and only thing we for sure knows should time + # out. + self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + self.sock.settimeout(4) + self.sock.bind(self.addr_local) + self.sockOperation(1, 4, self.sock.recv, 1024) - _t1 = time.time() - self.failUnlessRaises(socket.error, self.sock.recv, 1024) - _t2 = time.time() - - _delta = abs(_t1 - _t2) - self.assert_(_delta < _timeout + self.fuzz, - "timeout (%g) is %g seconds more than expected (%g)" - %(_delta, self.fuzz, _timeout)) - def testAcceptTimeout(self): - # Test accept() timeout - _timeout = 2 - self.sock.settimeout(_timeout) self.sock.bind(self.addr_local) self.sock.listen(5) + self.sock.settimeout(2) + self.sockOperation(1, 2, self.sock.accept) - _t1 = time.time() - self.failUnlessRaises(socket.error, self.sock.accept) - _t2 = time.time() - - _delta = abs(_t1 - _t2) - self.assert_(_delta < _timeout + self.fuzz, - "timeout (%g) is %g seconds more than expected (%g)" - %(_delta, self.fuzz, _timeout)) - def testRecvfromTimeout(self): - # Test recvfrom() timeout - _timeout = 2 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - self.sock.settimeout(_timeout) + self.sock.settimeout(2) self.sock.bind(self.addr_local) - - _t1 = time.time() - self.failUnlessRaises(socket.error, self.sock.recvfrom, 8192) - _t2 = time.time() - - _delta = abs(_t1 - _t2) - self.assert_(_delta < _timeout + self.fuzz, - "timeout (%g) is %g seconds more than expected (%g)" - %(_delta, self.fuzz, _timeout)) - + self.sockOperation(1, 2, self.sock.recvfrom, 8192) + def testSend(self): - # Test send() timeout - # couldn't figure out how to test it - pass + self.sock.bind(self.addr_local) + self.sock.listen(1) + self.cli_sock.connect(self.sock.getsockname()) + self.cli_sock.settimeout(4) + self.sockOperation(100, 4, self.cli_sock.send, "X" * 200000) def testSendto(self): - # Test sendto() timeout - # couldn't figure out how to test it - pass + self.sock.bind(self.addr_local) + self.sock.listen(1) + self.cli_sock.connect(self.sock.getsockname()) + self.cli_sock.settimeout(4) + # The address argument is ignored since we already connected. + self.sockOperation(100, 4, self.cli_sock.sendto, "X" * 200000, + self.sock.getsockname()) def testSendall(self): - # Test sendall() timeout - # couldn't figure out how to test it - pass + self.sock.bind(self.addr_local) + self.sock.listen(1) + self.cli_sock.connect(self.sock.getsockname()) + self.cli_sock.settimeout(4) + self.sockOperation(100, 4, self.cli_sock.sendall, "X" * 200000) - def test_main(): test_support.requires('network') test_support.run_unittest(CreationTestCase, TimeoutTestCase)