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