blob: 291205723585a47be3c0a838770c72ffd2e65abe [file] [log] [blame]
Guido van Rossum66172522001-04-06 16:32:22 +00001# test asynchat -- requires threading
2
Guido van Rossum9df3eab2001-04-14 14:35:43 +00003import thread # If this fails, we can't test this module
Guido van Rossumdca060c2001-04-06 16:43:49 +00004import asyncore, asynchat, socket, threading, time
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +00005import unittest
Guido van Rossum806c2462007-08-06 23:33:07 +00006import sys
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +00007from test import test_support
Guido van Rossum66172522001-04-06 16:32:22 +00008
9HOST = "127.0.0.1"
Michael W. Hudson73909422005-06-20 13:45:34 +000010PORT = 54322
Guido van Rossum806c2462007-08-06 23:33:07 +000011SERVER_QUIT = b'QUIT\n'
Guido van Rossum66172522001-04-06 16:32:22 +000012
13class echo_server(threading.Thread):
Guido van Rossum806c2462007-08-06 23:33:07 +000014 # parameter to determine the number of bytes passed back to the
15 # client each send
16 chunk_size = 1
Guido van Rossum66172522001-04-06 16:32:22 +000017
Christian Heimesaf98da12008-01-27 15:18:18 +000018 def __init__(self, event):
19 threading.Thread.__init__(self)
20 self.event = event
21
Guido van Rossum66172522001-04-06 16:32:22 +000022 def run(self):
23 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Guido van Rossumf3ee46b2001-04-15 00:42:13 +000024 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000025 global PORT
26 PORT = test_support.bind_port(sock, HOST, PORT)
Guido van Rossum66172522001-04-06 16:32:22 +000027 sock.listen(1)
Christian Heimesaf98da12008-01-27 15:18:18 +000028 self.event.set()
Guido van Rossum66172522001-04-06 16:32:22 +000029 conn, client = sock.accept()
Guido van Rossum806c2462007-08-06 23:33:07 +000030 self.buffer = b""
31 # collect data until quit message is seen
32 while SERVER_QUIT not in self.buffer:
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000033 data = conn.recv(1)
Guido van Rossum66172522001-04-06 16:32:22 +000034 if not data:
35 break
Guido van Rossum806c2462007-08-06 23:33:07 +000036 self.buffer = self.buffer + data
37
38 # remove the SERVER_QUIT message
39 self.buffer = self.buffer.replace(SERVER_QUIT, b'')
40
41 # re-send entire set of collected data
42 try:
43 # this may fail on some tests, such as test_close_when_done, since
44 # the client closes the channel when it's done sending
45 while self.buffer:
46 n = conn.send(self.buffer[:self.chunk_size])
47 time.sleep(0.001)
48 self.buffer = self.buffer[n:]
49 except:
50 pass
51
Guido van Rossum66172522001-04-06 16:32:22 +000052 conn.close()
53 sock.close()
54
55class echo_client(asynchat.async_chat):
56
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000057 def __init__(self, terminator):
Guido van Rossum66172522001-04-06 16:32:22 +000058 asynchat.async_chat.__init__(self)
Guido van Rossum806c2462007-08-06 23:33:07 +000059 self.contents = []
Guido van Rossum66172522001-04-06 16:32:22 +000060 self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
61 self.connect((HOST, PORT))
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000062 self.set_terminator(terminator)
Guido van Rossum076da092007-07-12 07:58:54 +000063 self.buffer = b""
Guido van Rossum66172522001-04-06 16:32:22 +000064
65 def handle_connect(self):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000066 pass
Guido van Rossum806c2462007-08-06 23:33:07 +000067
68 if sys.platform == 'darwin':
69 # select.poll returns a select.POLLHUP at the end of the tests
70 # on darwin, so just ignore it
71 def handle_expt(self):
72 pass
Guido van Rossum66172522001-04-06 16:32:22 +000073
74 def collect_incoming_data(self, data):
Guido van Rossum806c2462007-08-06 23:33:07 +000075 self.buffer += data
Guido van Rossum66172522001-04-06 16:32:22 +000076
77 def found_terminator(self):
Guido van Rossum806c2462007-08-06 23:33:07 +000078 self.contents.append(self.buffer)
Guido van Rossum076da092007-07-12 07:58:54 +000079 self.buffer = b""
Guido van Rossum66172522001-04-06 16:32:22 +000080
Guido van Rossum66172522001-04-06 16:32:22 +000081
Christian Heimesaf98da12008-01-27 15:18:18 +000082def start_echo_server():
83 event = threading.Event()
84 s = echo_server(event)
85 s.start()
86 event.wait()
87 event.clear()
88 time.sleep(0.01) # Give server time to start accepting.
89 return s, event
90
91
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000092class TestAsynchat(unittest.TestCase):
Guido van Rossum806c2462007-08-06 23:33:07 +000093 usepoll = False
94
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000095 def setUp (self):
96 pass
97
98 def tearDown (self):
99 pass
100
Guido van Rossum806c2462007-08-06 23:33:07 +0000101 def line_terminator_check(self, term, server_chunk):
Christian Heimesaf98da12008-01-27 15:18:18 +0000102 event = threading.Event()
103 s = echo_server(event)
Guido van Rossum806c2462007-08-06 23:33:07 +0000104 s.chunk_size = server_chunk
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000105 s.start()
Christian Heimesaf98da12008-01-27 15:18:18 +0000106 event.wait()
107 event.clear()
108 time.sleep(0.01) # Give server time to start accepting.
Guido van Rossum806c2462007-08-06 23:33:07 +0000109 c = echo_client(term)
110 c.push(b"hello ")
Jeremy Hyltonb4df71f2007-08-29 14:56:40 +0000111 c.push(bytes("world%s" % term, "ascii"))
112 c.push(bytes("I'm not dead yet!%s" % term, "ascii"))
Guido van Rossum806c2462007-08-06 23:33:07 +0000113 c.push(SERVER_QUIT)
114 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Michael W. Hudson73909422005-06-20 13:45:34 +0000115 s.join()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000116
Guido van Rossum806c2462007-08-06 23:33:07 +0000117 self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000118
Guido van Rossum806c2462007-08-06 23:33:07 +0000119 # the line terminator tests below check receiving variously-sized
120 # chunks back from the server in order to exercise all branches of
121 # async_chat.handle_read
122
123 def test_line_terminator1(self):
124 # test one-character terminator
125 for l in (1,2,3):
Guido van Rossum98297ee2007-11-06 21:34:58 +0000126 self.line_terminator_check('\n', l)
Guido van Rossum806c2462007-08-06 23:33:07 +0000127
128 def test_line_terminator2(self):
129 # test two-character terminator
130 for l in (1,2,3):
Guido van Rossum98297ee2007-11-06 21:34:58 +0000131 self.line_terminator_check('\r\n', l)
Guido van Rossum806c2462007-08-06 23:33:07 +0000132
133 def test_line_terminator3(self):
134 # test three-character terminator
135 for l in (1,2,3):
Guido van Rossum98297ee2007-11-06 21:34:58 +0000136 self.line_terminator_check('qqq', l)
Guido van Rossum806c2462007-08-06 23:33:07 +0000137
138 def numeric_terminator_check(self, termlen):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000139 # Try reading a fixed number of bytes
Christian Heimesaf98da12008-01-27 15:18:18 +0000140 s, event = start_echo_server()
Guido van Rossum806c2462007-08-06 23:33:07 +0000141 c = echo_client(termlen)
142 data = b"hello world, I'm not dead yet!\n"
143 c.push(data)
144 c.push(SERVER_QUIT)
145 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Michael W. Hudson73909422005-06-20 13:45:34 +0000146 s.join()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000147
Guido van Rossum806c2462007-08-06 23:33:07 +0000148 self.assertEqual(c.contents, [data[:termlen]])
149
150 def test_numeric_terminator1(self):
151 # check that ints & longs both work (since type is
152 # explicitly checked in async_chat.handle_read)
153 self.numeric_terminator_check(1)
154
155 def test_numeric_terminator2(self):
156 self.numeric_terminator_check(6)
157
158 def test_none_terminator(self):
159 # Try reading a fixed number of bytes
Christian Heimesaf98da12008-01-27 15:18:18 +0000160 s, event = start_echo_server()
Guido van Rossum806c2462007-08-06 23:33:07 +0000161 c = echo_client(None)
162 data = b"hello world, I'm not dead yet!\n"
163 c.push(data)
164 c.push(SERVER_QUIT)
165 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
166 s.join()
167
168 self.assertEqual(c.contents, [])
169 self.assertEqual(c.buffer, data)
170
171 def test_simple_producer(self):
Christian Heimesaf98da12008-01-27 15:18:18 +0000172 s, event = start_echo_server()
Guido van Rossum806c2462007-08-06 23:33:07 +0000173 c = echo_client(b'\n')
174 data = b"hello world\nI'm not dead yet!\n"
175 p = asynchat.simple_producer(data+SERVER_QUIT, buffer_size=8)
176 c.push_with_producer(p)
177 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
178 s.join()
179
180 self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
181
182 def test_string_producer(self):
Christian Heimesaf98da12008-01-27 15:18:18 +0000183 s, event = start_echo_server()
Guido van Rossum806c2462007-08-06 23:33:07 +0000184 c = echo_client(b'\n')
185 data = b"hello world\nI'm not dead yet!\n"
186 c.push_with_producer(data+SERVER_QUIT)
187 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
188 s.join()
189
190 self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
191
192 def test_empty_line(self):
193 # checks that empty lines are handled correctly
Christian Heimesaf98da12008-01-27 15:18:18 +0000194 s, event = start_echo_server()
Guido van Rossum806c2462007-08-06 23:33:07 +0000195 c = echo_client(b'\n')
Jeremy Hyltonb4df71f2007-08-29 14:56:40 +0000196 c.push(b"hello world\n\nI'm not dead yet!\n")
Guido van Rossum806c2462007-08-06 23:33:07 +0000197 c.push(SERVER_QUIT)
198 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
199 s.join()
200
201 self.assertEqual(c.contents,
202 [b"hello world", b"", b"I'm not dead yet!"])
203
204 def test_close_when_done(self):
Christian Heimesaf98da12008-01-27 15:18:18 +0000205 s, event = start_echo_server()
Guido van Rossum806c2462007-08-06 23:33:07 +0000206 c = echo_client(b'\n')
Jeremy Hyltonb4df71f2007-08-29 14:56:40 +0000207 c.push(b"hello world\nI'm not dead yet!\n")
Guido van Rossum806c2462007-08-06 23:33:07 +0000208 c.push(SERVER_QUIT)
209 c.close_when_done()
210 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
211 s.join()
212
213 self.assertEqual(c.contents, [])
214 # the server might have been able to send a byte or two back, but this
215 # at least checks that it received something and didn't just fail
216 # (which could still result in the client not having received anything)
217 self.assertTrue(len(s.buffer) > 0)
218
219
220class TestAsynchat_WithPoll(TestAsynchat):
221 usepoll = True
222
223class TestHelperFunctions(unittest.TestCase):
224 def test_find_prefix_at_end(self):
225 self.assertEqual(asynchat.find_prefix_at_end("qwerty\r", "\r\n"), 1)
226 self.assertEqual(asynchat.find_prefix_at_end("qwertydkjf", "\r\n"), 0)
227
228class TestFifo(unittest.TestCase):
229 def test_basic(self):
230 f = asynchat.fifo()
231 f.push(7)
232 f.push(b'a')
233 self.assertEqual(len(f), 2)
234 self.assertEqual(f.first(), 7)
235 self.assertEqual(f.pop(), (1, 7))
236 self.assertEqual(len(f), 1)
237 self.assertEqual(f.first(), b'a')
238 self.assertEqual(f.is_empty(), False)
239 self.assertEqual(f.pop(), (1, b'a'))
240 self.assertEqual(len(f), 0)
241 self.assertEqual(f.is_empty(), True)
242 self.assertEqual(f.pop(), (0, None))
243
244 def test_given_list(self):
245 f = asynchat.fifo([b'x', 17, 3])
246 self.assertEqual(len(f), 3)
247 self.assertEqual(f.pop(), (1, b'x'))
248 self.assertEqual(f.pop(), (1, 17))
249 self.assertEqual(f.pop(), (1, 3))
250 self.assertEqual(f.pop(), (0, None))
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000251
252
253def test_main(verbose=None):
Guido van Rossum806c2462007-08-06 23:33:07 +0000254 test_support.run_unittest(TestAsynchat, TestAsynchat_WithPoll,
255 TestHelperFunctions, TestFifo)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000256
257if __name__ == "__main__":
258 test_main(verbose=True)