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