mirror of
https://github.com/python/cpython.git
synced 2025-07-31 23:23:11 +00:00

svn+ssh://pythondev@svn.python.org/python/trunk ........ r61913 | benjamin.peterson | 2008-03-25 22:14:42 +0100 (Tue, 25 Mar 2008) | 2 lines Merged the ACKS from py3k ........ r61915 | thomas.heller | 2008-03-25 22:18:39 +0100 (Tue, 25 Mar 2008) | 1 line Make _ctypes.c PY_SSIZE_T_CLEAN. ........ r61916 | benjamin.peterson | 2008-03-25 22:55:50 +0100 (Tue, 25 Mar 2008) | 3 lines Opps! I merged the revisions, but forgot to add the header to ACKS ........ r61918 | andrew.kuchling | 2008-03-26 01:16:50 +0100 (Wed, 26 Mar 2008) | 1 line Minor docstring typos ........ r61919 | andrew.kuchling | 2008-03-26 01:30:02 +0100 (Wed, 26 Mar 2008) | 1 line Add various items ........ r61922 | neal.norwitz | 2008-03-26 05:55:51 +0100 (Wed, 26 Mar 2008) | 6 lines Try to get this test to be less flaky. It was failing sometimes because the connect would succeed before the timeout occurred. Try using an address and port that hopefully doesn't exist to ensure we get no response. If this doesn't work, we can use a public address close to python.org and hopefully that address never gets taken. ........ r61923 | jerry.seutter | 2008-03-26 06:03:03 +0100 (Wed, 26 Mar 2008) | 1 line Changed test so it no longer runs as a side effect of importing. ........ r61924 | neal.norwitz | 2008-03-26 06:19:41 +0100 (Wed, 26 Mar 2008) | 5 lines Ensure that the mailbox is closed to prevent problems on Windows with removing an open file. This doesn't seem to be a problem in 2.6, but that appears to be somewhat accidental (specific to reference counting). When this gets merged to 3.0, it will make the 3.0 code simpler. ........ r61925 | jerry.seutter | 2008-03-26 06:32:51 +0100 (Wed, 26 Mar 2008) | 1 line Changed test so it no longer runs as a side effect of importing. ........ r61926 | jerry.seutter | 2008-03-26 06:58:14 +0100 (Wed, 26 Mar 2008) | 1 line Changed test so it no longer runs as a side effect of importing. ........ r61928 | georg.brandl | 2008-03-26 10:04:36 +0100 (Wed, 26 Mar 2008) | 2 lines Add Josiah. ........ r61929 | georg.brandl | 2008-03-26 10:32:46 +0100 (Wed, 26 Mar 2008) | 2 lines Add an example for an RFC 822 continuation. ........ r61931 | benjamin.peterson | 2008-03-26 12:57:47 +0100 (Wed, 26 Mar 2008) | 2 lines Added help options to PDB ........ r61935 | christian.heimes | 2008-03-26 13:32:49 +0100 (Wed, 26 Mar 2008) | 1 line Prepare integration of bytearray backport branch ........ r61938 | christian.heimes | 2008-03-26 13:50:43 +0100 (Wed, 26 Mar 2008) | 3 lines Removed merge tracking for "svnmerge" for svn+ssh://pythondev@svn.python.org/python/branches/trunk-bytearray ........ r61943 | georg.brandl | 2008-03-26 13:57:47 +0100 (Wed, 26 Mar 2008) | 2 lines Fix and simplify error handling, silencing a compiler warning. ........
198 lines
6.6 KiB
Python
198 lines
6.6 KiB
Python
"""Unit tests for socket timeout feature."""
|
|
|
|
import unittest
|
|
from test import test_support
|
|
|
|
# This requires the 'network' resource as given on the regrtest command line.
|
|
skip_expected = not test_support.is_resource_enabled('network')
|
|
|
|
import time
|
|
import socket
|
|
|
|
|
|
class CreationTestCase(unittest.TestCase):
|
|
"""Test case for socket.gettimeout() and socket.settimeout()"""
|
|
|
|
def setUp(self):
|
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
def tearDown(self):
|
|
self.sock.close()
|
|
|
|
def testObjectCreation(self):
|
|
# Test Socket creation
|
|
self.assertEqual(self.sock.gettimeout(), None,
|
|
"timeout not disabled by default")
|
|
|
|
def testFloatReturnValue(self):
|
|
# Test return value of gettimeout()
|
|
self.sock.settimeout(7.345)
|
|
self.assertEqual(self.sock.gettimeout(), 7.345)
|
|
|
|
self.sock.settimeout(3)
|
|
self.assertEqual(self.sock.gettimeout(), 3)
|
|
|
|
self.sock.settimeout(None)
|
|
self.assertEqual(self.sock.gettimeout(), None)
|
|
|
|
def testReturnType(self):
|
|
# Test return type of gettimeout()
|
|
self.sock.settimeout(1)
|
|
self.assertEqual(type(self.sock.gettimeout()), type(1.0))
|
|
|
|
self.sock.settimeout(3.9)
|
|
self.assertEqual(type(self.sock.gettimeout()), type(1.0))
|
|
|
|
def testTypeCheck(self):
|
|
# Test type checking by settimeout()
|
|
self.sock.settimeout(0)
|
|
self.sock.settimeout(0)
|
|
self.sock.settimeout(0.0)
|
|
self.sock.settimeout(None)
|
|
self.assertRaises(TypeError, self.sock.settimeout, "")
|
|
self.assertRaises(TypeError, self.sock.settimeout, "")
|
|
self.assertRaises(TypeError, self.sock.settimeout, ())
|
|
self.assertRaises(TypeError, self.sock.settimeout, [])
|
|
self.assertRaises(TypeError, self.sock.settimeout, {})
|
|
self.assertRaises(TypeError, self.sock.settimeout, 0j)
|
|
|
|
def testRangeCheck(self):
|
|
# Test range checking by settimeout()
|
|
self.assertRaises(ValueError, self.sock.settimeout, -1)
|
|
self.assertRaises(ValueError, self.sock.settimeout, -1)
|
|
self.assertRaises(ValueError, self.sock.settimeout, -1.0)
|
|
|
|
def testTimeoutThenBlocking(self):
|
|
# Test settimeout() followed by setblocking()
|
|
self.sock.settimeout(10)
|
|
self.sock.setblocking(1)
|
|
self.assertEqual(self.sock.gettimeout(), None)
|
|
self.sock.setblocking(0)
|
|
self.assertEqual(self.sock.gettimeout(), 0.0)
|
|
|
|
self.sock.settimeout(10)
|
|
self.sock.setblocking(0)
|
|
self.assertEqual(self.sock.gettimeout(), 0.0)
|
|
self.sock.setblocking(1)
|
|
self.assertEqual(self.sock.gettimeout(), None)
|
|
|
|
def testBlockingThenTimeout(self):
|
|
# Test setblocking() followed by settimeout()
|
|
self.sock.setblocking(0)
|
|
self.sock.settimeout(1)
|
|
self.assertEqual(self.sock.gettimeout(), 1)
|
|
|
|
self.sock.setblocking(1)
|
|
self.sock.settimeout(1)
|
|
self.assertEqual(self.sock.gettimeout(), 1)
|
|
|
|
|
|
class TimeoutTestCase(unittest.TestCase):
|
|
"""Test case for socket.socket() timeout functions"""
|
|
|
|
# There are a number of tests here trying to make sure that an operation
|
|
# doesn't take too much longer than expected. But competing machine
|
|
# activity makes it inevitable that such tests will fail at times.
|
|
# When fuzz was at 1.0, I (tim) routinely saw bogus failures on Win2K
|
|
# and Win98SE. Boosting it to 2.0 helped a lot, but isn't a real
|
|
# solution.
|
|
fuzz = 2.0
|
|
|
|
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)
|
|
|
|
def tearDown(self):
|
|
self.sock.close()
|
|
|
|
def testConnectTimeout(self):
|
|
# Choose a private address that is unlikely to exist to prevent
|
|
# failures due to the connect succeeding before the timeout.
|
|
# Use a dotted IP address to avoid including the DNS lookup time
|
|
# with the connect time. This avoids failing the assertion that
|
|
# the timeout occurred fast enough.
|
|
addr = ('10.0.0.0', 12345)
|
|
|
|
# Test connect() timeout
|
|
_timeout = 0.001
|
|
self.sock.settimeout(_timeout)
|
|
|
|
_t1 = time.time()
|
|
self.failUnlessRaises(socket.error, self.sock.connect, addr)
|
|
_t2 = time.time()
|
|
|
|
_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)
|
|
|
|
_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)
|
|
|
|
_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.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))
|
|
|
|
def testSend(self):
|
|
# Test send() timeout
|
|
# couldn't figure out how to test it
|
|
pass
|
|
|
|
def testSendto(self):
|
|
# Test sendto() timeout
|
|
# couldn't figure out how to test it
|
|
pass
|
|
|
|
def testSendall(self):
|
|
# Test sendall() timeout
|
|
# couldn't figure out how to test it
|
|
pass
|
|
|
|
|
|
def test_main():
|
|
test_support.requires('network')
|
|
test_support.run_unittest(CreationTestCase, TimeoutTestCase)
|
|
|
|
if __name__ == "__main__":
|
|
test_main()
|