blob: c81ec0de45093c132bb6669cb60e4074fe6af690 [file] [log] [blame]
R. David Murray59beec32009-03-30 19:04:00 +00001# test asynchat
Guido van Rossum66172522001-04-06 16:32:22 +00002
Victor Stinner09227b92010-04-27 23:03:16 +00003import asyncore, asynchat, socket, 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
Victor Stinner09227b92010-04-27 23:03:16 +00007try:
8 import threading
9except ImportError:
10 threading = None
R. David Murray59beec32009-03-30 19:04:00 +000011
Trent Nelsone41b0062008-04-08 23:47:30 +000012HOST = test_support.HOST
Facundo Batistaec624232007-07-29 14:23:08 +000013SERVER_QUIT = 'QUIT\n'
Guido van Rossum66172522001-04-06 16:32:22 +000014
Victor Stinner09227b92010-04-27 23:03:16 +000015if threading:
16 class echo_server(threading.Thread):
17 # parameter to determine the number of bytes passed back to the
18 # client each send
19 chunk_size = 1
Guido van Rossum66172522001-04-06 16:32:22 +000020
Victor Stinner09227b92010-04-27 23:03:16 +000021 def __init__(self, event):
22 threading.Thread.__init__(self)
23 self.event = event
24 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
25 self.port = test_support.bind_port(self.sock)
26 # This will be set if the client wants us to wait before echoing data
27 # back.
28 self.start_resend_event = None
Neal Norwitz6e070812008-01-27 01:44:05 +000029
Victor Stinner09227b92010-04-27 23:03:16 +000030 def run(self):
31 self.sock.listen(1)
32 self.event.set()
33 conn, client = self.sock.accept()
34 self.buffer = ""
35 # collect data until quit message is seen
36 while SERVER_QUIT not in self.buffer:
37 data = conn.recv(1)
38 if not data:
39 break
40 self.buffer = self.buffer + data
Facundo Batistaec624232007-07-29 14:23:08 +000041
Victor Stinner09227b92010-04-27 23:03:16 +000042 # remove the SERVER_QUIT message
43 self.buffer = self.buffer.replace(SERVER_QUIT, '')
Facundo Batistaec624232007-07-29 14:23:08 +000044
Victor Stinner09227b92010-04-27 23:03:16 +000045 if self.start_resend_event:
46 self.start_resend_event.wait()
Collin Winter22272512010-03-17 22:36:26 +000047
Victor Stinner09227b92010-04-27 23:03:16 +000048 # re-send entire set of collected data
49 try:
50 # this may fail on some tests, such as test_close_when_done, since
51 # the client closes the channel when it's done sending
52 while self.buffer:
53 n = conn.send(self.buffer[:self.chunk_size])
54 time.sleep(0.001)
55 self.buffer = self.buffer[n:]
56 except:
57 pass
58
59 conn.close()
60 self.sock.close()
61
62 class echo_client(asynchat.async_chat):
63
64 def __init__(self, terminator, server_port):
65 asynchat.async_chat.__init__(self)
66 self.contents = []
67 self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
68 self.connect((HOST, server_port))
69 self.set_terminator(terminator)
70 self.buffer = ''
71
72 def handle_connect(self):
Facundo Batistaec624232007-07-29 14:23:08 +000073 pass
74
Victor Stinner09227b92010-04-27 23:03:16 +000075 if sys.platform == 'darwin':
76 # select.poll returns a select.POLLHUP at the end of the tests
77 # on darwin, so just ignore it
78 def handle_expt(self):
79 pass
Guido van Rossum66172522001-04-06 16:32:22 +000080
Victor Stinner09227b92010-04-27 23:03:16 +000081 def collect_incoming_data(self, data):
82 self.buffer += data
Guido van Rossum66172522001-04-06 16:32:22 +000083
Victor Stinner09227b92010-04-27 23:03:16 +000084 def found_terminator(self):
85 self.contents.append(self.buffer)
86 self.buffer = ""
Guido van Rossum66172522001-04-06 16:32:22 +000087
Guido van Rossum66172522001-04-06 16:32:22 +000088
Victor Stinner09227b92010-04-27 23:03:16 +000089 def start_echo_server():
90 event = threading.Event()
91 s = echo_server(event)
92 s.start()
93 event.wait()
94 event.clear()
95 time.sleep(0.01) # Give server time to start accepting.
96 return s, event
Neal Norwitz6e070812008-01-27 01:44:05 +000097
98
Victor Stinner09227b92010-04-27 23:03:16 +000099@unittest.skipUnless(threading, 'Threading required for this test.')
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000100class TestAsynchat(unittest.TestCase):
Facundo Batistaec624232007-07-29 14:23:08 +0000101 usepoll = False
102
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000103 def setUp (self):
Antoine Pitrou643e85d2009-10-30 17:55:21 +0000104 self._threads = test_support.threading_setup()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000105
106 def tearDown (self):
Antoine Pitrou643e85d2009-10-30 17:55:21 +0000107 test_support.threading_cleanup(*self._threads)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000108
Facundo Batistaec624232007-07-29 14:23:08 +0000109 def line_terminator_check(self, term, server_chunk):
Neal Norwitz6e070812008-01-27 01:44:05 +0000110 event = threading.Event()
111 s = echo_server(event)
Facundo Batistaec624232007-07-29 14:23:08 +0000112 s.chunk_size = server_chunk
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000113 s.start()
Neal Norwitz6e070812008-01-27 01:44:05 +0000114 event.wait()
115 event.clear()
116 time.sleep(0.01) # Give server time to start accepting.
Trent Nelsone41b0062008-04-08 23:47:30 +0000117 c = echo_client(term, s.port)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000118 c.push("hello ")
Facundo Batistaec624232007-07-29 14:23:08 +0000119 c.push("world%s" % term)
120 c.push("I'm not dead yet!%s" % term)
121 c.push(SERVER_QUIT)
Facundo Batista49504422007-07-31 03:03:34 +0000122 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Michael W. Hudson73909422005-06-20 13:45:34 +0000123 s.join()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000124
Facundo Batistaec624232007-07-29 14:23:08 +0000125 self.assertEqual(c.contents, ["hello world", "I'm not dead yet!"])
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000126
Facundo Batistaec624232007-07-29 14:23:08 +0000127 # the line terminator tests below check receiving variously-sized
128 # chunks back from the server in order to exercise all branches of
129 # async_chat.handle_read
130
131 def test_line_terminator1(self):
132 # test one-character terminator
133 for l in (1,2,3):
134 self.line_terminator_check('\n', l)
135
136 def test_line_terminator2(self):
137 # test two-character terminator
138 for l in (1,2,3):
139 self.line_terminator_check('\r\n', l)
140
141 def test_line_terminator3(self):
142 # test three-character terminator
143 for l in (1,2,3):
144 self.line_terminator_check('qqq', l)
145
146 def numeric_terminator_check(self, termlen):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000147 # Try reading a fixed number of bytes
Neal Norwitz6e070812008-01-27 01:44:05 +0000148 s, event = start_echo_server()
Trent Nelsone41b0062008-04-08 23:47:30 +0000149 c = echo_client(termlen, s.port)
Facundo Batistaec624232007-07-29 14:23:08 +0000150 data = "hello world, I'm not dead yet!\n"
151 c.push(data)
152 c.push(SERVER_QUIT)
Facundo Batista49504422007-07-31 03:03:34 +0000153 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Michael W. Hudson73909422005-06-20 13:45:34 +0000154 s.join()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000155
Facundo Batistaec624232007-07-29 14:23:08 +0000156 self.assertEqual(c.contents, [data[:termlen]])
157
158 def test_numeric_terminator1(self):
159 # check that ints & longs both work (since type is
160 # explicitly checked in async_chat.handle_read)
161 self.numeric_terminator_check(1)
162 self.numeric_terminator_check(1L)
163
164 def test_numeric_terminator2(self):
165 self.numeric_terminator_check(6L)
166
167 def test_none_terminator(self):
168 # Try reading a fixed number of bytes
Neal Norwitz6e070812008-01-27 01:44:05 +0000169 s, event = start_echo_server()
Trent Nelsone41b0062008-04-08 23:47:30 +0000170 c = echo_client(None, s.port)
Facundo Batistaec624232007-07-29 14:23:08 +0000171 data = "hello world, I'm not dead yet!\n"
172 c.push(data)
173 c.push(SERVER_QUIT)
Facundo Batista49504422007-07-31 03:03:34 +0000174 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Facundo Batistaec624232007-07-29 14:23:08 +0000175 s.join()
176
177 self.assertEqual(c.contents, [])
178 self.assertEqual(c.buffer, data)
179
180 def test_simple_producer(self):
Neal Norwitz6e070812008-01-27 01:44:05 +0000181 s, event = start_echo_server()
Trent Nelsone41b0062008-04-08 23:47:30 +0000182 c = echo_client('\n', s.port)
Facundo Batistaec624232007-07-29 14:23:08 +0000183 data = "hello world\nI'm not dead yet!\n"
184 p = asynchat.simple_producer(data+SERVER_QUIT, buffer_size=8)
185 c.push_with_producer(p)
Facundo Batista49504422007-07-31 03:03:34 +0000186 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Facundo Batistaec624232007-07-29 14:23:08 +0000187 s.join()
188
189 self.assertEqual(c.contents, ["hello world", "I'm not dead yet!"])
190
191 def test_string_producer(self):
Neal Norwitz6e070812008-01-27 01:44:05 +0000192 s, event = start_echo_server()
Trent Nelsone41b0062008-04-08 23:47:30 +0000193 c = echo_client('\n', s.port)
Facundo Batistaec624232007-07-29 14:23:08 +0000194 data = "hello world\nI'm not dead yet!\n"
195 c.push_with_producer(data+SERVER_QUIT)
Facundo Batista49504422007-07-31 03:03:34 +0000196 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Facundo Batistaec624232007-07-29 14:23:08 +0000197 s.join()
198
199 self.assertEqual(c.contents, ["hello world", "I'm not dead yet!"])
200
201 def test_empty_line(self):
202 # checks that empty lines are handled correctly
Neal Norwitz6e070812008-01-27 01:44:05 +0000203 s, event = start_echo_server()
Trent Nelsone41b0062008-04-08 23:47:30 +0000204 c = echo_client('\n', s.port)
Facundo Batistaec624232007-07-29 14:23:08 +0000205 c.push("hello world\n\nI'm not dead yet!\n")
206 c.push(SERVER_QUIT)
Facundo Batista49504422007-07-31 03:03:34 +0000207 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Facundo Batistaec624232007-07-29 14:23:08 +0000208 s.join()
209
210 self.assertEqual(c.contents, ["hello world", "", "I'm not dead yet!"])
211
212 def test_close_when_done(self):
Neal Norwitz6e070812008-01-27 01:44:05 +0000213 s, event = start_echo_server()
Collin Winter22272512010-03-17 22:36:26 +0000214 s.start_resend_event = threading.Event()
Trent Nelsone41b0062008-04-08 23:47:30 +0000215 c = echo_client('\n', s.port)
Facundo Batistaec624232007-07-29 14:23:08 +0000216 c.push("hello world\nI'm not dead yet!\n")
217 c.push(SERVER_QUIT)
218 c.close_when_done()
Facundo Batista49504422007-07-31 03:03:34 +0000219 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Collin Winter22272512010-03-17 22:36:26 +0000220
221 # Only allow the server to start echoing data back to the client after
222 # the client has closed its connection. This prevents a race condition
223 # where the server echoes all of its data before we can check that it
224 # got any down below.
225 s.start_resend_event.set()
Facundo Batistaec624232007-07-29 14:23:08 +0000226 s.join()
227
228 self.assertEqual(c.contents, [])
229 # the server might have been able to send a byte or two back, but this
230 # at least checks that it received something and didn't just fail
231 # (which could still result in the client not having received anything)
232 self.assertTrue(len(s.buffer) > 0)
233
234
235class TestAsynchat_WithPoll(TestAsynchat):
236 usepoll = True
237
238class TestHelperFunctions(unittest.TestCase):
239 def test_find_prefix_at_end(self):
240 self.assertEqual(asynchat.find_prefix_at_end("qwerty\r", "\r\n"), 1)
241 self.assertEqual(asynchat.find_prefix_at_end("qwertydkjf", "\r\n"), 0)
242
243class TestFifo(unittest.TestCase):
244 def test_basic(self):
245 f = asynchat.fifo()
246 f.push(7)
247 f.push('a')
248 self.assertEqual(len(f), 2)
249 self.assertEqual(f.first(), 7)
250 self.assertEqual(f.pop(), (1, 7))
251 self.assertEqual(len(f), 1)
252 self.assertEqual(f.first(), 'a')
253 self.assertEqual(f.is_empty(), False)
254 self.assertEqual(f.pop(), (1, 'a'))
255 self.assertEqual(len(f), 0)
256 self.assertEqual(f.is_empty(), True)
257 self.assertEqual(f.pop(), (0, None))
258
259 def test_given_list(self):
260 f = asynchat.fifo(['x', 17, 3])
261 self.assertEqual(len(f), 3)
262 self.assertEqual(f.pop(), (1, 'x'))
263 self.assertEqual(f.pop(), (1, 17))
264 self.assertEqual(f.pop(), (1, 3))
265 self.assertEqual(f.pop(), (0, None))
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000266
267
268def test_main(verbose=None):
Facundo Batistaec624232007-07-29 14:23:08 +0000269 test_support.run_unittest(TestAsynchat, TestAsynchat_WithPoll,
270 TestHelperFunctions, TestFifo)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000271
272if __name__ == "__main__":
273 test_main(verbose=True)