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