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