test_timeout.py 10.7 KB
Newer Older
1
"""Unit tests for socket timeout feature."""
2 3

import unittest
4
from test import support
5

6
# This requires the 'network' resource as given on the regrtest command line.
7
skip_expected = not support.is_resource_enabled('network')
8

9
import time
10
import errno
11 12
import socket

13

14
class CreationTestCase(unittest.TestCase):
15
    """Test case for socket.gettimeout() and socket.settimeout()"""
16

17
    def setUp(self):
18
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
19 20

    def tearDown(self):
21
        self.sock.close()
22 23

    def testObjectCreation(self):
24
        # Test Socket creation
25 26
        self.assertEqual(self.sock.gettimeout(), None,
                         "timeout not disabled by default")
27 28

    def testFloatReturnValue(self):
29
        # Test return value of gettimeout()
30 31
        self.sock.settimeout(7.345)
        self.assertEqual(self.sock.gettimeout(), 7.345)
32

33 34
        self.sock.settimeout(3)
        self.assertEqual(self.sock.gettimeout(), 3)
35

36 37
        self.sock.settimeout(None)
        self.assertEqual(self.sock.gettimeout(), None)
38

39
    def testReturnType(self):
40
        # Test return type of gettimeout()
41 42
        self.sock.settimeout(1)
        self.assertEqual(type(self.sock.gettimeout()), type(1.0))
43

44 45
        self.sock.settimeout(3.9)
        self.assertEqual(type(self.sock.gettimeout()), type(1.0))
46 47

    def testTypeCheck(self):
48
        # Test type checking by settimeout()
49
        self.sock.settimeout(0)
50
        self.sock.settimeout(0)
51 52 53
        self.sock.settimeout(0.0)
        self.sock.settimeout(None)
        self.assertRaises(TypeError, self.sock.settimeout, "")
54
        self.assertRaises(TypeError, self.sock.settimeout, "")
55 56 57 58
        self.assertRaises(TypeError, self.sock.settimeout, ())
        self.assertRaises(TypeError, self.sock.settimeout, [])
        self.assertRaises(TypeError, self.sock.settimeout, {})
        self.assertRaises(TypeError, self.sock.settimeout, 0j)
59 60

    def testRangeCheck(self):
61
        # Test range checking by settimeout()
62
        self.assertRaises(ValueError, self.sock.settimeout, -1)
63
        self.assertRaises(ValueError, self.sock.settimeout, -1)
64
        self.assertRaises(ValueError, self.sock.settimeout, -1.0)
65

66
    def testTimeoutThenBlocking(self):
67
        # Test settimeout() followed by setblocking()
68 69 70 71
        self.sock.settimeout(10)
        self.sock.setblocking(1)
        self.assertEqual(self.sock.gettimeout(), None)
        self.sock.setblocking(0)
72
        self.assertEqual(self.sock.gettimeout(), 0.0)
73 74 75

        self.sock.settimeout(10)
        self.sock.setblocking(0)
76
        self.assertEqual(self.sock.gettimeout(), 0.0)
77 78
        self.sock.setblocking(1)
        self.assertEqual(self.sock.gettimeout(), None)
79 80

    def testBlockingThenTimeout(self):
81
        # Test setblocking() followed by settimeout()
82 83 84
        self.sock.setblocking(0)
        self.sock.settimeout(1)
        self.assertEqual(self.sock.gettimeout(), 1)
85

86 87 88
        self.sock.setblocking(1)
        self.sock.settimeout(1)
        self.assertEqual(self.sock.gettimeout(), 1)
89

90

91
class TimeoutTestCase(unittest.TestCase):
92 93 94 95 96 97 98
    # There are a number of tests here trying to make sure that an operation
    # doesn't take too much longer than expected.  But competing machine
    # activity makes it inevitable that such tests will fail at times.
    # When fuzz was at 1.0, I (tim) routinely saw bogus failures on Win2K
    # and Win98SE.  Boosting it to 2.0 helped a lot, but isn't a real
    # solution.
    fuzz = 2.0
99

100 101
    localhost = '127.0.0.1'

102
    def setUp(self):
103
        raise NotImplementedError()
104

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    tearDown = setUp

    def _sock_operation(self, count, timeout, method, *args):
        """
        Test the specified socket method.

        The method is run at most `count` times and must raise a socket.timeout
        within `timeout` + self.fuzz seconds.
        """
        self.sock.settimeout(timeout)
        method = getattr(self.sock, method)
        for i in range(count):
            t1 = time.time()
            try:
                method(*args)
            except socket.timeout as e:
                delta = time.time() - t1
                break
        else:
            self.fail('socket.timeout was not raised')
        # These checks should account for timing unprecision
        self.assertLess(delta, timeout + self.fuzz)
        self.assertGreater(delta, timeout - 1.0)
128

129 130 131 132 133 134 135 136

class TCPTimeoutTestCase(TimeoutTestCase):
    """TCP test case for socket.socket() timeout functions"""

    def setUp(self):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.addr_remote = ('www.python.org.', 80)

137 138 139
    def tearDown(self):
        self.sock.close()

140
    def testConnectTimeout(self):
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
        # Testing connect timeout is tricky: we need to have IP connectivity
        # to a host that silently drops our packets.  We can't simulate this
        # from Python because it's a function of the underlying TCP/IP stack.
        # So, the following Snakebite host has been defined:
        blackhole = ('blackhole.snakebite.net', 56666)

        # Blackhole has been configured to silently drop any incoming packets.
        # No RSTs (for TCP) or ICMP UNREACH (for UDP/ICMP) will be sent back
        # to hosts that attempt to connect to this address: which is exactly
        # what we need to confidently test connect timeout.

        # However, we want to prevent false positives.  It's not unreasonable
        # to expect certain hosts may not be able to reach the blackhole, due
        # to firewalling or general network configuration.  In order to improve
        # our confidence in testing the blackhole, a corresponding 'whitehole'
        # has also been set up using one port higher:
        whitehole = ('whitehole.snakebite.net', 56667)

        # This address has been configured to immediately drop any incoming
        # packets as well, but it does it respectfully with regards to the
        # incoming protocol.  RSTs are sent for TCP packets, and ICMP UNREACH
        # is sent for UDP/ICMP packets.  This means our attempts to connect to
        # it should be met immediately with ECONNREFUSED.  The test case has
        # been structured around this premise: if we get an ECONNREFUSED from
        # the whitehole, we proceed with testing connect timeout against the
        # blackhole.  If we don't, we skip the test (with a message about not
        # getting the required RST from the whitehole within the required
        # timeframe).

        # For the records, the whitehole/blackhole configuration has been set
        # up using the 'pf' firewall (available on BSDs), using the following:
        #
        #   ext_if="bge0"
        #
        #   blackhole_ip="35.8.247.6"
        #   whitehole_ip="35.8.247.6"
        #   blackhole_port="56666"
        #   whitehole_port="56667"
        #
        #   block return in log quick on $ext_if proto { tcp udp } \
        #       from any to $whitehole_ip port $whitehole_port
        #   block drop in log quick on $ext_if proto { tcp udp } \
        #       from any to $blackhole_ip port $blackhole_port
        #

        skip = True
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        # Use a timeout of 3 seconds.  Why 3?  Because it's more than 1, and
        # less than 5.  i.e. no particular reason.  Feel free to tweak it if
        # you feel a different value would be more appropriate.
        timeout = 3
        sock.settimeout(timeout)
        try:
            sock.connect((whitehole))
        except socket.timeout:
            pass
        except IOError as err:
            if err.errno == errno.ECONNREFUSED:
                skip = False
        finally:
            sock.close()
            del sock

        if skip:
            self.skipTest(
                "We didn't receive a connection reset (RST) packet from "
                "{}:{} within {} seconds, so we're unable to test connect "
                "timeout against the corresponding {}:{} (which is "
                "configured to silently drop packets)."
                    .format(
                        whitehole[0],
                        whitehole[1],
                        timeout,
                        blackhole[0],
                        blackhole[1],
                    )
            )

        # All that hard work just to test if connect times out in 0.001s ;-)
        self.addr_remote = blackhole
        with support.transient_internet(self.addr_remote[0]):
            self._sock_operation(1, 0.001, 'connect', self.addr_remote)
223 224

    def testRecvTimeout(self):
225
        # Test recv() timeout
226 227
        with support.transient_internet(self.addr_remote[0]):
            self.sock.connect(self.addr_remote)
228
            self._sock_operation(1, 1.5, 'recv', 1024)
229 230

    def testAcceptTimeout(self):
231
        # Test accept() timeout
232
        support.bind_port(self.sock, self.localhost)
233
        self.sock.listen(5)
234
        self._sock_operation(1, 1.5, 'accept')
235

236 237
    def testSend(self):
        # Test send() timeout
238 239 240 241 242 243
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serv:
            support.bind_port(serv, self.localhost)
            serv.listen(5)
            self.sock.connect(serv.getsockname())
            # Send a lot of data in order to bypass buffering in the TCP stack.
            self._sock_operation(100, 1.5, 'send', b"X" * 200000)
244 245 246

    def testSendto(self):
        # Test sendto() timeout
247 248 249 250 251 252 253
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serv:
            support.bind_port(serv, self.localhost)
            serv.listen(5)
            self.sock.connect(serv.getsockname())
            # The address argument is ignored since we already connected.
            self._sock_operation(100, 1.5, 'sendto', b"X" * 200000,
                                 serv.getsockname())
254 255 256

    def testSendall(self):
        # Test sendall() timeout
257 258 259 260 261 262
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serv:
            support.bind_port(serv, self.localhost)
            serv.listen(5)
            self.sock.connect(serv.getsockname())
            # Send a lot of data in order to bypass buffering in the TCP stack.
            self._sock_operation(100, 1.5, 'sendall', b"X" * 200000)
263 264 265 266 267 268 269 270


class UDPTimeoutTestCase(TimeoutTestCase):
    """UDP test case for socket.socket() timeout functions"""

    def setUp(self):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

271 272 273
    def tearDown(self):
        self.sock.close()

274
    def testRecvfromTimeout(self):
275
        # Test recvfrom() timeout
276 277
        # Prevent "Address already in use" socket exceptions
        support.bind_port(self.sock, self.localhost)
278
        self._sock_operation(1, 1.5, 'recvfrom', 1024)
279 280


281
def test_main():
282
    support.requires('network')
283 284 285 286 287
    support.run_unittest(
        CreationTestCase,
        TCPTimeoutTestCase,
        UDPTimeoutTestCase,
    )
288 289

if __name__ == "__main__":
290
    test_main()