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)
|
2004-08-06 01:30:46 -03:00
|
|
|
self.addr_remote = ('www.python.org', 80)
|
2002-06-12 17:22:49 -03:00
|
|
|
self.addr_local = ('127.0.0.1', 25339)
|
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):
|
2002-08-22 17:22:16 -03:00
|
|
|
# Test connect() timeout
|
2002-09-03 16:17:47 -03:00
|
|
|
_timeout = 0.001
|
2002-06-12 17:22:49 -03:00
|
|
|
self.sock.settimeout(_timeout)
|
2002-06-06 18:08:16 -03:00
|
|
|
|
|
|
|
_t1 = time.time()
|
2002-06-12 17:22:49 -03:00
|
|
|
self.failUnlessRaises(socket.error, self.sock.connect,
|
|
|
|
self.addr_remote)
|
2002-06-06 18:08:16 -03:00
|
|
|
_t2 = time.time()
|
|
|
|
|
|
|
|
_delta = abs(_t1 - _t2)
|
2002-06-12 17:22:49 -03:00
|
|
|
self.assert_(_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
|
2002-06-12 17:22:49 -03:00
|
|
|
self.sock.connect(self.addr_remote)
|
|
|
|
self.sock.settimeout(_timeout)
|
2002-06-06 18:08:16 -03:00
|
|
|
|
|
|
|
_t1 = time.time()
|
2002-06-12 17:22:49 -03:00
|
|
|
self.failUnlessRaises(socket.error, self.sock.recv, 1024)
|
2002-06-06 18:08:16 -03:00
|
|
|
_t2 = time.time()
|
|
|
|
|
|
|
|
_delta = abs(_t1 - _t2)
|
2002-06-12 17:22:49 -03:00
|
|
|
self.assert_(_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)
|
|
|
|
self.sock.bind(self.addr_local)
|
|
|
|
self.sock.listen(5)
|
2002-06-06 18:08:16 -03:00
|
|
|
|
|
|
|
_t1 = time.time()
|
2002-06-12 17:22:49 -03:00
|
|
|
self.failUnlessRaises(socket.error, self.sock.accept)
|
2002-06-06 18:08:16 -03:00
|
|
|
_t2 = time.time()
|
|
|
|
|
|
|
|
_delta = abs(_t1 - _t2)
|
2002-06-12 17:22:49 -03:00
|
|
|
self.assert_(_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 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)
|
|
|
|
self.sock.bind(self.addr_local)
|
2002-06-06 18:08:16 -03:00
|
|
|
|
|
|
|
_t1 = time.time()
|
2002-06-12 17:22:49 -03:00
|
|
|
self.failUnlessRaises(socket.error, self.sock.recvfrom, 8192)
|
2002-06-06 18:08:16 -03:00
|
|
|
_t2 = time.time()
|
|
|
|
|
|
|
|
_delta = abs(_t1 - _t2)
|
2002-06-12 17:22:49 -03:00
|
|
|
self.assert_(_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 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()
|