]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
Add unittests for safe sharing and make_shareable() 3123/head
authorMatt Benjamin <matt@cohortfs.com>
Wed, 10 Dec 2014 17:31:12 +0000 (12:31 -0500)
committerMatt Benjamin <matt@cohortfs.com>
Thu, 11 Dec 2014 00:13:08 +0000 (19:13 -0500)
Checks the following assertions:

1. a bufferlist shared with buffer::list::share yields shared
raw memory

2. a bufferlist copied with buffer::list::list(const list&) yields
shared raw memory

3. an unshareable bufferlist (bufferlist with unshareable segments)
copied with buffer::list::list(const list&) yields unshared raw
memory

4. an unshareable bufferlist claimed with CLAIM_ALLOW_NONSHAREABLE
yields shared raw memory

Signed-off-by: Matt Benjamin <matt@cohortfs.com>
src/test/bufferlist.cc

index 73c03c5bd531295227c3d9273cc9394926dfafb1..1e11e6f1d28d3ea4e4bb91f0f9e925813f78905d 100644 (file)
@@ -2261,6 +2261,48 @@ TEST(BufferList, TestDirectAppend) {
   ASSERT_EQ(memcmp(bl.c_str(), correct, curpos), 0);
 }
 
+TEST(BufferList, TestCloneNonShareable) {
+  bufferlist bl;
+  std::string str = "sharetest";
+  bl.append(str.c_str(), 9);
+  bufferlist bl_share;
+  bl_share.share(bl);
+  bufferlist bl_noshare;
+  buffer::ptr unraw = buffer::create_unshareable(10);
+  unraw.copy_in(0, 9, str.c_str());
+  bl_noshare.append(unraw);
+  bufferlist bl_copied_share = bl_share;
+  bufferlist bl_copied_noshare = bl_noshare;
+
+  // assert shared bufferlist has same buffers
+  bufferlist::iterator iter_bl = bl.begin();
+  bufferlist::iterator iter_bl_share = bl_share.begin();
+  // ok, this considers ptr::off, but it's still a true assertion (and below)
+  ASSERT_TRUE(iter_bl.get_current_ptr().c_str() ==
+             iter_bl_share.get_current_ptr().c_str());
+
+  // assert copy of shareable bufferlist has same buffers
+  iter_bl = bl.begin();
+  bufferlist::iterator iter_bl_copied_share = bl_copied_share.begin();
+  ASSERT_TRUE(iter_bl.get_current_ptr().c_str() ==
+             iter_bl_copied_share.get_current_ptr().c_str());
+
+  // assert copy of non-shareable bufferlist has different buffers
+  bufferlist::iterator iter_bl_copied_noshare = bl_copied_noshare.begin();
+  ASSERT_FALSE(iter_bl.get_current_ptr().c_str() ==
+              iter_bl_copied_noshare.get_current_ptr().c_str());
+
+  // assert that claim with CLAIM_ALLOW_NONSHAREABLE overrides safe-sharing
+  bufferlist bl_claim_noshare_override;
+  void* addr = bl_noshare.begin().get_current_ptr().c_str();
+  bl_claim_noshare_override.claim(bl_noshare,
+                                 buffer::list::CLAIM_ALLOW_NONSHAREABLE);
+  bufferlist::iterator iter_bl_noshare_override =
+    bl_claim_noshare_override.begin();
+  ASSERT_TRUE(addr /* the original first segment of bl_noshare() */ ==
+             iter_bl_noshare_override.get_current_ptr().c_str());
+}
+
 TEST(BufferList, TestCopyAll) {
   const static size_t BIG_SZ = 10737414;
   ceph::shared_ptr <unsigned char> big(