blob: c79fe6f6138b4fa4b71a98911ce74b2d4dea1450 [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'
Guido van Rossum66172522001-04-06 16:32:22 +000018
Victor Stinner45df8202010-04-28 22:31:17 +000019if threading:
20 class echo_server(threading.Thread):
21 # parameter to determine the number of bytes passed back to the
22 # client each send
23 chunk_size = 1
Guido van Rossum66172522001-04-06 16:32:22 +000024
Victor Stinner45df8202010-04-28 22:31:17 +000025 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 data
31 # back.
32 self.start_resend_event = None
Christian Heimesaf98da12008-01-27 15:18:18 +000033
Victor Stinner45df8202010-04-28 22:31:17 +000034 def run(self):
35 self.sock.listen(1)
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
Victor Stinner45df8202010-04-28 22:31:17 +000046 # remove the SERVER_QUIT message
47 self.buffer = self.buffer.replace(SERVER_QUIT, b'')
Guido van Rossum806c2462007-08-06 23:33:07 +000048
Victor Stinner45df8202010-04-28 22:31:17 +000049 if self.start_resend_event:
50 self.start_resend_event.wait()
Collin Winter8641c562010-03-17 23:49:15 +000051
Victor Stinner45df8202010-04-28 22:31:17 +000052 # re-send entire set of collected data
53 try:
54 # this may fail on some tests, such as test_close_when_done, since
55 # 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
Guido van Rossum806c2462007-08-06 23:33:07 +000062
Victor Stinner45df8202010-04-28 22:31:17 +000063 conn.close()
64 self.sock.close()
Guido van Rossum66172522001-04-06 16:32:22 +000065
Victor Stinner45df8202010-04-28 22:31:17 +000066 class echo_client(asynchat.async_chat):
Guido van Rossum66172522001-04-06 16:32:22 +000067
Victor Stinner45df8202010-04-28 22:31:17 +000068 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""
Guido van Rossum66172522001-04-06 16:32:22 +000075
Victor Stinner45df8202010-04-28 22:31:17 +000076 def handle_connect(self):
77 pass
Guido van Rossum806c2462007-08-06 23:33:07 +000078
Victor Stinner45df8202010-04-28 22:31:17 +000079 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):
83 pass
Guido van Rossum66172522001-04-06 16:32:22 +000084
Victor Stinner45df8202010-04-28 22:31:17 +000085 def collect_incoming_data(self, data):
86 self.buffer += data
Guido van Rossum66172522001-04-06 16:32:22 +000087
Victor Stinner45df8202010-04-28 22:31:17 +000088 def found_terminator(self):
89 self.contents.append(self.buffer)
90 self.buffer = b""
91
92 def 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
Victor Stinner45df8202010-04-28 22:31:17 +0000102@unittest.skipUnless(threading, 'Threading required for this test.')
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000103class TestAsynchat(unittest.TestCase):
Guido van Rossum806c2462007-08-06 23:33:07 +0000104 usepoll = False
105
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000106 def setUp (self):
Antoine Pitroue03866f2009-10-30 17:58:27 +0000107 self._threads = support.threading_setup()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000108
109 def tearDown (self):
Antoine Pitroue03866f2009-10-30 17:58:27 +0000110 support.threading_cleanup(*self._threads)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000111
Guido van Rossum806c2462007-08-06 23:33:07 +0000112 def line_terminator_check(self, term, server_chunk):
Christian Heimesaf98da12008-01-27 15:18:18 +0000113 event = threading.Event()
114 s = echo_server(event)
Guido van Rossum806c2462007-08-06 23:33:07 +0000115 s.chunk_size = server_chunk
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000116 s.start()
Christian Heimesaf98da12008-01-27 15:18:18 +0000117 event.wait()
118 event.clear()
119 time.sleep(0.01) # Give server time to start accepting.
Christian Heimes5e696852008-04-09 08:37:03 +0000120 c = echo_client(term, s.port)
Guido van Rossum806c2462007-08-06 23:33:07 +0000121 c.push(b"hello ")
Josiah Carlsond74900e2008-07-07 04:15:08 +0000122 c.push(b"world" + term)
123 c.push(b"I'm not dead yet!" + term)
Guido van Rossum806c2462007-08-06 23:33:07 +0000124 c.push(SERVER_QUIT)
125 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Michael W. Hudson73909422005-06-20 13:45:34 +0000126 s.join()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000127
Guido van Rossum806c2462007-08-06 23:33:07 +0000128 self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000129
Guido van Rossum806c2462007-08-06 23:33:07 +0000130 # the line terminator tests below check receiving variously-sized
131 # chunks back from the server in order to exercise all branches of
132 # async_chat.handle_read
133
134 def test_line_terminator1(self):
135 # test one-character terminator
136 for l in (1,2,3):
Josiah Carlsond74900e2008-07-07 04:15:08 +0000137 self.line_terminator_check(b'\n', l)
Guido van Rossum806c2462007-08-06 23:33:07 +0000138
139 def test_line_terminator2(self):
140 # test two-character terminator
141 for l in (1,2,3):
Josiah Carlsond74900e2008-07-07 04:15:08 +0000142 self.line_terminator_check(b'\r\n', l)
Guido van Rossum806c2462007-08-06 23:33:07 +0000143
144 def test_line_terminator3(self):
145 # test three-character terminator
146 for l in (1,2,3):
Josiah Carlsond74900e2008-07-07 04:15:08 +0000147 self.line_terminator_check(b'qqq', l)
Guido van Rossum806c2462007-08-06 23:33:07 +0000148
149 def numeric_terminator_check(self, termlen):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000150 # Try reading a fixed number of bytes
Christian Heimesaf98da12008-01-27 15:18:18 +0000151 s, event = start_echo_server()
Christian Heimes5e696852008-04-09 08:37:03 +0000152 c = echo_client(termlen, s.port)
Guido van Rossum806c2462007-08-06 23:33:07 +0000153 data = b"hello world, I'm not dead yet!\n"
154 c.push(data)
155 c.push(SERVER_QUIT)
156 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Michael W. Hudson73909422005-06-20 13:45:34 +0000157 s.join()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000158
Guido van Rossum806c2462007-08-06 23:33:07 +0000159 self.assertEqual(c.contents, [data[:termlen]])
160
161 def test_numeric_terminator1(self):
162 # check that ints & longs both work (since type is
163 # explicitly checked in async_chat.handle_read)
164 self.numeric_terminator_check(1)
165
166 def test_numeric_terminator2(self):
167 self.numeric_terminator_check(6)
168
169 def test_none_terminator(self):
170 # Try reading a fixed number of bytes
Christian Heimesaf98da12008-01-27 15:18:18 +0000171 s, event = start_echo_server()
Christian Heimes5e696852008-04-09 08:37:03 +0000172 c = echo_client(None, s.port)
Guido van Rossum806c2462007-08-06 23:33:07 +0000173 data = b"hello world, I'm not dead yet!\n"
174 c.push(data)
175 c.push(SERVER_QUIT)
176 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
177 s.join()
178
179 self.assertEqual(c.contents, [])
180 self.assertEqual(c.buffer, data)
181
182 def test_simple_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 p = asynchat.simple_producer(data+SERVER_QUIT, buffer_size=8)
187 c.push_with_producer(p)
188 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
189 s.join()
190
191 self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
192
193 def test_string_producer(self):
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)
Guido van Rossum806c2462007-08-06 23:33:07 +0000196 data = b"hello world\nI'm not dead yet!\n"
197 c.push_with_producer(data+SERVER_QUIT)
198 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
199 s.join()
200
201 self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
202
203 def test_empty_line(self):
204 # checks that empty lines are handled correctly
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\n\nI'm not dead yet!\n")
Guido van Rossum806c2462007-08-06 23:33:07 +0000208 c.push(SERVER_QUIT)
209 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
210 s.join()
211
212 self.assertEqual(c.contents,
213 [b"hello world", b"", b"I'm not dead yet!"])
214
215 def test_close_when_done(self):
Christian Heimesaf98da12008-01-27 15:18:18 +0000216 s, event = start_echo_server()
Collin Winter8641c562010-03-17 23:49:15 +0000217 s.start_resend_event = threading.Event()
Christian Heimes5e696852008-04-09 08:37:03 +0000218 c = echo_client(b'\n', s.port)
Josiah Carlsond74900e2008-07-07 04:15:08 +0000219 c.push(b"hello world\nI'm not dead yet!\n")
Guido van Rossum806c2462007-08-06 23:33:07 +0000220 c.push(SERVER_QUIT)
221 c.close_when_done()
222 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Collin Winter8641c562010-03-17 23:49:15 +0000223
224 # Only allow the server to start echoing data back to the client after
225 # the client has closed its connection. This prevents a race condition
226 # where the server echoes all of its data before we can check that it
227 # got any down below.
228 s.start_resend_event.set()
Guido van Rossum806c2462007-08-06 23:33:07 +0000229 s.join()
230
231 self.assertEqual(c.contents, [])
232 # the server might have been able to send a byte or two back, but this
233 # at least checks that it received something and didn't just fail
234 # (which could still result in the client not having received anything)
Alexandre Vassalotti953f5582009-07-22 21:29:01 +0000235 self.assertGreater(len(s.buffer), 0)
Guido van Rossum806c2462007-08-06 23:33:07 +0000236
237
238class TestAsynchat_WithPoll(TestAsynchat):
239 usepoll = True
240
241class TestHelperFunctions(unittest.TestCase):
242 def test_find_prefix_at_end(self):
243 self.assertEqual(asynchat.find_prefix_at_end("qwerty\r", "\r\n"), 1)
244 self.assertEqual(asynchat.find_prefix_at_end("qwertydkjf", "\r\n"), 0)
245
246class TestFifo(unittest.TestCase):
247 def test_basic(self):
248 f = asynchat.fifo()
249 f.push(7)
250 f.push(b'a')
251 self.assertEqual(len(f), 2)
252 self.assertEqual(f.first(), 7)
253 self.assertEqual(f.pop(), (1, 7))
254 self.assertEqual(len(f), 1)
255 self.assertEqual(f.first(), b'a')
256 self.assertEqual(f.is_empty(), False)
257 self.assertEqual(f.pop(), (1, b'a'))
258 self.assertEqual(len(f), 0)
259 self.assertEqual(f.is_empty(), True)
260 self.assertEqual(f.pop(), (0, None))
261
262 def test_given_list(self):
263 f = asynchat.fifo([b'x', 17, 3])
264 self.assertEqual(len(f), 3)
265 self.assertEqual(f.pop(), (1, b'x'))
266 self.assertEqual(f.pop(), (1, 17))
267 self.assertEqual(f.pop(), (1, 3))
268 self.assertEqual(f.pop(), (0, None))
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000269
270
271def test_main(verbose=None):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000272 support.run_unittest(TestAsynchat, TestAsynchat_WithPoll,
Guido van Rossum806c2462007-08-06 23:33:07 +0000273 TestHelperFunctions, TestFifo)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000274
275if __name__ == "__main__":
276 test_main(verbose=True)