2002-06-12 16:57:18 -03:00
|
|
|
"""Unit tests for socket timeout feature."""
|
2002-06-06 18:08:16 -03:00
|
|
|
|
|
|
|
import unittest
|
2002-07-23 16:04:11 -03:00
|
|
|
from test import test_support
|
2002-06-06 18:08:16 -03:00
|
|
|
|
2003-02-28 15:57:03 -04:00
|
|
|
# This requires the 'network' resource as given on the regrtest command line.
|
|
|
|
skip_expected = not test_support.is_resource_enabled('network')
|
|
|
|
|
2002-06-06 18:08:16 -03:00
|
|
|
import time
|
|
|
|
import socket
|
|
|
|
|
2002-06-12 16:57:18 -03:00
|
|
|
|
2002-06-12 16:18:08 -03:00
|
|
|
class CreationTestCase(unittest.TestCase):
|
2002-06-12 17:22:49 -03:00
|
|
|
"""Test case for socket.gettimeout() and socket.settimeout()"""
|
2002-06-12 16:57:18 -03:00
|
|
|
|
2002-06-06 18:08:16 -03:00
|
|
|
def setUp(self):
|
2002-06-12 17:22:49 -03:00
|
|
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2002-06-06 18:08:16 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
2002-06-12 17:22:49 -03:00
|
|
|
self.sock.close()
|
2002-06-06 18:08:16 -03:00
|
|
|
|
|
|
|
def testObjectCreation(self):
|
2002-08-22 17:22:16 -03:00
|
|
|
# Test Socket creation
|
2002-06-12 17:22:49 -03:00
|
|
|
self.assertEqual(self.sock.gettimeout(), None,
|
|
|
|
"timeout not disabled by default")
|
2002-06-06 18:08:16 -03:00
|
|
|
|
|
|
|
def testFloatReturnValue(self):
|
2002-08-22 17:22:16 -03:00
|
|
|
# Test return value of gettimeout()
|
2002-06-12 17:22:49 -03:00
|
|
|
self.sock.settimeout(7.345)
|
|
|
|
self.assertEqual(self.sock.gettimeout(), 7.345)
|
2002-06-06 18:08:16 -03:00
|
|
|
|
2002-06-12 17:22:49 -03:00
|
|
|
self.sock.settimeout(3)
|
|
|
|
self.assertEqual(self.sock.gettimeout(), 3)
|
2002-06-06 18:08:16 -03:00
|
|
|
|
2002-06-12 17:22:49 -03:00
|
|
|
self.sock.settimeout(None)
|
|
|
|
self.assertEqual(self.sock.gettimeout(), None)
|
2002-06-06 18:08:16 -03:00
|
|
|
|
2002-06-12 17:22:49 -03:00
|
|
|
def testReturnType(self):
|
2002-08-22 17:22:16 -03:00
|
|
|
# Test return type of gettimeout()
|
2002-06-12 17:22:49 -03:00
|
|
|
self.sock.settimeout(1)
|
|
|
|
self.assertEqual(type(self.sock.gettimeout()), type(1.0))
|
2002-06-06 18:08:16 -03:00
|
|
|
|
2002-06-12 17:22:49 -03:00
|
|
|
self.sock.settimeout(3.9)
|
|
|
|
self.assertEqual(type(self.sock.gettimeout()), type(1.0))
|
2002-06-12 16:57:18 -03:00
|
|
|
|
|
|
|
def testTypeCheck(self):
|
2002-08-22 17:22:16 -03:00
|
|
|
# Test type checking by settimeout()
|
2002-06-12 17:22:49 -03:00
|
|
|
self.sock.settimeout(0)
|
|
|
|
self.sock.settimeout(0L)
|
|
|
|
self.sock.settimeout(0.0)
|
|
|
|
self.sock.settimeout(None)
|
|
|
|
self.assertRaises(TypeError, self.sock.settimeout, "")
|
|
|
|
self.assertRaises(TypeError, self.sock.settimeout, u"")
|
|
|
|
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)
|
2002-06-12 16:57:18 -03:00
|
|
|
|
|
|
|
def testRangeCheck(self):
|
2002-08-22 17:22:16 -03:00
|
|
|
# Test range checking by settimeout()
|
2002-06-12 17:22:49 -03:00
|
|
|
self.assertRaises(ValueError, self.sock.settimeout, -1)
|
|
|
|
self.assertRaises(ValueError, self.sock.settimeout, -1L)
|
|
|
|
self.assertRaises(ValueError, self.sock.settimeout, -1.0)
|
2002-06-12 16:57:18 -03:00
|
|
|
|
2002-06-13 12:07:44 -03:00
|
|
|
def testTimeoutThenBlocking(self):
|
2002-08-22 17:22:16 -03:00
|
|
|
# Test settimeout() followed by setblocking()
|
2002-06-12 17:22:49 -03:00
|
|
|
self.sock.settimeout(10)
|
|
|
|
self.sock.setblocking(1)
|
|
|
|
self.assertEqual(self.sock.gettimeout(), None)
|
|
|
|
self.sock.setblocking(0)
|
2002-06-13 12:07:44 -03:00
|
|
|
self.assertEqual(self.sock.gettimeout(), 0.0)
|
2002-06-12 17:22:49 -03:00
|
|
|
|
|
|
|
self.sock.settimeout(10)
|
|
|
|
self.sock.setblocking(0)
|
2002-06-13 12:07:44 -03:00
|
|
|
self.assertEqual(self.sock.gettimeout(), 0.0)
|
2002-06-12 17:22:49 -03:00
|
|
|
self.sock.setblocking(1)
|
|
|
|
self.assertEqual(self.sock.gettimeout(), None)
|
2002-06-12 16:57:18 -03:00
|
|
|
|
|
|
|
def testBlockingThenTimeout(self):
|
2002-08-22 17:22:16 -03:00
|
|
|
# Test setblocking() followed by settimeout()
|
2002-06-12 17:22:49 -03:00
|
|
|
self.sock.setblocking(0)
|
|
|
|
self.sock.settimeout(1)
|
|
|
|
self.assertEqual(self.sock.gettimeout(), 1)
|
2002-06-12 16:57:18 -03:00
|
|
|
|
2002-06-12 17:22:49 -03:00
|
|
|
self.sock.setblocking(1)
|
|
|
|
self.sock.settimeout(1)
|
|
|
|
self.assertEqual(self.sock.gettimeout(), 1)
|
2002-06-12 16:57:18 -03:00
|
|
|
|
2002-06-06 18:08:16 -03:00
|
|
|
|
2002-06-12 16:18:08 -03:00
|
|
|
class TimeoutTestCase(unittest.TestCase):
|
2002-06-12 17:22:49 -03:00
|
|
|
"""Test case for socket.socket() timeout functions"""
|
|
|
|
|
2003-02-21 12:45:41 -04:00
|
|
|
# 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
|
2002-06-12 16:57:18 -03:00
|
|
|
|
2002-06-06 18:08:16 -03:00
|
|
|
def setUp(self):
|
2002-06-12 17:22:49 -03:00
|
|
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2006-06-11 17:25:56 -03:00
|
|
|
self.addr_remote = ('www.python.org.', 80)
|
2010-02-06 00:27:21 -04:00
|
|
|
self.localhost = '127.0.0.1'
|
2002-06-06 18:08:16 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
2002-06-12 17:22:49 -03:00
|
|
|
self.sock.close()
|
2002-06-06 18:08:16 -03:00
|
|
|
|
|
|
|
def testConnectTimeout(self):
|
2008-03-26 01:55:51 -03:00
|
|
|
# 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
|
2008-03-23 00:43:33 -03:00
|
|
|
# with the connect time. This avoids failing the assertion that
|
|
|
|
# the timeout occurred fast enough.
|
2008-03-26 01:55:51 -03:00
|
|
|
addr = ('10.0.0.0', 12345)
|
2008-03-23 00:43:33 -03:00
|
|
|
|
|
|
|
# Test connect() timeout
|
|
|
|
_timeout = 0.001
|
|
|
|
self.sock.settimeout(_timeout)
|
|
|
|
|
2002-06-06 18:08:16 -03:00
|
|
|
_t1 = time.time()
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertRaises(socket.error, self.sock.connect, addr)
|
2002-06-06 18:08:16 -03:00
|
|
|
_t2 = time.time()
|
|
|
|
|
|
|
|
_delta = abs(_t1 - _t2)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(_delta < _timeout + self.fuzz,
|
2004-06-11 12:57:49 -03:00
|
|
|
"timeout (%g) is more than %g seconds more than expected (%g)"
|
2002-06-12 17:22:49 -03:00
|
|
|
%(_delta, self.fuzz, _timeout))
|
2002-06-06 18:08:16 -03:00
|
|
|
|
|
|
|
def testRecvTimeout(self):
|
2002-08-22 17:22:16 -03:00
|
|
|
# Test recv() timeout
|
2002-06-06 18:08:16 -03:00
|
|
|
_timeout = 0.02
|
|
|
|
|
2010-10-29 15:17:19 -03:00
|
|
|
with test_support.transient_internet(self.addr_remote[0]):
|
|
|
|
self.sock.connect(self.addr_remote)
|
|
|
|
self.sock.settimeout(_timeout)
|
2002-06-06 18:08:16 -03:00
|
|
|
|
2010-10-29 15:17:19 -03:00
|
|
|
_t1 = time.time()
|
|
|
|
self.assertRaises(socket.timeout, self.sock.recv, 1024)
|
|
|
|
_t2 = time.time()
|
|
|
|
|
|
|
|
_delta = abs(_t1 - _t2)
|
|
|
|
self.assertTrue(_delta < _timeout + self.fuzz,
|
|
|
|
"timeout (%g) is %g seconds more than expected (%g)"
|
|
|
|
%(_delta, self.fuzz, _timeout))
|
2002-06-06 18:08:16 -03:00
|
|
|
|
|
|
|
def testAcceptTimeout(self):
|
2002-08-22 17:22:16 -03:00
|
|
|
# Test accept() timeout
|
2002-06-06 18:08:16 -03:00
|
|
|
_timeout = 2
|
2002-06-12 17:22:49 -03:00
|
|
|
self.sock.settimeout(_timeout)
|
2010-02-06 00:27:21 -04:00
|
|
|
# Prevent "Address already in use" socket exceptions
|
|
|
|
test_support.bind_port(self.sock, self.localhost)
|
2002-06-12 17:22:49 -03:00
|
|
|
self.sock.listen(5)
|
2002-06-06 18:08:16 -03:00
|
|
|
|
|
|
|
_t1 = time.time()
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertRaises(socket.error, self.sock.accept)
|
2002-06-06 18:08:16 -03:00
|
|
|
_t2 = time.time()
|
|
|
|
|
|
|
|
_delta = abs(_t1 - _t2)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(_delta < _timeout + self.fuzz,
|
2002-06-12 17:22:49 -03:00
|
|
|
"timeout (%g) is %g seconds more than expected (%g)"
|
|
|
|
%(_delta, self.fuzz, _timeout))
|
2002-06-06 18:08:16 -03:00
|
|
|
|
|
|
|
def testRecvfromTimeout(self):
|
2002-08-22 17:22:16 -03:00
|
|
|
# Test recvfrom() timeout
|
2002-06-06 18:08:16 -03:00
|
|
|
_timeout = 2
|
2002-06-12 17:22:49 -03:00
|
|
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
self.sock.settimeout(_timeout)
|
2010-02-06 00:27:21 -04:00
|
|
|
# Prevent "Address already in use" socket exceptions
|
|
|
|
test_support.bind_port(self.sock, self.localhost)
|
2002-06-06 18:08:16 -03:00
|
|
|
|
|
|
|
_t1 = time.time()
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertRaises(socket.error, self.sock.recvfrom, 8192)
|
2002-06-06 18:08:16 -03:00
|
|
|
_t2 = time.time()
|
|
|
|
|
|
|
|
_delta = abs(_t1 - _t2)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(_delta < _timeout + self.fuzz,
|
2002-06-12 17:22:49 -03:00
|
|
|
"timeout (%g) is %g seconds more than expected (%g)"
|
|
|
|
%(_delta, self.fuzz, _timeout))
|
2002-06-06 18:08:16 -03:00
|
|
|
|
|
|
|
def testSend(self):
|
2002-08-22 17:22:16 -03:00
|
|
|
# Test send() timeout
|
2002-06-06 18:08:16 -03:00
|
|
|
# couldn't figure out how to test it
|
|
|
|
pass
|
|
|
|
|
|
|
|
def testSendto(self):
|
2002-08-22 17:22:16 -03:00
|
|
|
# Test sendto() timeout
|
2002-06-06 18:08:16 -03:00
|
|
|
# couldn't figure out how to test it
|
|
|
|
pass
|
|
|
|
|
|
|
|
def testSendall(self):
|
2002-08-22 17:22:16 -03:00
|
|
|
# Test sendall() timeout
|
2002-06-06 18:08:16 -03:00
|
|
|
# couldn't figure out how to test it
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2003-02-17 10:51:41 -04:00
|
|
|
def test_main():
|
2003-02-28 15:57:03 -04:00
|
|
|
test_support.requires('network')
|
2003-05-01 14:45:56 -03:00
|
|
|
test_support.run_unittest(CreationTestCase, TimeoutTestCase)
|
2002-06-06 18:08:16 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2003-02-17 10:51:41 -04:00
|
|
|
test_main()
|