cpython: b86f3af4746c (original) (raw)
Mercurial > cpython
changeset 78691:b86f3af4746c
Issue #15285: Refactor connect timeout test in test_timeout. [#15285]
Trent Nelson trent@trent.me | |
---|---|
date | Mon, 20 Aug 2012 21:40:21 -0400 |
parents | f8eabfed9a1d(current diff)9c22222af1f9(diff) |
children | 221a640475f7 |
files | Misc/NEWS |
diffstat | 2 files changed, 86 insertions(+), 8 deletions(-)[+] [-] Lib/test/test_timeout.py 90 Misc/NEWS 4 |
line wrap: on
line diff
--- a/Lib/test/test_timeout.py +++ b/Lib/test/test_timeout.py @@ -138,14 +138,88 @@ class TCPTimeoutTestCase(TimeoutTestCase self.sock.close() def testConnectTimeout(self):
# Choose a private address that is unlikely to exist to prevent[](#l1.7)
# failures due to the connect succeeding before the timeout.[](#l1.8)
# Use a dotted IP address to avoid including the DNS lookup time[](#l1.9)
# with the connect time. This avoids failing the assertion that[](#l1.10)
# the timeout occurred fast enough.[](#l1.11)
addr = ('10.0.0.0', 12345)[](#l1.12)
with support.transient_internet(addr[0]):[](#l1.13)
self._sock_operation(1, 0.001, 'connect', addr)[](#l1.14)
# Testing connect timeout is tricky: we need to have IP connectivity[](#l1.15)
# to a host that silently drops our packets. We can't simulate this[](#l1.16)
# from Python because it's a function of the underlying TCP/IP stack.[](#l1.17)
# So, the following Snakebite host has been defined:[](#l1.18)
blackhole = ('blackhole.snakebite.net', 56666)[](#l1.19)
# Blackhole has been configured to silently drop any incoming packets.[](#l1.21)
# No RSTs (for TCP) or ICMP UNREACH (for UDP/ICMP) will be sent back[](#l1.22)
# to hosts that attempt to connect to this address: which is exactly[](#l1.23)
# what we need to confidently test connect timeout.[](#l1.24)
# However, we want to prevent false positives. It's not unreasonable[](#l1.26)
# to expect certain hosts may not be able to reach the blackhole, due[](#l1.27)
# to firewalling or general network configuration. In order to improve[](#l1.28)
# our confidence in testing the blackhole, a corresponding 'whitehole'[](#l1.29)
# has also been set up using one port higher:[](#l1.30)
whitehole = ('whitehole.snakebite.net', 56667)[](#l1.31)
# This address has been configured to immediately drop any incoming[](#l1.33)
# packets as well, but it does it respectfully with regards to the[](#l1.34)
# incoming protocol. RSTs are sent for TCP packets, and ICMP UNREACH[](#l1.35)
# is sent for UDP/ICMP packets. This means our attempts to connect to[](#l1.36)
# it should be met immediately with ECONNREFUSED. The test case has[](#l1.37)
# been structured around this premise: if we get an ECONNREFUSED from[](#l1.38)
# the whitehole, we proceed with testing connect timeout against the[](#l1.39)
# blackhole. If we don't, we skip the test (with a message about not[](#l1.40)
# getting the required RST from the whitehole within the required[](#l1.41)
# timeframe).[](#l1.42)
# For the records, the whitehole/blackhole configuration has been set[](#l1.44)
# up using the 'pf' firewall (available on BSDs), using the following:[](#l1.45)
#[](#l1.46)
# ext_if="bge0"[](#l1.47)
#[](#l1.48)
# blackhole_ip="35.8.247.6"[](#l1.49)
# whitehole_ip="35.8.247.6"[](#l1.50)
# blackhole_port="56666"[](#l1.51)
# whitehole_port="56667"[](#l1.52)
#[](#l1.53)
# block return in log quick on $ext_if proto { tcp udp } \[](#l1.54)
# from any to <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>w</mi><mi>h</mi><mi>i</mi><mi>t</mi><mi>e</mi><mi>h</mi><mi>o</mi><mi>l</mi><msub><mi>e</mi><mi>i</mi></msub><mi>p</mi><mi>p</mi><mi>o</mi><mi>r</mi><mi>t</mi></mrow><annotation encoding="application/x-tex">whitehole_ip port </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">hi</span><span class="mord mathnormal">t</span><span class="mord mathnormal">e</span><span class="mord mathnormal">h</span><span class="mord mathnormal">o</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord"><span class="mord mathnormal">e</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3117em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">pp</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span><span class="mord mathnormal">t</span></span></span></span>whitehole_port[](#l1.55)
# block drop in log quick on $ext_if proto { tcp udp } \[](#l1.56)
# from any to <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>b</mi><mi>l</mi><mi>a</mi><mi>c</mi><mi>k</mi><mi>h</mi><mi>o</mi><mi>l</mi><msub><mi>e</mi><mi>i</mi></msub><mi>p</mi><mi>p</mi><mi>o</mi><mi>r</mi><mi>t</mi></mrow><annotation encoding="application/x-tex">blackhole_ip port </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">b</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">a</span><span class="mord mathnormal">c</span><span class="mord mathnormal">kh</span><span class="mord mathnormal">o</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord"><span class="mord mathnormal">e</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3117em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">pp</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span><span class="mord mathnormal">t</span></span></span></span>blackhole_port[](#l1.57)
#[](#l1.58)
skip = True[](#l1.60)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)[](#l1.61)
# Use a timeout of 3 seconds. Why 3? Because it's more than 1, and[](#l1.62)
# less than 5. i.e. no particular reason. Feel free to tweak it if[](#l1.63)
# you feel a different value would be more appropriate.[](#l1.64)
timeout = 3[](#l1.65)
sock.settimeout(timeout)[](#l1.66)
try:[](#l1.67)
sock.connect((whitehole))[](#l1.68)
except socket.timeout:[](#l1.69)
pass[](#l1.70)
except IOError as err:[](#l1.71)
if err.errno == errno.ECONNREFUSED:[](#l1.72)
skip = False[](#l1.73)
finally:[](#l1.74)
sock.close()[](#l1.75)
del sock[](#l1.76)
if skip:[](#l1.78)
self.skipTest([](#l1.79)
"We didn't receive a connection reset (RST) packet from "[](#l1.80)
"{}:{} within {} seconds, so we're unable to test connect "[](#l1.81)
"timeout against the corresponding {}:{} (which is "[](#l1.82)
"configured to silently drop packets)."[](#l1.83)
.format([](#l1.84)
whitehole[0],[](#l1.85)
whitehole[1],[](#l1.86)
timeout,[](#l1.87)
blackhole[0],[](#l1.88)
blackhole[1],[](#l1.89)
)[](#l1.90)
)[](#l1.91)
# All that hard work just to test if connect times out in 0.001s ;-)[](#l1.93)
self.addr_remote = blackhole[](#l1.94)
with support.transient_internet(self.addr_remote[0]):[](#l1.95)
self._sock_operation(1, 0.001, 'connect', self.addr_remote)[](#l1.96)
def testRecvTimeout(self): # Test recv() timeout
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -86,6 +86,10 @@ Documentation Tests ----- +- Issue #15285: Refactor the approach for testing connect timeouts using