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