blob: 5fcc5290c1ae0b1ab2bf09bd2d285a5f39639f4f [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
6from test import test_support
Guido van Rossum66172522001-04-06 16:32:22 +00007
8HOST = "127.0.0.1"
Michael W. Hudson73909422005-06-20 13:45:34 +00009PORT = 54322
Facundo Batistaec624232007-07-29 14:23:08 +000010SERVER_QUIT = 'QUIT\n'
Guido van Rossum66172522001-04-06 16:32:22 +000011
12class echo_server(threading.Thread):
Facundo Batistaec624232007-07-29 14:23:08 +000013 # parameter to determine the number of bytes passed back to the
14 # client each send
15 chunk_size = 1
Guido van Rossum66172522001-04-06 16:32:22 +000016
17 def run(self):
18 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Guido van Rossumf3ee46b2001-04-15 00:42:13 +000019 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Neal Norwitz62bc8aa2006-06-13 04:08:53 +000020 global PORT
21 PORT = test_support.bind_port(sock, HOST, PORT)
Guido van Rossum66172522001-04-06 16:32:22 +000022 sock.listen(1)
23 conn, client = sock.accept()
Facundo Batistaec624232007-07-29 14:23:08 +000024 self.buffer = ""
25 # collect data until quit message is seen
26 while SERVER_QUIT not in self.buffer:
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000027 data = conn.recv(1)
Guido van Rossum66172522001-04-06 16:32:22 +000028 if not data:
29 break
Facundo Batistaec624232007-07-29 14:23:08 +000030 self.buffer = self.buffer + data
31
32 # remove the SERVER_QUIT message
33 self.buffer = self.buffer.replace(SERVER_QUIT, '')
34
35 # re-send entire set of collected data
36 try:
37 # this may fail on some tests, such as test_close_when_done, since
38 # the client closes the channel when it's done sending
39 while self.buffer:
40 n = conn.send(self.buffer[:self.chunk_size])
41 time.sleep(0.001)
42 self.buffer = self.buffer[n:]
43 except:
44 pass
45
Guido van Rossum66172522001-04-06 16:32:22 +000046 conn.close()
47 sock.close()
48
49class echo_client(asynchat.async_chat):
50
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000051 def __init__(self, terminator):
Guido van Rossum66172522001-04-06 16:32:22 +000052 asynchat.async_chat.__init__(self)
Facundo Batistaec624232007-07-29 14:23:08 +000053 self.contents = []
Guido van Rossum66172522001-04-06 16:32:22 +000054 self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
55 self.connect((HOST, PORT))
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000056 self.set_terminator(terminator)
Facundo Batistaec624232007-07-29 14:23:08 +000057 self.buffer = ''
Guido van Rossum66172522001-04-06 16:32:22 +000058
59 def handle_connect(self):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000060 pass
61 ##print "Connected"
Guido van Rossum66172522001-04-06 16:32:22 +000062
63 def collect_incoming_data(self, data):
Facundo Batistaec624232007-07-29 14:23:08 +000064 self.buffer += data
Guido van Rossum66172522001-04-06 16:32:22 +000065
66 def found_terminator(self):
Facundo Batistaec624232007-07-29 14:23:08 +000067 self.contents.append(self.buffer)
Guido van Rossum66172522001-04-06 16:32:22 +000068 self.buffer = ""
Guido van Rossum66172522001-04-06 16:32:22 +000069
Guido van Rossum66172522001-04-06 16:32:22 +000070
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000071class TestAsynchat(unittest.TestCase):
Facundo Batistaec624232007-07-29 14:23:08 +000072 usepoll = False
73
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000074 def setUp (self):
75 pass
76
77 def tearDown (self):
78 pass
79
Facundo Batistaec624232007-07-29 14:23:08 +000080 def line_terminator_check(self, term, server_chunk):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000081 s = echo_server()
Facundo Batistaec624232007-07-29 14:23:08 +000082 s.chunk_size = server_chunk
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000083 s.start()
Facundo Batistaec624232007-07-29 14:23:08 +000084 time.sleep(0.5) # Give server time to initialize
85 c = echo_client(term)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000086 c.push("hello ")
Facundo Batistaec624232007-07-29 14:23:08 +000087 c.push("world%s" % term)
88 c.push("I'm not dead yet!%s" % term)
89 c.push(SERVER_QUIT)
90 asyncore.loop(use_poll=self.usepoll)#, count=5, timeout=5)
Michael W. Hudson73909422005-06-20 13:45:34 +000091 s.join()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000092
Facundo Batistaec624232007-07-29 14:23:08 +000093 self.assertEqual(c.contents, ["hello world", "I'm not dead yet!"])
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000094
Facundo Batistaec624232007-07-29 14:23:08 +000095 # the line terminator tests below check receiving variously-sized
96 # chunks back from the server in order to exercise all branches of
97 # async_chat.handle_read
98
99 def test_line_terminator1(self):
100 # test one-character terminator
101 for l in (1,2,3):
102 self.line_terminator_check('\n', l)
103
104 def test_line_terminator2(self):
105 # test two-character terminator
106 for l in (1,2,3):
107 self.line_terminator_check('\r\n', l)
108
109 def test_line_terminator3(self):
110 # test three-character terminator
111 for l in (1,2,3):
112 self.line_terminator_check('qqq', l)
113
114 def numeric_terminator_check(self, termlen):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000115 # Try reading a fixed number of bytes
116 s = echo_server()
117 s.start()
Facundo Batistaec624232007-07-29 14:23:08 +0000118 time.sleep(0.5) # Give server time to initialize
119 c = echo_client(termlen)
120 data = "hello world, I'm not dead yet!\n"
121 c.push(data)
122 c.push(SERVER_QUIT)
123 asyncore.loop(use_poll=self.usepoll)#, count=5, timeout=5)
Michael W. Hudson73909422005-06-20 13:45:34 +0000124 s.join()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000125
Facundo Batistaec624232007-07-29 14:23:08 +0000126 self.assertEqual(c.contents, [data[:termlen]])
127
128 def test_numeric_terminator1(self):
129 # check that ints & longs both work (since type is
130 # explicitly checked in async_chat.handle_read)
131 self.numeric_terminator_check(1)
132 self.numeric_terminator_check(1L)
133
134 def test_numeric_terminator2(self):
135 self.numeric_terminator_check(6L)
136
137 def test_none_terminator(self):
138 # Try reading a fixed number of bytes
139 s = echo_server()
140 s.start()
141 time.sleep(0.5) # Give server time to initialize
142 c = echo_client(None)
143 data = "hello world, I'm not dead yet!\n"
144 c.push(data)
145 c.push(SERVER_QUIT)
146 asyncore.loop(use_poll=self.usepoll)#, count=5, timeout=5)
147 s.join()
148
149 self.assertEqual(c.contents, [])
150 self.assertEqual(c.buffer, data)
151
152 def test_simple_producer(self):
153 s = echo_server()
154 s.start()
155 time.sleep(0.5) # Give server time to initialize
156 c = echo_client('\n')
157 data = "hello world\nI'm not dead yet!\n"
158 p = asynchat.simple_producer(data+SERVER_QUIT, buffer_size=8)
159 c.push_with_producer(p)
160 asyncore.loop(use_poll=self.usepoll)#, count=5, timeout=5)
161 s.join()
162
163 self.assertEqual(c.contents, ["hello world", "I'm not dead yet!"])
164
165 def test_string_producer(self):
166 s = echo_server()
167 s.start()
168 time.sleep(0.5) # Give server time to initialize
169 c = echo_client('\n')
170 data = "hello world\nI'm not dead yet!\n"
171 c.push_with_producer(data+SERVER_QUIT)
172 asyncore.loop(use_poll=self.usepoll)#, count=5, timeout=5)
173 s.join()
174
175 self.assertEqual(c.contents, ["hello world", "I'm not dead yet!"])
176
177 def test_empty_line(self):
178 # checks that empty lines are handled correctly
179 s = echo_server()
180 s.start()
181 time.sleep(0.5) # Give server time to initialize
182 c = echo_client('\n')
183 c.push("hello world\n\nI'm not dead yet!\n")
184 c.push(SERVER_QUIT)
185 asyncore.loop(use_poll=self.usepoll)
186 s.join()
187
188 self.assertEqual(c.contents, ["hello world", "", "I'm not dead yet!"])
189
190 def test_close_when_done(self):
191 s = echo_server()
192 s.start()
193 time.sleep(0.5) # Give server time to initialize
194 c = echo_client('\n')
195 c.push("hello world\nI'm not dead yet!\n")
196 c.push(SERVER_QUIT)
197 c.close_when_done()
198 asyncore.loop(use_poll=self.usepoll)#, count=5, timeout=5)
199 s.join()
200
201 self.assertEqual(c.contents, [])
202 # the server might have been able to send a byte or two back, but this
203 # at least checks that it received something and didn't just fail
204 # (which could still result in the client not having received anything)
205 self.assertTrue(len(s.buffer) > 0)
206
207
208class TestAsynchat_WithPoll(TestAsynchat):
209 usepoll = True
210
211class TestHelperFunctions(unittest.TestCase):
212 def test_find_prefix_at_end(self):
213 self.assertEqual(asynchat.find_prefix_at_end("qwerty\r", "\r\n"), 1)
214 self.assertEqual(asynchat.find_prefix_at_end("qwertydkjf", "\r\n"), 0)
215
216class TestFifo(unittest.TestCase):
217 def test_basic(self):
218 f = asynchat.fifo()
219 f.push(7)
220 f.push('a')
221 self.assertEqual(len(f), 2)
222 self.assertEqual(f.first(), 7)
223 self.assertEqual(f.pop(), (1, 7))
224 self.assertEqual(len(f), 1)
225 self.assertEqual(f.first(), 'a')
226 self.assertEqual(f.is_empty(), False)
227 self.assertEqual(f.pop(), (1, 'a'))
228 self.assertEqual(len(f), 0)
229 self.assertEqual(f.is_empty(), True)
230 self.assertEqual(f.pop(), (0, None))
231
232 def test_given_list(self):
233 f = asynchat.fifo(['x', 17, 3])
234 self.assertEqual(len(f), 3)
235 self.assertEqual(f.pop(), (1, 'x'))
236 self.assertEqual(f.pop(), (1, 17))
237 self.assertEqual(f.pop(), (1, 3))
238 self.assertEqual(f.pop(), (0, None))
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000239
240
241def test_main(verbose=None):
Facundo Batistaec624232007-07-29 14:23:08 +0000242 test_support.run_unittest(TestAsynchat, TestAsynchat_WithPoll,
243 TestHelperFunctions, TestFifo)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000244
245if __name__ == "__main__":
246 test_main(verbose=True)