blob: c97978ed45b139c8811d04a4295e78c8d7bd0ae1 [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
18 def run(self):
19 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Guido van Rossumf3ee46b2001-04-15 00:42:13 +000020 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Neal Norwitz62bc8aa2006-06-13 04:08:53 +000021 global PORT
22 PORT = test_support.bind_port(sock, HOST, PORT)
Guido van Rossum66172522001-04-06 16:32:22 +000023 sock.listen(1)
24 conn, client = sock.accept()
Facundo Batistaec624232007-07-29 14:23:08 +000025 self.buffer = ""
26 # collect data until quit message is seen
27 while SERVER_QUIT not in self.buffer:
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000028 data = conn.recv(1)
Guido van Rossum66172522001-04-06 16:32:22 +000029 if not data:
30 break
Facundo Batistaec624232007-07-29 14:23:08 +000031 self.buffer = self.buffer + data
32
33 # remove the SERVER_QUIT message
34 self.buffer = self.buffer.replace(SERVER_QUIT, '')
35
36 # re-send entire set of collected data
37 try:
38 # this may fail on some tests, such as test_close_when_done, since
39 # the client closes the channel when it's done sending
40 while self.buffer:
41 n = conn.send(self.buffer[:self.chunk_size])
42 time.sleep(0.001)
43 self.buffer = self.buffer[n:]
44 except:
45 pass
46
Guido van Rossum66172522001-04-06 16:32:22 +000047 conn.close()
48 sock.close()
49
50class echo_client(asynchat.async_chat):
51
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000052 def __init__(self, terminator):
Guido van Rossum66172522001-04-06 16:32:22 +000053 asynchat.async_chat.__init__(self)
Facundo Batistaec624232007-07-29 14:23:08 +000054 self.contents = []
Guido van Rossum66172522001-04-06 16:32:22 +000055 self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
56 self.connect((HOST, PORT))
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000057 self.set_terminator(terminator)
Facundo Batistaec624232007-07-29 14:23:08 +000058 self.buffer = ''
Guido van Rossum66172522001-04-06 16:32:22 +000059
60 def handle_connect(self):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000061 pass
Facundo Batista49504422007-07-31 03:03:34 +000062
63 if sys.platform == 'darwin':
64 # select.poll returns a select.POLLHUP at the end of the tests
65 # on darwin, so just ignore it
66 def handle_expt(self):
67 pass
Guido van Rossum66172522001-04-06 16:32:22 +000068
69 def collect_incoming_data(self, data):
Facundo Batistaec624232007-07-29 14:23:08 +000070 self.buffer += data
Guido van Rossum66172522001-04-06 16:32:22 +000071
72 def found_terminator(self):
Facundo Batistaec624232007-07-29 14:23:08 +000073 self.contents.append(self.buffer)
Guido van Rossum66172522001-04-06 16:32:22 +000074 self.buffer = ""
Guido van Rossum66172522001-04-06 16:32:22 +000075
Guido van Rossum66172522001-04-06 16:32:22 +000076
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000077class TestAsynchat(unittest.TestCase):
Facundo Batistaec624232007-07-29 14:23:08 +000078 usepoll = False
79
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000080 def setUp (self):
81 pass
82
83 def tearDown (self):
84 pass
85
Facundo Batistaec624232007-07-29 14:23:08 +000086 def line_terminator_check(self, term, server_chunk):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000087 s = echo_server()
Facundo Batistaec624232007-07-29 14:23:08 +000088 s.chunk_size = server_chunk
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000089 s.start()
Facundo Batistaec624232007-07-29 14:23:08 +000090 time.sleep(0.5) # Give server time to initialize
91 c = echo_client(term)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000092 c.push("hello ")
Facundo Batistaec624232007-07-29 14:23:08 +000093 c.push("world%s" % term)
94 c.push("I'm not dead yet!%s" % term)
95 c.push(SERVER_QUIT)
Facundo Batista49504422007-07-31 03:03:34 +000096 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Michael W. Hudson73909422005-06-20 13:45:34 +000097 s.join()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000098
Facundo Batistaec624232007-07-29 14:23:08 +000099 self.assertEqual(c.contents, ["hello world", "I'm not dead yet!"])
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000100
Facundo Batistaec624232007-07-29 14:23:08 +0000101 # the line terminator tests below check receiving variously-sized
102 # chunks back from the server in order to exercise all branches of
103 # async_chat.handle_read
104
105 def test_line_terminator1(self):
106 # test one-character terminator
107 for l in (1,2,3):
108 self.line_terminator_check('\n', l)
109
110 def test_line_terminator2(self):
111 # test two-character terminator
112 for l in (1,2,3):
113 self.line_terminator_check('\r\n', l)
114
115 def test_line_terminator3(self):
116 # test three-character terminator
117 for l in (1,2,3):
118 self.line_terminator_check('qqq', l)
119
120 def numeric_terminator_check(self, termlen):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000121 # Try reading a fixed number of bytes
122 s = echo_server()
123 s.start()
Facundo Batistaec624232007-07-29 14:23:08 +0000124 time.sleep(0.5) # Give server time to initialize
125 c = echo_client(termlen)
126 data = "hello world, I'm not dead yet!\n"
127 c.push(data)
128 c.push(SERVER_QUIT)
Facundo Batista49504422007-07-31 03:03:34 +0000129 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Michael W. Hudson73909422005-06-20 13:45:34 +0000130 s.join()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000131
Facundo Batistaec624232007-07-29 14:23:08 +0000132 self.assertEqual(c.contents, [data[:termlen]])
133
134 def test_numeric_terminator1(self):
135 # check that ints & longs both work (since type is
136 # explicitly checked in async_chat.handle_read)
137 self.numeric_terminator_check(1)
138 self.numeric_terminator_check(1L)
139
140 def test_numeric_terminator2(self):
141 self.numeric_terminator_check(6L)
142
143 def test_none_terminator(self):
144 # Try reading a fixed number of bytes
145 s = echo_server()
146 s.start()
147 time.sleep(0.5) # Give server time to initialize
148 c = echo_client(None)
149 data = "hello world, I'm not dead yet!\n"
150 c.push(data)
151 c.push(SERVER_QUIT)
Facundo Batista49504422007-07-31 03:03:34 +0000152 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Facundo Batistaec624232007-07-29 14:23:08 +0000153 s.join()
154
155 self.assertEqual(c.contents, [])
156 self.assertEqual(c.buffer, data)
157
158 def test_simple_producer(self):
159 s = echo_server()
160 s.start()
161 time.sleep(0.5) # Give server time to initialize
162 c = echo_client('\n')
163 data = "hello world\nI'm not dead yet!\n"
164 p = asynchat.simple_producer(data+SERVER_QUIT, buffer_size=8)
165 c.push_with_producer(p)
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, ["hello world", "I'm not dead yet!"])
170
171 def test_string_producer(self):
172 s = echo_server()
173 s.start()
174 time.sleep(0.5) # Give server time to initialize
175 c = echo_client('\n')
176 data = "hello world\nI'm not dead yet!\n"
177 c.push_with_producer(data+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, ["hello world", "I'm not dead yet!"])
182
183 def test_empty_line(self):
184 # checks that empty lines are handled correctly
185 s = echo_server()
186 s.start()
187 time.sleep(0.5) # Give server time to initialize
188 c = echo_client('\n')
189 c.push("hello world\n\nI'm not dead yet!\n")
190 c.push(SERVER_QUIT)
Facundo Batista49504422007-07-31 03:03:34 +0000191 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
Facundo Batistaec624232007-07-29 14:23:08 +0000192 s.join()
193
194 self.assertEqual(c.contents, ["hello world", "", "I'm not dead yet!"])
195
196 def test_close_when_done(self):
197 s = echo_server()
198 s.start()
199 time.sleep(0.5) # Give server time to initialize
200 c = echo_client('\n')
201 c.push("hello world\nI'm not dead yet!\n")
202 c.push(SERVER_QUIT)
203 c.close_when_done()
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, [])
208 # the server might have been able to send a byte or two back, but this
209 # at least checks that it received something and didn't just fail
210 # (which could still result in the client not having received anything)
211 self.assertTrue(len(s.buffer) > 0)
212
213
214class TestAsynchat_WithPoll(TestAsynchat):
215 usepoll = True
216
217class TestHelperFunctions(unittest.TestCase):
218 def test_find_prefix_at_end(self):
219 self.assertEqual(asynchat.find_prefix_at_end("qwerty\r", "\r\n"), 1)
220 self.assertEqual(asynchat.find_prefix_at_end("qwertydkjf", "\r\n"), 0)
221
222class TestFifo(unittest.TestCase):
223 def test_basic(self):
224 f = asynchat.fifo()
225 f.push(7)
226 f.push('a')
227 self.assertEqual(len(f), 2)
228 self.assertEqual(f.first(), 7)
229 self.assertEqual(f.pop(), (1, 7))
230 self.assertEqual(len(f), 1)
231 self.assertEqual(f.first(), 'a')
232 self.assertEqual(f.is_empty(), False)
233 self.assertEqual(f.pop(), (1, 'a'))
234 self.assertEqual(len(f), 0)
235 self.assertEqual(f.is_empty(), True)
236 self.assertEqual(f.pop(), (0, None))
237
238 def test_given_list(self):
239 f = asynchat.fifo(['x', 17, 3])
240 self.assertEqual(len(f), 3)
241 self.assertEqual(f.pop(), (1, 'x'))
242 self.assertEqual(f.pop(), (1, 17))
243 self.assertEqual(f.pop(), (1, 3))
244 self.assertEqual(f.pop(), (0, None))
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000245
246
247def test_main(verbose=None):
Facundo Batistaec624232007-07-29 14:23:08 +0000248 test_support.run_unittest(TestAsynchat, TestAsynchat_WithPoll,
249 TestHelperFunctions, TestFifo)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000250
251if __name__ == "__main__":
252 test_main(verbose=True)