Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_timeout.py @ 86127:a3d09c19bb93
merge with 3.3
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Sun, 06 Oct 2013 19:24:00 +0200 |
| parents | 89df1984adda |
| children |
| rev | line source |
|---|---|
|
23791
cde6608739c4
Add some more basic tests to validate the argument checking of
Guido van Rossum <guido@python.org>
parents:
23790
diff
changeset
|
1 """Unit tests for socket timeout feature.""" |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
85206
5880c7d33e13
Issue #16463: Fix a transient test_timeout failure.
Charles-François Natali <cf.natali@gmail.com>
parents:
78690
diff
changeset
|
3 import functools |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 import unittest |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46115
diff
changeset
|
5 from test import support |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 |
|
28112
2c8c2c6b2ab7
Fix SF bugs #692951 and 692988, test_timeout.py needs 'network' resource
Neal Norwitz <nnorwitz@gmail.com>
parents:
28011
diff
changeset
|
7 # This requires the 'network' resource as given on the regrtest command line. |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46115
diff
changeset
|
8 skip_expected = not support.is_resource_enabled('network') |
|
28112
2c8c2c6b2ab7
Fix SF bugs #692951 and 692988, test_timeout.py needs 'network' resource
Neal Norwitz <nnorwitz@gmail.com>
parents:
28011
diff
changeset
|
9 |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 import time |
|
67400
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
11 import errno |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 import socket |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 |
|
23791
cde6608739c4
Add some more basic tests to validate the argument checking of
Guido van Rossum <guido@python.org>
parents:
23790
diff
changeset
|
14 |
|
85206
5880c7d33e13
Issue #16463: Fix a transient test_timeout failure.
Charles-François Natali <cf.natali@gmail.com>
parents:
78690
diff
changeset
|
15 @functools.lru_cache() |
|
5880c7d33e13
Issue #16463: Fix a transient test_timeout failure.
Charles-François Natali <cf.natali@gmail.com>
parents:
78690
diff
changeset
|
16 def resolve_address(host, port): |
|
5880c7d33e13
Issue #16463: Fix a transient test_timeout failure.
Charles-François Natali <cf.natali@gmail.com>
parents:
78690
diff
changeset
|
17 """Resolve an (host, port) to an address. |
|
5880c7d33e13
Issue #16463: Fix a transient test_timeout failure.
Charles-François Natali <cf.natali@gmail.com>
parents:
78690
diff
changeset
|
18 |
|
5880c7d33e13
Issue #16463: Fix a transient test_timeout failure.
Charles-François Natali <cf.natali@gmail.com>
parents:
78690
diff
changeset
|
19 We must perform name resolution before timeout tests, otherwise it will be |
|
5880c7d33e13
Issue #16463: Fix a transient test_timeout failure.
Charles-François Natali <cf.natali@gmail.com>
parents:
78690
diff
changeset
|
20 performed by connect(). |
|
5880c7d33e13
Issue #16463: Fix a transient test_timeout failure.
Charles-François Natali <cf.natali@gmail.com>
parents:
78690
diff
changeset
|
21 """ |
|
5880c7d33e13
Issue #16463: Fix a transient test_timeout failure.
Charles-François Natali <cf.natali@gmail.com>
parents:
78690
diff
changeset
|
22 with support.transient_internet(host): |
|
5880c7d33e13
Issue #16463: Fix a transient test_timeout failure.
Charles-François Natali <cf.natali@gmail.com>
parents:
78690
diff
changeset
|
23 return socket.getaddrinfo(host, port, socket.AF_INET, |
|
5880c7d33e13
Issue #16463: Fix a transient test_timeout failure.
Charles-François Natali <cf.natali@gmail.com>
parents:
78690
diff
changeset
|
24 socket.SOCK_STREAM)[0][4] |
|
5880c7d33e13
Issue #16463: Fix a transient test_timeout failure.
Charles-François Natali <cf.natali@gmail.com>
parents:
78690
diff
changeset
|
25 |
|
5880c7d33e13
Issue #16463: Fix a transient test_timeout failure.
Charles-François Natali <cf.natali@gmail.com>
parents:
78690
diff
changeset
|
26 |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
27 class CreationTestCase(unittest.TestCase): |
|
23793
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
28 """Test case for socket.gettimeout() and socket.settimeout()""" |
|
23791
cde6608739c4
Add some more basic tests to validate the argument checking of
Guido van Rossum <guido@python.org>
parents:
23790
diff
changeset
|
29 |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 def setUp(self): |
|
23793
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
31 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 def tearDown(self): |
|
23793
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
34 self.sock.close() |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 def testObjectCreation(self): |
|
24964
d90a6add7ca3
Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
37 # Test Socket creation |
|
23793
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
38 self.assertEqual(self.sock.gettimeout(), None, |
|
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
39 "timeout not disabled by default") |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 def testFloatReturnValue(self): |
|
24964
d90a6add7ca3
Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
42 # Test return value of gettimeout() |
|
23793
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
43 self.sock.settimeout(7.345) |
|
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
44 self.assertEqual(self.sock.gettimeout(), 7.345) |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 |
|
23793
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
46 self.sock.settimeout(3) |
|
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
47 self.assertEqual(self.sock.gettimeout(), 3) |
|
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
48 |
|
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
49 self.sock.settimeout(None) |
|
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
50 self.assertEqual(self.sock.gettimeout(), None) |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 def testReturnType(self): |
|
24964
d90a6add7ca3
Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
53 # Test return type of gettimeout() |
|
23793
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
54 self.sock.settimeout(1) |
|
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
55 self.assertEqual(type(self.sock.gettimeout()), type(1.0)) |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 |
|
23793
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
57 self.sock.settimeout(3.9) |
|
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
58 self.assertEqual(type(self.sock.gettimeout()), type(1.0)) |
|
23791
cde6608739c4
Add some more basic tests to validate the argument checking of
Guido van Rossum <guido@python.org>
parents:
23790
diff
changeset
|
59 |
|
cde6608739c4
Add some more basic tests to validate the argument checking of
Guido van Rossum <guido@python.org>
parents:
23790
diff
changeset
|
60 def testTypeCheck(self): |
|
24964
d90a6add7ca3
Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
61 # Test type checking by settimeout() |
|
23793
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
62 self.sock.settimeout(0) |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
63 self.sock.settimeout(0) |
|
23793
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
64 self.sock.settimeout(0.0) |
|
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
65 self.sock.settimeout(None) |
|
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
66 self.assertRaises(TypeError, self.sock.settimeout, "") |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
67 self.assertRaises(TypeError, self.sock.settimeout, "") |
|
23793
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
68 self.assertRaises(TypeError, self.sock.settimeout, ()) |
|
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
69 self.assertRaises(TypeError, self.sock.settimeout, []) |
|
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
70 self.assertRaises(TypeError, self.sock.settimeout, {}) |
|
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
71 self.assertRaises(TypeError, self.sock.settimeout, 0j) |
|
23791
cde6608739c4
Add some more basic tests to validate the argument checking of
Guido van Rossum <guido@python.org>
parents:
23790
diff
changeset
|
72 |
|
cde6608739c4
Add some more basic tests to validate the argument checking of
Guido van Rossum <guido@python.org>
parents:
23790
diff
changeset
|
73 def testRangeCheck(self): |
|
24964
d90a6add7ca3
Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
74 # Test range checking by settimeout() |
|
23793
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
75 self.assertRaises(ValueError, self.sock.settimeout, -1) |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
76 self.assertRaises(ValueError, self.sock.settimeout, -1) |
|
23793
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
77 self.assertRaises(ValueError, self.sock.settimeout, -1.0) |
|
23791
cde6608739c4
Add some more basic tests to validate the argument checking of
Guido van Rossum <guido@python.org>
parents:
23790
diff
changeset
|
78 |
|
23815
203d1412a7e8
Major overhaul of timeout sockets:
Guido van Rossum <guido@python.org>
parents:
23793
diff
changeset
|
79 def testTimeoutThenBlocking(self): |
|
24964
d90a6add7ca3
Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
80 # Test settimeout() followed by setblocking() |
|
23793
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
81 self.sock.settimeout(10) |
|
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
82 self.sock.setblocking(1) |
|
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
83 self.assertEqual(self.sock.gettimeout(), None) |
|
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
84 self.sock.setblocking(0) |
|
23815
203d1412a7e8
Major overhaul of timeout sockets:
Guido van Rossum <guido@python.org>
parents:
23793
diff
changeset
|
85 self.assertEqual(self.sock.gettimeout(), 0.0) |
|
23791
cde6608739c4
Add some more basic tests to validate the argument checking of
Guido van Rossum <guido@python.org>
parents:
23790
diff
changeset
|
86 |
|
23793
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
87 self.sock.settimeout(10) |
|
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
88 self.sock.setblocking(0) |
|
23815
203d1412a7e8
Major overhaul of timeout sockets:
Guido van Rossum <guido@python.org>
parents:
23793
diff
changeset
|
89 self.assertEqual(self.sock.gettimeout(), 0.0) |
|
23793
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
90 self.sock.setblocking(1) |
|
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
91 self.assertEqual(self.sock.gettimeout(), None) |
|
23791
cde6608739c4
Add some more basic tests to validate the argument checking of
Guido van Rossum <guido@python.org>
parents:
23790
diff
changeset
|
92 |
|
cde6608739c4
Add some more basic tests to validate the argument checking of
Guido van Rossum <guido@python.org>
parents:
23790
diff
changeset
|
93 def testBlockingThenTimeout(self): |
|
24964
d90a6add7ca3
Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
94 # Test setblocking() followed by settimeout() |
|
23793
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
95 self.sock.setblocking(0) |
|
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
96 self.sock.settimeout(1) |
|
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
97 self.assertEqual(self.sock.gettimeout(), 1) |
|
23791
cde6608739c4
Add some more basic tests to validate the argument checking of
Guido van Rossum <guido@python.org>
parents:
23790
diff
changeset
|
98 |
|
23793
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
99 self.sock.setblocking(1) |
|
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
100 self.sock.settimeout(1) |
|
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
101 self.assertEqual(self.sock.gettimeout(), 1) |
|
23791
cde6608739c4
Add some more basic tests to validate the argument checking of
Guido van Rossum <guido@python.org>
parents:
23790
diff
changeset
|
102 |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
104 class TimeoutTestCase(unittest.TestCase): |
|
28011
0eb948364990
Doubled TimeoutTestCase.fuzz, to slash the frequency of bogus failures
Tim Peters <tim.peters@gmail.com>
parents:
27912
diff
changeset
|
105 # There are a number of tests here trying to make sure that an operation |
|
0eb948364990
Doubled TimeoutTestCase.fuzz, to slash the frequency of bogus failures
Tim Peters <tim.peters@gmail.com>
parents:
27912
diff
changeset
|
106 # doesn't take too much longer than expected. But competing machine |
|
0eb948364990
Doubled TimeoutTestCase.fuzz, to slash the frequency of bogus failures
Tim Peters <tim.peters@gmail.com>
parents:
27912
diff
changeset
|
107 # activity makes it inevitable that such tests will fail at times. |
|
0eb948364990
Doubled TimeoutTestCase.fuzz, to slash the frequency of bogus failures
Tim Peters <tim.peters@gmail.com>
parents:
27912
diff
changeset
|
108 # When fuzz was at 1.0, I (tim) routinely saw bogus failures on Win2K |
|
0eb948364990
Doubled TimeoutTestCase.fuzz, to slash the frequency of bogus failures
Tim Peters <tim.peters@gmail.com>
parents:
27912
diff
changeset
|
109 # and Win98SE. Boosting it to 2.0 helped a lot, but isn't a real |
|
0eb948364990
Doubled TimeoutTestCase.fuzz, to slash the frequency of bogus failures
Tim Peters <tim.peters@gmail.com>
parents:
27912
diff
changeset
|
110 # solution. |
|
0eb948364990
Doubled TimeoutTestCase.fuzz, to slash the frequency of bogus failures
Tim Peters <tim.peters@gmail.com>
parents:
27912
diff
changeset
|
111 fuzz = 2.0 |
|
23791
cde6608739c4
Add some more basic tests to validate the argument checking of
Guido van Rossum <guido@python.org>
parents:
23790
diff
changeset
|
112 |
|
85302
7728b073c77c
Issue #18792: Use "127.0.0.1" or "::1" instead of "localhost" as much as possible, since "localhost" goes through a DNS lookup under recent Windows versions.
Antoine Pitrou <solipsis@pitrou.net>
parents:
85206
diff
changeset
|
113 localhost = support.HOST |
|
67296
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
114 |
|
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
115 def setUp(self): |
|
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
116 raise NotImplementedError() |
|
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
117 |
|
67400
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
118 tearDown = setUp |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
119 |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
120 def _sock_operation(self, count, timeout, method, *args): |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
121 """ |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
122 Test the specified socket method. |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
123 |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
124 The method is run at most `count` times and must raise a socket.timeout |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
125 within `timeout` + self.fuzz seconds. |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
126 """ |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
127 self.sock.settimeout(timeout) |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
128 method = getattr(self.sock, method) |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
129 for i in range(count): |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
130 t1 = time.time() |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
131 try: |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
132 method(*args) |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
133 except socket.timeout as e: |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
134 delta = time.time() - t1 |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
135 break |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
136 else: |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
137 self.fail('socket.timeout was not raised') |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
138 # These checks should account for timing unprecision |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
139 self.assertLess(delta, timeout + self.fuzz) |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
140 self.assertGreater(delta, timeout - 1.0) |
|
67296
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
141 |
|
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
142 |
|
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
143 class TCPTimeoutTestCase(TimeoutTestCase): |
|
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
144 """TCP test case for socket.socket() timeout functions""" |
|
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
145 |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 def setUp(self): |
|
23793
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
147 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
85206
5880c7d33e13
Issue #16463: Fix a transient test_timeout failure.
Charles-François Natali <cf.natali@gmail.com>
parents:
78690
diff
changeset
|
148 self.addr_remote = resolve_address('www.python.org.', 80) |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 |
|
67400
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
150 def tearDown(self): |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
151 self.sock.close() |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
152 |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 def testConnectTimeout(self): |
|
78690
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
154 # Testing connect timeout is tricky: we need to have IP connectivity |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
155 # to a host that silently drops our packets. We can't simulate this |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
156 # from Python because it's a function of the underlying TCP/IP stack. |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
157 # So, the following Snakebite host has been defined: |
|
85206
5880c7d33e13
Issue #16463: Fix a transient test_timeout failure.
Charles-François Natali <cf.natali@gmail.com>
parents:
78690
diff
changeset
|
158 blackhole = resolve_address('blackhole.snakebite.net', 56666) |
|
78690
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
159 |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
160 # Blackhole has been configured to silently drop any incoming packets. |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
161 # No RSTs (for TCP) or ICMP UNREACH (for UDP/ICMP) will be sent back |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
162 # to hosts that attempt to connect to this address: which is exactly |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
163 # what we need to confidently test connect timeout. |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
164 |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
165 # However, we want to prevent false positives. It's not unreasonable |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
166 # to expect certain hosts may not be able to reach the blackhole, due |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
167 # to firewalling or general network configuration. In order to improve |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
168 # our confidence in testing the blackhole, a corresponding 'whitehole' |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
169 # has also been set up using one port higher: |
|
85206
5880c7d33e13
Issue #16463: Fix a transient test_timeout failure.
Charles-François Natali <cf.natali@gmail.com>
parents:
78690
diff
changeset
|
170 whitehole = resolve_address('whitehole.snakebite.net', 56667) |
|
78690
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
171 |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
172 # This address has been configured to immediately drop any incoming |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
173 # packets as well, but it does it respectfully with regards to the |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
174 # incoming protocol. RSTs are sent for TCP packets, and ICMP UNREACH |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
175 # is sent for UDP/ICMP packets. This means our attempts to connect to |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
176 # it should be met immediately with ECONNREFUSED. The test case has |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
177 # been structured around this premise: if we get an ECONNREFUSED from |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
178 # the whitehole, we proceed with testing connect timeout against the |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
179 # blackhole. If we don't, we skip the test (with a message about not |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
180 # getting the required RST from the whitehole within the required |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
181 # timeframe). |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
182 |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
183 # For the records, the whitehole/blackhole configuration has been set |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
184 # up using the 'pf' firewall (available on BSDs), using the following: |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
185 # |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
186 # ext_if="bge0" |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
187 # |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
188 # blackhole_ip="35.8.247.6" |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
189 # whitehole_ip="35.8.247.6" |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
190 # blackhole_port="56666" |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
191 # whitehole_port="56667" |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
192 # |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
193 # block return in log quick on $ext_if proto { tcp udp } \ |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
194 # from any to $whitehole_ip port $whitehole_port |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
195 # block drop in log quick on $ext_if proto { tcp udp } \ |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
196 # from any to $blackhole_ip port $blackhole_port |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
197 # |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
198 |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
199 skip = True |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
200 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
201 # Use a timeout of 3 seconds. Why 3? Because it's more than 1, and |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
202 # less than 5. i.e. no particular reason. Feel free to tweak it if |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
203 # you feel a different value would be more appropriate. |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
204 timeout = 3 |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
205 sock.settimeout(timeout) |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
206 try: |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
207 sock.connect((whitehole)) |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
208 except socket.timeout: |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
209 pass |
|
81039
7d69d04522e3
Replace IOError with OSError (#16715)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
78690
diff
changeset
|
210 except OSError as err: |
|
78690
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
211 if err.errno == errno.ECONNREFUSED: |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
212 skip = False |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
213 finally: |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
214 sock.close() |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
215 del sock |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
216 |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
217 if skip: |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
218 self.skipTest( |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
219 "We didn't receive a connection reset (RST) packet from " |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
220 "{}:{} within {} seconds, so we're unable to test connect " |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
221 "timeout against the corresponding {}:{} (which is " |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
222 "configured to silently drop packets)." |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
223 .format( |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
224 whitehole[0], |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
225 whitehole[1], |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
226 timeout, |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
227 blackhole[0], |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
228 blackhole[1], |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
229 ) |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
230 ) |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
231 |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
232 # All that hard work just to test if connect times out in 0.001s ;-) |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
233 self.addr_remote = blackhole |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
234 with support.transient_internet(self.addr_remote[0]): |
|
9c22222af1f9
Issue #15285: Refactor connect timeout test in test_timeout.
Trent Nelson <trent@trent.me>
parents:
67400
diff
changeset
|
235 self._sock_operation(1, 0.001, 'connect', self.addr_remote) |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 def testRecvTimeout(self): |
|
24964
d90a6add7ca3
Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
238 # Test recv() timeout |
|
65718
61a9ae522dcc
Issue #7547: fix transient failures due to network glitches in test_timeout.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58740
diff
changeset
|
239 with support.transient_internet(self.addr_remote[0]): |
|
61a9ae522dcc
Issue #7547: fix transient failures due to network glitches in test_timeout.
Antoine Pitrou <solipsis@pitrou.net>
parents:
58740
diff
changeset
|
240 self.sock.connect(self.addr_remote) |
|
67400
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
241 self._sock_operation(1, 1.5, 'recv', 1024) |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
242 |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 def testAcceptTimeout(self): |
|
24964
d90a6add7ca3
Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
244 # Test accept() timeout |
|
58740
0eb422624db5
Merged revisions 78014 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55593
diff
changeset
|
245 support.bind_port(self.sock, self.localhost) |
|
23793
7cdc531e6a50
Docstring, layout and style tweaking. Increase fuzz to 1 second.
Guido van Rossum <guido@python.org>
parents:
23791
diff
changeset
|
246 self.sock.listen(5) |
|
67400
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
247 self._sock_operation(1, 1.5, 'accept') |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
248 |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
249 def testSend(self): |
|
24964
d90a6add7ca3
Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
250 # Test send() timeout |
|
67400
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
251 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serv: |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
252 support.bind_port(serv, self.localhost) |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
253 serv.listen(5) |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
254 self.sock.connect(serv.getsockname()) |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
255 # Send a lot of data in order to bypass buffering in the TCP stack. |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
256 self._sock_operation(100, 1.5, 'send', b"X" * 200000) |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 def testSendto(self): |
|
24964
d90a6add7ca3
Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
259 # Test sendto() timeout |
|
67400
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
260 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serv: |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
261 support.bind_port(serv, self.localhost) |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
262 serv.listen(5) |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
263 self.sock.connect(serv.getsockname()) |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
264 # The address argument is ignored since we already connected. |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
265 self._sock_operation(100, 1.5, 'sendto', b"X" * 200000, |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
266 serv.getsockname()) |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
267 |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
268 def testSendall(self): |
|
24964
d90a6add7ca3
Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
269 # Test sendall() timeout |
|
67400
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
270 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serv: |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
271 support.bind_port(serv, self.localhost) |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
272 serv.listen(5) |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
273 self.sock.connect(serv.getsockname()) |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
274 # Send a lot of data in order to bypass buffering in the TCP stack. |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
275 self._sock_operation(100, 1.5, 'sendall', b"X" * 200000) |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
276 |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
277 |
|
67296
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
278 class UDPTimeoutTestCase(TimeoutTestCase): |
|
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
279 """UDP test case for socket.socket() timeout functions""" |
|
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
280 |
|
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
281 def setUp(self): |
|
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
282 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
|
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
283 |
|
67400
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
284 def tearDown(self): |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
285 self.sock.close() |
|
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
286 |
|
67296
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
287 def testRecvfromTimeout(self): |
|
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
288 # Test recvfrom() timeout |
|
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
289 # Prevent "Address already in use" socket exceptions |
|
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
290 support.bind_port(self.sock, self.localhost) |
|
67400
71e1a9961836
Issue #1677694: Refactor and improve test_timeout. Original patch by
Antoine Pitrou <solipsis@pitrou.net>
parents:
67296
diff
changeset
|
291 self._sock_operation(1, 1.5, 'recvfrom', 1024) |
|
67296
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
292 |
|
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
293 |
|
27912
c5ebacbba0be
Actually run these tests from regrtest.py.
Neal Norwitz <nnorwitz@gmail.com>
parents:
25106
diff
changeset
|
294 def test_main(): |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46115
diff
changeset
|
295 support.requires('network') |
|
67296
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
296 support.run_unittest( |
|
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
297 CreationTestCase, |
|
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
298 TCPTimeoutTestCase, |
|
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
299 UDPTimeoutTestCase, |
|
f5b105c9f46b
test_timeout: move testRecvfromTimeout() to a UDP-specific test case
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65718
diff
changeset
|
300 ) |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
301 |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 if __name__ == "__main__": |
|
27912
c5ebacbba0be
Actually run these tests from regrtest.py.
Neal Norwitz <nnorwitz@gmail.com>
parents:
25106
diff
changeset
|
303 test_main() |
