blob: be8e116fdc3e5a65514d673eda0d1e1e5c9e15d7 [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
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)
Thomas Wouters0e3f5912006-08-11 14:57:12 +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()
Guido van Rossum806c2462007-08-06 23:33:07 +000025 self.buffer = b""
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
Guido van Rossum806c2462007-08-06 23:33:07 +000031 self.buffer = self.buffer + data
32
33 # remove the SERVER_QUIT message
34 self.buffer = self.buffer.replace(SERVER_QUIT, b'')
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)
Guido van Rossum806c2462007-08-06 23:33:07 +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)
Guido van Rossum076da092007-07-12 07:58:54 +000058 self.buffer = b""
Guido van Rossum66172522001-04-06 16:32:22 +000059
60 def handle_connect(self):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000061 pass
Guido van Rossum806c2462007-08-06 23:33:07 +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):
Guido van Rossum806c2462007-08-06 23:33:07 +000070 self.buffer += data
Guido van Rossum66172522001-04-06 16:32:22 +000071
72 def found_terminator(self):
Guido van Rossum806c2462007-08-06 23:33:07 +000073 self.contents.append(self.buffer)
Guido van Rossum076da092007-07-12 07:58:54 +000074 self.buffer = b""
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):
Guido van Rossum806c2462007-08-06 23:33:07 +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
Guido van Rossum806c2462007-08-06 23:33:07 +000086 def line_terminator_check(self, term, server_chunk):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000087 s = echo_server()
Guido van Rossum806c2462007-08-06 23:33:07 +000088 s.chunk_size = server_chunk
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000089 s.start()
Guido van Rossum806c2462007-08-06 23:33:07 +000090 time.sleep(0.5) # Give server time to initialize
91 c = echo_client(term)
92 c.push(b"hello ")
93 c.push(bytes("world%s" % term))
94 c.push(bytes("I'm not dead yet!%s" % term))
95 c.push(SERVER_QUIT)
96 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
Guido van Rossum806c2462007-08-06 23:33:07 +000099 self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +0000100
Guido van Rossum806c2462007-08-06 23:33:07 +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(b'\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(b'\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(b'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()
Guido van Rossum806c2462007-08-06 23:33:07 +0000124 time.sleep(0.5) # Give server time to initialize
125 c = echo_client(termlen)
126 data = b"hello world, I'm not dead yet!\n"
127 c.push(data)
128 c.push(SERVER_QUIT)
129 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
Guido van Rossum806c2462007-08-06 23:33:07 +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
139 def test_numeric_terminator2(self):
140 self.numeric_terminator_check(6)
141
142 def test_none_terminator(self):
143 # Try reading a fixed number of bytes
144 s = echo_server()
145 s.start()
146 time.sleep(0.5) # Give server time to initialize
147 c = echo_client(None)
148 data = b"hello world, I'm not dead yet!\n"
149 c.push(data)
150 c.push(SERVER_QUIT)
151 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
152 s.join()
153
154 self.assertEqual(c.contents, [])
155 self.assertEqual(c.buffer, data)
156
157 def test_simple_producer(self):
158 s = echo_server()
159 s.start()
160 time.sleep(0.5) # Give server time to initialize
161 c = echo_client(b'\n')
162 data = b"hello world\nI'm not dead yet!\n"
163 p = asynchat.simple_producer(data+SERVER_QUIT, buffer_size=8)
164 c.push_with_producer(p)
165 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
166 s.join()
167
168 self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
169
170 def test_string_producer(self):
171 s = echo_server()
172 s.start()
173 time.sleep(0.5) # Give server time to initialize
174 c = echo_client(b'\n')
175 data = b"hello world\nI'm not dead yet!\n"
176 c.push_with_producer(data+SERVER_QUIT)
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_empty_line(self):
183 # checks that empty lines are handled correctly
184 s = echo_server()
185 s.start()
186 time.sleep(0.5) # Give server time to initialize
187 c = echo_client(b'\n')
188 c.push("hello world\n\nI'm not dead yet!\n")
189 c.push(SERVER_QUIT)
190 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
191 s.join()
192
193 self.assertEqual(c.contents,
194 [b"hello world", b"", b"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(b'\n')
201 c.push("hello world\nI'm not dead yet!\n")
202 c.push(SERVER_QUIT)
203 c.close_when_done()
204 asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
205 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(b'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(), b'a')
232 self.assertEqual(f.is_empty(), False)
233 self.assertEqual(f.pop(), (1, b'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([b'x', 17, 3])
240 self.assertEqual(len(f), 3)
241 self.assertEqual(f.pop(), (1, b'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):
Guido van Rossum806c2462007-08-06 23:33:07 +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)