Parcourir la source

[2931] Allow unsubscribing from notifications

Includes minor test fixes.
Michal 'vorner' Vaner il y a 12 ans
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
           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
         my_id = self._last_notif_id
@@ -636,6 +638,23 @@ class ModuleCCSession(ConfigData):
                 { my_id: callback }
         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):
     """This class is used in a configuration user interface. It contains
        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()
         self.assertEqual(fake_session.message_queue, [])
         self.assertEqual(notifications, [
-            ("second", "event", {'param': True})
+            ("first", "event", {'param': True})
         ])
         del notifications[:]
         # 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
         # messages again.
-        mccs.unsubscribe_notification(id2)
+        mccs.unsubscribe_notification(id1)
         fake_session.group_sendmsg({"notification": ["event", {
             "param": True
         }]}, "notifications/group")