Commits

Anonymous committed c39f943

Added better unit test for disconnect.

  • Participants
  • Parent commits c7c1fe9

Comments (0)

Files changed (4)

stompclient/connection.py

         self.socket_timeout = socket_timeout
         self._sock = None
         self._buffer = FrameBuffer()
-        self._disconnected = threading.Event()
+        self._connected = threading.Event()
 
+    @property
+    def connected(self):
+        """
+        Whether this connection thinks it is currently connected.
+        """
+        return self._connected.is_set()
+    
     def connect(self):
         """
         Connects to the STOMP server if not already connected.
         sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
         sock.settimeout(self.socket_timeout)
         self._sock = sock
-        self._disconnected.clear()
+        self._connected.set()
         
     def disconnect(self, conf=None):
         """
             pass
         self._sock = None
         self._buffer.clear()
-        self._disconnected.set()
+        self._connected.clear()
     
     def send(self, frame):
         """
             # Read bytes from socket until we have read a frame (or timeout out) and then return it.
             received_frame = None
             try:
-                while not self._disconnected.is_set():
+                while self._connected.is_set():
                     bytes = self._sock.recv(8192)
                     self._buffer.append(bytes)
                     received_frame = self._buffer.extract_frame()

stompclient/duplex.py

         Sends DISCONNECT frame and disconnect from the server.
         """
         try:
-            with self.subscription_lock:
-                # Need a copy since unsubscribe() removes the destination from the collection.
-                subcpy = copy(self.subscribed_destinations)
-                for destination in subcpy:
-                    self.unsubscribe(destination)
-            disconnect = frame.DisconnectFrame(extra_headers=extra_headers)
-            result = self.send_frame(disconnect)
-            self.connection.disconnect()
-            return result
-        except NotConnectedError:
-            pass
+            if not self.connection.connected:
+                with self.subscription_lock:
+                    # Need a copy since unsubscribe() removes the destination from the collection.
+                    subcpy = copy(self.subscribed_destinations)
+                    for destination in subcpy:
+                        self.unsubscribe(destination)
+                disconnect = frame.DisconnectFrame(extra_headers=extra_headers)
+                result = self.send_frame(disconnect)
+                self.connection.disconnect()
+                return result
         finally:
             self.shutdown_event.set()
             

stompclient/simplex.py

 
     def disconnect(self, conf=None, extra_headers=None):
         """Disconnect from the server."""
-        try:
+        print repr(self.connection.connected)
+        if self.connection.connected:
             disconnect = frame.DisconnectFrame(extra_headers=extra_headers)
             result = self.send_frame(disconnect)
             self.connection.disconnect()
             return result
-        except NotConnectedError:
-            pass
         
     def send(self, destination, body=None, transaction=None, extra_headers=None):
         """

stompclient/tests/test_simplex.py

 Tests for the simple (publish-only) client.
 """
 from unittest import TestCase
-from mockutil import MockingConnectionPool
+
+from mock import sentinel
 
 from stompclient.simplex import PublishClient
 from stompclient import frame
 
+from stompclient.tests.mockutil import MockingConnectionPool
+
 __authors__ = ['"Hans Lellelid" <hans@xmpl.org>']
 __copyright__ = "Copyright 2010 Hans Lellelid"
 __license__ = """Licensed under the Apache License, Version 2.0 (the "License");
         self.mockpool = MockingConnectionPool()
         self.mockconn = self.mockpool.connection
         self.client = PublishClient('127.0.0.1', 1234, connection_pool=self.mockpool)
+        self.mockconn.connected = False
         
     def test_connect(self):
         """ Test connect. """
         expected = frame.Frame(command='CONNECT', headers={'login': 'foo', 'passcode': 'bar'})
         
         self.assertEquals(expected, sentframe)
-        
+    
     def test_disconnect(self):
         """ Test disconnect. """
+        self.client.connect()
+        
+        #self.mockconn.connected.side_effect = lambda: True
+        print self.mockconn.send.call_args
+        self.mockconn.connected = True
+        
         self.client.disconnect()
         
+        print self.mockconn.send.call_args
+        
         (sentframe,) = self.mockconn.send.call_args[0]
         expected = frame.Frame('DISCONNECT')
         self.assertEquals(expected, sentframe)
+        self.assertTrue(self.mockconn.disconnect.called)
         
-        self.assertTrue(self.mockconn.disconnect.called)
+    def test_disconnect_notconnected(self):
+        """ Test disconnect when already disconnected """
+        self.client.disconnect()
+        self.assertFalse(self.mockconn.send.called)
+        self.assertFalse(self.mockconn.disconnect.called)
         
     def test_send(self):
         """ Test send. """