blob: f93a52d8c98973add312f5eec7e07777ff19bb4c [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
Victor Stinner45df8202010-04-28 22:31:17 +00008import asyncore, asynchat, socket, time
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +00009import unittest
Guido van Rossum806c2462007-08-06 23:33:07 +000010import sys
Victor Stinner45df8202010-04-28 22:31:17 +000011try:
12 import threading
13except ImportError:
14 threading = None
Guido van Rossum66172522001-04-06 16:32:22 +000015
Benjamin Petersonee8712c2008-05-20 21:35:26 +000016HOST = support.HOST
Guido van Rossum806c2462007-08-06 23:33:07 +000017SERVER_QUIT = b'QUIT\n'
Giampaolo Rodola'3cb09062013-05-16 15:21:53 +020018TIMEOUT = 3.0
Guido van Rossum66172522001-04-06 16:32:22 +000019
Victor Stinner45df8202010-04-28 22:31:17 +000020if threading:
21 class echo_server(threading.Thread):
22 # parameter to determine the number of bytes passed back to the
23 # client each send
24 chunk_size = 1
Guido van Rossum66172522001-04-06 16:32:22 +000025
Victor Stinner45df8202010-04-28 22:31:17 +000026 def __init__(self, event):
27 threading.Thread.__init__(self)
28 self.event = event
29 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
30 self.port = support.bind_port(self.sock)
31 # This will be set if the client wants us to wait before echoing data
32 # back.
33 self.start_resend_event = None
Christian Heimesaf98da12008-01-27 15:18:18 +000034
Victor Stinner45df8202010-04-28 22:31:17 +000035 def run(self):
36 self.sock.listen(1)
37 self.event.set()
38 conn, client = self.sock.accept()
39 self.buffer = b""
40 # collect data until quit message is seen
41 while SERVER_QUIT not in self.buffer:
42 data = conn.recv(1)
43 if not data:
44 break
45 self.buffer = self.buffer + data
Guido van Rossum806c2462007-08-06 23:33:07 +000046
Victor Stinner45df8202010-04-28 22:31:17 +000047 # remove the SERVER_QUIT message
48 self.buffer = self.buffer.replace(SERVER_QUIT, b'')
Guido van Rossum806c2462007-08-06 23:33:07 +000049
Victor Stinner45df8202010-04-28 22:31:17 +000050 if self.start_resend_event:
51 self.start_resend_event.wait()
Collin Winter8641c562010-03-17 23:49:15 +000052
Victor Stinner45df8202010-04-28 22:31:17 +000053 # re-send entire set of collected data
54 try:
55 # this may fail on some tests, such as test_close_when_done, since
56 # the client closes the channel when it's done sending
57 while self.buffer:
58 n = conn.send(self.buffer[:self.chunk_size])
59 time.sleep(0.001)
60 self.buffer = self.buffer[n:]
61 except:
62 pass
Guido van Rossum806c2462007-08-06 23:33:07 +000063
Victor Stinner45df8202010-04-28 22:31:17 +000064 conn.close()
65 self.sock.close()
Guido van Rossum66172522001-04-06 16:32:22 +000066
Victor Stinner45df8202010-04-28 22:31:17 +000067 class echo_client(asynchat.async_chat):
Guido van Rossum66172522001-04-06 16:32:22 +000068
Victor Stinner45df8202010-04-28 22:31:17 +000069 def __init__(self, terminator, server_port):
70 asynchat.async_chat.__init__(self)
71 self.contents = []
72 self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
73 self.connect((HOST, server_port))
74 self.set_terminator(terminator)
75 self.buffer = b""
Guido van Rossum66172522001-04-06 16:32:22 +000076
Victor Stinner45df8202010-04-28 22:31:17 +000077 def handle_connect(self):
78 pass
Guido van Rossum806c2462007-08-06 23:33:07 +000079
Victor Stinner45df8202010-04-28 22:31:17 +000080 if sys.platform == 'darwin':
81 # select.poll returns a select.POLLHUP at the end of the tests
82 # on darwin, so just ignore it
83 def handle_expt(self):
84 pass
Guido van Rossum66172522001-04-06 16:32:22 +000085
Victor Stinner45df8202010-04-28 22:31:17 +000086 def collect_incoming_data(self, data):
87 self.buffer += data
Guido van Rossum66172522001-04-06 16:32:22 +000088
Victor Stinner45df8202010-04-28 22:31:17 +000089 def found_terminator(self):
90 self.contents.append(self.buffer)
91 self.buffer = b""
92
93 def start_echo_server():
94 event = threading.Event()
95 s = echo_server(event)
96 s.start()
97 event.wait()
98 event.clear()
99 time.sleep(0.01) # Give server time to start accepting.
100 return s, event
Guido van Rossum66172522001-04-06 16:32:22 +0000101
Guido van Rossum66172522001-04-06 16:32:22 +0000102
Victor Stinner45df8202010-04-28 22:31:17 +0000103@unittest.skipUnless(threading, 'Threading required for this test.')
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000104class TestAsynchat(unittest.TestCase):
Guido van Rossum806c2462007-08-06 23:33:07 +0000105 usepoll = False
106
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000107 def setUp (self):
Antoine Pitroue03866f2009-10-30 17:58:27 +0000108 self._threads = support.threading_setup()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000109
110 def tearDown (self):
Antoine Pitroue03866f2009-10-30 17:58:27 +0000111 support.threading_cleanup(*self._threads)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000112
Guido van Rossum806c2462007-08-06 23:33:07 +0000113 def line_terminator_check(self, term, server_chunk):
Christian Heimesaf98da12008-01-27 15:18:18 +0000114 event = threading.Event()
115 s = echo_server(event)
Guido van Rossum806c2462007-08-06 23:33:07 +0000116 s.chunk_size = server_chunk
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000117 s.start()
Christian Heimesaf98da12008-01-27 15:18:18 +0000118 event.wait()
119 event.clear()
120 time.sleep(0.01) # Give server time to start accepting.
Christian Heimes5e696852008-04-09 08:37:03 +0000121 c = echo_client(term, s.port)
Guido van Rossum806c2462007-08-06 23:33:07 +0000122 c.push(b"hello ")
Josiah Carlsond74900e2008-07-07 04:15:08 +0000123 c.push(b"world" + term)
124 c.push(b"I'm not dead yet!" + term)
Guido van Rossum806c2462007-08-06 23:33:07 +0000125 c.push(SERVER_QUIT)
126 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Giampaolo Rodola'3cb09062013-05-16 15:21:53 +0200127 s.join(timeout=TIMEOUT)
128 if s.is_alive():
129 self.fail("join() timed out")
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000130
Guido van Rossum806c2462007-08-06 23:33:07 +0000131 self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000132
Guido van Rossum806c2462007-08-06 23:33:07 +0000133 # the line terminator tests below check receiving variously-sized
134 # chunks back from the server in order to exercise all branches of
135 # async_chat.handle_read
136
137 def test_line_terminator1(self):
138 # test one-character terminator
139 for l in (1,2,3):
Josiah Carlsond74900e2008-07-07 04:15:08 +0000140 self.line_terminator_check(b'\n', l)
Guido van Rossum806c2462007-08-06 23:33:07 +0000141
142 def test_line_terminator2(self):
143 # test two-character terminator
144 for l in (1,2,3):
Josiah Carlsond74900e2008-07-07 04:15:08 +0000145 self.line_terminator_check(b'\r\n', l)
Guido van Rossum806c2462007-08-06 23:33:07 +0000146
147 def test_line_terminator3(self):
148 # test three-character terminator
149 for l in (1,2,3):
Josiah Carlsond74900e2008-07-07 04:15:08 +0000150 self.line_terminator_check(b'qqq', l)
Guido van Rossum806c2462007-08-06 23:33:07 +0000151
152 def numeric_terminator_check(self, termlen):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000153 # Try reading a fixed number of bytes
Christian Heimesaf98da12008-01-27 15:18:18 +0000154 s, event = start_echo_server()
Christian Heimes5e696852008-04-09 08:37:03 +0000155 c = echo_client(termlen, s.port)
Guido van Rossum806c2462007-08-06 23:33:07 +0000156 data = b"hello world, I'm not dead yet!\n"
157 c.push(data)
158 c.push(SERVER_QUIT)
159 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Giampaolo Rodola'3cb09062013-05-16 15:21:53 +0200160 s.join(timeout=TIMEOUT)
161 if s.is_alive():
162 self.fail("join() timed out")
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000163
Guido van Rossum806c2462007-08-06 23:33:07 +0000164 self.assertEqual(c.contents, [data[:termlen]])
165
166 def test_numeric_terminator1(self):
167 # check that ints & longs both work (since type is
168 # explicitly checked in async_chat.handle_read)
169 self.numeric_terminator_check(1)
170
171 def test_numeric_terminator2(self):
172 self.numeric_terminator_check(6)
173
174 def test_none_terminator(self):
175 # Try reading a fixed number of bytes
Christian Heimesaf98da12008-01-27 15:18:18 +0000176 s, event = start_echo_server()
Christian Heimes5e696852008-04-09 08:37:03 +0000177 c = echo_client(None, s.port)
Guido van Rossum806c2462007-08-06 23:33:07 +0000178 data = b"hello world, I'm not dead yet!\n"
179 c.push(data)
180 c.push(SERVER_QUIT)
181 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Giampaolo Rodola'3cb09062013-05-16 15:21:53 +0200182 s.join(timeout=TIMEOUT)
183 if s.is_alive():
184 self.fail("join() timed out")
Guido van Rossum806c2462007-08-06 23:33:07 +0000185
186 self.assertEqual(c.contents, [])
187 self.assertEqual(c.buffer, data)
188
189 def test_simple_producer(self):
Christian Heimesaf98da12008-01-27 15:18:18 +0000190 s, event = start_echo_server()
Christian Heimes5e696852008-04-09 08:37:03 +0000191 c = echo_client(b'\n', s.port)
Guido van Rossum806c2462007-08-06 23:33:07 +0000192 data = b"hello world\nI'm not dead yet!\n"
193 p = asynchat.simple_producer(data+SERVER_QUIT, buffer_size=8)
194 c.push_with_producer(p)
195 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Giampaolo Rodola'3cb09062013-05-16 15:21:53 +0200196 s.join(timeout=TIMEOUT)
197 if s.is_alive():
198 self.fail("join() timed out")
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_string_producer(self):
Christian Heimesaf98da12008-01-27 15:18:18 +0000203 s, event = start_echo_server()
Christian Heimes5e696852008-04-09 08:37:03 +0000204 c = echo_client(b'\n', s.port)
Guido van Rossum806c2462007-08-06 23:33:07 +0000205 data = b"hello world\nI'm not dead yet!\n"
206 c.push_with_producer(data+SERVER_QUIT)
207 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Giampaolo Rodola'3cb09062013-05-16 15:21:53 +0200208 s.join(timeout=TIMEOUT)
209 if s.is_alive():
210 self.fail("join() timed out")
Guido van Rossum806c2462007-08-06 23:33:07 +0000211
212 self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
213
214 def test_empty_line(self):
215 # checks that empty lines are handled correctly
Christian Heimesaf98da12008-01-27 15:18:18 +0000216 s, event = start_echo_server()
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\n\nI'm not dead yet!\n")
Guido van Rossum806c2462007-08-06 23:33:07 +0000219 c.push(SERVER_QUIT)
220 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Giampaolo Rodola'3cb09062013-05-16 15:21:53 +0200221 s.join(timeout=TIMEOUT)
222 if s.is_alive():
223 self.fail("join() timed out")
Guido van Rossum806c2462007-08-06 23:33:07 +0000224
225 self.assertEqual(c.contents,
226 [b"hello world", b"", b"I'm not dead yet!"])
227
228 def test_close_when_done(self):
Christian Heimesaf98da12008-01-27 15:18:18 +0000229 s, event = start_echo_server()
Collin Winter8641c562010-03-17 23:49:15 +0000230 s.start_resend_event = threading.Event()
Christian Heimes5e696852008-04-09 08:37:03 +0000231 c = echo_client(b'\n', s.port)
Josiah Carlsond74900e2008-07-07 04:15:08 +0000232 c.push(b"hello world\nI'm not dead yet!\n")
Guido van Rossum806c2462007-08-06 23:33:07 +0000233 c.push(SERVER_QUIT)
234 c.close_when_done()
235 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Collin Winter8641c562010-03-17 23:49:15 +0000236
237 # Only allow the server to start echoing data back to the client after
238 # the client has closed its connection. This prevents a race condition
239 # where the server echoes all of its data before we can check that it
240 # got any down below.
241 s.start_resend_event.set()
Giampaolo Rodola'3cb09062013-05-16 15:21:53 +0200242 s.join(timeout=TIMEOUT)
243 if s.is_alive():
244 self.fail("join() timed out")
Guido van Rossum806c2462007-08-06 23:33:07 +0000245
246 self.assertEqual(c.contents, [])
247 # the server might have been able to send a byte or two back, but this
248 # at least checks that it received something and didn't just fail
249 # (which could still result in the client not having received anything)
Alexandre Vassalotti953f5582009-07-22 21:29:01 +0000250 self.assertGreater(len(s.buffer), 0)
Guido van Rossum806c2462007-08-06 23:33:07 +0000251
252
253class TestAsynchat_WithPoll(TestAsynchat):
254 usepoll = True
255
256class TestHelperFunctions(unittest.TestCase):
257 def test_find_prefix_at_end(self):
258 self.assertEqual(asynchat.find_prefix_at_end("qwerty\r", "\r\n"), 1)
259 self.assertEqual(asynchat.find_prefix_at_end("qwertydkjf", "\r\n"), 0)
260
261class TestFifo(unittest.TestCase):
262 def test_basic(self):
263 f = asynchat.fifo()
264 f.push(7)
265 f.push(b'a')
266 self.assertEqual(len(f), 2)
267 self.assertEqual(f.first(), 7)
268 self.assertEqual(f.pop(), (1, 7))
269 self.assertEqual(len(f), 1)
270 self.assertEqual(f.first(), b'a')
271 self.assertEqual(f.is_empty(), False)
272 self.assertEqual(f.pop(), (1, b'a'))
273 self.assertEqual(len(f), 0)
274 self.assertEqual(f.is_empty(), True)
275 self.assertEqual(f.pop(), (0, None))
276
277 def test_given_list(self):
278 f = asynchat.fifo([b'x', 17, 3])
279 self.assertEqual(len(f), 3)
280 self.assertEqual(f.pop(), (1, b'x'))
281 self.assertEqual(f.pop(), (1, 17))
282 self.assertEqual(f.pop(), (1, 3))
283 self.assertEqual(f.pop(), (0, None))
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000284
285
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000286if __name__ == "__main__":
Brett Cannon3e9a9ae2013-06-12 21:25:59 -0400287 unittest.main()