testing: pools
[m6w6/libmemcached] / test / tests / memcached / util.cpp
index 9612b1d08b5d61f10d24f09cf5424f1d0e0e3062..3fb018a3a635708e624daed8fd98b0c55ecdc612 100644 (file)
@@ -73,4 +73,94 @@ TEST_CASE("memcached_util") {
       REQUIRE_SUCCESS(rc);
     }
   }
+
+  SECTION("pool") {
+    SECTION("deprecated") {
+      auto conf = "--SERVER=host10.example.com --SERVER=host11.example.com --SERVER=host10.example.com --POOL-MIN=10 --POOL-MAX=32";
+      auto pool = memcached_pool(S(conf));
+      REQUIRE(pool);
+
+      memcached_return_t rc;
+      auto memc = memcached_pool_pop(pool, false, &rc);
+      REQUIRE(memc);
+      REQUIRE(MEMCACHED_SUCCESS == rc);
+
+      REQUIRE(MEMCACHED_SUCCESS == memcached_pool_push(pool, memc));
+      REQUIRE(nullptr == memcached_pool_destroy(pool));
+    }
+
+    SECTION("basic") {
+      auto test = MemcachedCluster::mixed();
+      auto memc = &test.memc;
+      memcached_return_t rc;
+
+      constexpr auto POOL_MIN = 5;
+      constexpr auto POOL_MAX = 10;
+
+      auto pool = memcached_pool_create(memc, POOL_MIN, POOL_MAX);
+      REQUIRE(pool);
+
+      array<memcached_st *, POOL_MAX> hold{};
+      for (auto &h : hold) {
+        h = memcached_pool_fetch(pool, nullptr, &rc);
+        REQUIRE_SUCCESS(rc);
+        REQUIRE(h);
+      }
+
+      SECTION("depleted") {
+        REQUIRE(nullptr == memcached_pool_fetch(pool, nullptr, &rc));
+        REQUIRE_RC(MEMCACHED_NOTFOUND, rc);
+      }
+
+      SECTION("usable") {
+        for (auto h : hold) {
+          auto s = to_string(reinterpret_cast<uintptr_t>(h));
+          REQUIRE_SUCCESS(memcached_set(h, s.c_str(), s.length(), s.c_str(), s.length(), 0, 0));
+        }
+        for (auto h : hold) {
+          auto s = to_string(reinterpret_cast<uintptr_t>(h));
+          Malloced val(memcached_get(h, s.c_str(), s.length(), nullptr, nullptr, &rc));
+          REQUIRE_SUCCESS(rc);
+          REQUIRE(*val);
+          REQUIRE(h == reinterpret_cast<memcached_st *>(stoul(*val)));
+        }
+
+        REQUIRE_SUCCESS(memcached_set(hold[0], S(__func__), "0", 1, 0, 0));
+        uint64_t inc = 0;
+        for (auto h : hold) {
+          uint64_t val;
+          REQUIRE_SUCCESS(memcached_increment(h, S(__func__), 1, &val));
+          CHECK(++inc == val);
+        }
+      }
+
+      SECTION("behaviors") {
+        uint64_t val;
+        REQUIRE_SUCCESS(memcached_pool_behavior_get(pool, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK, &val));
+        REQUIRE_FALSE(val == 9999);
+        REQUIRE_SUCCESS(memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK, 9999));
+        REQUIRE_SUCCESS(memcached_pool_behavior_get(pool, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK, &val));
+        REQUIRE(val == 9999);
+
+        for (auto &h : hold) {
+          REQUIRE_FALSE(9999 == memcached_behavior_get(h, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK));
+          REQUIRE_SUCCESS(memcached_pool_release(pool, h));
+          h = memcached_pool_fetch(pool, nullptr, &rc);
+          REQUIRE_SUCCESS(rc);
+          REQUIRE(h);
+          REQUIRE(9999 == memcached_behavior_get(h, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK));
+        }
+      }
+
+      for (auto h : hold) {
+        REQUIRE_SUCCESS(memcached_pool_release(pool, h));
+        auto again = memcached_pool_fetch(pool, nullptr, &rc);
+        REQUIRE_SUCCESS(rc);
+        REQUIRE(again);
+        REQUIRE_SUCCESS(memcached_pool_release(pool, again));
+      }
+
+      REQUIRE(memc == memcached_pool_destroy(pool));
+    }
+  }
 }