[Python-checkins] r50482 - in sandbox/trunk/pdb: mconnection.py test/test_mconnection.py

matt.fleming python-checkins at python.org
Fri Jul 7 21:47:25 CEST 2006


Author: matt.fleming
Date: Fri Jul  7 21:47:25 2006
New Revision: 50482

Modified:
   sandbox/trunk/pdb/mconnection.py
   sandbox/trunk/pdb/test/test_mconnection.py
Log:
Raise exception on pdbserver-side if client connection closes unexpectedly. 


Modified: sandbox/trunk/pdb/mconnection.py
==============================================================================
--- sandbox/trunk/pdb/mconnection.py	(original)
+++ sandbox/trunk/pdb/mconnection.py	Fri Jul  7 21:47:25 2006
@@ -138,6 +138,8 @@
             line = self.input.recv(bufsize)
         except socket.error, e:
             raise ReadError, e[1]
+        if len(line) == 0:
+            raise ReadError, 'Connection closed'
         if line[-1] != '\n': line += '\n'
         return line
 
@@ -155,6 +157,7 @@
         """ Specify the address to connection to. """
         MConnectionInterface.__init__(self)
         self._sock = self.output = self.input = None
+        self.connected = True
 
     def connect(self, addr):
         """Connect to the server. 'input' reads data from the
@@ -171,6 +174,7 @@
             self._sock.connect((self.host, self.port))
         except socket.error, e:
             raise ConnectionFailed, e[1]
+        self.connected = True
 
     def write(self, msg):
         try:
@@ -183,6 +187,8 @@
             line = self._sock.recv(bufsize)
         except socket.error, e:
             raise ReadError, e[1]
+        if len(line) == 0:
+            raise ReadError, 'Connection closed'
         return line
 
     def disconnect(self):
@@ -192,5 +198,7 @@
             return
         else:
             self._sock.close()
+        self._sock = None
+        self.connected = False
 
 

Modified: sandbox/trunk/pdb/test/test_mconnection.py
==============================================================================
--- sandbox/trunk/pdb/test/test_mconnection.py	(original)
+++ sandbox/trunk/pdb/test/test_mconnection.py	Fri Jul  7 21:47:25 2006
@@ -19,7 +19,7 @@
 
 sys.path.append("..")
 from mconnection import (MConnectionServerTCP, MConnectionClientTCP,
-                         MConnectionSerial, ConnectionFailed)
+                         MConnectionSerial, ConnectionFailed, ReadError)
 
 # Try to connect the client to addr either until we've tried MAXTRIES
 # times or until it succeeds.
@@ -27,8 +27,7 @@
     for i in range(MAXTRIES):
         try:
             client.connect(addr)
-            # The _sock variable appears when there's a connection
-            if client._sock: break
+            if client.connected: break
         except ConnectionFailed:
                 pass
 
@@ -96,7 +95,26 @@
         """(tcp) Test invald hostname, port pair. """
         addr = 'localhost 8000'
         self.assertRaises(ConnectionFailed, self.server.connect, addr)
-        
+
+    def testServerReadError(self):
+        """(tcp) Test the ReadError exception."""
+        thread.start_new_thread(self.server.connect, (__addr__,))
+
+        while self.server._sock == None:
+            pass
+
+        repeatedConnect(self.client, __addr__)
+        self.client.disconnect()
+        self.assertRaises(ReadError, self.server.readline)
+
+        self.server.disconnect()
+
+        thread.start_new_thread(self.client.connect, (__addr__,))
+        self.server.connect(__addr__)
+
+        self.server.disconnect()
+        self.assertRaises(ReadError, self.client.readline)
+
     def tearDown(self):
         self.server.disconnect()
         self.client.disconnect()


More information about the Python-checkins mailing list