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-06-12 16:18:08 -03:00
|
|
|
import test_support
|
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):
|
|
|
|
"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-06-12 17:22:49 -03:00
|
|
|
"Test return value of gettimeout()"
|
|
|
|
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):
|
|
|
|
"Test return type of gettimeout()"
|
|
|
|
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-06-12 17:22:49 -03:00
|
|
|
"Test type checking by settimeout()"
|
|
|
|
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-06-12 17:22:49 -03:00
|
|
|
"Test range checking by settimeout()"
|
|
|
|
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-06-12 17:22:49 -03:00
|
|
|
"Test settimeout() followed by setblocking()"
|
|
|
|
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-06-12 17:22:49 -03:00
|
|
|
"Test setblocking() followed by settimeout()"
|
|
|
|
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"""
|
|
|
|
|
|
|
|
fuzz = 1.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)
|
|
|
|
self.addr_remote = ('www.google.com', 80)
|
|
|
|
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):
|
|
|
|
"Test connect() timeout"
|
|
|
|
_timeout = 0.02
|
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,
|
|
|
|
"timeout (%g) is %g seconds more than expected (%g)"
|
|
|
|
%(_delta, self.fuzz, _timeout))
|
2002-06-06 18:08:16 -03:00
|
|
|
|
|
|
|
def testRecvTimeout(self):
|
|
|
|
"Test recv() timeout"
|
|
|
|
_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-06-12 17:22:49 -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-06-12 17:22:49 -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):
|
|
|
|
"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
|
|
|
|
|
|
|
|
|
2002-06-13 17:24:17 -03:00
|
|
|
def main():
|
2002-06-06 18:08:16 -03:00
|
|
|
suite = unittest.TestSuite()
|
2002-06-12 16:18:08 -03:00
|
|
|
suite.addTest(unittest.makeSuite(CreationTestCase))
|
|
|
|
suite.addTest(unittest.makeSuite(TimeoutTestCase))
|
|
|
|
test_support.run_suite(suite)
|
2002-06-06 18:08:16 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2002-06-13 17:24:17 -03:00
|
|
|
main()
|