| 1 |
"""Unit tests for socket timeout feature.""" |
| 2 |
|
| 3 |
import unittest |
| 4 |
from test import test_support |
| 5 |
|
| 6 |
|
| 7 |
skip_expected = not test_support.is_resource_enabled('network') |
| 8 |
|
| 9 |
import time |
| 10 |
import socket |
| 11 |
|
| 12 |
|
| 13 |
class CreationTestCase(unittest.TestCase): |
| 14 |
"""Test case for socket.gettimeout() and socket.settimeout()""" |
| 15 |
|
| 16 |
def setUp(self): |
| 17 |
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 18 |
|
| 19 |
def tearDown(self): |
| 20 |
self.sock.close() |
| 21 |
|
| 22 |
def testObjectCreation(self): |
| 23 |
|
| 24 |
self.assertEqual(self.sock.gettimeout(), None, |
| 25 |
"timeout not disabled by default") |
| 26 |
|
| 27 |
def testFloatReturnValue(self): |
| 28 |
|
| 29 |
self.sock.settimeout(7.345) |
| 30 |
self.assertEqual(self.sock.gettimeout(), 7.345) |
| 31 |
|
| 32 |
self.sock.settimeout(3) |
| 33 |
self.assertEqual(self.sock.gettimeout(), 3) |
| 34 |
|
| 35 |
self.sock.settimeout(None) |
| 36 |
self.assertEqual(self.sock.gettimeout(), None) |
| 37 |
|
| 38 |
def testReturnType(self): |
| 39 |
|
| 40 |
self.sock.settimeout(1) |
| 41 |
self.assertEqual(type(self.sock.gettimeout()), type(1.0)) |
| 42 |
|
| 43 |
self.sock.settimeout(3.9) |
| 44 |
self.assertEqual(type(self.sock.gettimeout()), type(1.0)) |
| 45 |
|
| 46 |
def testTypeCheck(self): |
| 47 |
|
| 48 |
self.sock.settimeout(0) |
| 49 |
self.sock.settimeout(0L) |
| 50 |
self.sock.settimeout(0.0) |
| 51 |
self.sock.settimeout(None) |
| 52 |
self.assertRaises(TypeError, self.sock.settimeout, "") |
| 53 |
self.assertRaises(TypeError, self.sock.settimeout, u"") |
| 54 |
self.assertRaises(TypeError, self.sock.settimeout, ()) |
| 55 |
self.assertRaises(TypeError, self.sock.settimeout, []) |
| 56 |
self.assertRaises(TypeError, self.sock.settimeout, {}) |
| 57 |
self.assertRaises(TypeError, self.sock.settimeout, 0j) |
| 58 |
|
| 59 |
def testRangeCheck(self): |
| 60 |
|
| 61 |
self.assertRaises(ValueError, self.sock.settimeout, -1) |
| 62 |
self.assertRaises(ValueError, self.sock.settimeout, -1L) |
| 63 |
self.assertRaises(ValueError, self.sock.settimeout, -1.0) |
| 64 |
|
| 65 |
def testTimeoutThenBlocking(self): |
| 66 |
|
| 67 |
self.sock.settimeout(10) |
| 68 |
self.sock.setblocking(1) |
| 69 |
self.assertEqual(self.sock.gettimeout(), None) |
| 70 |
self.sock.setblocking(0) |
| 71 |
self.assertEqual(self.sock.gettimeout(), 0.0) |
| 72 |
|
| 73 |
self.sock.settimeout(10) |
| 74 |
self.sock.setblocking(0) |
| 75 |
self.assertEqual(self.sock.gettimeout(), 0.0) |
| 76 |
self.sock.setblocking(1) |
| 77 |
self.assertEqual(self.sock.gettimeout(), None) |
| 78 |
|
| 79 |
def testBlockingThenTimeout(self): |
| 80 |
|
| 81 |
self.sock.setblocking(0) |
| 82 |
self.sock.settimeout(1) |
| 83 |
self.assertEqual(self.sock.gettimeout(), 1) |
| 84 |
|
| 85 |
self.sock.setblocking(1) |
| 86 |
self.sock.settimeout(1) |
| 87 |
self.assertEqual(self.sock.gettimeout(), 1) |
| 88 |
|
| 89 |
|
| 90 |
class TimeoutTestCase(unittest.TestCase): |
| 91 |
"""Test case for socket.socket() timeout functions""" |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
fuzz = 2.0 |
| 100 |
|
| 101 |
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' |
| 105 |
|
| 106 |
def tearDown(self): |
| 107 |
self.sock.close() |
| 108 |
|
| 109 |
@unittest.skipIf(True, 'need to replace these hosts; see bpo-35518') |
| 110 |
def testConnectTimeout(self): |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
addr = ('10.0.0.0', 12345) |
| 117 |
|
| 118 |
|
| 119 |
_timeout = 0.001 |
| 120 |
self.sock.settimeout(_timeout) |
| 121 |
|
| 122 |
_t1 = time.time() |
| 123 |
self.assertRaises(socket.error, self.sock.connect, addr) |
| 124 |
_t2 = time.time() |
| 125 |
|
| 126 |
_delta = abs(_t1 - _t2) |
| 127 |
self.assertTrue(_delta < _timeout + self.fuzz, |
| 128 |
"timeout (%g) is more than %g seconds more than expected (%g)" |
| 129 |
%(_delta, self.fuzz, _timeout)) |
| 130 |
|
| 131 |
def testRecvTimeout(self): |
| 132 |
|
| 133 |
_timeout = 0.02 |
| 134 |
|
| 135 |
with test_support.transient_internet(self.addr_remote[0]): |
| 136 |
self.sock.connect(self.addr_remote) |
| 137 |
self.sock.settimeout(_timeout) |
| 138 |
|
| 139 |
_t1 = time.time() |
| 140 |
self.assertRaises(socket.timeout, self.sock.recv, 1024) |
| 141 |
_t2 = time.time() |
| 142 |
|
| 143 |
_delta = abs(_t1 - _t2) |
| 144 |
self.assertTrue(_delta < _timeout + self.fuzz, |
| 145 |
"timeout (%g) is %g seconds more than expected (%g)" |
| 146 |
%(_delta, self.fuzz, _timeout)) |
| 147 |
|
| 148 |
def testAcceptTimeout(self): |
| 149 |
|
| 150 |
_timeout = 2 |
| 151 |
self.sock.settimeout(_timeout) |
| 152 |
|
| 153 |
test_support.bind_port(self.sock, self.localhost) |
| 154 |
self.sock.listen(5) |
| 155 |
|
| 156 |
_t1 = time.time() |
| 157 |
self.assertRaises(socket.error, self.sock.accept) |
| 158 |
_t2 = time.time() |
| 159 |
|
| 160 |
_delta = abs(_t1 - _t2) |
| 161 |
self.assertTrue(_delta < _timeout + self.fuzz, |
| 162 |
"timeout (%g) is %g seconds more than expected (%g)" |
| 163 |
%(_delta, self.fuzz, _timeout)) |
| 164 |
|
| 165 |
def testRecvfromTimeout(self): |
| 166 |
|
| 167 |
_timeout = 2 |
| 168 |
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 169 |
self.sock.settimeout(_timeout) |
| 170 |
|
| 171 |
test_support.bind_port(self.sock, self.localhost) |
| 172 |
|
| 173 |
_t1 = time.time() |
| 174 |
self.assertRaises(socket.error, self.sock.recvfrom, 8192) |
| 175 |
_t2 = time.time() |
| 176 |
|
| 177 |
_delta = abs(_t1 - _t2) |
| 178 |
self.assertTrue(_delta < _timeout + self.fuzz, |
| 179 |
"timeout (%g) is %g seconds more than expected (%g)" |
| 180 |
%(_delta, self.fuzz, _timeout)) |
| 181 |
|
| 182 |
@unittest.skip('test not implemented') |
| 183 |
def testSend(self): |
| 184 |
|
| 185 |
|
| 186 |
pass |
| 187 |
|
| 188 |
@unittest.skip('test not implemented') |
| 189 |
def testSendto(self): |
| 190 |
|
| 191 |
|
| 192 |
pass |
| 193 |
|
| 194 |
@unittest.skip('test not implemented') |
| 195 |
def testSendall(self): |
| 196 |
|
| 197 |
|
| 198 |
pass |
| 199 |
|
| 200 |
|
| 201 |
def test_main(): |
| 202 |
test_support.requires('network') |
| 203 |
test_support.run_unittest(CreationTestCase, TimeoutTestCase) |
| 204 |
|
| 205 |
if __name__ == "__main__": |
| 206 |
test_main() |