From: Michael Wallner Date: Wed, 7 Oct 2020 11:17:39 +0000 (+0200) Subject: testing: touch X-Git-Tag: 1.1.0-beta1~236^2~15 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=a6874b396cec9cacc3e424585d904bc36c7439be;p=awesomized%2Flibmemcached testing: touch --- diff --git a/test/lib/common.hpp b/test/lib/common.hpp index 517b4263..b5ef3525 100644 --- a/test/lib/common.hpp +++ b/test/lib/common.hpp @@ -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 index 00000000..35da7dc9 --- /dev/null +++ b/test/tests/bin/memtouch.cpp @@ -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(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 index 00000000..982236b5 --- /dev/null +++ b/test/tests/memcached/touch.cpp @@ -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)); + } +}