blob: af1d5c6a0675c5aba3ea03151f2b41ee490ef156 [file] [log] [blame]
R. David Murray59beec32009-03-30 19:04:00 +00001# test asynchat
Guido van Rossum66172522001-04-06 16:32:22 +00002
Guido van Rossumdca060c2001-04-06 16:43:49 +00003import asyncore, asynchat, socket, threading, time
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +00004import unittest
Facundo Batista49504422007-07-31 03:03:34 +00005import sys
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +00006from test import test_support
Guido van Rossum66172522001-04-06 16:32:22 +00007
R. David Murray59beec32009-03-30 19:04:00 +00008# Skip tests if thread module does not exist.
9test_support.import_module('thread')
10
Trent Nelsone41b0062008-04-08 23:47:30 +000011HOST = test_support.HOST
Facundo Batistaec624232007-07-29 14:23:08 +000012SERVER_QUIT = 'QUIT\n'
Guido van Rossum66172522001-04-06 16:32:22 +000013
14class echo_server(threading.Thread):
Facundo Batistaec624232007-07-29 14:23:08 +000015 # parameter to determine the number of bytes passed back to the
16 # client each send
17 chunk_size = 1
Guido van Rossum66172522001-04-06 16:32:22 +000018
Neal Norwitz6e070812008-01-27 01:44:05 +000019 def __init__(self, event):
20 threading.Thread.__init__(self)
21 self.event = event
Trent Nelsone41b0062008-04-08 23:47:30 +000022 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
23 self.port = test_support.bind_port(self.sock)
Collin Winter22272512010-03-17 22:36:26 +000024 # This will be set if the client wants us to wait before echoing data
25 # back.
26 self.start_resend_event = None
Neal Norwitz6e070812008-01-27 01:44:05 +000027
Guido van Rossum66172522001-04-06 16:32:22 +000028 def run(self):
Trent Nelsone41b0062008-04-08 23:47:30 +000029 self.sock.listen(1)
Neal Norwitz6e070812008-01-27 01:44:05 +000030 self.event.set()
Trent Nelsone41b0062008-04-08 23:47:30 +000031 conn, client = self.sock.accept()
Facundo Batistaec624232007-07-29 14:23:08 +000032 self.buffer = ""
33 # collect data until quit message is seen
34 while SERVER_QUIT not in self.buffer:
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000035 data = conn.recv(1)
Guido van Rossum66172522001-04-06 16:32:22 +000036 if not data:
37 break
Facundo Batistaec624232007-07-29 14:23:08 +000038 self.buffer = self.buffer + data
39
40 # remove the SERVER_QUIT message
41 self.buffer = self.buffer.replace(SERVER_QUIT, '')
42
Collin Winter22272512010-03-17 22:36:26 +000043 if self.start_resend_event:
44 self.start_resend_event.wait()
45
Facundo Batistaec624232007-07-29 14:23:08 +000046 # re-send entire set of collected data
47 try:
48 # this may fail on some tests, such as test_close_when_done, since
49 # the client closes the channel when it's done sending
50 while self.buffer:
51 n = conn.send(self.buffer[:self.chunk_size])
52 time.sleep(0.001)
53 self.buffer = self.buffer[n:]
54 except:
55 pass
56
Guido van Rossum66172522001-04-06 16:32:22 +000057 conn.close()
Trent Nelsone41b0062008-04-08 23:47:30 +000058 self.sock.close()
Guido van Rossum66172522001-04-06 16:32:22 +000059
60class echo_client(asynchat.async_chat):
61
Trent Nelsone41b0062008-04-08 23:47:30 +000062 def __init__(self, terminator, server_port):
Guido van Rossum66172522001-04-06 16:32:22 +000063 asynchat.async_chat.__init__(self)
Facundo Batistaec624232007-07-29 14:23:08 +000064 self.contents = []
Guido van Rossum66172522001-04-06 16:32:22 +000065 self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
Trent Nelsone41b0062008-04-08 23:47:30 +000066 self.connect((HOST, server_port))
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000067 self.set_terminator(terminator)
Facundo Batistaec624232007-07-29 14:23:08 +000068 self.buffer = ''
Guido van Rossum66172522001-04-06 16:32:22 +000069
70 def handle_connect(self):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000071 pass
Facundo Batista49504422007-07-31 03:03:34 +000072
73 if sys.platform == 'darwin':
74 # select.poll returns a select.POLLHUP at the end of the tests
75 # on darwin, so just ignore it
76 def handle_expt(self):
77 pass
Guido van Rossum66172522001-04-06 16:32:22 +000078
79 def collect_incoming_data(self, data):
Facundo Batistaec624232007-07-29 14:23:08 +000080 self.buffer += data
Guido van Rossum66172522001-04-06 16:32:22 +000081
82 def found_terminator(self):
Facundo Batistaec624232007-07-29 14:23:08 +000083 self.contents.append(self.buffer)
Guido van Rossum66172522001-04-06 16:32:22 +000084 self.buffer = ""
Guido van Rossum66172522001-04-06 16:32:22 +000085
Guido van Rossum66172522001-04-06 16:32:22 +000086
Neal Norwitz6e070812008-01-27 01:44:05 +000087def start_echo_server():
88 event = threading.Event()
89 s = echo_server(event)
90 s.start()
91 event.wait()
92 event.clear()
93 time.sleep(0.01) # Give server time to start accepting.
94 return s, event
95
96
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000097class TestAsynchat(unittest.TestCase):
Facundo Batistaec624232007-07-29 14:23:08 +000098 usepoll = False
99
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000100 def setUp (self):
Antoine Pitrou643e85d2009-10-30 17:55:21 +0000101 self._threads = test_support.threading_setup()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000102
103 def tearDown (self):
Antoine Pitrou643e85d2009-10-30 17:55:21 +0000104 test_support.threading_cleanup(*self._threads)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000105
Facundo Batistaec624232007-07-29 14:23:08 +0000106 def line_terminator_check(self, term, server_chunk):
Neal Norwitz6e070812008-01-27 01:44:05 +0000107 event = threading.Event()
108 s = echo_server(event)
Facundo Batistaec624232007-07-29 14:23:08 +0000109 s.chunk_size = server_chunk
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000110 s.start()
Neal Norwitz6e070812008-01-27 01:44:05 +0000111 event.wait()
112 event.clear()
113 time.sleep(0.01) # Give server time to start accepting.
Trent Nelsone41b0062008-04-08 23:47:30 +0000114 c = echo_client(term, s.port)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000115 c.push("hello ")
Facundo Batistaec624232007-07-29 14:23:08 +0000116 c.push("world%s" % term)
117 c.push("I'm not dead yet!%s" % term)
118 c.push(SERVER_QUIT)
Facundo Batista49504422007-07-31 03:03:34 +0000119 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Michael W. Hudson73909422005-06-20 13:45:34 +0000120 s.join()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000121
Facundo Batistaec624232007-07-29 14:23:08 +0000122 self.assertEqual(c.contents, ["hello world", "I'm not dead yet!"])
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000123
Facundo Batistaec624232007-07-29 14:23:08 +0000124 # the line terminator tests below check receiving variously-sized
125 # chunks back from the server in order to exercise all branches of
126 # async_chat.handle_read
127
128 def test_line_terminator1(self):
129 # test one-character terminator
130 for l in (1,2,3):
131 self.line_terminator_check('\n', l)
132
133 def test_line_terminator2(self):
134 # test two-character terminator
135 for l in (1,2,3):
136 self.line_terminator_check('\r\n', l)
137
138 def test_line_terminator3(self):
139 # test three-character terminator
140 for l in (1,2,3):
141 self.line_terminator_check('qqq', l)
142
143 def numeric_terminator_check(self, termlen):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000144 # Try reading a fixed number of bytes
Neal Norwitz6e070812008-01-27 01:44:05 +0000145 s, event = start_echo_server()
Trent Nelsone41b0062008-04-08 23:47:30 +0000146 c = echo_client(termlen, s.port)
Facundo Batistaec624232007-07-29 14:23:08 +0000147 data = "hello world, I'm not dead yet!\n"
148 c.push(data)
149 c.push(SERVER_QUIT)
Facundo Batista49504422007-07-31 03:03:34 +0000150 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Michael W. Hudson73909422005-06-20 13:45:34 +0000151 s.join()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000152
Facundo Batistaec624232007-07-29 14:23:08 +0000153 self.assertEqual(c.contents, [data[:termlen]])
154
155 def test_numeric_terminator1(self):
156 # check that ints & longs both work (since type is
157 # explicitly checked in async_chat.handle_read)
158 self.numeric_terminator_check(1)
159 self.numeric_terminator_check(1L)
160
161 def test_numeric_terminator2(self):
162 self.numeric_terminator_check(6L)
163
164 def test_none_terminator(self):
165 # Try reading a fixed number of bytes
Neal Norwitz6e070812008-01-27 01:44:05 +0000166 s, event = start_echo_server()
Trent Nelsone41b0062008-04-08 23:47:30 +0000167 c = echo_client(None, s.port)
Facundo Batistaec624232007-07-29 14:23:08 +0000168 data = "hello world, I'm not dead yet!\n"
169 c.push(data)
170 c.push(SERVER_QUIT)
Facundo Batista49504422007-07-31 03:03:34 +0000171 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Facundo Batistaec624232007-07-29 14:23:08 +0000172 s.join()
173
174 self.assertEqual(c.contents, [])
175 self.assertEqual(c.buffer, data)
176
177 def test_simple_producer(self):
Neal Norwitz6e070812008-01-27 01:44:05 +0000178 s, event = start_echo_server()
Trent Nelsone41b0062008-04-08 23:47:30 +0000179 c = echo_client('\n', s.port)
Facundo Batistaec624232007-07-29 14:23:08 +0000180 data = "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)
Facundo Batista49504422007-07-31 03:03:34 +0000183 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Facundo Batistaec624232007-07-29 14:23:08 +0000184 s.join()
185
186 self.assertEqual(c.contents, ["hello world", "I'm not dead yet!"])
187
188 def test_string_producer(self):
Neal Norwitz6e070812008-01-27 01:44:05 +0000189 s, event = start_echo_server()
Trent Nelsone41b0062008-04-08 23:47:30 +0000190 c = echo_client('\n', s.port)
Facundo Batistaec624232007-07-29 14:23:08 +0000191 data = "hello world\nI'm not dead yet!\n"
192 c.push_with_producer(data+SERVER_QUIT)
Facundo Batista49504422007-07-31 03:03:34 +0000193 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Facundo Batistaec624232007-07-29 14:23:08 +0000194 s.join()
195
196 self.assertEqual(c.contents, ["hello world", "I'm not dead yet!"])
197
198 def test_empty_line(self):
199 # checks that empty lines are handled correctly
Neal Norwitz6e070812008-01-27 01:44:05 +0000200 s, event = start_echo_server()
Trent Nelsone41b0062008-04-08 23:47:30 +0000201 c = echo_client('\n', s.port)
Facundo Batistaec624232007-07-29 14:23:08 +0000202 c.push("hello world\n\nI'm not dead yet!\n")
203 c.push(SERVER_QUIT)
Facundo Batista49504422007-07-31 03:03:34 +0000204 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Facundo Batistaec624232007-07-29 14:23:08 +0000205 s.join()
206
207 self.assertEqual(c.contents, ["hello world", "", "I'm not dead yet!"])
208
209 def test_close_when_done(self):
Neal Norwitz6e070812008-01-27 01:44:05 +0000210 s, event = start_echo_server()
Collin Winter22272512010-03-17 22:36:26 +0000211 s.start_resend_event = threading.Event()
Trent Nelsone41b0062008-04-08 23:47:30 +0000212 c = echo_client('\n', s.port)
Facundo Batistaec624232007-07-29 14:23:08 +0000213 c.push("hello world\nI'm not dead yet!\n")
214 c.push(SERVER_QUIT)
215 c.close_when_done()
Facundo Batista49504422007-07-31 03:03:34 +0000216 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Collin Winter22272512010-03-17 22:36:26 +0000217
218 # Only allow the server to start echoing data back to the client after
219 # the client has closed its connection. This prevents a race condition
220 # where the server echoes all of its data before we can check that it
221 # got any down below.
222 s.start_resend_event.set()
Facundo Batistaec624232007-07-29 14:23:08 +0000223 s.join()
224
225 self.assertEqual(c.contents, [])
226 # the server might have been able to send a byte or two back, but this
227 # at least checks that it received something and didn't just fail
228 # (which could still result in the client not having received anything)
229 self.assertTrue(len(s.buffer) > 0)
230
231
232class TestAsynchat_WithPoll(TestAsynchat):
233 usepoll = True
234
235class TestHelperFunctions(unittest.TestCase):
236 def test_find_prefix_at_end(self):
237 self.assertEqual(asynchat.find_prefix_at_end("qwerty\r", "\r\n"), 1)
238 self.assertEqual(asynchat.find_prefix_at_end("qwertydkjf", "\r\n"), 0)
239
240class TestFifo(unittest.TestCase):
241 def test_basic(self):
242 f = asynchat.fifo()
243 f.push(7)
244 f.push('a')
245 self.assertEqual(len(f), 2)
246 self.assertEqual(f.first(), 7)
247 self.assertEqual(f.pop(), (1, 7))
248 self.assertEqual(len(f), 1)
249 self.assertEqual(f.first(), 'a')
250 self.assertEqual(f.is_empty(), False)
251 self.assertEqual(f.pop(), (1, 'a'))
252 self.assertEqual(len(f), 0)
253 self.assertEqual(f.is_empty(), True)
254 self.assertEqual(f.pop(), (0, None))
255
256 def test_given_list(self):
257 f = asynchat.fifo(['x', 17, 3])
258 self.assertEqual(len(f), 3)
259 self.assertEqual(f.pop(), (1, 'x'))
260 self.assertEqual(f.pop(), (1, 17))
261 self.assertEqual(f.pop(), (1, 3))
262 self.assertEqual(f.pop(), (0, None))
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000263
264
265def test_main(verbose=None):
Facundo Batistaec624232007-07-29 14:23:08 +0000266 test_support.run_unittest(TestAsynchat, TestAsynchat_WithPoll,
267 TestHelperFunctions, TestFifo)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000268
269if __name__ == "__main__":
270 test_main(verbose=True)