bin: consolidate clients
[awesomized/libmemcached] / test / tests / memcached / stat.cpp
1 #include "test/lib/common.hpp"
2 #include "test/lib/MemcachedCluster.hpp"
3
4 static memcached_return_t item_counter(const memcached_instance_st *, const char *key, size_t, const char *value, size_t, void *context)
5 {
6 if (key == "curr_items"s) {
7 auto counter = static_cast<size_t *>(context);
8 *counter += stoul(value);
9 }
10
11 return MEMCACHED_SUCCESS;
12 }
13
14 static memcached_return_t stat_null(const memcached_instance_st *, const char *, size_t, const char *, size_t, void *) {
15 return MEMCACHED_SUCCESS;
16 }
17
18 TEST_CASE("memcached_stat") {
19 MemcachedCluster test;
20 auto memc = &test.memc;
21
22 SECTION("invalid arguments") {
23 size_t count = 0;
24 REQUIRE_RC(MEMCACHED_INVALID_ARGUMENTS, memcached_stat_execute(memc, "BAD_ARG", item_counter, &count));
25 }
26
27 SECTION("execute") {
28 for (auto i = 0; i < 64; ++i) {
29 auto key = random_ascii_string(12) + to_string(i);
30 REQUIRE_SUCCESS(memcached_set(memc, key.c_str(), key.length(), nullptr, 0, 0, 0));
31 }
32
33 memcached_quit(memc);
34
35 size_t count = 0;
36 REQUIRE_SUCCESS(memcached_stat_execute(memc, nullptr, item_counter, &count));
37 REQUIRE(count == 64);
38
39 auto arg = GENERATE(as<string>(), "slabs", "items", "sizes");
40 REQUIRE_SUCCESS(memcached_stat_execute(memc, arg.c_str(), stat_null, nullptr));
41 }
42
43 SECTION("servername") {
44 for (auto &server : test.cluster.getServers()) {
45 auto port = server.getSocketOrPort();
46 if (holds_alternative<int>(port)) {
47 memcached_stat_st stats;
48 REQUIRE_SUCCESS(memcached_stat_servername(&stats, nullptr, "localhost", get<int>(port)));
49 REQUIRE(stats.pid);
50 }
51 }
52 }
53
54 SECTION("get_keys/get_value") { // oh my
55 memcached_return_t rc;
56 auto servers = test.cluster.getServers();
57 Malloced stats(memcached_stat(memc, nullptr, &rc));
58 Malloced keys(memcached_stat_get_keys(memc, nullptr, nullptr));
59
60 REQUIRE_SUCCESS(rc);
61 REQUIRE(*stats);
62 REQUIRE(*keys);
63
64 for (auto i = 0U; i < memcached_server_count(memc); ++i) {
65 auto this_stat = &(*stats)[i];
66
67 for (auto key = *keys; *key; ++key) {
68 Malloced val(memcached_stat_get_value(memc, this_stat, *key, &rc));
69 REQUIRE_SUCCESS(rc);
70 REQUIRE(*val);
71 }
72 for (auto &server : test.cluster.getServers()) {
73 if (this_stat->pid == server.getPid()) {
74 goto found;
75 }
76 }
77
78 FAIL("no server matches pid " << this_stat->pid);
79 found:;
80 }
81 }
82 }