mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
SF patch #760257: add socket.timeout exception
(Contributed by Bob Halley) Add unittests for the new socket.timeout exception.
This commit is contained in:
parent
10f3018023
commit
11a35f545b
1 changed files with 57 additions and 2 deletions
|
@ -682,10 +682,65 @@ class SmallBufferedFileObjectClassTestCase(FileObjectClassTestCase):
|
||||||
|
|
||||||
bufsize = 2 # Exercise the buffering code
|
bufsize = 2 # Exercise the buffering code
|
||||||
|
|
||||||
|
class TCPTimeoutTest(SocketTCPTest):
|
||||||
|
|
||||||
|
def testTCPTimeout(self):
|
||||||
|
def raise_timeout(*args, **kwargs):
|
||||||
|
self.serv.settimeout(1.0)
|
||||||
|
self.serv.accept()
|
||||||
|
self.failUnlessRaises(socket.timeout, raise_timeout,
|
||||||
|
"Error generating a timeout exception (TCP)")
|
||||||
|
|
||||||
|
def testTimeoutZero(self):
|
||||||
|
ok = False
|
||||||
|
try:
|
||||||
|
self.serv.settimeout(0.0)
|
||||||
|
foo = self.serv.accept()
|
||||||
|
except socket.timeout:
|
||||||
|
self.fail("caught timeout instead of error (TCP)")
|
||||||
|
except socket.error:
|
||||||
|
ok = True
|
||||||
|
except:
|
||||||
|
self.fail("caught unexpected exception (TCP)")
|
||||||
|
if not ok:
|
||||||
|
self.fail("accept() returned success when we did not expect it")
|
||||||
|
|
||||||
|
class UDPTimeoutTest(SocketTCPTest):
|
||||||
|
|
||||||
|
def testUDPTimeout(self):
|
||||||
|
def raise_timeout(*args, **kwargs):
|
||||||
|
self.serv.settimeout(1.0)
|
||||||
|
self.serv.recv(1024)
|
||||||
|
self.failUnlessRaises(socket.timeout, raise_timeout,
|
||||||
|
"Error generating a timeout exception (UDP)")
|
||||||
|
|
||||||
|
def testTimeoutZero(self):
|
||||||
|
ok = False
|
||||||
|
try:
|
||||||
|
self.serv.settimeout(0.0)
|
||||||
|
foo = self.serv.recv(1024)
|
||||||
|
except socket.timeout:
|
||||||
|
self.fail("caught timeout instead of error (UDP)")
|
||||||
|
except socket.error:
|
||||||
|
ok = True
|
||||||
|
except:
|
||||||
|
self.fail("caught unexpected exception (UDP)")
|
||||||
|
if not ok:
|
||||||
|
self.fail("recv() returned success when we did not expect it")
|
||||||
|
|
||||||
|
class TestExceptions(unittest.TestCase):
|
||||||
|
|
||||||
|
def testExceptionTree(self):
|
||||||
|
self.assert_(issubclass(socket.error, Exception))
|
||||||
|
self.assert_(issubclass(socket.herror, socket.error))
|
||||||
|
self.assert_(issubclass(socket.gaierror, socket.error))
|
||||||
|
self.assert_(issubclass(socket.timeout, socket.error))
|
||||||
|
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
tests = [ GeneralModuleTests, BasicTCPTest ]
|
tests = [GeneralModuleTests, BasicTCPTest, TCPTimeoutTest, TestExceptions]
|
||||||
if sys.platform != 'mac':
|
if sys.platform != 'mac':
|
||||||
tests.append(BasicUDPTest)
|
tests.extend([ BasicUDPTest, UDPTimeoutTest ])
|
||||||
|
|
||||||
tests.extend([
|
tests.extend([
|
||||||
NonBlockingTCPTests,
|
NonBlockingTCPTests,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue