Merge branch 'gh-actions' into catch
[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 TEST_CASE("memcached_stat") {
15 MemcachedCluster test;
16 auto memc = &test.memc;
17
18 SECTION("invalid arguments") {
19 size_t count = 0;
20 REQUIRE_RC(MEMCACHED_INVALID_ARGUMENTS, memcached_stat_execute(memc, "BAD_ARG", item_counter, &count));
21 }
22
23 SECTION("execute") {
24 for (auto i = 0; i < 64; ++i) {
25 auto key = random_ascii_string(12) + to_string(i);
26 REQUIRE_SUCCESS(memcached_set(memc, key.c_str(), key.length(), nullptr, 0, 0, 0));
27 }
28
29 memcached_quit(memc);
30
31 size_t count = 0;
32 REQUIRE_SUCCESS(memcached_stat_execute(memc, nullptr, item_counter, &count));
33 REQUIRE(count == 64);
34 }
35
36 SECTION("servername") {
37 for (auto &server : test.cluster.getServers()) {
38 auto port = server.getSocketOrPort();
39 if (holds_alternative<int>(port)) {
40 memcached_stat_st stats;
41 REQUIRE_SUCCESS(memcached_stat_servername(&stats, nullptr, "localhost", get<int>(port)));
42 REQUIRE(stats.pid);
43 }
44 }
45 }
46
47 SECTION("get_keys/get_value") { // oh my
48 memcached_return_t rc;
49 auto servers = test.cluster.getServers();
50 Malloced stats(memcached_stat(memc, nullptr, &rc));
51 Malloced keys(memcached_stat_get_keys(memc, nullptr, nullptr));
52
53 REQUIRE_SUCCESS(rc);
54 REQUIRE(*stats);
55 REQUIRE(*keys);
56
57 for (auto i = 0U; i < memcached_server_count(memc); ++i) {
58 auto this_stat = &(*stats)[i];
59
60 for (auto key = *keys; *key; ++key) {
61 Malloced val(memcached_stat_get_value(memc, this_stat, *key, &rc));
62 REQUIRE_SUCCESS(rc);
63 REQUIRE(*val);
64 }
65 for (auto &server : test.cluster.getServers()) {
66 if (this_stat->pid == server.getPid()) {
67 goto found;
68 }
69 }
70
71 FAIL("no server matches pid " << this_stat->pid);
72 found:;
73 }
74 }
75 }