Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 1 | # test asynchat -- requires threading |
| 2 | |
Guido van Rossum | 9df3eab | 2001-04-14 14:35:43 +0000 | [diff] [blame] | 3 | import thread # If this fails, we can't test this module |
Guido van Rossum | dca060c | 2001-04-06 16:43:49 +0000 | [diff] [blame] | 4 | import asyncore, asynchat, socket, threading, time |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 5 | import unittest |
| 6 | from test import test_support |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 7 | |
| 8 | HOST = "127.0.0.1" |
Michael W. Hudson | 7390942 | 2005-06-20 13:45:34 +0000 | [diff] [blame] | 9 | PORT = 54322 |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 10 | |
| 11 | class echo_server(threading.Thread): |
| 12 | |
| 13 | def run(self): |
| 14 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Guido van Rossum | f3ee46b | 2001-04-15 00:42:13 +0000 | [diff] [blame] | 15 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame^] | 16 | global PORT |
| 17 | PORT = test_support.bind_port(sock, HOST, PORT) |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 18 | sock.listen(1) |
| 19 | conn, client = sock.accept() |
| 20 | buffer = "" |
| 21 | while "\n" not in buffer: |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 22 | data = conn.recv(1) |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 23 | 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 | |
| 32 | class echo_client(asynchat.async_chat): |
| 33 | |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 34 | def __init__(self, terminator): |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 35 | asynchat.async_chat.__init__(self) |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 36 | self.contents = None |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 37 | self.create_socket(socket.AF_INET, socket.SOCK_STREAM) |
| 38 | self.connect((HOST, PORT)) |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 39 | self.set_terminator(terminator) |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 40 | self.buffer = "" |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 41 | |
| 42 | def handle_connect(self): |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 43 | pass |
| 44 | ##print "Connected" |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 45 | |
| 46 | def collect_incoming_data(self, data): |
| 47 | self.buffer = self.buffer + data |
| 48 | |
| 49 | def found_terminator(self): |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 50 | #print "Received:", repr(self.buffer) |
| 51 | self.contents = self.buffer |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 52 | self.buffer = "" |
| 53 | self.close() |
| 54 | |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 55 | |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 56 | class 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. Hudson | 7390942 | 2005-06-20 13:45:34 +0000 | [diff] [blame] | 71 | s.join() |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 72 | |
| 73 | self.assertEqual(c.contents, 'hello world') |
| 74 | |
| 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 |
| 80 | c = echo_client(6L) |
| 81 | c.push("hello ") |
| 82 | c.push("world\n") |
| 83 | asyncore.loop() |
Michael W. Hudson | 7390942 | 2005-06-20 13:45:34 +0000 | [diff] [blame] | 84 | s.join() |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 85 | |
| 86 | self.assertEqual(c.contents, 'hello ') |
| 87 | |
| 88 | |
| 89 | def test_main(verbose=None): |
| 90 | test_support.run_unittest(TestAsynchat) |
| 91 | |
| 92 | if __name__ == "__main__": |
| 93 | test_main(verbose=True) |