]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[2108] Use the test memory segment in InMemoryClient unittests
authorMukund Sivaraman <muks@isc.org>
Thu, 6 Sep 2012 00:49:20 +0000 (06:19 +0530)
committerMukund Sivaraman <muks@isc.org>
Thu, 6 Sep 2012 00:49:26 +0000 (06:19 +0530)
This has the setThrowCount() which we can use to check leaks during
allocation problems.

src/lib/datasrc/memory/tests/memory_client_unittest.cc

index 88e47b63ed7d6e5e1783a1abcac04e9e11f6b3f7..048d7becedf7eaefd9f0c009dcc8ca9a1a9166fe 100644 (file)
@@ -41,6 +41,27 @@ using namespace isc::datasrc;
 using namespace isc::datasrc::memory;
 
 namespace {
+// Memory segment specified for tests.  It normally behaves like a "local"
+// memory segment.  If "throw count" is set to non 0 via setThrowCount(),
+// it continues the normal behavior up to the specified number of calls to
+// allocate(), and throws an exception at the next call.
+class TestMemorySegment : public isc::util::MemorySegmentLocal {
+public:
+    TestMemorySegment() : throw_count_(0) {}
+    virtual void* allocate(size_t size) {
+        if (throw_count_ > 0) {
+            if (--throw_count_ == 0) {
+                throw std::bad_alloc();
+            }
+        }
+        return (isc::util::MemorySegmentLocal::allocate(size));
+    }
+    void setThrowCount(size_t count) { throw_count_ = count; }
+
+private:
+    size_t throw_count_;
+};
+
 class MemoryClientTest : public ::testing::Test {
 protected:
     MemoryClientTest() : zclass_(RRClass::IN()),
@@ -61,7 +82,7 @@ protected:
     }
     const RRClass zclass_;
     const Name zname1, zname2, zname3;
-    isc::util::MemorySegmentLocal mem_sgmt_;
+    TestMemorySegment mem_sgmt_;
     InMemoryClient* client_;
     memory::ZoneTable* zone_table;
 };