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 | |
Guido van Rossum | dca060c | 2001-04-06 16:43:49 +0000 | [diff] [blame] | 8 | import asyncore, asynchat, socket, threading, time |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 9 | import unittest |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 10 | import sys |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 11 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 12 | HOST = support.HOST |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 13 | SERVER_QUIT = b'QUIT\n' |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 14 | |
| 15 | class echo_server(threading.Thread): |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 16 | # parameter to determine the number of bytes passed back to the |
| 17 | # client each send |
| 18 | chunk_size = 1 |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 19 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 20 | def __init__(self, event): |
| 21 | threading.Thread.__init__(self) |
| 22 | self.event = event |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 23 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 24 | self.port = support.bind_port(self.sock) |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 26 | def run(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 27 | self.sock.listen(1) |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 28 | self.event.set() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 29 | conn, client = self.sock.accept() |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 30 | self.buffer = b"" |
| 31 | # collect data until quit message is seen |
| 32 | while SERVER_QUIT not in self.buffer: |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 33 | data = conn.recv(1) |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 34 | if not data: |
| 35 | break |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 36 | self.buffer = self.buffer + data |
| 37 | |
| 38 | # remove the SERVER_QUIT message |
| 39 | self.buffer = self.buffer.replace(SERVER_QUIT, b'') |
| 40 | |
| 41 | # re-send entire set of collected data |
| 42 | try: |
| 43 | # this may fail on some tests, such as test_close_when_done, since |
| 44 | # the client closes the channel when it's done sending |
| 45 | while self.buffer: |
| 46 | n = conn.send(self.buffer[:self.chunk_size]) |
| 47 | time.sleep(0.001) |
| 48 | self.buffer = self.buffer[n:] |
| 49 | except: |
| 50 | pass |
| 51 | |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 52 | conn.close() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 53 | self.sock.close() |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 54 | |
| 55 | class echo_client(asynchat.async_chat): |
| 56 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 57 | def __init__(self, terminator, server_port): |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 58 | asynchat.async_chat.__init__(self) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 59 | self.contents = [] |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 60 | self.create_socket(socket.AF_INET, socket.SOCK_STREAM) |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 61 | self.connect((HOST, server_port)) |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 62 | self.set_terminator(terminator) |
Guido van Rossum | 076da09 | 2007-07-12 07:58:54 +0000 | [diff] [blame] | 63 | self.buffer = b"" |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 64 | |
| 65 | def handle_connect(self): |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 66 | pass |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 67 | |
| 68 | if sys.platform == 'darwin': |
| 69 | # select.poll returns a select.POLLHUP at the end of the tests |
| 70 | # on darwin, so just ignore it |
| 71 | def handle_expt(self): |
| 72 | pass |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 73 | |
| 74 | def collect_incoming_data(self, data): |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 75 | self.buffer += data |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 76 | |
| 77 | def found_terminator(self): |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 78 | self.contents.append(self.buffer) |
Guido van Rossum | 076da09 | 2007-07-12 07:58:54 +0000 | [diff] [blame] | 79 | self.buffer = b"" |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 81 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 82 | def start_echo_server(): |
| 83 | event = threading.Event() |
| 84 | s = echo_server(event) |
| 85 | s.start() |
| 86 | event.wait() |
| 87 | event.clear() |
| 88 | time.sleep(0.01) # Give server time to start accepting. |
| 89 | return s, event |
| 90 | |
| 91 | |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 92 | class TestAsynchat(unittest.TestCase): |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 93 | usepoll = False |
| 94 | |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 95 | def setUp (self): |
| 96 | pass |
| 97 | |
| 98 | def tearDown (self): |
| 99 | pass |
| 100 | |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 101 | def line_terminator_check(self, term, server_chunk): |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 102 | event = threading.Event() |
| 103 | s = echo_server(event) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 104 | s.chunk_size = server_chunk |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 105 | s.start() |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 106 | event.wait() |
| 107 | event.clear() |
| 108 | time.sleep(0.01) # Give server time to start accepting. |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 109 | c = echo_client(term, s.port) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 110 | c.push(b"hello ") |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 111 | c.push(b"world" + term) |
| 112 | c.push(b"I'm not dead yet!" + term) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 113 | c.push(SERVER_QUIT) |
| 114 | asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) |
Michael W. Hudson | 7390942 | 2005-06-20 13:45:34 +0000 | [diff] [blame] | 115 | s.join() |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 116 | |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 117 | 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] | 118 | |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 119 | # the line terminator tests below check receiving variously-sized |
| 120 | # chunks back from the server in order to exercise all branches of |
| 121 | # async_chat.handle_read |
| 122 | |
| 123 | def test_line_terminator1(self): |
| 124 | # test one-character terminator |
| 125 | for l in (1,2,3): |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 126 | self.line_terminator_check(b'\n', l) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 127 | |
| 128 | def test_line_terminator2(self): |
| 129 | # test two-character terminator |
| 130 | for l in (1,2,3): |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 131 | self.line_terminator_check(b'\r\n', l) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 132 | |
| 133 | def test_line_terminator3(self): |
| 134 | # test three-character terminator |
| 135 | for l in (1,2,3): |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 136 | self.line_terminator_check(b'qqq', l) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 137 | |
| 138 | def numeric_terminator_check(self, termlen): |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 139 | # Try reading a fixed number of bytes |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 140 | s, event = start_echo_server() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 141 | c = echo_client(termlen, s.port) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 142 | data = b"hello world, I'm not dead yet!\n" |
| 143 | c.push(data) |
| 144 | c.push(SERVER_QUIT) |
| 145 | asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) |
Michael W. Hudson | 7390942 | 2005-06-20 13:45:34 +0000 | [diff] [blame] | 146 | s.join() |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 147 | |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 148 | self.assertEqual(c.contents, [data[:termlen]]) |
| 149 | |
| 150 | def test_numeric_terminator1(self): |
| 151 | # check that ints & longs both work (since type is |
| 152 | # explicitly checked in async_chat.handle_read) |
| 153 | self.numeric_terminator_check(1) |
| 154 | |
| 155 | def test_numeric_terminator2(self): |
| 156 | self.numeric_terminator_check(6) |
| 157 | |
| 158 | def test_none_terminator(self): |
| 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(None, 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) |
| 166 | s.join() |
| 167 | |
| 168 | self.assertEqual(c.contents, []) |
| 169 | self.assertEqual(c.buffer, data) |
| 170 | |
| 171 | def test_simple_producer(self): |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 172 | s, event = start_echo_server() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 173 | c = echo_client(b'\n', s.port) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 174 | data = b"hello world\nI'm not dead yet!\n" |
| 175 | p = asynchat.simple_producer(data+SERVER_QUIT, buffer_size=8) |
| 176 | c.push_with_producer(p) |
| 177 | asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) |
| 178 | s.join() |
| 179 | |
| 180 | self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"]) |
| 181 | |
| 182 | def test_string_producer(self): |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 183 | s, event = start_echo_server() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 184 | c = echo_client(b'\n', s.port) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 185 | data = b"hello world\nI'm not dead yet!\n" |
| 186 | c.push_with_producer(data+SERVER_QUIT) |
| 187 | asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) |
| 188 | s.join() |
| 189 | |
| 190 | self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"]) |
| 191 | |
| 192 | def test_empty_line(self): |
| 193 | # checks that empty lines are handled correctly |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 194 | s, event = start_echo_server() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 195 | c = echo_client(b'\n', s.port) |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 196 | 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] | 197 | c.push(SERVER_QUIT) |
| 198 | asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) |
| 199 | s.join() |
| 200 | |
| 201 | self.assertEqual(c.contents, |
| 202 | [b"hello world", b"", b"I'm not dead yet!"]) |
| 203 | |
| 204 | def test_close_when_done(self): |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 205 | s, event = start_echo_server() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 206 | c = echo_client(b'\n', s.port) |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 207 | c.push(b"hello world\nI'm not dead yet!\n") |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 208 | c.push(SERVER_QUIT) |
| 209 | c.close_when_done() |
| 210 | asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) |
| 211 | s.join() |
| 212 | |
| 213 | self.assertEqual(c.contents, []) |
| 214 | # the server might have been able to send a byte or two back, but this |
| 215 | # at least checks that it received something and didn't just fail |
| 216 | # (which could still result in the client not having received anything) |
| 217 | self.assertTrue(len(s.buffer) > 0) |
| 218 | |
| 219 | |
| 220 | class TestAsynchat_WithPoll(TestAsynchat): |
| 221 | usepoll = True |
| 222 | |
| 223 | class TestHelperFunctions(unittest.TestCase): |
| 224 | def test_find_prefix_at_end(self): |
| 225 | self.assertEqual(asynchat.find_prefix_at_end("qwerty\r", "\r\n"), 1) |
| 226 | self.assertEqual(asynchat.find_prefix_at_end("qwertydkjf", "\r\n"), 0) |
| 227 | |
| 228 | class TestFifo(unittest.TestCase): |
| 229 | def test_basic(self): |
| 230 | f = asynchat.fifo() |
| 231 | f.push(7) |
| 232 | f.push(b'a') |
| 233 | self.assertEqual(len(f), 2) |
| 234 | self.assertEqual(f.first(), 7) |
| 235 | self.assertEqual(f.pop(), (1, 7)) |
| 236 | self.assertEqual(len(f), 1) |
| 237 | self.assertEqual(f.first(), b'a') |
| 238 | self.assertEqual(f.is_empty(), False) |
| 239 | self.assertEqual(f.pop(), (1, b'a')) |
| 240 | self.assertEqual(len(f), 0) |
| 241 | self.assertEqual(f.is_empty(), True) |
| 242 | self.assertEqual(f.pop(), (0, None)) |
| 243 | |
| 244 | def test_given_list(self): |
| 245 | f = asynchat.fifo([b'x', 17, 3]) |
| 246 | self.assertEqual(len(f), 3) |
| 247 | self.assertEqual(f.pop(), (1, b'x')) |
| 248 | self.assertEqual(f.pop(), (1, 17)) |
| 249 | self.assertEqual(f.pop(), (1, 3)) |
| 250 | self.assertEqual(f.pop(), (0, None)) |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 251 | |
| 252 | |
| 253 | def test_main(verbose=None): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 254 | support.run_unittest(TestAsynchat, TestAsynchat_WithPoll, |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 255 | TestHelperFunctions, TestFifo) |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 256 | |
| 257 | if __name__ == "__main__": |
| 258 | test_main(verbose=True) |