blob: 7629296b002e3b4d76ce755d6f98597bed780f10 [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
Guido van Rossum66172522001-04-06 16:32:22 +000010
11class echo_server(threading.Thread):
12
13 def run(self):
14 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Guido van Rossumf3ee46b2001-04-15 00:42:13 +000015 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000016 global PORT
17 PORT = test_support.bind_port(sock, HOST, PORT)
Guido van Rossum66172522001-04-06 16:32:22 +000018 sock.listen(1)
19 conn, client = sock.accept()
Guido van Rossum076da092007-07-12 07:58:54 +000020 buffer = b""
21 while b"\n" not in buffer:
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000022 data = conn.recv(1)
Guido van Rossum66172522001-04-06 16:32:22 +000023 if not data:
24 break
25 buffer = buffer + data
26 while buffer:
27 n = conn.send(buffer)
28 buffer = buffer[n:]
29 conn.close()
30 sock.close()
31
32class echo_client(asynchat.async_chat):
33
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000034 def __init__(self, terminator):
Guido van Rossum66172522001-04-06 16:32:22 +000035 asynchat.async_chat.__init__(self)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000036 self.contents = None
Guido van Rossum66172522001-04-06 16:32:22 +000037 self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
38 self.connect((HOST, PORT))
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000039 self.set_terminator(terminator)
Guido van Rossum076da092007-07-12 07:58:54 +000040 self.buffer = b""
Guido van Rossum66172522001-04-06 16:32:22 +000041
42 def handle_connect(self):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000043 pass
44 ##print "Connected"
Guido van Rossum66172522001-04-06 16:32:22 +000045
46 def collect_incoming_data(self, data):
47 self.buffer = self.buffer + data
48
49 def found_terminator(self):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000050 #print "Received:", repr(self.buffer)
51 self.contents = self.buffer
Guido van Rossum076da092007-07-12 07:58:54 +000052 self.buffer = b""
Guido van Rossum66172522001-04-06 16:32:22 +000053 self.close()
54
Guido van Rossum66172522001-04-06 16:32:22 +000055
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000056class TestAsynchat(unittest.TestCase):
57 def setUp (self):
58 pass
59
60 def tearDown (self):
61 pass
62
63 def test_line_terminator(self):
64 s = echo_server()
65 s.start()
66 time.sleep(1) # Give server time to initialize
67 c = echo_client('\n')
68 c.push("hello ")
69 c.push("world\n")
70 asyncore.loop()
Michael W. Hudson73909422005-06-20 13:45:34 +000071 s.join()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000072
Guido van Rossum076da092007-07-12 07:58:54 +000073 self.assertEqual(c.contents, b'hello world')
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000074
75 def test_numeric_terminator(self):
76 # Try reading a fixed number of bytes
77 s = echo_server()
78 s.start()
79 time.sleep(1) # Give server time to initialize
Guido van Rossume2a383d2007-01-15 16:59:06 +000080 c = echo_client(6)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000081 c.push("hello ")
82 c.push("world\n")
83 asyncore.loop()
Michael W. Hudson73909422005-06-20 13:45:34 +000084 s.join()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000085
Guido van Rossum076da092007-07-12 07:58:54 +000086 self.assertEqual(c.contents, b'hello ')
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000087
88
89def test_main(verbose=None):
90 test_support.run_unittest(TestAsynchat)
91
92if __name__ == "__main__":
93 test_main(verbose=True)