blob: 4c79458e8d970d30c07b195eb17fa4268ce9bfd5 [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
Facundo Batista49504422007-07-31 03:03:34 +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
Facundo Batistaec624232007-07-29 14:23:08 +000011SERVER_QUIT = 'QUIT\n'
Guido van Rossum66172522001-04-06 16:32:22 +000012
13class echo_server(threading.Thread):
Facundo Batistaec624232007-07-29 14:23:08 +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
Neal Norwitz6e070812008-01-27 01:44:05 +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)
Neal Norwitz62bc8aa2006-06-13 04:08:53 +000025 global PORT
26 PORT = test_support.bind_port(sock, HOST, PORT)
Guido van Rossum66172522001-04-06 16:32:22 +000027 sock.listen(1)
Neal Norwitz6e070812008-01-27 01:44:05 +000028 self.event.set()
Guido van Rossum66172522001-04-06 16:32:22 +000029 conn, client = sock.accept()
Facundo Batistaec624232007-07-29 14:23:08 +000030 self.buffer = ""
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
Facundo Batistaec624232007-07-29 14:23:08 +000036 self.buffer = self.buffer + data
37
38 # remove the SERVER_QUIT message
39 self.buffer = self.buffer.replace(SERVER_QUIT, '')
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)
Facundo Batistaec624232007-07-29 14:23:08 +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)
Facundo Batistaec624232007-07-29 14:23:08 +000063 self.buffer = ''
Guido van Rossum66172522001-04-06 16:32:22 +000064
65 def handle_connect(self):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000066 pass
Facundo Batista49504422007-07-31 03:03:34 +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):
Facundo Batistaec624232007-07-29 14:23:08 +000075 self.buffer += data
Guido van Rossum66172522001-04-06 16:32:22 +000076
77 def found_terminator(self):
Facundo Batistaec624232007-07-29 14:23:08 +000078 self.contents.append(self.buffer)
Guido van Rossum66172522001-04-06 16:32:22 +000079 self.buffer = ""
Guido van Rossum66172522001-04-06 16:32:22 +000080
Guido van Rossum66172522001-04-06 16:32:22 +000081
Neal Norwitz6e070812008-01-27 01:44:05 +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):
Facundo Batistaec624232007-07-29 14:23:08 +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
Facundo Batistaec624232007-07-29 14:23:08 +0000101 def line_terminator_check(self, term, server_chunk):
Neal Norwitz6e070812008-01-27 01:44:05 +0000102 event = threading.Event()
103 s = echo_server(event)
Facundo Batistaec624232007-07-29 14:23:08 +0000104 s.chunk_size = server_chunk
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000105 s.start()
Neal Norwitz6e070812008-01-27 01:44:05 +0000106 event.wait()
107 event.clear()
108 time.sleep(0.01) # Give server time to start accepting.
Facundo Batistaec624232007-07-29 14:23:08 +0000109 c = echo_client(term)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000110 c.push("hello ")
Facundo Batistaec624232007-07-29 14:23:08 +0000111 c.push("world%s" % term)
112 c.push("I'm not dead yet!%s" % term)
113 c.push(SERVER_QUIT)
Facundo Batista49504422007-07-31 03:03:34 +0000114 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
Facundo Batistaec624232007-07-29 14:23:08 +0000117 self.assertEqual(c.contents, ["hello world", "I'm not dead yet!"])
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000118
Facundo Batistaec624232007-07-29 14:23:08 +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):
126 self.line_terminator_check('\n', l)
127
128 def test_line_terminator2(self):
129 # test two-character terminator
130 for l in (1,2,3):
131 self.line_terminator_check('\r\n', l)
132
133 def test_line_terminator3(self):
134 # test three-character terminator
135 for l in (1,2,3):
136 self.line_terminator_check('qqq', l)
137
138 def numeric_terminator_check(self, termlen):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000139 # Try reading a fixed number of bytes
Neal Norwitz6e070812008-01-27 01:44:05 +0000140 s, event = start_echo_server()
Facundo Batistaec624232007-07-29 14:23:08 +0000141 c = echo_client(termlen)
142 data = "hello world, I'm not dead yet!\n"
143 c.push(data)
144 c.push(SERVER_QUIT)
Facundo Batista49504422007-07-31 03:03:34 +0000145 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
Facundo Batistaec624232007-07-29 14:23:08 +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 self.numeric_terminator_check(1L)
155
156 def test_numeric_terminator2(self):
157 self.numeric_terminator_check(6L)
158
159 def test_none_terminator(self):
160 # Try reading a fixed number of bytes
Neal Norwitz6e070812008-01-27 01:44:05 +0000161 s, event = start_echo_server()
Facundo Batistaec624232007-07-29 14:23:08 +0000162 c = echo_client(None)
163 data = "hello world, I'm not dead yet!\n"
164 c.push(data)
165 c.push(SERVER_QUIT)
Facundo Batista49504422007-07-31 03:03:34 +0000166 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Facundo Batistaec624232007-07-29 14:23:08 +0000167 s.join()
168
169 self.assertEqual(c.contents, [])
170 self.assertEqual(c.buffer, data)
171
172 def test_simple_producer(self):
Neal Norwitz6e070812008-01-27 01:44:05 +0000173 s, event = start_echo_server()
Facundo Batistaec624232007-07-29 14:23:08 +0000174 c = echo_client('\n')
175 data = "hello world\nI'm not dead yet!\n"
176 p = asynchat.simple_producer(data+SERVER_QUIT, buffer_size=8)
177 c.push_with_producer(p)
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, ["hello world", "I'm not dead yet!"])
182
183 def test_string_producer(self):
Neal Norwitz6e070812008-01-27 01:44:05 +0000184 s, event = start_echo_server()
Facundo Batistaec624232007-07-29 14:23:08 +0000185 c = echo_client('\n')
186 data = "hello world\nI'm not dead yet!\n"
187 c.push_with_producer(data+SERVER_QUIT)
Facundo Batista49504422007-07-31 03:03:34 +0000188 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Facundo Batistaec624232007-07-29 14:23:08 +0000189 s.join()
190
191 self.assertEqual(c.contents, ["hello world", "I'm not dead yet!"])
192
193 def test_empty_line(self):
194 # checks that empty lines are handled correctly
Neal Norwitz6e070812008-01-27 01:44:05 +0000195 s, event = start_echo_server()
Facundo Batistaec624232007-07-29 14:23:08 +0000196 c = echo_client('\n')
197 c.push("hello world\n\nI'm not dead yet!\n")
198 c.push(SERVER_QUIT)
Facundo Batista49504422007-07-31 03:03:34 +0000199 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Facundo Batistaec624232007-07-29 14:23:08 +0000200 s.join()
201
202 self.assertEqual(c.contents, ["hello world", "", "I'm not dead yet!"])
203
204 def test_close_when_done(self):
Neal Norwitz6e070812008-01-27 01:44:05 +0000205 s, event = start_echo_server()
Facundo Batistaec624232007-07-29 14:23:08 +0000206 c = echo_client('\n')
207 c.push("hello world\nI'm not dead yet!\n")
208 c.push(SERVER_QUIT)
209 c.close_when_done()
Facundo Batista49504422007-07-31 03:03:34 +0000210 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Facundo Batistaec624232007-07-29 14:23:08 +0000211 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('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(), 'a')
238 self.assertEqual(f.is_empty(), False)
239 self.assertEqual(f.pop(), (1, '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(['x', 17, 3])
246 self.assertEqual(len(f), 3)
247 self.assertEqual(f.pop(), (1, '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):
Facundo Batistaec624232007-07-29 14:23:08 +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)