testing: touch
authorMichael Wallner <mike@php.net>
Wed, 7 Oct 2020 11:17:39 +0000 (13:17 +0200)
committerMichael Wallner <mike@php.net>
Wed, 7 Oct 2020 11:17:39 +0000 (13:17 +0200)
test/lib/common.hpp
test/tests/bin/memtouch.cpp [new file with mode: 0644]
test/tests/memcached/touch.cpp [new file with mode: 0644]

index 517b426342e7ea596af1f6098333df51557ed1e7..b5ef3525d2f35503d38a5ca54f38f3c4b8bf3246 100644 (file)
@@ -113,6 +113,12 @@ public:
   Malloced(T *ptr_)
   : ptr{ptr_}
   {}
+  Malloced &operator=(T *ptr_) {
+    if (ptr)
+      free(ptr);
+    ptr = ptr_;
+    return *this;
+  }
   ~Malloced() {
     if(ptr)
       free(ptr);
diff --git a/test/tests/bin/memtouch.cpp b/test/tests/bin/memtouch.cpp
new file mode 100644 (file)
index 0000000..35da7dc
--- /dev/null
@@ -0,0 +1,64 @@
+#include "test/lib/common.hpp"
+#include "test/lib/Shell.hpp"
+#include "test/lib/Server.hpp"
+#include "test/lib/Retry.hpp"
+#include "test/lib/ReturnMatcher.hpp"
+
+using Catch::Matchers::Contains;
+
+TEST_CASE("bin/memtouch") {
+  Shell sh{string{TESTING_ROOT "/../src/bin"}};
+
+  SECTION("no servers provided") {
+    string output;
+    REQUIRE_FALSE(sh.run("memtouch", output));
+    REQUIRE(output == "No Servers provided\n");
+  }
+
+  SECTION("--help") {
+    string output;
+    REQUIRE(sh.run("memtouch --help", output));
+    REQUIRE_THAT(output, Contains("memtouch"));
+    REQUIRE_THAT(output, Contains("v1"));
+    REQUIRE_THAT(output, Contains("help"));
+    REQUIRE_THAT(output, Contains("version"));
+    REQUIRE_THAT(output, Contains("option"));
+    REQUIRE_THAT(output, Contains("--"));
+    REQUIRE_THAT(output, Contains("="));
+  }
+
+  SECTION("with server") {
+    Server server{MEMCACHED_BINARY, {"-p", random_port_string}};
+    MemcachedPtr memc;
+    LoneReturnMatcher test{*memc};
+
+    REQUIRE(server.ensureListening());
+    auto port = get<int>(server.getSocketOrPort());
+    auto comm = "memtouch --servers=localhost:" + to_string(port) + " ";
+
+    REQUIRE_SUCCESS(memcached_server_add(*memc, "localhost", port));
+
+    SECTION("found") {
+      REQUIRE_SUCCESS(memcached_set(*memc, S("memtouch"), S("memtouch-SET"), 0, 0));
+
+      string output;
+      REQUIRE(sh.run(comm + "memtouch", output));
+      REQUIRE(output.empty());
+    }
+
+    SECTION("not found") {
+      memcached_delete(*memc, S("memtouch"), 0);
+
+      string output;
+      REQUIRE_FALSE(sh.run(comm + "memtouch", output));
+      REQUIRE(output.empty());
+    }
+
+    SECTION("expires") {
+      REQUIRE_SUCCESS(memcached_set(*memc, S("memtouch"), S("memtouch"), 60, 0));
+      REQUIRE_SUCCESS(memcached_exist(*memc, S("memtouch")));
+      REQUIRE(sh.run(comm + "--expire=" + to_string(time(nullptr) - 2) + " memtouch"));
+      REQUIRE_RC(MEMCACHED_NOTFOUND, memcached_exist(*memc, S("memtouch")));
+    }
+  }
+}
diff --git a/test/tests/memcached/touch.cpp b/test/tests/memcached/touch.cpp
new file mode 100644 (file)
index 0000000..982236b
--- /dev/null
@@ -0,0 +1,48 @@
+#include "test/lib/common.hpp"
+#include "test/lib/MemcachedCluster.hpp"
+
+TEST_CASE("memcached_touch") {
+  auto test = MemcachedCluster::mixed();
+  auto memc = &test.memc;
+  memcached_return_t rc;
+  auto binary = GENERATE(0, 1);
+
+  test.enableBinaryProto(binary);
+
+  DYNAMIC_SECTION("touch binary=" << binary) {
+    REQUIRE_FALSE(memcached_get(memc, S(__func__), nullptr, nullptr, &rc));
+    REQUIRE_RC(MEMCACHED_NOTFOUND, rc);
+
+    REQUIRE_SUCCESS(memcached_set(memc, S(__func__), S(__func__), 2, 0));
+
+    Malloced val(memcached_get(memc, S(__func__), nullptr, nullptr, &rc));
+    REQUIRE_SUCCESS(rc);
+    REQUIRE(*val);
+
+    REQUIRE_SUCCESS(memcached_touch(memc, S(__func__), 60));
+    val = memcached_get(memc, S(__func__), nullptr, nullptr, &rc);
+    REQUIRE_SUCCESS(rc);
+    REQUIRE(*val);
+
+    REQUIRE_SUCCESS(memcached_touch(memc, S(__func__), time(nullptr) - 2));
+    val = memcached_get(memc, S(__func__), nullptr, nullptr, &rc);
+    REQUIRE_RC(MEMCACHED_NOTFOUND, rc);
+    REQUIRE_FALSE(*val);
+  }
+
+  DYNAMIC_SECTION("touch_by_key binary=" << binary) {
+    REQUIRE_RC(MEMCACHED_NOTFOUND, memcached_touch_by_key(memc, S(__func__), S(__func__), 60));
+    REQUIRE_SUCCESS(memcached_set_by_key(memc, S(__func__), S(__func__), S(__func__), 2, 0));
+
+    Malloced val(memcached_get_by_key(memc, S(__func__), S(__func__), nullptr, nullptr, &rc));
+    REQUIRE_SUCCESS(rc);
+    REQUIRE(*val);
+
+    REQUIRE_SUCCESS(memcached_touch_by_key(memc, S(__func__), S(__func__), time(nullptr) - 2));
+    val = memcached_get_by_key(memc, S(__func__), S(__func__), nullptr, nullptr, &rc);
+    REQUIRE_RC(MEMCACHED_NOTFOUND, rc);
+    REQUIRE_FALSE(*val);
+
+    REQUIRE_RC(MEMCACHED_NOTFOUND, memcached_touch_by_key(memc, S(__func__), S(__func__), 60));
+  }
+}