R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 1 | # test asynchat |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 2 | |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 3 | from test import support |
| 4 | |
| 5 | # If this fails, the test will be skipped. |
| 6 | thread = support.import_module('_thread') |
| 7 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 8 | import asynchat |
| 9 | import asyncore |
Victor Stinner | 45cff66 | 2014-07-24 18:49:36 +0200 | [diff] [blame] | 10 | import errno |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 11 | import socket |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 12 | import sys |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 13 | import time |
| 14 | import unittest |
Giampaolo Rodola' | bd04876 | 2014-06-21 13:58:30 +0200 | [diff] [blame] | 15 | import warnings |
Victor Stinner | 45cff66 | 2014-07-24 18:49:36 +0200 | [diff] [blame] | 16 | import unittest.mock |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 17 | try: |
| 18 | import threading |
| 19 | except ImportError: |
| 20 | threading = None |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 21 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 22 | HOST = support.HOST |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 23 | SERVER_QUIT = b'QUIT\n' |
Giampaolo Rodola' | 3cb0906 | 2013-05-16 15:21:53 +0200 | [diff] [blame] | 24 | TIMEOUT = 3.0 |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 25 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 26 | if threading: |
| 27 | class echo_server(threading.Thread): |
| 28 | # parameter to determine the number of bytes passed back to the |
| 29 | # client each send |
| 30 | chunk_size = 1 |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 31 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 32 | def __init__(self, event): |
| 33 | threading.Thread.__init__(self) |
| 34 | self.event = event |
| 35 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 36 | self.port = support.bind_port(self.sock) |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 37 | # This will be set if the client wants us to wait before echoing |
| 38 | # data back. |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 39 | self.start_resend_event = None |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 40 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 41 | def run(self): |
Charles-François Natali | 6e20460 | 2014-07-23 19:28:13 +0100 | [diff] [blame] | 42 | self.sock.listen() |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 43 | self.event.set() |
| 44 | conn, client = self.sock.accept() |
| 45 | self.buffer = b"" |
| 46 | # collect data until quit message is seen |
| 47 | while SERVER_QUIT not in self.buffer: |
| 48 | data = conn.recv(1) |
| 49 | if not data: |
| 50 | break |
| 51 | self.buffer = self.buffer + data |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 52 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 53 | # remove the SERVER_QUIT message |
| 54 | self.buffer = self.buffer.replace(SERVER_QUIT, b'') |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 55 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 56 | if self.start_resend_event: |
| 57 | self.start_resend_event.wait() |
Collin Winter | 8641c56 | 2010-03-17 23:49:15 +0000 | [diff] [blame] | 58 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 59 | # re-send entire set of collected data |
| 60 | try: |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 61 | # this may fail on some tests, such as test_close_when_done, |
| 62 | # since the client closes the channel when it's done sending |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 63 | while self.buffer: |
| 64 | n = conn.send(self.buffer[:self.chunk_size]) |
| 65 | time.sleep(0.001) |
| 66 | self.buffer = self.buffer[n:] |
| 67 | except: |
| 68 | pass |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 69 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 70 | conn.close() |
| 71 | self.sock.close() |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 72 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 73 | class echo_client(asynchat.async_chat): |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 74 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 75 | def __init__(self, terminator, server_port): |
| 76 | asynchat.async_chat.__init__(self) |
| 77 | self.contents = [] |
| 78 | self.create_socket(socket.AF_INET, socket.SOCK_STREAM) |
| 79 | self.connect((HOST, server_port)) |
| 80 | self.set_terminator(terminator) |
| 81 | self.buffer = b"" |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 82 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 83 | def handle_connect(self): |
| 84 | pass |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 85 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 86 | if sys.platform == 'darwin': |
| 87 | # select.poll returns a select.POLLHUP at the end of the tests |
| 88 | # on darwin, so just ignore it |
| 89 | def handle_expt(self): |
| 90 | pass |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 91 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 92 | def collect_incoming_data(self, data): |
| 93 | self.buffer += data |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 94 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 95 | def found_terminator(self): |
| 96 | self.contents.append(self.buffer) |
| 97 | self.buffer = b"" |
| 98 | |
| 99 | def start_echo_server(): |
| 100 | event = threading.Event() |
| 101 | s = echo_server(event) |
| 102 | s.start() |
| 103 | event.wait() |
| 104 | event.clear() |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 105 | time.sleep(0.01) # Give server time to start accepting. |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 106 | return s, event |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 107 | |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 108 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 109 | @unittest.skipUnless(threading, 'Threading required for this test.') |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 110 | class TestAsynchat(unittest.TestCase): |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 111 | usepoll = False |
| 112 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 113 | def setUp(self): |
Antoine Pitrou | e03866f | 2009-10-30 17:58:27 +0000 | [diff] [blame] | 114 | self._threads = support.threading_setup() |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 115 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 116 | def tearDown(self): |
Antoine Pitrou | e03866f | 2009-10-30 17:58:27 +0000 | [diff] [blame] | 117 | support.threading_cleanup(*self._threads) |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 118 | |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 119 | def line_terminator_check(self, term, server_chunk): |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 120 | event = threading.Event() |
| 121 | s = echo_server(event) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 122 | s.chunk_size = server_chunk |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 123 | s.start() |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 124 | event.wait() |
| 125 | event.clear() |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 126 | time.sleep(0.01) # Give server time to start accepting. |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 127 | c = echo_client(term, s.port) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 128 | c.push(b"hello ") |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 129 | c.push(b"world" + term) |
| 130 | c.push(b"I'm not dead yet!" + term) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 131 | c.push(SERVER_QUIT) |
| 132 | asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) |
Giampaolo Rodola' | 3cb0906 | 2013-05-16 15:21:53 +0200 | [diff] [blame] | 133 | s.join(timeout=TIMEOUT) |
| 134 | if s.is_alive(): |
| 135 | self.fail("join() timed out") |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 136 | |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 137 | self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"]) |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 138 | |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 139 | # the line terminator tests below check receiving variously-sized |
| 140 | # chunks back from the server in order to exercise all branches of |
| 141 | # async_chat.handle_read |
| 142 | |
| 143 | def test_line_terminator1(self): |
| 144 | # test one-character terminator |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 145 | for l in (1, 2, 3): |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 146 | self.line_terminator_check(b'\n', l) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 147 | |
| 148 | def test_line_terminator2(self): |
| 149 | # test two-character terminator |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 150 | for l in (1, 2, 3): |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 151 | self.line_terminator_check(b'\r\n', l) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 152 | |
| 153 | def test_line_terminator3(self): |
| 154 | # test three-character terminator |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 155 | for l in (1, 2, 3): |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 156 | self.line_terminator_check(b'qqq', l) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 157 | |
| 158 | def numeric_terminator_check(self, termlen): |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 159 | # Try reading a fixed number of bytes |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 160 | s, event = start_echo_server() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 161 | c = echo_client(termlen, s.port) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 162 | data = b"hello world, I'm not dead yet!\n" |
| 163 | c.push(data) |
| 164 | c.push(SERVER_QUIT) |
| 165 | asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) |
Giampaolo Rodola' | 3cb0906 | 2013-05-16 15:21:53 +0200 | [diff] [blame] | 166 | s.join(timeout=TIMEOUT) |
| 167 | if s.is_alive(): |
| 168 | self.fail("join() timed out") |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 169 | |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 170 | self.assertEqual(c.contents, [data[:termlen]]) |
| 171 | |
| 172 | def test_numeric_terminator1(self): |
| 173 | # check that ints & longs both work (since type is |
| 174 | # explicitly checked in async_chat.handle_read) |
| 175 | self.numeric_terminator_check(1) |
| 176 | |
| 177 | def test_numeric_terminator2(self): |
| 178 | self.numeric_terminator_check(6) |
| 179 | |
| 180 | def test_none_terminator(self): |
| 181 | # Try reading a fixed number of bytes |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 182 | s, event = start_echo_server() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 183 | c = echo_client(None, s.port) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 184 | data = b"hello world, I'm not dead yet!\n" |
| 185 | c.push(data) |
| 186 | c.push(SERVER_QUIT) |
| 187 | asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) |
Giampaolo Rodola' | 3cb0906 | 2013-05-16 15:21:53 +0200 | [diff] [blame] | 188 | s.join(timeout=TIMEOUT) |
| 189 | if s.is_alive(): |
| 190 | self.fail("join() timed out") |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 191 | |
| 192 | self.assertEqual(c.contents, []) |
| 193 | self.assertEqual(c.buffer, data) |
| 194 | |
| 195 | def test_simple_producer(self): |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 196 | s, event = start_echo_server() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 197 | c = echo_client(b'\n', s.port) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 198 | data = b"hello world\nI'm not dead yet!\n" |
| 199 | p = asynchat.simple_producer(data+SERVER_QUIT, buffer_size=8) |
| 200 | c.push_with_producer(p) |
| 201 | asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) |
Giampaolo Rodola' | 3cb0906 | 2013-05-16 15:21:53 +0200 | [diff] [blame] | 202 | s.join(timeout=TIMEOUT) |
| 203 | if s.is_alive(): |
| 204 | self.fail("join() timed out") |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 205 | |
| 206 | self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"]) |
| 207 | |
| 208 | def test_string_producer(self): |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 209 | s, event = start_echo_server() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 210 | c = echo_client(b'\n', s.port) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 211 | data = b"hello world\nI'm not dead yet!\n" |
| 212 | c.push_with_producer(data+SERVER_QUIT) |
| 213 | asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) |
Giampaolo Rodola' | 3cb0906 | 2013-05-16 15:21:53 +0200 | [diff] [blame] | 214 | s.join(timeout=TIMEOUT) |
| 215 | if s.is_alive(): |
| 216 | self.fail("join() timed out") |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 217 | |
| 218 | self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"]) |
| 219 | |
| 220 | def test_empty_line(self): |
| 221 | # checks that empty lines are handled correctly |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 222 | s, event = start_echo_server() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 223 | c = echo_client(b'\n', s.port) |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 224 | c.push(b"hello world\n\nI'm not dead yet!\n") |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 225 | c.push(SERVER_QUIT) |
| 226 | asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) |
Giampaolo Rodola' | 3cb0906 | 2013-05-16 15:21:53 +0200 | [diff] [blame] | 227 | s.join(timeout=TIMEOUT) |
| 228 | if s.is_alive(): |
| 229 | self.fail("join() timed out") |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 230 | |
| 231 | self.assertEqual(c.contents, |
| 232 | [b"hello world", b"", b"I'm not dead yet!"]) |
| 233 | |
| 234 | def test_close_when_done(self): |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 235 | s, event = start_echo_server() |
Collin Winter | 8641c56 | 2010-03-17 23:49:15 +0000 | [diff] [blame] | 236 | s.start_resend_event = threading.Event() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 237 | c = echo_client(b'\n', s.port) |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 238 | c.push(b"hello world\nI'm not dead yet!\n") |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 239 | c.push(SERVER_QUIT) |
| 240 | c.close_when_done() |
| 241 | asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) |
Collin Winter | 8641c56 | 2010-03-17 23:49:15 +0000 | [diff] [blame] | 242 | |
| 243 | # Only allow the server to start echoing data back to the client after |
| 244 | # the client has closed its connection. This prevents a race condition |
| 245 | # where the server echoes all of its data before we can check that it |
| 246 | # got any down below. |
| 247 | s.start_resend_event.set() |
Giampaolo Rodola' | 3cb0906 | 2013-05-16 15:21:53 +0200 | [diff] [blame] | 248 | s.join(timeout=TIMEOUT) |
| 249 | if s.is_alive(): |
| 250 | self.fail("join() timed out") |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 251 | |
| 252 | self.assertEqual(c.contents, []) |
| 253 | # the server might have been able to send a byte or two back, but this |
| 254 | # at least checks that it received something and didn't just fail |
| 255 | # (which could still result in the client not having received anything) |
Alexandre Vassalotti | 953f558 | 2009-07-22 21:29:01 +0000 | [diff] [blame] | 256 | self.assertGreater(len(s.buffer), 0) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 257 | |
Victor Stinner | d9e810a | 2014-07-08 00:00:30 +0200 | [diff] [blame] | 258 | def test_push(self): |
| 259 | # Issue #12523: push() should raise a TypeError if it doesn't get |
| 260 | # a bytes string |
| 261 | s, event = start_echo_server() |
| 262 | c = echo_client(b'\n', s.port) |
| 263 | data = b'bytes\n' |
| 264 | c.push(data) |
| 265 | c.push(bytearray(data)) |
| 266 | c.push(memoryview(data)) |
| 267 | self.assertRaises(TypeError, c.push, 10) |
| 268 | self.assertRaises(TypeError, c.push, 'unicode') |
| 269 | c.push(SERVER_QUIT) |
| 270 | asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) |
| 271 | s.join(timeout=TIMEOUT) |
| 272 | self.assertEqual(c.contents, [b'bytes', b'bytes', b'bytes']) |
| 273 | |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 274 | |
| 275 | class TestAsynchat_WithPoll(TestAsynchat): |
| 276 | usepoll = True |
| 277 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 278 | |
Victor Stinner | 45cff66 | 2014-07-24 18:49:36 +0200 | [diff] [blame] | 279 | class TestAsynchatMocked(unittest.TestCase): |
| 280 | def test_blockingioerror(self): |
| 281 | # Issue #16133: handle_read() must ignore BlockingIOError |
| 282 | sock = unittest.mock.Mock() |
| 283 | sock.recv.side_effect = BlockingIOError(errno.EAGAIN) |
| 284 | |
| 285 | dispatcher = asynchat.async_chat() |
| 286 | dispatcher.set_socket(sock) |
| 287 | self.addCleanup(dispatcher.del_channel) |
| 288 | |
| 289 | with unittest.mock.patch.object(dispatcher, 'handle_error') as error: |
| 290 | dispatcher.handle_read() |
| 291 | self.assertFalse(error.called) |
| 292 | |
| 293 | |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 294 | class TestHelperFunctions(unittest.TestCase): |
| 295 | def test_find_prefix_at_end(self): |
| 296 | self.assertEqual(asynchat.find_prefix_at_end("qwerty\r", "\r\n"), 1) |
| 297 | self.assertEqual(asynchat.find_prefix_at_end("qwertydkjf", "\r\n"), 0) |
| 298 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 299 | |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 300 | class TestFifo(unittest.TestCase): |
| 301 | def test_basic(self): |
Berker Peksag | e4857f3 | 2014-07-09 03:12:23 +0300 | [diff] [blame] | 302 | with self.assertWarns(DeprecationWarning) as cm: |
Giampaolo Rodola' | bd04876 | 2014-06-21 13:58:30 +0200 | [diff] [blame] | 303 | f = asynchat.fifo() |
Berker Peksag | e4857f3 | 2014-07-09 03:12:23 +0300 | [diff] [blame] | 304 | self.assertEqual(str(cm.warning), |
| 305 | "fifo class will be removed in Python 3.6") |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 306 | f.push(7) |
| 307 | f.push(b'a') |
| 308 | self.assertEqual(len(f), 2) |
| 309 | self.assertEqual(f.first(), 7) |
| 310 | self.assertEqual(f.pop(), (1, 7)) |
| 311 | self.assertEqual(len(f), 1) |
| 312 | self.assertEqual(f.first(), b'a') |
| 313 | self.assertEqual(f.is_empty(), False) |
| 314 | self.assertEqual(f.pop(), (1, b'a')) |
| 315 | self.assertEqual(len(f), 0) |
| 316 | self.assertEqual(f.is_empty(), True) |
| 317 | self.assertEqual(f.pop(), (0, None)) |
| 318 | |
| 319 | def test_given_list(self): |
Berker Peksag | e4857f3 | 2014-07-09 03:12:23 +0300 | [diff] [blame] | 320 | with self.assertWarns(DeprecationWarning) as cm: |
Giampaolo Rodola' | bd04876 | 2014-06-21 13:58:30 +0200 | [diff] [blame] | 321 | f = asynchat.fifo([b'x', 17, 3]) |
Berker Peksag | e4857f3 | 2014-07-09 03:12:23 +0300 | [diff] [blame] | 322 | self.assertEqual(str(cm.warning), |
| 323 | "fifo class will be removed in Python 3.6") |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 324 | self.assertEqual(len(f), 3) |
| 325 | self.assertEqual(f.pop(), (1, b'x')) |
| 326 | self.assertEqual(f.pop(), (1, 17)) |
| 327 | self.assertEqual(f.pop(), (1, 3)) |
| 328 | self.assertEqual(f.pop(), (0, None)) |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 329 | |
| 330 | |
Victor Stinner | 630a4f6 | 2014-07-08 00:26:36 +0200 | [diff] [blame] | 331 | class TestNotConnected(unittest.TestCase): |
| 332 | def test_disallow_negative_terminator(self): |
| 333 | # Issue #11259 |
| 334 | client = asynchat.async_chat() |
| 335 | self.assertRaises(ValueError, client.set_terminator, -1) |
| 336 | |
| 337 | |
| 338 | |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 339 | if __name__ == "__main__": |
Brett Cannon | 3e9a9ae | 2013-06-12 21:25:59 -0400 | [diff] [blame] | 340 | unittest.main() |