Browse Source

[2850] Make isWritable() not throw

Mukund Sivaraman 12 years ago
parent
commit
a1d8193790

+ 3 - 0
src/lib/datasrc/memory/zone_table_segment.h

@@ -112,6 +112,9 @@ public:
     /// derived class documentation).  In general, however, the user
     /// should only rely on this interface rather than assume a specific
     /// definition for a specific type of segment.
+    ///
+    /// \throw None This method's implementations must be
+    /// exception-free.
     virtual bool isWritable() const = 0;
 
     /// \brief Create an instance depending on the memory segment model

+ 5 - 2
src/lib/datasrc/memory/zone_table_segment_mapped.cc

@@ -222,9 +222,12 @@ ZoneTableSegmentMapped::getMemorySegment() {
 bool
 ZoneTableSegmentMapped::isWritable() const {
     if (!mem_sgmt_) {
-        isc_throw(isc::Unexpected,
-                  "isWritable() called without calling reset() first");
+        // If reset() was never performed for this segment, or if the
+        // most recent reset() had failed, then the segment is not
+        // writable.
+        return (false);
     }
+
     return ((current_mode_ == CREATE) || (current_mode_ == READ_WRITE));
 }
 

+ 2 - 3
src/lib/datasrc/memory/zone_table_segment_mapped.h

@@ -67,9 +67,8 @@ public:
     ///
     /// Segments successfully opened in CREATE or READ_WRITE modes are
     /// writable. Segments opened in READ_ONLY mode are not writable.
-    ///
-    /// \throws isc::Unexpected if this method is called without a
-    /// successful \c reset() call first.
+    /// If there was a failure in \c reset(), the segment is not
+    /// writable.
     virtual bool isWritable() const;
 
     /// \brief The mode using which to open a ZoneTableSegment around a

+ 14 - 6
src/lib/datasrc/tests/memory/zone_table_segment_mapped_unittest.cc

@@ -66,8 +66,9 @@ TEST_F(ZoneTableSegmentMappedTest, getMemorySegmentUninitialized) {
 }
 
 TEST_F(ZoneTableSegmentMappedTest, isWritableUninitialized) {
-    // This should throw as we haven't called reset() yet.
-    EXPECT_THROW(ztable_segment_->isWritable(), isc::Unexpected);
+    // isWritable() must return false by default, when the segment has
+    // not been reset() yet.
+    EXPECT_FALSE(ztable_segment_->isWritable());
 }
 
 ZoneData*
@@ -104,7 +105,10 @@ TEST_F(ZoneTableSegmentMappedTest, resetBadConfig) {
     // The following should still throw, unaffected by the failed opens.
     EXPECT_THROW(ztable_segment_->getHeader(), isc::Unexpected);
     EXPECT_THROW(ztable_segment_->getMemorySegment(), isc::Unexpected);
-    EXPECT_THROW(ztable_segment_->isWritable(), isc::Unexpected);
+
+    // isWritable() must still return false, because the segment has not
+    // been successfully reset() yet.
+    EXPECT_FALSE(ztable_segment_->isWritable());
 }
 
 TEST_F(ZoneTableSegmentMappedTest, reset) {
@@ -118,7 +122,10 @@ TEST_F(ZoneTableSegmentMappedTest, reset) {
     // The following should still throw, unaffected by the failed open.
     EXPECT_THROW(ztable_segment_->getHeader(), isc::Unexpected);
     EXPECT_THROW(ztable_segment_->getMemorySegment(), isc::Unexpected);
-    EXPECT_THROW(ztable_segment_->isWritable(), isc::Unexpected);
+
+    // isWritable() must still return false, because the segment has not
+    // been successfully reset() yet.
+    EXPECT_FALSE(ztable_segment_->isWritable());
 
     // READ_WRITE mode must create the mapped file if it doesn't exist
     // (and must not result in an exception).
@@ -130,7 +137,6 @@ TEST_F(ZoneTableSegmentMappedTest, reset) {
     // The following method calls should no longer throw:
     EXPECT_NO_THROW(ztable_segment_->getHeader());
     EXPECT_NO_THROW(ztable_segment_->getMemorySegment());
-    EXPECT_NO_THROW(ztable_segment_->isWritable());
 
     // Let's try to re-open the mapped file in READ_ONLY mode. It should
     // not fail now.
@@ -154,7 +160,9 @@ TEST_F(ZoneTableSegmentMappedTest, reset) {
     // The following should throw now.
     EXPECT_THROW(ztable_segment_->getHeader(), isc::Unexpected);
     EXPECT_THROW(ztable_segment_->getMemorySegment(), isc::Unexpected);
-    EXPECT_THROW(ztable_segment_->isWritable(), isc::Unexpected);
+
+    // isWritable() must return false, because the last reset() failed.
+    EXPECT_FALSE(ztable_segment_->isWritable());
 
     // READ_WRITE with an existing map file ought to work too. This
     // would use existing named addresses.