blob: f93587acbef2747961fba74b1379308293a14d62 [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)
Guido van Rossum66172522001-04-06 16:32:22 +000016 sock.bind((HOST, PORT))
17 sock.listen(1)
18 conn, client = sock.accept()
19 buffer = ""
20 while "\n" not in buffer:
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000021 data = conn.recv(1)
Guido van Rossum66172522001-04-06 16:32:22 +000022 if not data:
23 break
24 buffer = buffer + data
25 while buffer:
26 n = conn.send(buffer)
27 buffer = buffer[n:]
28 conn.close()
29 sock.close()
30
31class echo_client(asynchat.async_chat):
32
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000033 def __init__(self, terminator):
Guido van Rossum66172522001-04-06 16:32:22 +000034 asynchat.async_chat.__init__(self)
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000035 self.contents = None
Guido van Rossum66172522001-04-06 16:32:22 +000036 self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
37 self.connect((HOST, PORT))
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000038 self.set_terminator(terminator)
Guido van Rossum66172522001-04-06 16:32:22 +000039 self.buffer = ""
Guido van Rossum66172522001-04-06 16:32:22 +000040
41 def handle_connect(self):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000042 pass
43 ##print "Connected"
Guido van Rossum66172522001-04-06 16:32:22 +000044
45 def collect_incoming_data(self, data):
46 self.buffer = self.buffer + data
47
48 def found_terminator(self):
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000049 #print "Received:", repr(self.buffer)
50 self.contents = self.buffer
Guido van Rossum66172522001-04-06 16:32:22 +000051 self.buffer = ""
52 self.close()
53
Guido van Rossum66172522001-04-06 16:32:22 +000054
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000055class TestAsynchat(unittest.TestCase):
56 def setUp (self):
57 pass
58
59 def tearDown (self):
60 pass
61
62 def test_line_terminator(self):
63 s = echo_server()
64 s.start()
65 time.sleep(1) # Give server time to initialize
66 c = echo_client('\n')
67 c.push("hello ")
68 c.push("world\n")
69 asyncore.loop()
Michael W. Hudson73909422005-06-20 13:45:34 +000070 s.join()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000071
72 self.assertEqual(c.contents, 'hello world')
73
74 def test_numeric_terminator(self):
75 # Try reading a fixed number of bytes
76 s = echo_server()
77 s.start()
78 time.sleep(1) # Give server time to initialize
79 c = echo_client(6L)
80 c.push("hello ")
81 c.push("world\n")
82 asyncore.loop()
Michael W. Hudson73909422005-06-20 13:45:34 +000083 s.join()
Andrew M. Kuchling5ac25342005-06-09 14:56:31 +000084
85 self.assertEqual(c.contents, 'hello ')
86
87
88def test_main(verbose=None):
89 test_support.run_unittest(TestAsynchat)
90
91if __name__ == "__main__":
92 test_main(verbose=True)