Browse Source

[1259] Check that stuff is not called after commit

Because it just makes no sense. So we raise an exception.
Michal 'vorner' Vaner 13 years ago
parent
commit
8031993390
2 changed files with 27 additions and 0 deletions
  1. 17 0
      src/lib/python/isc/xfrin/diff.py
  2. 10 0
      src/lib/python/isc/xfrin/tests/diff_tests.py

+ 17 - 0
src/lib/python/isc/xfrin/diff.py

@@ -58,6 +58,15 @@ class Diff:
                              str(datasource))
                              str(datasource))
         self.__buffer = []
         self.__buffer = []
 
 
+    def __check_commited(self):
+        """
+        This checks if the diff is already commited. If it is, it raises
+        ValueError. This check is for methods that need to work only on
+        yet uncommited diffs.
+        """
+        if self.__updater is None:
+            raise ValueError("The diff is already commited, you come late")
+
     def __data_common(self, rr, operation):
     def __data_common(self, rr, operation):
         """
         """
         Schedules an operation with rr.
         Schedules an operation with rr.
@@ -65,6 +74,7 @@ class Diff:
         It does all the real work of add_data and remove_data, including
         It does all the real work of add_data and remove_data, including
         all checks.
         all checks.
         """
         """
+        self.__check_commited()
         if rr.get_rdata_count() != 1:
         if rr.get_rdata_count() != 1:
             raise ValueError('The rrset must contain exactly 1 Rdata, but ' +
             raise ValueError('The rrset must contain exactly 1 Rdata, but ' +
                              'it holds ' + str(rr.get_rdata_count()))
                              'it holds ' + str(rr.get_rdata_count()))
@@ -117,6 +127,7 @@ class Diff:
         It also can raise isc.datasrc.Error. If that happens, you should stop
         It also can raise isc.datasrc.Error. If that happens, you should stop
         using this object and abort the modification.
         using this object and abort the modification.
         """
         """
+        self.__check_commited()
         # First, compact the data
         # First, compact the data
         self.compact()
         self.compact()
         # Then pass the data inside the data source
         # Then pass the data inside the data source
@@ -138,8 +149,14 @@ class Diff:
 
 
         This might raise isc.datasrc.Error.
         This might raise isc.datasrc.Error.
         """
         """
+        self.__check_commited()
+        # Push the data inside the data source
         self.apply()
         self.apply()
+        # Make sure they are visible.
         self.__updater.commit()
         self.__updater.commit()
+        # Remove the updater. That will free some resources for one, but
+        # mark this object as already commited, so we can check
+        self.__updater = None
 
 
     def get_buffer(self):
     def get_buffer(self):
         """
         """

+ 10 - 0
src/lib/python/isc/xfrin/tests/diff_tests.py

@@ -199,12 +199,22 @@ class DiffTest(unittest.TestCase):
         """
         """
         diff = Diff(self, Name('example.org.'))
         diff = Diff(self, Name('example.org.'))
         diff.add_data(self.__rrset1)
         diff.add_data(self.__rrset1)
+        orig_apply = diff.apply
         diff.apply = self.__mock_apply
         diff.apply = self.__mock_apply
         diff.commit()
         diff.commit()
         self.assertTrue(self.__apply_called)
         self.assertTrue(self.__apply_called)
         self.assertTrue(self.__commit_called)
         self.assertTrue(self.__commit_called)
         # The data should be handled by apply which we replaced.
         # The data should be handled by apply which we replaced.
         self.assertEqual([], self.__data_operations)
         self.assertEqual([], self.__data_operations)
+        # Now check all range of other methods raise ValueError
+        self.assertRaises(ValueError, diff.commit)
+        self.assertRaises(ValueError, diff.add_data, self.__rrset2)
+        self.assertRaises(ValueError, diff.remove_data, self.__rrset1)
+        diff.apply = orig_apply
+        self.assertRaises(ValueError, diff.apply)
+        # This one does not state it should raise, so check it doesn't
+        # But it is NOP in this situation anyway
+        diff.compact()
 
 
 if __name__ == "__main__":
 if __name__ == "__main__":
     isc.log.init("bind10")
     isc.log.init("bind10")