blob: 9c4c71448c965960182c45c84665197d30f82173 [file] [log] [blame]
R. David Murray59beec32009-03-30 19:04:00 +00001# test asynchat
Guido van Rossum66172522001-04-06 16:32:22 +00002
Victor Stinnerfe9ebe42014-07-24 19:15:00 +02003import errno
4import asyncore
5import asynchat
6import socket
7import time
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +00008import unittest
Facundo Batista49504422007-07-31 03:03:34 +00009import sys
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000010from test import test_support
Victor Stinner09227b92010-04-27 23:03:16 +000011try:
12 import threading
13except ImportError:
14 threading = None
R. David Murray59beec32009-03-30 19:04:00 +000015
Trent Nelsone41b0062008-04-08 23:47:30 +000016HOST = test_support.HOST
Facundo Batistaec624232007-07-29 14:23:08 +000017SERVER_QUIT = 'QUIT\n'
Guido van Rossum66172522001-04-06 16:32:22 +000018
Victor Stinner09227b92010-04-27 23:03:16 +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 Stinner09227b92010-04-27 23:03:16 +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 = test_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
Neal Norwitz6e070812008-01-27 01:44:05 +000033
Victor Stinner09227b92010-04-27 23:03:16 +000034 def run(self):
35 self.sock.listen(1)
36 self.event.set()
37 conn, client = self.sock.accept()
38 self.buffer = ""
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
Facundo Batistaec624232007-07-29 14:23:08 +000045
Victor Stinner09227b92010-04-27 23:03:16 +000046 # remove the SERVER_QUIT message
47 self.buffer = self.buffer.replace(SERVER_QUIT, '')
Facundo Batistaec624232007-07-29 14:23:08 +000048
Victor Stinner09227b92010-04-27 23:03:16 +000049 if self.start_resend_event:
50 self.start_resend_event.wait()
Collin Winter22272512010-03-17 22:36:26 +000051
Victor Stinner09227b92010-04-27 23:03:16 +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
62
63 conn.close()
64 self.sock.close()
65
66 class 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 = ''
75
76 def handle_connect(self):
Facundo Batistaec624232007-07-29 14:23:08 +000077 pass
78
Victor Stinner09227b92010-04-27 23:03:16 +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 Stinner09227b92010-04-27 23:03:16 +000085 def collect_incoming_data(self, data):
86 self.buffer += data
Guido van Rossum66172522001-04-06 16:32:22 +000087
Victor Stinner09227b92010-04-27 23:03:16 +000088 def found_terminator(self):
89 self.contents.append(self.buffer)
90 self.buffer = ""
Guido van Rossum66172522001-04-06 16:32:22 +000091
Guido van Rossum66172522001-04-06 16:32:22 +000092
Victor Stinner09227b92010-04-27 23:03:16 +000093 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
Neal Norwitz6e070812008-01-27 01:44:05 +0000101
102
Victor Stinner09227b92010-04-27 23:03:16 +0000103@unittest.skipUnless(threading, 'Threading required for this test.')
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000104class TestAsynchat(unittest.TestCase):
Facundo Batistaec624232007-07-29 14:23:08 +0000105 usepoll = False
106
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000107 def setUp (self):
Antoine Pitrou643e85d2009-10-30 17:55:21 +0000108 self._threads = test_support.threading_setup()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000109
110 def tearDown (self):
Antoine Pitrou643e85d2009-10-30 17:55:21 +0000111 test_support.threading_cleanup(*self._threads)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000112
Facundo Batistaec624232007-07-29 14:23:08 +0000113 def line_terminator_check(self, term, server_chunk):
Neal Norwitz6e070812008-01-27 01:44:05 +0000114 event = threading.Event()
115 s = echo_server(event)
Facundo Batistaec624232007-07-29 14:23:08 +0000116 s.chunk_size = server_chunk
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000117 s.start()
Neal Norwitz6e070812008-01-27 01:44:05 +0000118 event.wait()
119 event.clear()
120 time.sleep(0.01) # Give server time to start accepting.
Trent Nelsone41b0062008-04-08 23:47:30 +0000121 c = echo_client(term, s.port)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000122 c.push("hello ")
Facundo Batistaec624232007-07-29 14:23:08 +0000123 c.push("world%s" % term)
124 c.push("I'm not dead yet!%s" % term)
125 c.push(SERVER_QUIT)
Facundo Batista49504422007-07-31 03:03:34 +0000126 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Michael W. Hudson73909422005-06-20 13:45:34 +0000127 s.join()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000128
Facundo Batistaec624232007-07-29 14:23:08 +0000129 self.assertEqual(c.contents, ["hello world", "I'm not dead yet!"])
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000130
Facundo Batistaec624232007-07-29 14:23:08 +0000131 # the line terminator tests below check receiving variously-sized
132 # chunks back from the server in order to exercise all branches of
133 # async_chat.handle_read
134
135 def test_line_terminator1(self):
136 # test one-character terminator
137 for l in (1,2,3):
138 self.line_terminator_check('\n', l)
139
140 def test_line_terminator2(self):
141 # test two-character terminator
142 for l in (1,2,3):
143 self.line_terminator_check('\r\n', l)
144
145 def test_line_terminator3(self):
146 # test three-character terminator
147 for l in (1,2,3):
148 self.line_terminator_check('qqq', l)
149
150 def numeric_terminator_check(self, termlen):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000151 # Try reading a fixed number of bytes
Neal Norwitz6e070812008-01-27 01:44:05 +0000152 s, event = start_echo_server()
Trent Nelsone41b0062008-04-08 23:47:30 +0000153 c = echo_client(termlen, s.port)
Facundo Batistaec624232007-07-29 14:23:08 +0000154 data = "hello world, I'm not dead yet!\n"
155 c.push(data)
156 c.push(SERVER_QUIT)
Facundo Batista49504422007-07-31 03:03:34 +0000157 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Michael W. Hudson73909422005-06-20 13:45:34 +0000158 s.join()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000159
Facundo Batistaec624232007-07-29 14:23:08 +0000160 self.assertEqual(c.contents, [data[:termlen]])
161
162 def test_numeric_terminator1(self):
163 # check that ints & longs both work (since type is
164 # explicitly checked in async_chat.handle_read)
165 self.numeric_terminator_check(1)
166 self.numeric_terminator_check(1L)
167
168 def test_numeric_terminator2(self):
169 self.numeric_terminator_check(6L)
170
171 def test_none_terminator(self):
172 # Try reading a fixed number of bytes
Neal Norwitz6e070812008-01-27 01:44:05 +0000173 s, event = start_echo_server()
Trent Nelsone41b0062008-04-08 23:47:30 +0000174 c = echo_client(None, s.port)
Facundo Batistaec624232007-07-29 14:23:08 +0000175 data = "hello world, I'm not dead yet!\n"
176 c.push(data)
177 c.push(SERVER_QUIT)
Facundo Batista49504422007-07-31 03:03:34 +0000178 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Facundo Batistaec624232007-07-29 14:23:08 +0000179 s.join()
180
181 self.assertEqual(c.contents, [])
182 self.assertEqual(c.buffer, data)
183
184 def test_simple_producer(self):
Neal Norwitz6e070812008-01-27 01:44:05 +0000185 s, event = start_echo_server()
Trent Nelsone41b0062008-04-08 23:47:30 +0000186 c = echo_client('\n', s.port)
Facundo Batistaec624232007-07-29 14:23:08 +0000187 data = "hello world\nI'm not dead yet!\n"
188 p = asynchat.simple_producer(data+SERVER_QUIT, buffer_size=8)
189 c.push_with_producer(p)
Facundo Batista49504422007-07-31 03:03:34 +0000190 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Facundo Batistaec624232007-07-29 14:23:08 +0000191 s.join()
192
193 self.assertEqual(c.contents, ["hello world", "I'm not dead yet!"])
194
195 def test_string_producer(self):
Neal Norwitz6e070812008-01-27 01:44:05 +0000196 s, event = start_echo_server()
Trent Nelsone41b0062008-04-08 23:47:30 +0000197 c = echo_client('\n', s.port)
Facundo Batistaec624232007-07-29 14:23:08 +0000198 data = "hello world\nI'm not dead yet!\n"
199 c.push_with_producer(data+SERVER_QUIT)
Facundo Batista49504422007-07-31 03:03:34 +0000200 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Facundo Batistaec624232007-07-29 14:23:08 +0000201 s.join()
202
203 self.assertEqual(c.contents, ["hello world", "I'm not dead yet!"])
204
205 def test_empty_line(self):
206 # checks that empty lines are handled correctly
Neal Norwitz6e070812008-01-27 01:44:05 +0000207 s, event = start_echo_server()
Trent Nelsone41b0062008-04-08 23:47:30 +0000208 c = echo_client('\n', s.port)
Facundo Batistaec624232007-07-29 14:23:08 +0000209 c.push("hello world\n\nI'm not dead yet!\n")
210 c.push(SERVER_QUIT)
Facundo Batista49504422007-07-31 03:03:34 +0000211 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Facundo Batistaec624232007-07-29 14:23:08 +0000212 s.join()
213
214 self.assertEqual(c.contents, ["hello world", "", "I'm not dead yet!"])
215
216 def test_close_when_done(self):
Neal Norwitz6e070812008-01-27 01:44:05 +0000217 s, event = start_echo_server()
Collin Winter22272512010-03-17 22:36:26 +0000218 s.start_resend_event = threading.Event()
Trent Nelsone41b0062008-04-08 23:47:30 +0000219 c = echo_client('\n', s.port)
Facundo Batistaec624232007-07-29 14:23:08 +0000220 c.push("hello world\nI'm not dead yet!\n")
221 c.push(SERVER_QUIT)
222 c.close_when_done()
Facundo Batista49504422007-07-31 03:03:34 +0000223 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Collin Winter22272512010-03-17 22:36:26 +0000224
225 # Only allow the server to start echoing data back to the client after
226 # the client has closed its connection. This prevents a race condition
227 # where the server echoes all of its data before we can check that it
228 # got any down below.
229 s.start_resend_event.set()
Facundo Batistaec624232007-07-29 14:23:08 +0000230 s.join()
231
232 self.assertEqual(c.contents, [])
233 # the server might have been able to send a byte or two back, but this
234 # at least checks that it received something and didn't just fail
235 # (which could still result in the client not having received anything)
236 self.assertTrue(len(s.buffer) > 0)
237
238
239class TestAsynchat_WithPoll(TestAsynchat):
240 usepoll = True
241
Victor Stinnerfe9ebe42014-07-24 19:15:00 +0200242
243class TestAsynchatMocked(unittest.TestCase):
244 def test_blockingioerror(self):
245 # Issue #16133: handle_read() must ignore blocking I/O errors like
246 # EAGAIN
247 class fake_socket:
248 def fileno(self):
249 return 0
250
251 def recv(self, size):
252 raise socket.error(errno.EAGAIN, "EAGAIN")
253
254 class MyChat(asynchat.async_chat):
255 def handle_error(self):
256 raise Exception("error")
257
258 sock = fake_socket()
259 dispatcher = MyChat()
260 dispatcher.set_socket(sock)
261 self.addCleanup(dispatcher.del_channel)
262
263 # must not call handle_error()
264 dispatcher.handle_read()
265
266
Facundo Batistaec624232007-07-29 14:23:08 +0000267class TestHelperFunctions(unittest.TestCase):
268 def test_find_prefix_at_end(self):
269 self.assertEqual(asynchat.find_prefix_at_end("qwerty\r", "\r\n"), 1)
270 self.assertEqual(asynchat.find_prefix_at_end("qwertydkjf", "\r\n"), 0)
271
272class TestFifo(unittest.TestCase):
273 def test_basic(self):
274 f = asynchat.fifo()
275 f.push(7)
276 f.push('a')
277 self.assertEqual(len(f), 2)
278 self.assertEqual(f.first(), 7)
279 self.assertEqual(f.pop(), (1, 7))
280 self.assertEqual(len(f), 1)
281 self.assertEqual(f.first(), 'a')
282 self.assertEqual(f.is_empty(), False)
283 self.assertEqual(f.pop(), (1, 'a'))
284 self.assertEqual(len(f), 0)
285 self.assertEqual(f.is_empty(), True)
286 self.assertEqual(f.pop(), (0, None))
287
288 def test_given_list(self):
289 f = asynchat.fifo(['x', 17, 3])
290 self.assertEqual(len(f), 3)
291 self.assertEqual(f.pop(), (1, 'x'))
292 self.assertEqual(f.pop(), (1, 17))
293 self.assertEqual(f.pop(), (1, 3))
294 self.assertEqual(f.pop(), (0, None))
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000295
296
297def test_main(verbose=None):
Facundo Batistaec624232007-07-29 14:23:08 +0000298 test_support.run_unittest(TestAsynchat, TestAsynchat_WithPoll,
Victor Stinnerfe9ebe42014-07-24 19:15:00 +0200299 TestAsynchatMocked,
Facundo Batistaec624232007-07-29 14:23:08 +0000300 TestHelperFunctions, TestFifo)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000301
302if __name__ == "__main__":
303 test_main(verbose=True)