Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1 | import asyncore |
| 2 | import unittest |
| 3 | import select |
| 4 | import os |
| 5 | import socket |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 6 | import sys |
| 7 | import time |
Giampaolo Rodolà | 8d2dc85 | 2010-05-06 18:06:30 +0000 | [diff] [blame] | 8 | import warnings |
Giampaolo Rodolà | 82e02b5 | 2010-05-18 20:11:58 +0000 | [diff] [blame] | 9 | import errno |
Giampaolo Rodola' | 350c94b | 2012-03-22 16:17:43 +0100 | [diff] [blame] | 10 | import struct |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 11 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 12 | from test import support |
| 13 | from test.support import TESTFN, run_unittest, unlink |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 14 | from io import BytesIO |
| 15 | from io import StringIO |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 16 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 17 | try: |
| 18 | import threading |
| 19 | except ImportError: |
| 20 | threading = None |
| 21 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 22 | HOST = support.HOST |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 23 | |
Charles-François Natali | e3540b4 | 2011-08-25 00:50:41 +0200 | [diff] [blame] | 24 | HAS_UNIX_SOCKETS = hasattr(socket, 'AF_UNIX') |
| 25 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 26 | class dummysocket: |
| 27 | def __init__(self): |
| 28 | self.closed = False |
| 29 | |
| 30 | def close(self): |
| 31 | self.closed = True |
| 32 | |
| 33 | def fileno(self): |
| 34 | return 42 |
| 35 | |
| 36 | class dummychannel: |
| 37 | def __init__(self): |
| 38 | self.socket = dummysocket() |
| 39 | |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 40 | def close(self): |
| 41 | self.socket.close() |
| 42 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 43 | class exitingdummy: |
| 44 | def __init__(self): |
| 45 | pass |
| 46 | |
| 47 | def handle_read_event(self): |
| 48 | raise asyncore.ExitNow() |
| 49 | |
| 50 | handle_write_event = handle_read_event |
Josiah Carlson | 91823c7 | 2008-07-11 23:26:37 +0000 | [diff] [blame] | 51 | handle_close = handle_read_event |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 52 | handle_expt_event = handle_read_event |
| 53 | |
| 54 | class crashingdummy: |
| 55 | def __init__(self): |
| 56 | self.error_handled = False |
| 57 | |
| 58 | def handle_read_event(self): |
| 59 | raise Exception() |
| 60 | |
| 61 | handle_write_event = handle_read_event |
Josiah Carlson | 91823c7 | 2008-07-11 23:26:37 +0000 | [diff] [blame] | 62 | handle_close = handle_read_event |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 63 | handle_expt_event = handle_read_event |
| 64 | |
| 65 | def handle_error(self): |
| 66 | self.error_handled = True |
| 67 | |
| 68 | # used when testing senders; just collects what it gets until newline is sent |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 69 | def capture_server(evt, buf, serv): |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 70 | try: |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 71 | serv.listen(5) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 72 | conn, addr = serv.accept() |
| 73 | except socket.timeout: |
| 74 | pass |
| 75 | else: |
| 76 | n = 200 |
| 77 | while n > 0: |
Guido van Rossum | 36e0a92 | 2007-07-20 04:05:57 +0000 | [diff] [blame] | 78 | r, w, e = select.select([conn], [], []) |
| 79 | if r: |
| 80 | data = conn.recv(10) |
Guido van Rossum | 36e0a92 | 2007-07-20 04:05:57 +0000 | [diff] [blame] | 81 | # keep everything except for the newline terminator |
| 82 | buf.write(data.replace(b'\n', b'')) |
| 83 | if b'\n' in data: |
| 84 | break |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 85 | n -= 1 |
| 86 | time.sleep(0.01) |
| 87 | |
| 88 | conn.close() |
| 89 | finally: |
| 90 | serv.close() |
| 91 | evt.set() |
| 92 | |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 93 | def bind_af_aware(sock, addr): |
| 94 | """Helper function to bind a socket according to its family.""" |
Charles-François Natali | e3540b4 | 2011-08-25 00:50:41 +0200 | [diff] [blame] | 95 | if HAS_UNIX_SOCKETS and sock.family == socket.AF_UNIX: |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 96 | # Make sure the path doesn't exist. |
| 97 | unlink(addr) |
| 98 | sock.bind(addr) |
| 99 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 100 | |
| 101 | class HelperFunctionTests(unittest.TestCase): |
| 102 | def test_readwriteexc(self): |
| 103 | # Check exception handling behavior of read, write and _exception |
| 104 | |
| 105 | # check that ExitNow exceptions in the object handler method |
| 106 | # bubbles all the way up through asyncore read/write/_exception calls |
| 107 | tr1 = exitingdummy() |
| 108 | self.assertRaises(asyncore.ExitNow, asyncore.read, tr1) |
| 109 | self.assertRaises(asyncore.ExitNow, asyncore.write, tr1) |
| 110 | self.assertRaises(asyncore.ExitNow, asyncore._exception, tr1) |
| 111 | |
| 112 | # check that an exception other than ExitNow in the object handler |
| 113 | # method causes the handle_error method to get called |
| 114 | tr2 = crashingdummy() |
| 115 | asyncore.read(tr2) |
| 116 | self.assertEqual(tr2.error_handled, True) |
| 117 | |
| 118 | tr2 = crashingdummy() |
| 119 | asyncore.write(tr2) |
| 120 | self.assertEqual(tr2.error_handled, True) |
| 121 | |
| 122 | tr2 = crashingdummy() |
| 123 | asyncore._exception(tr2) |
| 124 | self.assertEqual(tr2.error_handled, True) |
| 125 | |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 126 | # asyncore.readwrite uses constants in the select module that |
| 127 | # are not present in Windows systems (see this thread: |
| 128 | # http://mail.python.org/pipermail/python-list/2001-October/109973.html) |
| 129 | # These constants should be present as long as poll is available |
| 130 | |
Ezio Melotti | 63c4640 | 2010-07-27 22:03:33 +0000 | [diff] [blame] | 131 | @unittest.skipUnless(hasattr(select, 'poll'), 'select.poll required') |
| 132 | def test_readwrite(self): |
| 133 | # Check that correct methods are called by readwrite() |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 134 | |
Ezio Melotti | 63c4640 | 2010-07-27 22:03:33 +0000 | [diff] [blame] | 135 | attributes = ('read', 'expt', 'write', 'closed', 'error_handled') |
R. David Murray | 78532ba | 2009-04-12 15:35:44 +0000 | [diff] [blame] | 136 | |
Ezio Melotti | 63c4640 | 2010-07-27 22:03:33 +0000 | [diff] [blame] | 137 | expected = ( |
| 138 | (select.POLLIN, 'read'), |
| 139 | (select.POLLPRI, 'expt'), |
| 140 | (select.POLLOUT, 'write'), |
| 141 | (select.POLLERR, 'closed'), |
| 142 | (select.POLLHUP, 'closed'), |
| 143 | (select.POLLNVAL, 'closed'), |
| 144 | ) |
R. David Murray | 78532ba | 2009-04-12 15:35:44 +0000 | [diff] [blame] | 145 | |
Ezio Melotti | 63c4640 | 2010-07-27 22:03:33 +0000 | [diff] [blame] | 146 | class testobj: |
| 147 | def __init__(self): |
| 148 | self.read = False |
| 149 | self.write = False |
| 150 | self.closed = False |
| 151 | self.expt = False |
| 152 | self.error_handled = False |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 153 | |
Ezio Melotti | 63c4640 | 2010-07-27 22:03:33 +0000 | [diff] [blame] | 154 | def handle_read_event(self): |
| 155 | self.read = True |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 156 | |
Ezio Melotti | 63c4640 | 2010-07-27 22:03:33 +0000 | [diff] [blame] | 157 | def handle_write_event(self): |
| 158 | self.write = True |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 159 | |
Ezio Melotti | 63c4640 | 2010-07-27 22:03:33 +0000 | [diff] [blame] | 160 | def handle_close(self): |
| 161 | self.closed = True |
Josiah Carlson | 9f2f833 | 2008-07-07 05:04:12 +0000 | [diff] [blame] | 162 | |
Ezio Melotti | 63c4640 | 2010-07-27 22:03:33 +0000 | [diff] [blame] | 163 | def handle_expt_event(self): |
| 164 | self.expt = True |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 165 | |
Ezio Melotti | 63c4640 | 2010-07-27 22:03:33 +0000 | [diff] [blame] | 166 | def handle_error(self): |
| 167 | self.error_handled = True |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 168 | |
Ezio Melotti | 63c4640 | 2010-07-27 22:03:33 +0000 | [diff] [blame] | 169 | for flag, expectedattr in expected: |
| 170 | tobj = testobj() |
| 171 | self.assertEqual(getattr(tobj, expectedattr), False) |
| 172 | asyncore.readwrite(tobj, flag) |
R. David Murray | 78532ba | 2009-04-12 15:35:44 +0000 | [diff] [blame] | 173 | |
Ezio Melotti | 63c4640 | 2010-07-27 22:03:33 +0000 | [diff] [blame] | 174 | # Only the attribute modified by the routine we expect to be |
| 175 | # called should be True. |
| 176 | for attr in attributes: |
| 177 | self.assertEqual(getattr(tobj, attr), attr==expectedattr) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 178 | |
Ezio Melotti | 63c4640 | 2010-07-27 22:03:33 +0000 | [diff] [blame] | 179 | # check that ExitNow exceptions in the object handler method |
| 180 | # bubbles all the way up through asyncore readwrite call |
| 181 | tr1 = exitingdummy() |
| 182 | self.assertRaises(asyncore.ExitNow, asyncore.readwrite, tr1, flag) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 183 | |
Ezio Melotti | 63c4640 | 2010-07-27 22:03:33 +0000 | [diff] [blame] | 184 | # check that an exception other than ExitNow in the object handler |
| 185 | # method causes the handle_error method to get called |
| 186 | tr2 = crashingdummy() |
| 187 | self.assertEqual(tr2.error_handled, False) |
| 188 | asyncore.readwrite(tr2, flag) |
| 189 | self.assertEqual(tr2.error_handled, True) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 190 | |
| 191 | def test_closeall(self): |
| 192 | self.closeall_check(False) |
| 193 | |
| 194 | def test_closeall_default(self): |
| 195 | self.closeall_check(True) |
| 196 | |
| 197 | def closeall_check(self, usedefault): |
| 198 | # Check that close_all() closes everything in a given map |
| 199 | |
| 200 | l = [] |
| 201 | testmap = {} |
| 202 | for i in range(10): |
| 203 | c = dummychannel() |
| 204 | l.append(c) |
| 205 | self.assertEqual(c.socket.closed, False) |
| 206 | testmap[i] = c |
| 207 | |
| 208 | if usedefault: |
| 209 | socketmap = asyncore.socket_map |
| 210 | try: |
| 211 | asyncore.socket_map = testmap |
| 212 | asyncore.close_all() |
| 213 | finally: |
| 214 | testmap, asyncore.socket_map = asyncore.socket_map, socketmap |
| 215 | else: |
| 216 | asyncore.close_all(testmap) |
| 217 | |
| 218 | self.assertEqual(len(testmap), 0) |
| 219 | |
| 220 | for c in l: |
| 221 | self.assertEqual(c.socket.closed, True) |
| 222 | |
| 223 | def test_compact_traceback(self): |
| 224 | try: |
| 225 | raise Exception("I don't like spam!") |
| 226 | except: |
| 227 | real_t, real_v, real_tb = sys.exc_info() |
| 228 | r = asyncore.compact_traceback() |
| 229 | else: |
| 230 | self.fail("Expected exception") |
| 231 | |
| 232 | (f, function, line), t, v, info = r |
| 233 | self.assertEqual(os.path.split(f)[-1], 'test_asyncore.py') |
| 234 | self.assertEqual(function, 'test_compact_traceback') |
| 235 | self.assertEqual(t, real_t) |
| 236 | self.assertEqual(v, real_v) |
| 237 | self.assertEqual(info, '[%s|%s|%s]' % (f, function, line)) |
| 238 | |
| 239 | |
| 240 | class DispatcherTests(unittest.TestCase): |
| 241 | def setUp(self): |
| 242 | pass |
| 243 | |
| 244 | def tearDown(self): |
| 245 | asyncore.close_all() |
| 246 | |
| 247 | def test_basic(self): |
| 248 | d = asyncore.dispatcher() |
| 249 | self.assertEqual(d.readable(), True) |
| 250 | self.assertEqual(d.writable(), True) |
| 251 | |
| 252 | def test_repr(self): |
| 253 | d = asyncore.dispatcher() |
| 254 | self.assertEqual(repr(d), '<asyncore.dispatcher at %#x>' % id(d)) |
| 255 | |
| 256 | def test_log(self): |
| 257 | d = asyncore.dispatcher() |
| 258 | |
| 259 | # capture output of dispatcher.log() (to stderr) |
| 260 | fp = StringIO() |
| 261 | stderr = sys.stderr |
| 262 | l1 = "Lovely spam! Wonderful spam!" |
| 263 | l2 = "I don't like spam!" |
| 264 | try: |
| 265 | sys.stderr = fp |
| 266 | d.log(l1) |
| 267 | d.log(l2) |
| 268 | finally: |
| 269 | sys.stderr = stderr |
| 270 | |
| 271 | lines = fp.getvalue().splitlines() |
Ezio Melotti | 63c4640 | 2010-07-27 22:03:33 +0000 | [diff] [blame] | 272 | self.assertEqual(lines, ['log: %s' % l1, 'log: %s' % l2]) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 273 | |
| 274 | def test_log_info(self): |
| 275 | d = asyncore.dispatcher() |
| 276 | |
| 277 | # capture output of dispatcher.log_info() (to stdout via print) |
| 278 | fp = StringIO() |
| 279 | stdout = sys.stdout |
| 280 | l1 = "Have you got anything without spam?" |
| 281 | l2 = "Why can't she have egg bacon spam and sausage?" |
| 282 | l3 = "THAT'S got spam in it!" |
| 283 | try: |
| 284 | sys.stdout = fp |
| 285 | d.log_info(l1, 'EGGS') |
| 286 | d.log_info(l2) |
| 287 | d.log_info(l3, 'SPAM') |
| 288 | finally: |
| 289 | sys.stdout = stdout |
| 290 | |
| 291 | lines = fp.getvalue().splitlines() |
R. David Murray | 847f30e | 2009-04-13 01:22:04 +0000 | [diff] [blame] | 292 | expected = ['EGGS: %s' % l1, 'info: %s' % l2, 'SPAM: %s' % l3] |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 293 | |
Ezio Melotti | 63c4640 | 2010-07-27 22:03:33 +0000 | [diff] [blame] | 294 | self.assertEqual(lines, expected) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 295 | |
| 296 | def test_unhandled(self): |
| 297 | d = asyncore.dispatcher() |
R. David Murray | 78532ba | 2009-04-12 15:35:44 +0000 | [diff] [blame] | 298 | d.ignore_log_types = () |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 299 | |
| 300 | # capture output of dispatcher.log_info() (to stdout via print) |
| 301 | fp = StringIO() |
| 302 | stdout = sys.stdout |
| 303 | try: |
| 304 | sys.stdout = fp |
| 305 | d.handle_expt() |
| 306 | d.handle_read() |
| 307 | d.handle_write() |
| 308 | d.handle_connect() |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 309 | finally: |
| 310 | sys.stdout = stdout |
| 311 | |
| 312 | lines = fp.getvalue().splitlines() |
R. David Murray | 78532ba | 2009-04-12 15:35:44 +0000 | [diff] [blame] | 313 | expected = ['warning: unhandled incoming priority event', |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 314 | 'warning: unhandled read event', |
| 315 | 'warning: unhandled write event', |
Giampaolo Rodolà | 977c707 | 2010-10-04 21:08:36 +0000 | [diff] [blame] | 316 | 'warning: unhandled connect event'] |
Ezio Melotti | 63c4640 | 2010-07-27 22:03:33 +0000 | [diff] [blame] | 317 | self.assertEqual(lines, expected) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 318 | |
Giampaolo Rodolà | 8d2dc85 | 2010-05-06 18:06:30 +0000 | [diff] [blame] | 319 | def test_issue_8594(self): |
| 320 | # XXX - this test is supposed to be removed in next major Python |
| 321 | # version |
| 322 | d = asyncore.dispatcher(socket.socket()) |
| 323 | # make sure the error message no longer refers to the socket |
| 324 | # object but the dispatcher instance instead |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 325 | self.assertRaisesRegex(AttributeError, 'dispatcher instance', |
| 326 | getattr, d, 'foo') |
Giampaolo Rodolà | 8d2dc85 | 2010-05-06 18:06:30 +0000 | [diff] [blame] | 327 | # cheap inheritance with the underlying socket is supposed |
| 328 | # to still work but a DeprecationWarning is expected |
| 329 | with warnings.catch_warnings(record=True) as w: |
| 330 | warnings.simplefilter("always") |
| 331 | family = d.family |
| 332 | self.assertEqual(family, socket.AF_INET) |
Ezio Melotti | 63c4640 | 2010-07-27 22:03:33 +0000 | [diff] [blame] | 333 | self.assertEqual(len(w), 1) |
Giampaolo Rodolà | 8d2dc85 | 2010-05-06 18:06:30 +0000 | [diff] [blame] | 334 | self.assertTrue(issubclass(w[0].category, DeprecationWarning)) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 335 | |
Giampaolo Rodolà | 82e02b5 | 2010-05-18 20:11:58 +0000 | [diff] [blame] | 336 | def test_strerror(self): |
| 337 | # refers to bug #8573 |
| 338 | err = asyncore._strerror(errno.EPERM) |
| 339 | if hasattr(os, 'strerror'): |
| 340 | self.assertEqual(err, os.strerror(errno.EPERM)) |
| 341 | err = asyncore._strerror(-1) |
Giampaolo Rodolà | d2751fb | 2011-02-25 20:05:48 +0000 | [diff] [blame] | 342 | self.assertTrue(err != "") |
Giampaolo Rodolà | 82e02b5 | 2010-05-18 20:11:58 +0000 | [diff] [blame] | 343 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 344 | |
| 345 | class dispatcherwithsend_noread(asyncore.dispatcher_with_send): |
| 346 | def readable(self): |
| 347 | return False |
| 348 | |
| 349 | def handle_connect(self): |
| 350 | pass |
| 351 | |
| 352 | class DispatcherWithSendTests(unittest.TestCase): |
| 353 | usepoll = False |
| 354 | |
| 355 | def setUp(self): |
| 356 | pass |
| 357 | |
| 358 | def tearDown(self): |
| 359 | asyncore.close_all() |
| 360 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 361 | @unittest.skipUnless(threading, 'Threading required for this test.') |
Antoine Pitrou | 5ed353c | 2009-10-27 18:49:23 +0000 | [diff] [blame] | 362 | @support.reap_threads |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 363 | def test_send(self): |
Antoine Pitrou | 5ed353c | 2009-10-27 18:49:23 +0000 | [diff] [blame] | 364 | evt = threading.Event() |
Giampaolo Rodolà | 103a6d6 | 2011-02-25 22:21:22 +0000 | [diff] [blame] | 365 | sock = socket.socket() |
Antoine Pitrou | 5ed353c | 2009-10-27 18:49:23 +0000 | [diff] [blame] | 366 | sock.settimeout(3) |
| 367 | port = support.bind_port(sock) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 368 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 369 | cap = BytesIO() |
Antoine Pitrou | 5ed353c | 2009-10-27 18:49:23 +0000 | [diff] [blame] | 370 | args = (evt, cap, sock) |
| 371 | t = threading.Thread(target=capture_server, args=args) |
| 372 | t.start() |
| 373 | try: |
| 374 | # wait a little longer for the server to initialize (it sometimes |
| 375 | # refuses connections on slow machines without this wait) |
| 376 | time.sleep(0.2) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 377 | |
Antoine Pitrou | 5ed353c | 2009-10-27 18:49:23 +0000 | [diff] [blame] | 378 | data = b"Suppose there isn't a 16-ton weight?" |
| 379 | d = dispatcherwithsend_noread() |
Giampaolo Rodolà | 103a6d6 | 2011-02-25 22:21:22 +0000 | [diff] [blame] | 380 | d.create_socket() |
Antoine Pitrou | 5ed353c | 2009-10-27 18:49:23 +0000 | [diff] [blame] | 381 | d.connect((HOST, port)) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 382 | |
Antoine Pitrou | 5ed353c | 2009-10-27 18:49:23 +0000 | [diff] [blame] | 383 | # give time for socket to connect |
| 384 | time.sleep(0.1) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 385 | |
Antoine Pitrou | 5ed353c | 2009-10-27 18:49:23 +0000 | [diff] [blame] | 386 | d.send(data) |
| 387 | d.send(data) |
| 388 | d.send(b'\n') |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 389 | |
Antoine Pitrou | 5ed353c | 2009-10-27 18:49:23 +0000 | [diff] [blame] | 390 | n = 1000 |
| 391 | while d.out_buffer and n > 0: |
| 392 | asyncore.poll() |
| 393 | n -= 1 |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 394 | |
Antoine Pitrou | 5ed353c | 2009-10-27 18:49:23 +0000 | [diff] [blame] | 395 | evt.wait() |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 396 | |
Antoine Pitrou | 5ed353c | 2009-10-27 18:49:23 +0000 | [diff] [blame] | 397 | self.assertEqual(cap.getvalue(), data*2) |
| 398 | finally: |
| 399 | t.join() |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 400 | |
| 401 | |
| 402 | class DispatcherWithSendTests_UsePoll(DispatcherWithSendTests): |
| 403 | usepoll = True |
| 404 | |
Ezio Melotti | 63c4640 | 2010-07-27 22:03:33 +0000 | [diff] [blame] | 405 | @unittest.skipUnless(hasattr(asyncore, 'file_wrapper'), |
| 406 | 'asyncore.file_wrapper required') |
| 407 | class FileWrapperTest(unittest.TestCase): |
| 408 | def setUp(self): |
| 409 | self.d = b"It's not dead, it's sleeping!" |
Brett Cannon | 2d562f8 | 2010-10-29 22:40:44 +0000 | [diff] [blame] | 410 | with open(TESTFN, 'wb') as file: |
| 411 | file.write(self.d) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 412 | |
Ezio Melotti | 63c4640 | 2010-07-27 22:03:33 +0000 | [diff] [blame] | 413 | def tearDown(self): |
| 414 | unlink(TESTFN) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 415 | |
Ezio Melotti | 63c4640 | 2010-07-27 22:03:33 +0000 | [diff] [blame] | 416 | def test_recv(self): |
| 417 | fd = os.open(TESTFN, os.O_RDONLY) |
| 418 | w = asyncore.file_wrapper(fd) |
| 419 | os.close(fd) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 420 | |
Ezio Melotti | 63c4640 | 2010-07-27 22:03:33 +0000 | [diff] [blame] | 421 | self.assertNotEqual(w.fd, fd) |
| 422 | self.assertNotEqual(w.fileno(), fd) |
| 423 | self.assertEqual(w.recv(13), b"It's not dead") |
| 424 | self.assertEqual(w.read(6), b", it's") |
| 425 | w.close() |
| 426 | self.assertRaises(OSError, w.read, 1) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 427 | |
Ezio Melotti | 63c4640 | 2010-07-27 22:03:33 +0000 | [diff] [blame] | 428 | def test_send(self): |
| 429 | d1 = b"Come again?" |
| 430 | d2 = b"I want to buy some cheese." |
| 431 | fd = os.open(TESTFN, os.O_WRONLY | os.O_APPEND) |
| 432 | w = asyncore.file_wrapper(fd) |
| 433 | os.close(fd) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 434 | |
Ezio Melotti | 63c4640 | 2010-07-27 22:03:33 +0000 | [diff] [blame] | 435 | w.write(d1) |
| 436 | w.send(d2) |
| 437 | w.close() |
Brett Cannon | 2d562f8 | 2010-10-29 22:40:44 +0000 | [diff] [blame] | 438 | with open(TESTFN, 'rb') as file: |
| 439 | self.assertEqual(file.read(), self.d + d1 + d2) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 440 | |
Georg Brandl | cbb0ae4 | 2010-07-28 08:19:35 +0000 | [diff] [blame] | 441 | @unittest.skipUnless(hasattr(asyncore, 'file_dispatcher'), |
| 442 | 'asyncore.file_dispatcher required') |
| 443 | def test_dispatcher(self): |
| 444 | fd = os.open(TESTFN, os.O_RDONLY) |
| 445 | data = [] |
| 446 | class FileDispatcher(asyncore.file_dispatcher): |
| 447 | def handle_read(self): |
| 448 | data.append(self.recv(29)) |
| 449 | s = FileDispatcher(fd) |
| 450 | os.close(fd) |
| 451 | asyncore.loop(timeout=0.01, use_poll=True, count=2) |
| 452 | self.assertEqual(b"".join(data), self.d) |
| 453 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 454 | |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 455 | class BaseTestHandler(asyncore.dispatcher): |
| 456 | |
| 457 | def __init__(self, sock=None): |
| 458 | asyncore.dispatcher.__init__(self, sock) |
| 459 | self.flag = False |
| 460 | |
| 461 | def handle_accept(self): |
| 462 | raise Exception("handle_accept not supposed to be called") |
| 463 | |
Giampaolo Rodolà | 977c707 | 2010-10-04 21:08:36 +0000 | [diff] [blame] | 464 | def handle_accepted(self): |
| 465 | raise Exception("handle_accepted not supposed to be called") |
| 466 | |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 467 | def handle_connect(self): |
| 468 | raise Exception("handle_connect not supposed to be called") |
| 469 | |
| 470 | def handle_expt(self): |
| 471 | raise Exception("handle_expt not supposed to be called") |
| 472 | |
| 473 | def handle_close(self): |
| 474 | raise Exception("handle_close not supposed to be called") |
| 475 | |
| 476 | def handle_error(self): |
| 477 | raise |
| 478 | |
| 479 | |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 480 | class BaseServer(asyncore.dispatcher): |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 481 | """A server which listens on an address and dispatches the |
| 482 | connection to a handler. |
| 483 | """ |
| 484 | |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 485 | def __init__(self, family, addr, handler=BaseTestHandler): |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 486 | asyncore.dispatcher.__init__(self) |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 487 | self.create_socket(family) |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 488 | self.set_reuse_addr() |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 489 | bind_af_aware(self.socket, addr) |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 490 | self.listen(5) |
| 491 | self.handler = handler |
| 492 | |
| 493 | @property |
| 494 | def address(self): |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 495 | return self.socket.getsockname() |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 496 | |
Giampaolo Rodolà | 977c707 | 2010-10-04 21:08:36 +0000 | [diff] [blame] | 497 | def handle_accepted(self, sock, addr): |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 498 | self.handler(sock) |
| 499 | |
| 500 | def handle_error(self): |
| 501 | raise |
| 502 | |
| 503 | |
| 504 | class BaseClient(BaseTestHandler): |
| 505 | |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 506 | def __init__(self, family, address): |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 507 | BaseTestHandler.__init__(self) |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 508 | self.create_socket(family) |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 509 | self.connect(address) |
| 510 | |
| 511 | def handle_connect(self): |
| 512 | pass |
| 513 | |
| 514 | |
| 515 | class BaseTestAPI(unittest.TestCase): |
| 516 | |
| 517 | def tearDown(self): |
| 518 | asyncore.close_all() |
| 519 | |
| 520 | def loop_waiting_for_flag(self, instance, timeout=5): |
| 521 | timeout = float(timeout) / 100 |
| 522 | count = 100 |
| 523 | while asyncore.socket_map and count > 0: |
| 524 | asyncore.loop(timeout=0.01, count=1, use_poll=self.use_poll) |
| 525 | if instance.flag: |
| 526 | return |
| 527 | count -= 1 |
| 528 | time.sleep(timeout) |
| 529 | self.fail("flag not set") |
| 530 | |
| 531 | def test_handle_connect(self): |
| 532 | # make sure handle_connect is called on connect() |
| 533 | |
| 534 | class TestClient(BaseClient): |
| 535 | def handle_connect(self): |
| 536 | self.flag = True |
| 537 | |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 538 | server = BaseServer(self.family, self.addr) |
| 539 | client = TestClient(self.family, server.address) |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 540 | self.loop_waiting_for_flag(client) |
| 541 | |
| 542 | def test_handle_accept(self): |
| 543 | # make sure handle_accept() is called when a client connects |
| 544 | |
| 545 | class TestListener(BaseTestHandler): |
| 546 | |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 547 | def __init__(self, family, addr): |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 548 | BaseTestHandler.__init__(self) |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 549 | self.create_socket(family) |
| 550 | bind_af_aware(self.socket, addr) |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 551 | self.listen(5) |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 552 | self.address = self.socket.getsockname() |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 553 | |
| 554 | def handle_accept(self): |
| 555 | self.flag = True |
| 556 | |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 557 | server = TestListener(self.family, self.addr) |
| 558 | client = BaseClient(self.family, server.address) |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 559 | self.loop_waiting_for_flag(server) |
| 560 | |
Giampaolo Rodolà | 977c707 | 2010-10-04 21:08:36 +0000 | [diff] [blame] | 561 | def test_handle_accepted(self): |
| 562 | # make sure handle_accepted() is called when a client connects |
| 563 | |
| 564 | class TestListener(BaseTestHandler): |
| 565 | |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 566 | def __init__(self, family, addr): |
Giampaolo Rodolà | 977c707 | 2010-10-04 21:08:36 +0000 | [diff] [blame] | 567 | BaseTestHandler.__init__(self) |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 568 | self.create_socket(family) |
| 569 | bind_af_aware(self.socket, addr) |
Giampaolo Rodolà | 977c707 | 2010-10-04 21:08:36 +0000 | [diff] [blame] | 570 | self.listen(5) |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 571 | self.address = self.socket.getsockname() |
Giampaolo Rodolà | 977c707 | 2010-10-04 21:08:36 +0000 | [diff] [blame] | 572 | |
| 573 | def handle_accept(self): |
| 574 | asyncore.dispatcher.handle_accept(self) |
| 575 | |
| 576 | def handle_accepted(self, sock, addr): |
Benjamin Peterson | d6868b4 | 2010-10-31 17:34:34 +0000 | [diff] [blame] | 577 | sock.close() |
Giampaolo Rodolà | 977c707 | 2010-10-04 21:08:36 +0000 | [diff] [blame] | 578 | self.flag = True |
| 579 | |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 580 | server = TestListener(self.family, self.addr) |
| 581 | client = BaseClient(self.family, server.address) |
Giampaolo Rodolà | 977c707 | 2010-10-04 21:08:36 +0000 | [diff] [blame] | 582 | self.loop_waiting_for_flag(server) |
| 583 | |
| 584 | |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 585 | def test_handle_read(self): |
| 586 | # make sure handle_read is called on data received |
| 587 | |
| 588 | class TestClient(BaseClient): |
| 589 | def handle_read(self): |
| 590 | self.flag = True |
| 591 | |
| 592 | class TestHandler(BaseTestHandler): |
| 593 | def __init__(self, conn): |
| 594 | BaseTestHandler.__init__(self, conn) |
| 595 | self.send(b'x' * 1024) |
| 596 | |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 597 | server = BaseServer(self.family, self.addr, TestHandler) |
| 598 | client = TestClient(self.family, server.address) |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 599 | self.loop_waiting_for_flag(client) |
| 600 | |
| 601 | def test_handle_write(self): |
| 602 | # make sure handle_write is called |
| 603 | |
| 604 | class TestClient(BaseClient): |
| 605 | def handle_write(self): |
| 606 | self.flag = True |
| 607 | |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 608 | server = BaseServer(self.family, self.addr) |
| 609 | client = TestClient(self.family, server.address) |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 610 | self.loop_waiting_for_flag(client) |
| 611 | |
| 612 | def test_handle_close(self): |
| 613 | # make sure handle_close is called when the other end closes |
| 614 | # the connection |
| 615 | |
| 616 | class TestClient(BaseClient): |
| 617 | |
| 618 | def handle_read(self): |
| 619 | # in order to make handle_close be called we are supposed |
| 620 | # to make at least one recv() call |
| 621 | self.recv(1024) |
| 622 | |
| 623 | def handle_close(self): |
| 624 | self.flag = True |
| 625 | self.close() |
| 626 | |
| 627 | class TestHandler(BaseTestHandler): |
| 628 | def __init__(self, conn): |
| 629 | BaseTestHandler.__init__(self, conn) |
| 630 | self.close() |
| 631 | |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 632 | server = BaseServer(self.family, self.addr, TestHandler) |
| 633 | client = TestClient(self.family, server.address) |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 634 | self.loop_waiting_for_flag(client) |
| 635 | |
Charles-François Natali | d462119 | 2011-10-29 12:45:56 +0200 | [diff] [blame] | 636 | def test_handle_close_after_conn_broken(self): |
| 637 | # Check that ECONNRESET/EPIPE is correctly handled (issues #5661 and |
| 638 | # #11265). |
| 639 | |
| 640 | data = b'\0' * 128 |
| 641 | |
| 642 | class TestClient(BaseClient): |
| 643 | |
| 644 | def handle_write(self): |
| 645 | self.send(data) |
| 646 | |
| 647 | def handle_close(self): |
| 648 | self.flag = True |
| 649 | self.close() |
| 650 | |
Charles-François Natali | fea6cb0 | 2011-10-29 14:29:39 +0200 | [diff] [blame] | 651 | def handle_expt(self): |
| 652 | self.flag = True |
| 653 | self.close() |
| 654 | |
Charles-François Natali | d462119 | 2011-10-29 12:45:56 +0200 | [diff] [blame] | 655 | class TestHandler(BaseTestHandler): |
| 656 | |
| 657 | def handle_read(self): |
| 658 | self.recv(len(data)) |
| 659 | self.close() |
| 660 | |
| 661 | def writable(self): |
| 662 | return False |
| 663 | |
| 664 | server = BaseServer(self.family, self.addr, TestHandler) |
| 665 | client = TestClient(self.family, server.address) |
| 666 | self.loop_waiting_for_flag(client) |
| 667 | |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 668 | @unittest.skipIf(sys.platform.startswith("sunos"), |
| 669 | "OOB support is broken on Solaris") |
| 670 | def test_handle_expt(self): |
| 671 | # Make sure handle_expt is called on OOB data received. |
| 672 | # Note: this might fail on some platforms as OOB data is |
| 673 | # tenuously supported and rarely used. |
Charles-François Natali | e3540b4 | 2011-08-25 00:50:41 +0200 | [diff] [blame] | 674 | if HAS_UNIX_SOCKETS and self.family == socket.AF_UNIX: |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 675 | self.skipTest("Not applicable to AF_UNIX sockets.") |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 676 | |
| 677 | class TestClient(BaseClient): |
| 678 | def handle_expt(self): |
Charles-François Natali | cf85c30 | 2011-11-02 20:30:59 +0100 | [diff] [blame] | 679 | self.socket.recv(1024, socket.MSG_OOB) |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 680 | self.flag = True |
| 681 | |
| 682 | class TestHandler(BaseTestHandler): |
| 683 | def __init__(self, conn): |
| 684 | BaseTestHandler.__init__(self, conn) |
| 685 | self.socket.send(bytes(chr(244), 'latin-1'), socket.MSG_OOB) |
| 686 | |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 687 | server = BaseServer(self.family, self.addr, TestHandler) |
| 688 | client = TestClient(self.family, server.address) |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 689 | self.loop_waiting_for_flag(client) |
| 690 | |
| 691 | def test_handle_error(self): |
| 692 | |
| 693 | class TestClient(BaseClient): |
| 694 | def handle_write(self): |
| 695 | 1.0 / 0 |
| 696 | def handle_error(self): |
| 697 | self.flag = True |
| 698 | try: |
| 699 | raise |
| 700 | except ZeroDivisionError: |
| 701 | pass |
| 702 | else: |
| 703 | raise Exception("exception not raised") |
| 704 | |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 705 | server = BaseServer(self.family, self.addr) |
| 706 | client = TestClient(self.family, server.address) |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 707 | self.loop_waiting_for_flag(client) |
| 708 | |
| 709 | def test_connection_attributes(self): |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 710 | server = BaseServer(self.family, self.addr) |
| 711 | client = BaseClient(self.family, server.address) |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 712 | |
| 713 | # we start disconnected |
| 714 | self.assertFalse(server.connected) |
| 715 | self.assertTrue(server.accepting) |
Giampaolo Rodolà | 340d7d2 | 2010-05-12 00:33:15 +0000 | [diff] [blame] | 716 | # this can't be taken for granted across all platforms |
| 717 | #self.assertFalse(client.connected) |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 718 | self.assertFalse(client.accepting) |
| 719 | |
| 720 | # execute some loops so that client connects to server |
| 721 | asyncore.loop(timeout=0.01, use_poll=self.use_poll, count=100) |
| 722 | self.assertFalse(server.connected) |
| 723 | self.assertTrue(server.accepting) |
| 724 | self.assertTrue(client.connected) |
| 725 | self.assertFalse(client.accepting) |
| 726 | |
| 727 | # disconnect the client |
| 728 | client.close() |
| 729 | self.assertFalse(server.connected) |
| 730 | self.assertTrue(server.accepting) |
| 731 | self.assertFalse(client.connected) |
| 732 | self.assertFalse(client.accepting) |
| 733 | |
| 734 | # stop serving |
| 735 | server.close() |
| 736 | self.assertFalse(server.connected) |
| 737 | self.assertFalse(server.accepting) |
| 738 | |
| 739 | def test_create_socket(self): |
| 740 | s = asyncore.dispatcher() |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 741 | s.create_socket(self.family) |
| 742 | self.assertEqual(s.socket.family, self.family) |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 743 | SOCK_NONBLOCK = getattr(socket, 'SOCK_NONBLOCK', 0) |
| 744 | self.assertEqual(s.socket.type, socket.SOCK_STREAM | SOCK_NONBLOCK) |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 745 | |
| 746 | def test_bind(self): |
Charles-François Natali | e3540b4 | 2011-08-25 00:50:41 +0200 | [diff] [blame] | 747 | if HAS_UNIX_SOCKETS and self.family == socket.AF_UNIX: |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 748 | self.skipTest("Not applicable to AF_UNIX sockets.") |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 749 | s1 = asyncore.dispatcher() |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 750 | s1.create_socket(self.family) |
| 751 | s1.bind(self.addr) |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 752 | s1.listen(5) |
| 753 | port = s1.socket.getsockname()[1] |
| 754 | |
| 755 | s2 = asyncore.dispatcher() |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 756 | s2.create_socket(self.family) |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 757 | # EADDRINUSE indicates the socket was correctly bound |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 758 | self.assertRaises(socket.error, s2.bind, (self.addr[0], port)) |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 759 | |
| 760 | def test_set_reuse_addr(self): |
Charles-François Natali | e3540b4 | 2011-08-25 00:50:41 +0200 | [diff] [blame] | 761 | if HAS_UNIX_SOCKETS and self.family == socket.AF_UNIX: |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 762 | self.skipTest("Not applicable to AF_UNIX sockets.") |
| 763 | sock = socket.socket(self.family) |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 764 | try: |
| 765 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 766 | except socket.error: |
| 767 | unittest.skip("SO_REUSEADDR not supported on this platform") |
| 768 | else: |
| 769 | # if SO_REUSEADDR succeeded for sock we expect asyncore |
| 770 | # to do the same |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 771 | s = asyncore.dispatcher(socket.socket(self.family)) |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 772 | self.assertFalse(s.socket.getsockopt(socket.SOL_SOCKET, |
| 773 | socket.SO_REUSEADDR)) |
Benjamin Peterson | d6868b4 | 2010-10-31 17:34:34 +0000 | [diff] [blame] | 774 | s.socket.close() |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 775 | s.create_socket(self.family) |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 776 | s.set_reuse_addr() |
| 777 | self.assertTrue(s.socket.getsockopt(socket.SOL_SOCKET, |
| 778 | socket.SO_REUSEADDR)) |
| 779 | finally: |
| 780 | sock.close() |
| 781 | |
Giampaolo Rodola' | 350c94b | 2012-03-22 16:17:43 +0100 | [diff] [blame] | 782 | @unittest.skipUnless(threading, 'Threading required for this test.') |
| 783 | @support.reap_threads |
| 784 | def test_quick_connect(self): |
| 785 | # see: http://bugs.python.org/issue10340 |
Giampaolo Rodola' | 466a54f | 2012-03-23 13:29:01 +0100 | [diff] [blame] | 786 | if self.family in (socket.AF_INET, getattr(socket, "AF_INET6", object())): |
| 787 | server = BaseServer(self.family, self.addr) |
| 788 | t = threading.Thread(target=lambda: asyncore.loop(timeout=0.1, |
| 789 | count=500)) |
| 790 | t.start() |
Giampaolo Rodola' | 350c94b | 2012-03-22 16:17:43 +0100 | [diff] [blame] | 791 | |
Giampaolo Rodola' | 350c94b | 2012-03-22 16:17:43 +0100 | [diff] [blame] | 792 | |
Giampaolo Rodola' | c7ea19f | 2012-03-23 15:09:58 +0100 | [diff] [blame] | 793 | s = socket.socket(self.family, socket.SOCK_STREAM) |
Giampaolo Rodola' | a3481e7 | 2012-03-23 15:07:07 +0100 | [diff] [blame] | 794 | s.settimeout(.2) |
Ezio Melotti | 9a381c7 | 2010-07-27 22:24:13 +0000 | [diff] [blame] | 795 | s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, |
| 796 | struct.pack('ii', 1, 0)) |
Giampaolo Rodola' | a3481e7 | 2012-03-23 15:07:07 +0100 | [diff] [blame] | 797 | try: |
| 798 | s.connect(server.address) |
| 799 | except socket.error: |
| 800 | pass |
| 801 | finally: |
| 802 | s.close() |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 803 | |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 804 | class TestAPI_UseIPv4Sockets(BaseTestAPI): |
| 805 | family = socket.AF_INET |
| 806 | addr = (HOST, 0) |
| 807 | |
| 808 | @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 support required') |
| 809 | class TestAPI_UseIPv6Sockets(BaseTestAPI): |
| 810 | family = socket.AF_INET6 |
| 811 | addr = ('::1', 0) |
| 812 | |
Charles-François Natali | e3540b4 | 2011-08-25 00:50:41 +0200 | [diff] [blame] | 813 | @unittest.skipUnless(HAS_UNIX_SOCKETS, 'Unix sockets required') |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 814 | class TestAPI_UseUnixSockets(BaseTestAPI): |
Charles-François Natali | a045c05 | 2011-08-25 01:22:50 +0200 | [diff] [blame] | 815 | if HAS_UNIX_SOCKETS: |
| 816 | family = socket.AF_UNIX |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 817 | addr = support.TESTFN |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 818 | |
| 819 | def tearDown(self): |
| 820 | unlink(self.addr) |
| 821 | BaseTestAPI.tearDown(self) |
| 822 | |
| 823 | class TestAPI_UseIPv4Select(TestAPI_UseIPv4Sockets): |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 824 | use_poll = False |
| 825 | |
| 826 | @unittest.skipUnless(hasattr(select, 'poll'), 'select.poll required') |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 827 | class TestAPI_UseIPv4Poll(TestAPI_UseIPv4Sockets): |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 828 | use_poll = True |
| 829 | |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 830 | class TestAPI_UseIPv6Select(TestAPI_UseIPv6Sockets): |
| 831 | use_poll = False |
| 832 | |
| 833 | @unittest.skipUnless(hasattr(select, 'poll'), 'select.poll required') |
| 834 | class TestAPI_UseIPv6Poll(TestAPI_UseIPv6Sockets): |
| 835 | use_poll = True |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 836 | |
Charles-François Natali | 39648d1 | 2011-10-31 12:08:09 +0100 | [diff] [blame] | 837 | class TestAPI_UseUnixSocketsSelect(TestAPI_UseUnixSockets): |
| 838 | use_poll = False |
| 839 | |
| 840 | @unittest.skipUnless(hasattr(select, 'poll'), 'select.poll required') |
| 841 | class TestAPI_UseUnixSocketsPoll(TestAPI_UseUnixSockets): |
| 842 | use_poll = True |
| 843 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 844 | def test_main(): |
Giampaolo Rodolà | 8fc1178 | 2010-05-10 15:40:49 +0000 | [diff] [blame] | 845 | tests = [HelperFunctionTests, DispatcherTests, DispatcherWithSendTests, |
Charles-François Natali | e78cbec | 2011-08-24 23:24:05 +0200 | [diff] [blame] | 846 | DispatcherWithSendTests_UsePoll, FileWrapperTest, |
| 847 | TestAPI_UseIPv4Select, TestAPI_UseIPv4Poll, TestAPI_UseIPv6Select, |
Charles-François Natali | 39648d1 | 2011-10-31 12:08:09 +0100 | [diff] [blame] | 848 | TestAPI_UseIPv6Poll, TestAPI_UseUnixSocketsSelect, |
| 849 | TestAPI_UseUnixSocketsPoll] |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 850 | run_unittest(*tests) |
| 851 | |
| 852 | if __name__ == "__main__": |
| 853 | test_main() |