blob: c743d6bad9a3c0346719c9ed1c9f865a7dba74e8 [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)
Collin Winter8641c562010-03-17 23:49:15 +000025 # This will be set if the client wants us to wait before echoing data
26 # back.
27 self.start_resend_event = None
Christian Heimesaf98da12008-01-27 15:18:18 +000028
Guido van Rossum66172522001-04-06 16:32:22 +000029 def run(self):
Christian Heimes5e696852008-04-09 08:37:03 +000030 self.sock.listen(1)
Christian Heimesaf98da12008-01-27 15:18:18 +000031 self.event.set()
Christian Heimes5e696852008-04-09 08:37:03 +000032 conn, client = self.sock.accept()
Guido van Rossum806c2462007-08-06 23:33:07 +000033 self.buffer = b""
34 # collect data until quit message is seen
35 while SERVER_QUIT not in self.buffer:
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000036 data = conn.recv(1)
Guido van Rossum66172522001-04-06 16:32:22 +000037 if not data:
38 break
Guido van Rossum806c2462007-08-06 23:33:07 +000039 self.buffer = self.buffer + data
40
41 # remove the SERVER_QUIT message
42 self.buffer = self.buffer.replace(SERVER_QUIT, b'')
43
Collin Winter8641c562010-03-17 23:49:15 +000044 if self.start_resend_event:
45 self.start_resend_event.wait()
46
Guido van Rossum806c2462007-08-06 23:33:07 +000047 # re-send entire set of collected data
48 try:
49 # this may fail on some tests, such as test_close_when_done, since
50 # the client closes the channel when it's done sending
51 while self.buffer:
52 n = conn.send(self.buffer[:self.chunk_size])
53 time.sleep(0.001)
54 self.buffer = self.buffer[n:]
55 except:
56 pass
57
Guido van Rossum66172522001-04-06 16:32:22 +000058 conn.close()
Christian Heimes5e696852008-04-09 08:37:03 +000059 self.sock.close()
Guido van Rossum66172522001-04-06 16:32:22 +000060
61class echo_client(asynchat.async_chat):
62
Christian Heimes5e696852008-04-09 08:37:03 +000063 def __init__(self, terminator, server_port):
Guido van Rossum66172522001-04-06 16:32:22 +000064 asynchat.async_chat.__init__(self)
Guido van Rossum806c2462007-08-06 23:33:07 +000065 self.contents = []
Guido van Rossum66172522001-04-06 16:32:22 +000066 self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
Christian Heimes5e696852008-04-09 08:37:03 +000067 self.connect((HOST, server_port))
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000068 self.set_terminator(terminator)
Guido van Rossum076da092007-07-12 07:58:54 +000069 self.buffer = b""
Guido van Rossum66172522001-04-06 16:32:22 +000070
71 def handle_connect(self):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000072 pass
Guido van Rossum806c2462007-08-06 23:33:07 +000073
74 if sys.platform == 'darwin':
75 # select.poll returns a select.POLLHUP at the end of the tests
76 # on darwin, so just ignore it
77 def handle_expt(self):
78 pass
Guido van Rossum66172522001-04-06 16:32:22 +000079
80 def collect_incoming_data(self, data):
Guido van Rossum806c2462007-08-06 23:33:07 +000081 self.buffer += data
Guido van Rossum66172522001-04-06 16:32:22 +000082
83 def found_terminator(self):
Guido van Rossum806c2462007-08-06 23:33:07 +000084 self.contents.append(self.buffer)
Guido van Rossum076da092007-07-12 07:58:54 +000085 self.buffer = b""
Guido van Rossum66172522001-04-06 16:32:22 +000086
Guido van Rossum66172522001-04-06 16:32:22 +000087
Christian Heimesaf98da12008-01-27 15:18:18 +000088def start_echo_server():
89 event = threading.Event()
90 s = echo_server(event)
91 s.start()
92 event.wait()
93 event.clear()
94 time.sleep(0.01) # Give server time to start accepting.
95 return s, event
96
97
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000098class TestAsynchat(unittest.TestCase):
Guido van Rossum806c2462007-08-06 23:33:07 +000099 usepoll = False
100
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000101 def setUp (self):
Antoine Pitroue03866f2009-10-30 17:58:27 +0000102 self._threads = support.threading_setup()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000103
104 def tearDown (self):
Antoine Pitroue03866f2009-10-30 17:58:27 +0000105 support.threading_cleanup(*self._threads)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000106
Guido van Rossum806c2462007-08-06 23:33:07 +0000107 def line_terminator_check(self, term, server_chunk):
Christian Heimesaf98da12008-01-27 15:18:18 +0000108 event = threading.Event()
109 s = echo_server(event)
Guido van Rossum806c2462007-08-06 23:33:07 +0000110 s.chunk_size = server_chunk
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000111 s.start()
Christian Heimesaf98da12008-01-27 15:18:18 +0000112 event.wait()
113 event.clear()
114 time.sleep(0.01) # Give server time to start accepting.
Christian Heimes5e696852008-04-09 08:37:03 +0000115 c = echo_client(term, s.port)
Guido van Rossum806c2462007-08-06 23:33:07 +0000116 c.push(b"hello ")
Josiah Carlsond74900e2008-07-07 04:15:08 +0000117 c.push(b"world" + term)
118 c.push(b"I'm not dead yet!" + term)
Guido van Rossum806c2462007-08-06 23:33:07 +0000119 c.push(SERVER_QUIT)
120 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Michael W. Hudson73909422005-06-20 13:45:34 +0000121 s.join()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000122
Guido van Rossum806c2462007-08-06 23:33:07 +0000123 self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000124
Guido van Rossum806c2462007-08-06 23:33:07 +0000125 # the line terminator tests below check receiving variously-sized
126 # chunks back from the server in order to exercise all branches of
127 # async_chat.handle_read
128
129 def test_line_terminator1(self):
130 # test one-character terminator
131 for l in (1,2,3):
Josiah Carlsond74900e2008-07-07 04:15:08 +0000132 self.line_terminator_check(b'\n', l)
Guido van Rossum806c2462007-08-06 23:33:07 +0000133
134 def test_line_terminator2(self):
135 # test two-character terminator
136 for l in (1,2,3):
Josiah Carlsond74900e2008-07-07 04:15:08 +0000137 self.line_terminator_check(b'\r\n', l)
Guido van Rossum806c2462007-08-06 23:33:07 +0000138
139 def test_line_terminator3(self):
140 # test three-character terminator
141 for l in (1,2,3):
Josiah Carlsond74900e2008-07-07 04:15:08 +0000142 self.line_terminator_check(b'qqq', l)
Guido van Rossum806c2462007-08-06 23:33:07 +0000143
144 def numeric_terminator_check(self, termlen):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000145 # Try reading a fixed number of bytes
Christian Heimesaf98da12008-01-27 15:18:18 +0000146 s, event = start_echo_server()
Christian Heimes5e696852008-04-09 08:37:03 +0000147 c = echo_client(termlen, s.port)
Guido van Rossum806c2462007-08-06 23:33:07 +0000148 data = b"hello world, I'm not dead yet!\n"
149 c.push(data)
150 c.push(SERVER_QUIT)
151 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Michael W. Hudson73909422005-06-20 13:45:34 +0000152 s.join()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000153
Guido van Rossum806c2462007-08-06 23:33:07 +0000154 self.assertEqual(c.contents, [data[:termlen]])
155
156 def test_numeric_terminator1(self):
157 # check that ints & longs both work (since type is
158 # explicitly checked in async_chat.handle_read)
159 self.numeric_terminator_check(1)
160
161 def test_numeric_terminator2(self):
162 self.numeric_terminator_check(6)
163
164 def test_none_terminator(self):
165 # Try reading a fixed number of bytes
Christian Heimesaf98da12008-01-27 15:18:18 +0000166 s, event = start_echo_server()
Christian Heimes5e696852008-04-09 08:37:03 +0000167 c = echo_client(None, s.port)
Guido van Rossum806c2462007-08-06 23:33:07 +0000168 data = b"hello world, I'm not dead yet!\n"
169 c.push(data)
170 c.push(SERVER_QUIT)
171 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
172 s.join()
173
174 self.assertEqual(c.contents, [])
175 self.assertEqual(c.buffer, data)
176
177 def test_simple_producer(self):
Christian Heimesaf98da12008-01-27 15:18:18 +0000178 s, event = start_echo_server()
Christian Heimes5e696852008-04-09 08:37:03 +0000179 c = echo_client(b'\n', s.port)
Guido van Rossum806c2462007-08-06 23:33:07 +0000180 data = b"hello world\nI'm not dead yet!\n"
181 p = asynchat.simple_producer(data+SERVER_QUIT, buffer_size=8)
182 c.push_with_producer(p)
183 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
184 s.join()
185
186 self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
187
188 def test_string_producer(self):
Christian Heimesaf98da12008-01-27 15:18:18 +0000189 s, event = start_echo_server()
Christian Heimes5e696852008-04-09 08:37:03 +0000190 c = echo_client(b'\n', s.port)
Guido van Rossum806c2462007-08-06 23:33:07 +0000191 data = b"hello world\nI'm not dead yet!\n"
192 c.push_with_producer(data+SERVER_QUIT)
193 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
194 s.join()
195
196 self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
197
198 def test_empty_line(self):
199 # checks that empty lines are handled correctly
Christian Heimesaf98da12008-01-27 15:18:18 +0000200 s, event = start_echo_server()
Christian Heimes5e696852008-04-09 08:37:03 +0000201 c = echo_client(b'\n', s.port)
Josiah Carlsond74900e2008-07-07 04:15:08 +0000202 c.push(b"hello world\n\nI'm not dead yet!\n")
Guido van Rossum806c2462007-08-06 23:33:07 +0000203 c.push(SERVER_QUIT)
204 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
205 s.join()
206
207 self.assertEqual(c.contents,
208 [b"hello world", b"", b"I'm not dead yet!"])
209
210 def test_close_when_done(self):
Christian Heimesaf98da12008-01-27 15:18:18 +0000211 s, event = start_echo_server()
Collin Winter8641c562010-03-17 23:49:15 +0000212 s.start_resend_event = threading.Event()
Christian Heimes5e696852008-04-09 08:37:03 +0000213 c = echo_client(b'\n', s.port)
Josiah Carlsond74900e2008-07-07 04:15:08 +0000214 c.push(b"hello world\nI'm not dead yet!\n")
Guido van Rossum806c2462007-08-06 23:33:07 +0000215 c.push(SERVER_QUIT)
216 c.close_when_done()
217 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Collin Winter8641c562010-03-17 23:49:15 +0000218
219 # Only allow the server to start echoing data back to the client after
220 # the client has closed its connection. This prevents a race condition
221 # where the server echoes all of its data before we can check that it
222 # got any down below.
223 s.start_resend_event.set()
Guido van Rossum806c2462007-08-06 23:33:07 +0000224 s.join()
225
226 self.assertEqual(c.contents, [])
227 # the server might have been able to send a byte or two back, but this
228 # at least checks that it received something and didn't just fail
229 # (which could still result in the client not having received anything)
Alexandre Vassalotti953f5582009-07-22 21:29:01 +0000230 self.assertGreater(len(s.buffer), 0)
Guido van Rossum806c2462007-08-06 23:33:07 +0000231
232
233class TestAsynchat_WithPoll(TestAsynchat):
234 usepoll = True
235
236class TestHelperFunctions(unittest.TestCase):
237 def test_find_prefix_at_end(self):
238 self.assertEqual(asynchat.find_prefix_at_end("qwerty\r", "\r\n"), 1)
239 self.assertEqual(asynchat.find_prefix_at_end("qwertydkjf", "\r\n"), 0)
240
241class TestFifo(unittest.TestCase):
242 def test_basic(self):
243 f = asynchat.fifo()
244 f.push(7)
245 f.push(b'a')
246 self.assertEqual(len(f), 2)
247 self.assertEqual(f.first(), 7)
248 self.assertEqual(f.pop(), (1, 7))
249 self.assertEqual(len(f), 1)
250 self.assertEqual(f.first(), b'a')
251 self.assertEqual(f.is_empty(), False)
252 self.assertEqual(f.pop(), (1, b'a'))
253 self.assertEqual(len(f), 0)
254 self.assertEqual(f.is_empty(), True)
255 self.assertEqual(f.pop(), (0, None))
256
257 def test_given_list(self):
258 f = asynchat.fifo([b'x', 17, 3])
259 self.assertEqual(len(f), 3)
260 self.assertEqual(f.pop(), (1, b'x'))
261 self.assertEqual(f.pop(), (1, 17))
262 self.assertEqual(f.pop(), (1, 3))
263 self.assertEqual(f.pop(), (0, None))
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000264
265
266def test_main(verbose=None):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000267 support.run_unittest(TestAsynchat, TestAsynchat_WithPoll,
Guido van Rossum806c2462007-08-06 23:33:07 +0000268 TestHelperFunctions, TestFifo)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000269
270if __name__ == "__main__":
271 test_main(verbose=True)