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) |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 16 | sock.bind((HOST, PORT)) |
| 17 | sock.listen(1) |
| 18 | conn, client = sock.accept() |
| 19 | buffer = "" |
| 20 | while "\n" not in buffer: |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 21 | data = conn.recv(1) |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 22 | 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 | |
| 31 | class echo_client(asynchat.async_chat): |
| 32 | |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 33 | def __init__(self, terminator): |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 34 | asynchat.async_chat.__init__(self) |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 35 | self.contents = None |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 36 | self.create_socket(socket.AF_INET, socket.SOCK_STREAM) |
| 37 | self.connect((HOST, PORT)) |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 38 | self.set_terminator(terminator) |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 39 | self.buffer = "" |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 40 | |
| 41 | def handle_connect(self): |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 42 | pass |
| 43 | ##print "Connected" |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 44 | |
| 45 | def collect_incoming_data(self, data): |
| 46 | self.buffer = self.buffer + data |
| 47 | |
| 48 | def found_terminator(self): |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 49 | #print "Received:", repr(self.buffer) |
| 50 | self.contents = self.buffer |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 51 | self.buffer = "" |
| 52 | self.close() |
| 53 | |
Guido van Rossum | 6617252 | 2001-04-06 16:32:22 +0000 | [diff] [blame] | 54 | |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 55 | class 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. Hudson | 7390942 | 2005-06-20 13:45:34 +0000 | [diff] [blame] | 70 | s.join() |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 71 | |
| 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. Hudson | 7390942 | 2005-06-20 13:45:34 +0000 | [diff] [blame] | 83 | s.join() |
Andrew M. Kuchling | 5ac2534 | 2005-06-09 14:56:31 +0000 | [diff] [blame] | 84 | |
| 85 | self.assertEqual(c.contents, 'hello ') |
| 86 | |
| 87 | |
| 88 | def test_main(verbose=None): |
| 89 | test_support.run_unittest(TestAsynchat) |
| 90 | |
| 91 | if __name__ == "__main__": |
| 92 | test_main(verbose=True) |