blob: 3da5878f47746c7e4b071bc8a6a52371704788f1 [file] [log] [blame]
R. David Murraya21e4ca2009-03-31 23:16:50 +00001# test asynchat
Guido van Rossum66172522001-04-06 16:32:22 +00002
R. David Murraya21e4ca2009-03-31 23:16:50 +00003from test import support
4
5# If this fails, the test will be skipped.
6thread = support.import_module('_thread')
7
Guido van Rossumdca060c2001-04-06 16:43:49 +00008import asyncore, asynchat, socket, threading, time
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +00009import unittest
Guido van Rossum806c2462007-08-06 23:33:07 +000010import sys
Guido van Rossum66172522001-04-06 16:32:22 +000011
Benjamin Petersonee8712c2008-05-20 21:35:26 +000012HOST = support.HOST
Guido van Rossum806c2462007-08-06 23:33:07 +000013SERVER_QUIT = b'QUIT\n'
Guido van Rossum66172522001-04-06 16:32:22 +000014
15class echo_server(threading.Thread):
Guido van Rossum806c2462007-08-06 23:33:07 +000016 # parameter to determine the number of bytes passed back to the
17 # client each send
18 chunk_size = 1
Guido van Rossum66172522001-04-06 16:32:22 +000019
Christian Heimesaf98da12008-01-27 15:18:18 +000020 def __init__(self, event):
21 threading.Thread.__init__(self)
22 self.event = event
Christian Heimes5e696852008-04-09 08:37:03 +000023 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Benjamin Petersonee8712c2008-05-20 21:35:26 +000024 self.port = support.bind_port(self.sock)
Christian Heimesaf98da12008-01-27 15:18:18 +000025
Guido van Rossum66172522001-04-06 16:32:22 +000026 def run(self):
Christian Heimes5e696852008-04-09 08:37:03 +000027 self.sock.listen(1)
Christian Heimesaf98da12008-01-27 15:18:18 +000028 self.event.set()
Christian Heimes5e696852008-04-09 08:37:03 +000029 conn, client = self.sock.accept()
Guido van Rossum806c2462007-08-06 23:33:07 +000030 self.buffer = b""
31 # collect data until quit message is seen
32 while SERVER_QUIT not in self.buffer:
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000033 data = conn.recv(1)
Guido van Rossum66172522001-04-06 16:32:22 +000034 if not data:
35 break
Guido van Rossum806c2462007-08-06 23:33:07 +000036 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 Rossum66172522001-04-06 16:32:22 +000052 conn.close()
Christian Heimes5e696852008-04-09 08:37:03 +000053 self.sock.close()
Guido van Rossum66172522001-04-06 16:32:22 +000054
55class echo_client(asynchat.async_chat):
56
Christian Heimes5e696852008-04-09 08:37:03 +000057 def __init__(self, terminator, server_port):
Guido van Rossum66172522001-04-06 16:32:22 +000058 asynchat.async_chat.__init__(self)
Guido van Rossum806c2462007-08-06 23:33:07 +000059 self.contents = []
Guido van Rossum66172522001-04-06 16:32:22 +000060 self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
Christian Heimes5e696852008-04-09 08:37:03 +000061 self.connect((HOST, server_port))
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000062 self.set_terminator(terminator)
Guido van Rossum076da092007-07-12 07:58:54 +000063 self.buffer = b""
Guido van Rossum66172522001-04-06 16:32:22 +000064
65 def handle_connect(self):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000066 pass
Guido van Rossum806c2462007-08-06 23:33:07 +000067
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 Rossum66172522001-04-06 16:32:22 +000073
74 def collect_incoming_data(self, data):
Guido van Rossum806c2462007-08-06 23:33:07 +000075 self.buffer += data
Guido van Rossum66172522001-04-06 16:32:22 +000076
77 def found_terminator(self):
Guido van Rossum806c2462007-08-06 23:33:07 +000078 self.contents.append(self.buffer)
Guido van Rossum076da092007-07-12 07:58:54 +000079 self.buffer = b""
Guido van Rossum66172522001-04-06 16:32:22 +000080
Guido van Rossum66172522001-04-06 16:32:22 +000081
Christian Heimesaf98da12008-01-27 15:18:18 +000082def 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. Kuchling5ac25342005-06-09 14:56:31 +000092class TestAsynchat(unittest.TestCase):
Guido van Rossum806c2462007-08-06 23:33:07 +000093 usepoll = False
94
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000095 def setUp (self):
96 pass
97
98 def tearDown (self):
99 pass
100
Guido van Rossum806c2462007-08-06 23:33:07 +0000101 def line_terminator_check(self, term, server_chunk):
Christian Heimesaf98da12008-01-27 15:18:18 +0000102 event = threading.Event()
103 s = echo_server(event)
Guido van Rossum806c2462007-08-06 23:33:07 +0000104 s.chunk_size = server_chunk
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000105 s.start()
Christian Heimesaf98da12008-01-27 15:18:18 +0000106 event.wait()
107 event.clear()
108 time.sleep(0.01) # Give server time to start accepting.
Christian Heimes5e696852008-04-09 08:37:03 +0000109 c = echo_client(term, s.port)
Guido van Rossum806c2462007-08-06 23:33:07 +0000110 c.push(b"hello ")
Josiah Carlsond74900e2008-07-07 04:15:08 +0000111 c.push(b"world" + term)
112 c.push(b"I'm not dead yet!" + term)
Guido van Rossum806c2462007-08-06 23:33:07 +0000113 c.push(SERVER_QUIT)
114 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Michael W. Hudson73909422005-06-20 13:45:34 +0000115 s.join()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000116
Guido van Rossum806c2462007-08-06 23:33:07 +0000117 self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000118
Guido van Rossum806c2462007-08-06 23:33:07 +0000119 # 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 Carlsond74900e2008-07-07 04:15:08 +0000126 self.line_terminator_check(b'\n', l)
Guido van Rossum806c2462007-08-06 23:33:07 +0000127
128 def test_line_terminator2(self):
129 # test two-character terminator
130 for l in (1,2,3):
Josiah Carlsond74900e2008-07-07 04:15:08 +0000131 self.line_terminator_check(b'\r\n', l)
Guido van Rossum806c2462007-08-06 23:33:07 +0000132
133 def test_line_terminator3(self):
134 # test three-character terminator
135 for l in (1,2,3):
Josiah Carlsond74900e2008-07-07 04:15:08 +0000136 self.line_terminator_check(b'qqq', l)
Guido van Rossum806c2462007-08-06 23:33:07 +0000137
138 def numeric_terminator_check(self, termlen):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000139 # Try reading a fixed number of bytes
Christian Heimesaf98da12008-01-27 15:18:18 +0000140 s, event = start_echo_server()
Christian Heimes5e696852008-04-09 08:37:03 +0000141 c = echo_client(termlen, s.port)
Guido van Rossum806c2462007-08-06 23:33:07 +0000142 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. Hudson73909422005-06-20 13:45:34 +0000146 s.join()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000147
Guido van Rossum806c2462007-08-06 23:33:07 +0000148 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 Heimesaf98da12008-01-27 15:18:18 +0000160 s, event = start_echo_server()
Christian Heimes5e696852008-04-09 08:37:03 +0000161 c = echo_client(None, s.port)
Guido van Rossum806c2462007-08-06 23:33:07 +0000162 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 Heimesaf98da12008-01-27 15:18:18 +0000172 s, event = start_echo_server()
Christian Heimes5e696852008-04-09 08:37:03 +0000173 c = echo_client(b'\n', s.port)
Guido van Rossum806c2462007-08-06 23:33:07 +0000174 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 Heimesaf98da12008-01-27 15:18:18 +0000183 s, event = start_echo_server()
Christian Heimes5e696852008-04-09 08:37:03 +0000184 c = echo_client(b'\n', s.port)
Guido van Rossum806c2462007-08-06 23:33:07 +0000185 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 Heimesaf98da12008-01-27 15:18:18 +0000194 s, event = start_echo_server()
Christian Heimes5e696852008-04-09 08:37:03 +0000195 c = echo_client(b'\n', s.port)
Josiah Carlsond74900e2008-07-07 04:15:08 +0000196 c.push(b"hello world\n\nI'm not dead yet!\n")
Guido van Rossum806c2462007-08-06 23:33:07 +0000197 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 Heimesaf98da12008-01-27 15:18:18 +0000205 s, event = start_echo_server()
Christian Heimes5e696852008-04-09 08:37:03 +0000206 c = echo_client(b'\n', s.port)
Josiah Carlsond74900e2008-07-07 04:15:08 +0000207 c.push(b"hello world\nI'm not dead yet!\n")
Guido van Rossum806c2462007-08-06 23:33:07 +0000208 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
220class TestAsynchat_WithPoll(TestAsynchat):
221 usepoll = True
222
223class 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
228class 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. Kuchling5ac25342005-06-09 14:56:31 +0000251
252
253def test_main(verbose=None):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000254 support.run_unittest(TestAsynchat, TestAsynchat_WithPoll,
Guido van Rossum806c2462007-08-06 23:33:07 +0000255 TestHelperFunctions, TestFifo)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000256
257if __name__ == "__main__":
258 test_main(verbose=True)