blob: 14c0ec43d422ea5e4c3cedec79ccfb7f63d01344 [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
Victor Stinnerfd5d1b52014-07-08 00:16:54 +02005import asynchat
6import asyncore
Victor Stinner45cff662014-07-24 18:49:36 +02007import errno
Victor Stinnerfd5d1b52014-07-08 00:16:54 +02008import socket
Guido van Rossum806c2462007-08-06 23:33:07 +00009import sys
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020010import threading
Victor Stinnerfd5d1b52014-07-08 00:16:54 +020011import time
12import unittest
Victor Stinner45cff662014-07-24 18:49:36 +020013import unittest.mock
Guido van Rossum66172522001-04-06 16:32:22 +000014
Benjamin Petersonee8712c2008-05-20 21:35:26 +000015HOST = support.HOST
Guido van Rossum806c2462007-08-06 23:33:07 +000016SERVER_QUIT = b'QUIT\n'
Giampaolo Rodola'3cb09062013-05-16 15:21:53 +020017TIMEOUT = 3.0
Guido van Rossum66172522001-04-06 16:32:22 +000018
Guido van Rossum66172522001-04-06 16:32:22 +000019
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020020class echo_server(threading.Thread):
21 # parameter to determine the number of bytes passed back to the
22 # client each send
23 chunk_size = 1
Christian Heimesaf98da12008-01-27 15:18:18 +000024
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020025 def __init__(self, event):
26 threading.Thread.__init__(self)
27 self.event = event
28 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
29 self.port = support.bind_port(self.sock)
30 # This will be set if the client wants us to wait before echoing
31 # data back.
32 self.start_resend_event = None
Guido van Rossum806c2462007-08-06 23:33:07 +000033
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020034 def run(self):
35 self.sock.listen()
36 self.event.set()
37 conn, client = self.sock.accept()
38 self.buffer = b""
39 # collect data until quit message is seen
40 while SERVER_QUIT not in self.buffer:
41 data = conn.recv(1)
42 if not data:
43 break
44 self.buffer = self.buffer + data
Guido van Rossum806c2462007-08-06 23:33:07 +000045
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020046 # remove the SERVER_QUIT message
47 self.buffer = self.buffer.replace(SERVER_QUIT, b'')
Collin Winter8641c562010-03-17 23:49:15 +000048
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020049 if self.start_resend_event:
50 self.start_resend_event.wait()
51
52 # re-send entire set of collected data
53 try:
54 # this may fail on some tests, such as test_close_when_done,
55 # since the client closes the channel when it's done sending
56 while self.buffer:
57 n = conn.send(self.buffer[:self.chunk_size])
58 time.sleep(0.001)
59 self.buffer = self.buffer[n:]
60 except:
61 pass
62
63 conn.close()
64 self.sock.close()
65
66class echo_client(asynchat.async_chat):
67
68 def __init__(self, terminator, server_port):
69 asynchat.async_chat.__init__(self)
70 self.contents = []
71 self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
72 self.connect((HOST, server_port))
73 self.set_terminator(terminator)
74 self.buffer = b""
75
76 def handle_connect(self):
77 pass
78
79 if sys.platform == 'darwin':
80 # select.poll returns a select.POLLHUP at the end of the tests
81 # on darwin, so just ignore it
82 def handle_expt(self):
Victor Stinner45df8202010-04-28 22:31:17 +000083 pass
Guido van Rossum806c2462007-08-06 23:33:07 +000084
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020085 def collect_incoming_data(self, data):
86 self.buffer += data
Guido van Rossum66172522001-04-06 16:32:22 +000087
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020088 def found_terminator(self):
89 self.contents.append(self.buffer)
90 self.buffer = b""
Guido van Rossum66172522001-04-06 16:32:22 +000091
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020092def start_echo_server():
93 event = threading.Event()
94 s = echo_server(event)
95 s.start()
96 event.wait()
97 event.clear()
98 time.sleep(0.01) # Give server time to start accepting.
99 return s, event
Guido van Rossum66172522001-04-06 16:32:22 +0000100
Guido van Rossum66172522001-04-06 16:32:22 +0000101
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000102class TestAsynchat(unittest.TestCase):
Guido van Rossum806c2462007-08-06 23:33:07 +0000103 usepoll = False
104
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200105 def setUp(self):
Antoine Pitroue03866f2009-10-30 17:58:27 +0000106 self._threads = support.threading_setup()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000107
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200108 def tearDown(self):
Antoine Pitroue03866f2009-10-30 17:58:27 +0000109 support.threading_cleanup(*self._threads)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000110
Guido van Rossum806c2462007-08-06 23:33:07 +0000111 def line_terminator_check(self, term, server_chunk):
Christian Heimesaf98da12008-01-27 15:18:18 +0000112 event = threading.Event()
113 s = echo_server(event)
Guido van Rossum806c2462007-08-06 23:33:07 +0000114 s.chunk_size = server_chunk
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000115 s.start()
Christian Heimesaf98da12008-01-27 15:18:18 +0000116 event.wait()
117 event.clear()
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200118 time.sleep(0.01) # Give server time to start accepting.
Christian Heimes5e696852008-04-09 08:37:03 +0000119 c = echo_client(term, s.port)
Guido van Rossum806c2462007-08-06 23:33:07 +0000120 c.push(b"hello ")
Josiah Carlsond74900e2008-07-07 04:15:08 +0000121 c.push(b"world" + term)
122 c.push(b"I'm not dead yet!" + term)
Guido van Rossum806c2462007-08-06 23:33:07 +0000123 c.push(SERVER_QUIT)
124 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Victor Stinnerb9b69002017-09-14 14:40:56 -0700125 support.join_thread(s, timeout=TIMEOUT)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000126
Guido van Rossum806c2462007-08-06 23:33:07 +0000127 self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000128
Guido van Rossum806c2462007-08-06 23:33:07 +0000129 # the line terminator tests below check receiving variously-sized
130 # chunks back from the server in order to exercise all branches of
131 # async_chat.handle_read
132
133 def test_line_terminator1(self):
134 # test one-character terminator
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200135 for l in (1, 2, 3):
Josiah Carlsond74900e2008-07-07 04:15:08 +0000136 self.line_terminator_check(b'\n', l)
Guido van Rossum806c2462007-08-06 23:33:07 +0000137
138 def test_line_terminator2(self):
139 # test two-character terminator
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200140 for l in (1, 2, 3):
Josiah Carlsond74900e2008-07-07 04:15:08 +0000141 self.line_terminator_check(b'\r\n', l)
Guido van Rossum806c2462007-08-06 23:33:07 +0000142
143 def test_line_terminator3(self):
144 # test three-character terminator
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200145 for l in (1, 2, 3):
Josiah Carlsond74900e2008-07-07 04:15:08 +0000146 self.line_terminator_check(b'qqq', l)
Guido van Rossum806c2462007-08-06 23:33:07 +0000147
148 def numeric_terminator_check(self, termlen):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000149 # Try reading a fixed number of bytes
Christian Heimesaf98da12008-01-27 15:18:18 +0000150 s, event = start_echo_server()
Christian Heimes5e696852008-04-09 08:37:03 +0000151 c = echo_client(termlen, s.port)
Guido van Rossum806c2462007-08-06 23:33:07 +0000152 data = b"hello world, I'm not dead yet!\n"
153 c.push(data)
154 c.push(SERVER_QUIT)
155 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Victor Stinnerb9b69002017-09-14 14:40:56 -0700156 support.join_thread(s, timeout=TIMEOUT)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000157
Guido van Rossum806c2462007-08-06 23:33:07 +0000158 self.assertEqual(c.contents, [data[:termlen]])
159
160 def test_numeric_terminator1(self):
161 # check that ints & longs both work (since type is
162 # explicitly checked in async_chat.handle_read)
163 self.numeric_terminator_check(1)
164
165 def test_numeric_terminator2(self):
166 self.numeric_terminator_check(6)
167
168 def test_none_terminator(self):
169 # Try reading a fixed number of bytes
Christian Heimesaf98da12008-01-27 15:18:18 +0000170 s, event = start_echo_server()
Christian Heimes5e696852008-04-09 08:37:03 +0000171 c = echo_client(None, s.port)
Guido van Rossum806c2462007-08-06 23:33:07 +0000172 data = b"hello world, I'm not dead yet!\n"
173 c.push(data)
174 c.push(SERVER_QUIT)
175 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Victor Stinnerb9b69002017-09-14 14:40:56 -0700176 support.join_thread(s, timeout=TIMEOUT)
Guido van Rossum806c2462007-08-06 23:33:07 +0000177
178 self.assertEqual(c.contents, [])
179 self.assertEqual(c.buffer, data)
180
181 def test_simple_producer(self):
Christian Heimesaf98da12008-01-27 15:18:18 +0000182 s, event = start_echo_server()
Christian Heimes5e696852008-04-09 08:37:03 +0000183 c = echo_client(b'\n', s.port)
Guido van Rossum806c2462007-08-06 23:33:07 +0000184 data = b"hello world\nI'm not dead yet!\n"
185 p = asynchat.simple_producer(data+SERVER_QUIT, buffer_size=8)
186 c.push_with_producer(p)
187 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Victor Stinnerb9b69002017-09-14 14:40:56 -0700188 support.join_thread(s, timeout=TIMEOUT)
Guido van Rossum806c2462007-08-06 23:33:07 +0000189
190 self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
191
192 def test_string_producer(self):
Christian Heimesaf98da12008-01-27 15:18:18 +0000193 s, event = start_echo_server()
Christian Heimes5e696852008-04-09 08:37:03 +0000194 c = echo_client(b'\n', s.port)
Guido van Rossum806c2462007-08-06 23:33:07 +0000195 data = b"hello world\nI'm not dead yet!\n"
196 c.push_with_producer(data+SERVER_QUIT)
197 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Victor Stinnerb9b69002017-09-14 14:40:56 -0700198 support.join_thread(s, timeout=TIMEOUT)
Guido van Rossum806c2462007-08-06 23:33:07 +0000199
200 self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
201
202 def test_empty_line(self):
203 # checks that empty lines are handled correctly
Christian Heimesaf98da12008-01-27 15:18:18 +0000204 s, event = start_echo_server()
Christian Heimes5e696852008-04-09 08:37:03 +0000205 c = echo_client(b'\n', s.port)
Josiah Carlsond74900e2008-07-07 04:15:08 +0000206 c.push(b"hello world\n\nI'm not dead yet!\n")
Guido van Rossum806c2462007-08-06 23:33:07 +0000207 c.push(SERVER_QUIT)
208 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Victor Stinnerb9b69002017-09-14 14:40:56 -0700209 support.join_thread(s, timeout=TIMEOUT)
Guido van Rossum806c2462007-08-06 23:33:07 +0000210
211 self.assertEqual(c.contents,
212 [b"hello world", b"", b"I'm not dead yet!"])
213
214 def test_close_when_done(self):
Christian Heimesaf98da12008-01-27 15:18:18 +0000215 s, event = start_echo_server()
Collin Winter8641c562010-03-17 23:49:15 +0000216 s.start_resend_event = threading.Event()
Christian Heimes5e696852008-04-09 08:37:03 +0000217 c = echo_client(b'\n', s.port)
Josiah Carlsond74900e2008-07-07 04:15:08 +0000218 c.push(b"hello world\nI'm not dead yet!\n")
Guido van Rossum806c2462007-08-06 23:33:07 +0000219 c.push(SERVER_QUIT)
220 c.close_when_done()
221 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Collin Winter8641c562010-03-17 23:49:15 +0000222
223 # Only allow the server to start echoing data back to the client after
224 # the client has closed its connection. This prevents a race condition
225 # where the server echoes all of its data before we can check that it
226 # got any down below.
227 s.start_resend_event.set()
Victor Stinnerb9b69002017-09-14 14:40:56 -0700228 support.join_thread(s, timeout=TIMEOUT)
Guido van Rossum806c2462007-08-06 23:33:07 +0000229
230 self.assertEqual(c.contents, [])
231 # the server might have been able to send a byte or two back, but this
232 # at least checks that it received something and didn't just fail
233 # (which could still result in the client not having received anything)
Alexandre Vassalotti953f5582009-07-22 21:29:01 +0000234 self.assertGreater(len(s.buffer), 0)
Guido van Rossum806c2462007-08-06 23:33:07 +0000235
Victor Stinnerd9e810a2014-07-08 00:00:30 +0200236 def test_push(self):
237 # Issue #12523: push() should raise a TypeError if it doesn't get
238 # a bytes string
239 s, event = start_echo_server()
240 c = echo_client(b'\n', s.port)
241 data = b'bytes\n'
242 c.push(data)
243 c.push(bytearray(data))
244 c.push(memoryview(data))
245 self.assertRaises(TypeError, c.push, 10)
246 self.assertRaises(TypeError, c.push, 'unicode')
247 c.push(SERVER_QUIT)
248 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Victor Stinnerb9b69002017-09-14 14:40:56 -0700249 support.join_thread(s, timeout=TIMEOUT)
Victor Stinnerd9e810a2014-07-08 00:00:30 +0200250 self.assertEqual(c.contents, [b'bytes', b'bytes', b'bytes'])
251
Guido van Rossum806c2462007-08-06 23:33:07 +0000252
253class TestAsynchat_WithPoll(TestAsynchat):
254 usepoll = True
255
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200256
Victor Stinner45cff662014-07-24 18:49:36 +0200257class TestAsynchatMocked(unittest.TestCase):
258 def test_blockingioerror(self):
259 # Issue #16133: handle_read() must ignore BlockingIOError
260 sock = unittest.mock.Mock()
261 sock.recv.side_effect = BlockingIOError(errno.EAGAIN)
262
263 dispatcher = asynchat.async_chat()
264 dispatcher.set_socket(sock)
265 self.addCleanup(dispatcher.del_channel)
266
267 with unittest.mock.patch.object(dispatcher, 'handle_error') as error:
268 dispatcher.handle_read()
269 self.assertFalse(error.called)
270
271
Guido van Rossum806c2462007-08-06 23:33:07 +0000272class TestHelperFunctions(unittest.TestCase):
273 def test_find_prefix_at_end(self):
274 self.assertEqual(asynchat.find_prefix_at_end("qwerty\r", "\r\n"), 1)
275 self.assertEqual(asynchat.find_prefix_at_end("qwertydkjf", "\r\n"), 0)
276
Victor Stinnerfd5d1b52014-07-08 00:16:54 +0200277
Victor Stinner630a4f62014-07-08 00:26:36 +0200278class TestNotConnected(unittest.TestCase):
279 def test_disallow_negative_terminator(self):
280 # Issue #11259
281 client = asynchat.async_chat()
282 self.assertRaises(ValueError, client.set_terminator, -1)
283
284
285
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000286if __name__ == "__main__":
Brett Cannon3e9a9ae2013-06-12 21:25:59 -0400287 unittest.main()