Docstring, layout and style tweaking. Increase fuzz to 1 second.

This commit is contained in:
Guido van Rossum 2002-06-12 20:22:49 +00:00
parent 09e532bcec
commit 28774da364
1 changed files with 86 additions and 89 deletions

View File

@ -8,159 +8,156 @@
class CreationTestCase(unittest.TestCase): class CreationTestCase(unittest.TestCase):
"""Test Case for socket.gettimeout() and socket.settimeout()""" """Test case for socket.gettimeout() and socket.settimeout()"""
def setUp(self): def setUp(self):
self.__s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def tearDown(self): def tearDown(self):
self.__s.close() self.sock.close()
def testObjectCreation(self): def testObjectCreation(self):
"Test Socket creation" "Test Socket creation"
self.assertEqual(self.__s.gettimeout(), None, self.assertEqual(self.sock.gettimeout(), None,
"Timeout socket not default to disable (None)") "timeout not disabled by default")
def testFloatReturnValue(self): def testFloatReturnValue(self):
"Test return value of getter/setter" "Test return value of gettimeout()"
self.__s.settimeout(7.345) self.sock.settimeout(7.345)
self.assertEqual(self.__s.gettimeout(), 7.345, self.assertEqual(self.sock.gettimeout(), 7.345)
"settimeout() and gettimeout() return different result")
self.__s.settimeout(3) self.sock.settimeout(3)
self.assertEqual(self.__s.gettimeout(), 3, self.assertEqual(self.sock.gettimeout(), 3)
"settimeout() and gettimeout() return different result")
self.sock.settimeout(None)
self.assertEqual(self.sock.gettimeout(), None)
def testReturnType(self): def testReturnType(self):
"Test return type of getter/setter" "Test return type of gettimeout()"
self.__s.settimeout(1) self.sock.settimeout(1)
self.assertEqual(type(self.__s.gettimeout()), type(1.0), self.assertEqual(type(self.sock.gettimeout()), type(1.0))
"return type of gettimeout() is not FloatType")
self.__s.settimeout(3.9) self.sock.settimeout(3.9)
self.assertEqual(type(self.__s.gettimeout()), type(1.0), self.assertEqual(type(self.sock.gettimeout()), type(1.0))
"return type of gettimeout() is not FloatType")
self.__s.settimeout(None)
self.assertEqual(type(self.__s.gettimeout()), type(None),
"return type of gettimeout() is not None")
def testTypeCheck(self): def testTypeCheck(self):
"Test type checking by settimeout" "Test type checking by settimeout()"
self.__s.settimeout(0) self.sock.settimeout(0)
self.__s.settimeout(0L) self.sock.settimeout(0L)
self.__s.settimeout(0.0) self.sock.settimeout(0.0)
self.__s.settimeout(None) self.sock.settimeout(None)
self.assertRaises(TypeError, self.__s.settimeout, "") self.assertRaises(TypeError, self.sock.settimeout, "")
self.assertRaises(TypeError, self.__s.settimeout, u"") self.assertRaises(TypeError, self.sock.settimeout, u"")
self.assertRaises(TypeError, self.__s.settimeout, ()) self.assertRaises(TypeError, self.sock.settimeout, ())
self.assertRaises(TypeError, self.__s.settimeout, []) self.assertRaises(TypeError, self.sock.settimeout, [])
self.assertRaises(TypeError, self.__s.settimeout, {}) self.assertRaises(TypeError, self.sock.settimeout, {})
self.assertRaises(TypeError, self.__s.settimeout, 0j) self.assertRaises(TypeError, self.sock.settimeout, 0j)
def testRangeCheck(self): def testRangeCheck(self):
"Test range checking by settimeout" "Test range checking by settimeout()"
self.assertRaises(ValueError, self.__s.settimeout, -1) self.assertRaises(ValueError, self.sock.settimeout, -1)
self.assertRaises(ValueError, self.__s.settimeout, -1L) self.assertRaises(ValueError, self.sock.settimeout, -1L)
self.assertRaises(ValueError, self.__s.settimeout, -1.0) self.assertRaises(ValueError, self.sock.settimeout, -1.0)
def testTimeoutThenoBlocking(self): def testTimeoutThenoBlocking(self):
"Test settimeout followed by setblocking" "Test settimeout() followed by setblocking()"
self.__s.settimeout(10) self.sock.settimeout(10)
self.__s.setblocking(1) self.sock.setblocking(1)
self.assertEqual(self.__s.gettimeout(), None) self.assertEqual(self.sock.gettimeout(), None)
self.__s.setblocking(0) self.sock.setblocking(0)
self.assertEqual(self.__s.gettimeout(), None) self.assertEqual(self.sock.gettimeout(), None)
self.__s.settimeout(10) self.sock.settimeout(10)
self.__s.setblocking(0) self.sock.setblocking(0)
self.assertEqual(self.__s.gettimeout(), None) self.assertEqual(self.sock.gettimeout(), None)
self.__s.setblocking(1) self.sock.setblocking(1)
self.assertEqual(self.__s.gettimeout(), None) self.assertEqual(self.sock.gettimeout(), None)
def testBlockingThenTimeout(self): def testBlockingThenTimeout(self):
"Test setblocking followed by settimeout" "Test setblocking() followed by settimeout()"
self.__s.setblocking(0) self.sock.setblocking(0)
self.__s.settimeout(1) self.sock.settimeout(1)
self.assertEqual(self.__s.gettimeout(), 1) self.assertEqual(self.sock.gettimeout(), 1)
self.__s.setblocking(1) self.sock.setblocking(1)
self.__s.settimeout(1) self.sock.settimeout(1)
self.assertEqual(self.__s.gettimeout(), 1) self.assertEqual(self.sock.gettimeout(), 1)
class TimeoutTestCase(unittest.TestCase): class TimeoutTestCase(unittest.TestCase):
"""Test Case for socket.socket() timeout functions""" """Test case for socket.socket() timeout functions"""
fuzz = 1.0
def setUp(self): def setUp(self):
self.__s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.__addr_remote = ('www.google.com', 80) self.addr_remote = ('www.google.com', 80)
self.__addr_local = ('127.0.0.1', 25339) self.addr_local = ('127.0.0.1', 25339)
def tearDown(self): def tearDown(self):
self.__s.close() self.sock.close()
def testConnectTimeout(self): def testConnectTimeout(self):
"Test connect() timeout" "Test connect() timeout"
_timeout = 0.02 _timeout = 0.02
self.__s.settimeout(_timeout) self.sock.settimeout(_timeout)
_t1 = time.time() _t1 = time.time()
self.failUnlessRaises(socket.error, self.__s.connect, self.failUnlessRaises(socket.error, self.sock.connect,
self.__addr_remote) self.addr_remote)
_t2 = time.time() _t2 = time.time()
_delta = abs(_t1 - _t2) _delta = abs(_t1 - _t2)
self.assert_(_delta < _timeout + 0.5, self.assert_(_delta < _timeout + self.fuzz,
"timeout (%f) is 0.5 seconds more than required (%f)" "timeout (%g) is %g seconds more than expected (%g)"
%(_delta, _timeout)) %(_delta, self.fuzz, _timeout))
def testRecvTimeout(self): def testRecvTimeout(self):
"Test recv() timeout" "Test recv() timeout"
_timeout = 0.02 _timeout = 0.02
self.__s.connect(self.__addr_remote) self.sock.connect(self.addr_remote)
self.__s.settimeout(_timeout) self.sock.settimeout(_timeout)
_t1 = time.time() _t1 = time.time()
self.failUnlessRaises(socket.error, self.__s.recv, 1024) self.failUnlessRaises(socket.error, self.sock.recv, 1024)
_t2 = time.time() _t2 = time.time()
_delta = abs(_t1 - _t2) _delta = abs(_t1 - _t2)
self.assert_(_delta < _timeout + 0.5, self.assert_(_delta < _timeout + self.fuzz,
"timeout (%f) is 0.5 seconds more than required (%f)" "timeout (%g) is %g seconds more than expected (%g)"
%(_delta, _timeout)) %(_delta, self.fuzz, _timeout))
def testAcceptTimeout(self): def testAcceptTimeout(self):
"Test accept() timeout()" "Test accept() timeout"
_timeout = 2 _timeout = 2
self.__s.settimeout(_timeout) self.sock.settimeout(_timeout)
self.__s.bind(self.__addr_local) self.sock.bind(self.addr_local)
self.__s.listen(5) self.sock.listen(5)
_t1 = time.time() _t1 = time.time()
self.failUnlessRaises(socket.error, self.__s.accept) self.failUnlessRaises(socket.error, self.sock.accept)
_t2 = time.time() _t2 = time.time()
_delta = abs(_t1 - _t2) _delta = abs(_t1 - _t2)
self.assert_(_delta < _timeout + 0.5, self.assert_(_delta < _timeout + self.fuzz,
"timeout (%f) is 0.5 seconds more than required (%f)" "timeout (%g) is %g seconds more than expected (%g)"
%(_delta, _timeout)) %(_delta, self.fuzz, _timeout))
def testRecvfromTimeout(self): def testRecvfromTimeout(self):
"Test recvfrom() timeout()" "Test recvfrom() timeout"
_timeout = 2 _timeout = 2
self.__s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.__s.settimeout(_timeout) self.sock.settimeout(_timeout)
self.__s.bind(self.__addr_local) self.sock.bind(self.addr_local)
_t1 = time.time() _t1 = time.time()
self.failUnlessRaises(socket.error, self.__s.recvfrom, 8192) self.failUnlessRaises(socket.error, self.sock.recvfrom, 8192)
_t2 = time.time() _t2 = time.time()
_delta = abs(_t1 - _t2) _delta = abs(_t1 - _t2)
self.assert_(_delta < _timeout + 0.5, self.assert_(_delta < _timeout + self.fuzz,
"timeout (%f) is 0.5 seconds more than required (%f)" "timeout (%g) is %g seconds more than expected (%g)"
%(_delta, _timeout)) %(_delta, self.fuzz, _timeout))
def testSend(self): def testSend(self):
"Test send() timeout" "Test send() timeout"