Skip to content

Commit 5c85e3f

Browse files
author
Victor Stinner
committed
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Fix a ResourceWarning(unclosed socket).
1 parent 109761b commit 5c85e3f

1 file changed

Lines changed: 38 additions & 22 deletions

File tree

Lib/test/test_timeout.py

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ def testBlockingThenTimeout(self):
8888

8989

9090
class TimeoutTestCase(unittest.TestCase):
91-
"""Test case for socket.socket() timeout functions"""
92-
9391
# There are a number of tests here trying to make sure that an operation
9492
# doesn't take too much longer than expected. But competing machine
9593
# activity makes it inevitable that such tests will fail at times.
@@ -98,14 +96,22 @@ class TimeoutTestCase(unittest.TestCase):
9896
# solution.
9997
fuzz = 2.0
10098

99+
localhost = '127.0.0.1'
100+
101101
def setUp(self):
102-
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
103-
self.addr_remote = ('www.python.org.', 80)
104-
self.localhost = '127.0.0.1'
102+
raise NotImplementedError()
105103

106104
def tearDown(self):
107105
self.sock.close()
108106

107+
108+
class TCPTimeoutTestCase(TimeoutTestCase):
109+
"""TCP test case for socket.socket() timeout functions"""
110+
111+
def setUp(self):
112+
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
113+
self.addr_remote = ('www.python.org.', 80)
114+
109115
def testConnectTimeout(self):
110116
# Choose a private address that is unlikely to exist to prevent
111117
# failures due to the connect succeeding before the timeout.
@@ -161,10 +167,31 @@ def testAcceptTimeout(self):
161167
"timeout (%g) is %g seconds more than expected (%g)"
162168
%(_delta, self.fuzz, _timeout))
163169

170+
def testSend(self):
171+
# Test send() timeout
172+
# couldn't figure out how to test it
173+
pass
174+
175+
def testSendto(self):
176+
# Test sendto() timeout
177+
# couldn't figure out how to test it
178+
pass
179+
180+
def testSendall(self):
181+
# Test sendall() timeout
182+
# couldn't figure out how to test it
183+
pass
184+
185+
186+
class UDPTimeoutTestCase(TimeoutTestCase):
187+
"""UDP test case for socket.socket() timeout functions"""
188+
189+
def setUp(self):
190+
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
191+
164192
def testRecvfromTimeout(self):
165193
# Test recvfrom() timeout
166194
_timeout = 2
167-
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
168195
self.sock.settimeout(_timeout)
169196
# Prevent "Address already in use" socket exceptions
170197
support.bind_port(self.sock, self.localhost)
@@ -178,25 +205,14 @@ def testRecvfromTimeout(self):
178205
"timeout (%g) is %g seconds more than expected (%g)"
179206
%(_delta, self.fuzz, _timeout))
180207

181-
def testSend(self):
182-
# Test send() timeout
183-
# couldn't figure out how to test it
184-
pass
185-
186-
def testSendto(self):
187-
# Test sendto() timeout
188-
# couldn't figure out how to test it
189-
pass
190-
191-
def testSendall(self):
192-
# Test sendall() timeout
193-
# couldn't figure out how to test it
194-
pass
195-
196208

197209
def test_main():
198210
support.requires('network')
199-
support.run_unittest(CreationTestCase, TimeoutTestCase)
211+
support.run_unittest(
212+
CreationTestCase,
213+
TCPTimeoutTestCase,
214+
UDPTimeoutTestCase,
215+
)
200216

201217
if __name__ == "__main__":
202218
test_main()

0 commit comments

Comments
 (0)