"""Test case for socket.gettimeout() and socket.settimeout()"""
def setUp(self):
- self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-
- def tearDown(self):
- self.sock.close()
+ self.sock = self.enterContext(
+ socket.socket(socket.AF_INET, socket.SOCK_STREAM))
def testObjectCreation(self):
# Test Socket creation
def setUp(self):
raise NotImplementedError()
- tearDown = setUp
-
def _sock_operation(self, count, timeout, method, *args):
"""
Test the specified socket method.
"""TCP test case for socket.socket() timeout functions"""
def setUp(self):
- self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ self.sock = self.enterContext(
+ socket.socket(socket.AF_INET, socket.SOCK_STREAM))
self.addr_remote = resolve_address('www.python.org.', 80)
- def tearDown(self):
- self.sock.close()
-
def testConnectTimeout(self):
# Testing connect timeout is tricky: we need to have IP connectivity
# to a host that silently drops our packets. We can't simulate this
# for the current configuration.
skip = True
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- timeout = support.LOOPBACK_TIMEOUT
- sock.settimeout(timeout)
- try:
- sock.connect((whitehole))
- except TimeoutError:
- pass
- except OSError as err:
- if err.errno == errno.ECONNREFUSED:
- skip = False
- finally:
- sock.close()
- del sock
+ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
+ try:
+ timeout = support.LOOPBACK_TIMEOUT
+ sock.settimeout(timeout)
+ sock.connect((whitehole))
+ except TimeoutError:
+ pass
+ except OSError as err:
+ if err.errno == errno.ECONNREFUSED:
+ skip = False
if skip:
self.skipTest(
"""UDP test case for socket.socket() timeout functions"""
def setUp(self):
- self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
-
- def tearDown(self):
- self.sock.close()
+ self.sock = self.enterContext(
+ socket.socket(socket.AF_INET, socket.SOCK_DGRAM))
def testRecvfromTimeout(self):
# Test recvfrom() timeout