Browse Source

[2931] Allow unsubscribing from notifications

Includes minor test fixes.
Michal 'vorner' Vaner 12 years ago
parent
commit
18b3c00bb0

+ 19 - 0
src/lib/python/isc/config/ccsession.py

@@ -624,6 +624,8 @@ class ModuleCCSession(ConfigData):
 
 
           The callback should not raise exceptions, such exceptions are
           The callback should not raise exceptions, such exceptions are
           likely to propagate through the loop and terminate program.
           likely to propagate through the loop and terminate program.
+        Returns: Opaque id of the subscription. It can be used to cancel
+          the subscription by unsubscribe_notification.
         """
         """
         self._last_notif_id += 1
         self._last_notif_id += 1
         my_id = self._last_notif_id
         my_id = self._last_notif_id
@@ -636,6 +638,23 @@ class ModuleCCSession(ConfigData):
                 { my_id: callback }
                 { my_id: callback }
         return (notification_group, my_id)
         return (notification_group, my_id)
 
 
+    def unsubscribe_notification(self, nid):
+        """
+        Remove previous subscription for notifications. Pass the id returned
+        from subscribe_notification.
+
+        Throws:
+        - CCSessionError: for low-level communication errors.
+        - KeyError: The id does not correspond to valid subscription.
+        """
+        (group, cid) = nid
+        del self._notification_callbacks[group][cid]
+        if not self._notification_callbacks[group]:
+            # Removed the last one
+            self._session.group_unsubscribe(CC_GROUP_NOTIFICATION_PREFIX +
+                                            group)
+            del self._notification_callbacks[group]
+
 class UIModuleCCSession(MultiConfigData):
 class UIModuleCCSession(MultiConfigData):
     """This class is used in a configuration user interface. It contains
     """This class is used in a configuration user interface. It contains
        specific functions for getting, displaying, and sending
        specific functions for getting, displaying, and sending

+ 3 - 3
src/lib/python/isc/config/tests/ccsession_test.py

@@ -431,14 +431,14 @@ class TestModuleCCSession(unittest.TestCase):
         mccs.check_command()
         mccs.check_command()
         self.assertEqual(fake_session.message_queue, [])
         self.assertEqual(fake_session.message_queue, [])
         self.assertEqual(notifications, [
         self.assertEqual(notifications, [
-            ("second", "event", {'param': True})
+            ("first", "event", {'param': True})
         ])
         ])
         del notifications[:]
         del notifications[:]
         # If we try to unsubscribe again, it complains.
         # If we try to unsubscribe again, it complains.
-        self.assertRaises(KeyError, self.unsubscribe_notification, id1)
+        self.assertRaises(KeyError, mccs.unsubscribe_notification, id2)
         # Unsubscribe the other one too. From now on, it doesn't eat the
         # Unsubscribe the other one too. From now on, it doesn't eat the
         # messages again.
         # messages again.
-        mccs.unsubscribe_notification(id2)
+        mccs.unsubscribe_notification(id1)
         fake_session.group_sendmsg({"notification": ["event", {
         fake_session.group_sendmsg({"notification": ["event", {
             "param": True
             "param": True
         }]}, "notifications/group")
         }]}, "notifications/group")