Browse Source

merging trac ticket #242 (remote_config functions aren't fully cleaned up in CCSession)

git-svn-id: svn://bind10.isc.org/svn/bind10/trunk@2369 e5f2f494-b856-4b98-b285-d166d9295462
Jelte Jansen 15 years ago
parent
commit
ab3d01d800

+ 8 - 2
src/lib/python/isc/config/ccsession.py

@@ -150,6 +150,11 @@ class ModuleCCSession(ConfigData):
 
 
         self._remote_module_configs = {}
         self._remote_module_configs = {}
 
 
+    def __del__(self):
+        self._session.group_unsubscribe(self._module_name, "*")
+        for module_name in self._remote_module_configs:
+            self._session.group_unsubscribe(module_name)
+
     def start(self):
     def start(self):
         """Send the specification for this module to the configuration
         """Send the specification for this module to the configuration
            manager, and request the current non-default configuration.
            manager, and request the current non-default configuration.
@@ -244,7 +249,7 @@ class ModuleCCSession(ConfigData):
         self._session.group_subscribe(module_name);
         self._session.group_subscribe(module_name);
 
 
         # Get the current config for that module now
         # Get the current config for that module now
-        seq = self._session.group_sendmsg({ "command": [ "get_config", { "module_name": module_name } ] }, "ConfigManager")
+        seq = self._session.group_sendmsg(create_command(COMMAND_GET_CONFIG, { "module_name": module_name }), "ConfigManager")
         answer, env = self._session.group_recvmsg(False, seq)
         answer, env = self._session.group_recvmsg(False, seq)
         if answer:
         if answer:
             rcode, value = parse_answer(answer)
             rcode, value = parse_answer(answer)
@@ -259,6 +264,7 @@ class ModuleCCSession(ConfigData):
     def remove_remote_config(self, module_name):
     def remove_remote_config(self, module_name):
         """Removes the remote configuration access for this module"""
         """Removes the remote configuration access for this module"""
         if module_name in self._remote_module_configs:
         if module_name in self._remote_module_configs:
+            self._session.group_unsubscribe(module_name)
             del self._remote_module_configs[module_name]
             del self._remote_module_configs[module_name]
 
 
     def get_remote_config_value(self, module_name, identifier):
     def get_remote_config_value(self, module_name, identifier):
@@ -280,7 +286,7 @@ class ModuleCCSession(ConfigData):
     def __request_config(self):
     def __request_config(self):
         """Asks the configuration manager for the current configuration, and call the config handler if set.
         """Asks the configuration manager for the current configuration, and call the config handler if set.
            Raises a ModuleCCSessionError if there is no answer from the configuration manager"""
            Raises a ModuleCCSessionError if there is no answer from the configuration manager"""
-        seq = self._session.group_sendmsg({ "command": [ "get_config", { "module_name": self._module_name } ] }, "ConfigManager")
+        seq = self._session.group_sendmsg(create_command(COMMAND_GET_CONFIG, { "module_name": self._module_name }), "ConfigManager")
         answer, env = self._session.group_recvmsg(False, seq)
         answer, env = self._session.group_recvmsg(False, seq)
         if answer:
         if answer:
             rcode, value = parse_answer(answer)
             rcode, value = parse_answer(answer)

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

@@ -119,7 +119,10 @@ class TestModuleCCSession(unittest.TestCase):
 
 
     def test_start1(self):
     def test_start1(self):
         fake_session = FakeModuleCCSession()
         fake_session = FakeModuleCCSession()
+        self.assertFalse("Spec1" in fake_session.subscriptions)
         mccs = self.create_session("spec1.spec", None, None, fake_session)
         mccs = self.create_session("spec1.spec", None, None, fake_session)
+        self.assertTrue("Spec1" in fake_session.subscriptions)
+
         self.assertEqual(len(fake_session.message_queue), 0)
         self.assertEqual(len(fake_session.message_queue), 0)
         self.assertRaises(ModuleCCSessionError, mccs.start)
         self.assertRaises(ModuleCCSessionError, mccs.start)
         self.assertEqual(len(fake_session.message_queue), 2)
         self.assertEqual(len(fake_session.message_queue), 2)
@@ -139,6 +142,9 @@ class TestModuleCCSession(unittest.TestCase):
         self.assertEqual({'command': ['get_config', {'module_name': 'Spec1'}]},
         self.assertEqual({'command': ['get_config', {'module_name': 'Spec1'}]},
                          fake_session.get_message('ConfigManager', None))
                          fake_session.get_message('ConfigManager', None))
 
 
+        mccs = None
+        self.assertFalse("Spec1" in fake_session.subscriptions)
+
     def test_start2(self):
     def test_start2(self):
         fake_session = FakeModuleCCSession()
         fake_session = FakeModuleCCSession()
         mccs = self.create_session("spec2.spec", None, None, fake_session)
         mccs = self.create_session("spec2.spec", None, None, fake_session)
@@ -352,7 +358,9 @@ class TestModuleCCSession(unittest.TestCase):
 
 
         self.assertRaises(ModuleCCSessionError, mccs.get_remote_config_value, "Spec2", "item1")
         self.assertRaises(ModuleCCSessionError, mccs.get_remote_config_value, "Spec2", "item1")
 
 
+        self.assertFalse("Spec2" in fake_session.subscriptions)
         rmodname = mccs.add_remote_config(self.spec_file("spec2.spec"))
         rmodname = mccs.add_remote_config(self.spec_file("spec2.spec"))
+        self.assertTrue("Spec2" in fake_session.subscriptions)
         self.assertEqual("Spec2", rmodname)
         self.assertEqual("Spec2", rmodname)
         self.assertRaises(isc.cc.data.DataNotFoundError, mccs.get_remote_config_value, rmodname, "asdf")
         self.assertRaises(isc.cc.data.DataNotFoundError, mccs.get_remote_config_value, rmodname, "asdf")
         value, default = mccs.get_remote_config_value(rmodname, "item1")
         value, default = mccs.get_remote_config_value(rmodname, "item1")
@@ -360,7 +368,15 @@ class TestModuleCCSession(unittest.TestCase):
         self.assertEqual(True, default)
         self.assertEqual(True, default)
 
 
         mccs.remove_remote_config(rmodname)
         mccs.remove_remote_config(rmodname)
+        self.assertFalse("Spec2" in fake_session.subscriptions)
         self.assertRaises(ModuleCCSessionError, mccs.get_remote_config_value, "Spec2", "item1")
         self.assertRaises(ModuleCCSessionError, mccs.get_remote_config_value, "Spec2", "item1")
+
+        # test if unsubscription is alse sent when object is deleted
+        rmodname = mccs.add_remote_config(self.spec_file("spec2.spec"))
+        self.assertTrue("Spec2" in fake_session.subscriptions)
+        mccs = None
+        self.assertFalse("Spec2" in fake_session.subscriptions)
+        
     
     
 class fakeUIConn():
 class fakeUIConn():
     def __init__(self):
     def __init__(self):

+ 10 - 0
src/lib/python/isc/config/tests/unittest_fakesession.py

@@ -16,6 +16,16 @@ class FakeModuleCCSession:
         if instance_name:
         if instance_name:
             self.subscriptions[group_name].append(instance_name)
             self.subscriptions[group_name].append(instance_name)
             
             
+    def group_unsubscribe(self, group_name, instance_name = None):
+        if group_name in self.subscriptions:
+            if instance_name:
+                if len(self.subscriptions[group_name]) > 1:
+                    del self.subscriptions[group_name][instance_name]
+                else:
+                    del self.subscriptions[group_name]
+            else:
+                del self.subscriptions[group_name]
+            
 
 
     def has_subscription(self, group_name, instance_name = None):
     def has_subscription(self, group_name, instance_name = None):
         if group_name in self.subscriptions:
         if group_name in self.subscriptions: