update TODO
[awesomized/libmemcached] / test / tests / memcached / util.cpp
1 #include "test/lib/common.hpp"
2 #include "test/lib/MemcachedCluster.hpp"
3 #include "test/fixtures/callbacks.hpp"
4
5 #include "libmemcached/instance.hpp"
6 #include "libmemcachedutil/common.h"
7
8 TEST_CASE("memcached_util") {
9 SECTION("version_check") {
10 auto test = MemcachedCluster::mixed();
11 auto memc = &test.memc;
12
13 REQUIRE_SUCCESS(memcached_version(memc));
14 REQUIRE(libmemcached_util_version_check(memc, 0, 0, 0));
15 REQUIRE_FALSE(libmemcached_util_version_check(memc, 255, 255, 255));
16
17 auto instance = memcached_server_instance_by_position(memc, 0);
18 REQUIRE(libmemcached_util_version_check(memc, instance->major_version, instance->minor_version, instance->micro_version));
19 if (instance->micro_version) {
20 REQUIRE(libmemcached_util_version_check(memc, instance->major_version, instance->minor_version, instance->micro_version - 1));
21 if (instance->micro_version < 255) {
22 REQUIRE_FALSE(libmemcached_util_version_check(memc, instance->major_version, instance->minor_version, instance->micro_version + 1));
23 }
24 }
25 if (instance->minor_version) {
26 REQUIRE(libmemcached_util_version_check(memc, instance->major_version, instance->minor_version - 1, 255));
27 if (instance->minor_version < 255) {
28 REQUIRE_FALSE(libmemcached_util_version_check(memc, instance->major_version, instance->minor_version + 1, 0));
29 }
30 }
31 if (instance->major_version) {
32 REQUIRE(libmemcached_util_version_check(memc, instance->major_version - 1, 255, 255));
33 if (instance->major_version < 255) {
34 REQUIRE_FALSE(libmemcached_util_version_check(memc, instance->major_version + 1, 0, 0));
35 }
36 }
37 }
38
39 // see sasl.cpp for getpid2 test
40 SECTION("getpid") {
41 auto test = MemcachedCluster::network();
42 memcached_return_t rc;
43
44 for (auto &server : test.cluster.getServers()) {
45 REQUIRE(server.getPid() == libmemcached_util_getpid("localhost", get<int>(server.getSocketOrPort()), &rc));
46 REQUIRE_SUCCESS(rc);
47 }
48
49 REQUIRE(-1 == libmemcached_util_getpid("localhost", 1, &rc));
50 REQUIRE(memcached_fatal(rc));
51 }
52
53 // see sasl.cpp for ping2 test
54 SECTION("ping") {
55 auto test = MemcachedCluster::network();
56 auto memc = &test.memc;
57 memcached_server_fn fptr[] = {&ping_callback};
58
59 REQUIRE_SUCCESS(memcached_server_cursor(memc, fptr, nullptr, 1));
60 }
61
62 SECTION("flush") {
63 auto test = MemcachedCluster::network();
64
65 for (auto &server : test.cluster.getServers()) {
66 memcached_return_t rc;
67 REQUIRE(libmemcached_util_flush("localhost", get<int>(server.getSocketOrPort()), &rc));
68 REQUIRE_SUCCESS(rc);
69 }
70 }
71
72 SECTION("pool") {
73 SECTION("deprecated") {
74 auto conf = "--SERVER=host10.example.com --SERVER=host11.example.com --SERVER=host10.example.com --POOL-MIN=10 --POOL-MAX=32";
75 auto pool = memcached_pool(S(conf));
76 REQUIRE(pool);
77
78 memcached_return_t rc;
79 auto memc = memcached_pool_pop(pool, false, &rc);
80 REQUIRE(memc);
81 REQUIRE(MEMCACHED_SUCCESS == rc);
82
83 REQUIRE(MEMCACHED_SUCCESS == memcached_pool_push(pool, memc));
84 REQUIRE(nullptr == memcached_pool_destroy(pool));
85 }
86
87 SECTION("basic") {
88 auto test = MemcachedCluster::mixed();
89 auto memc = &test.memc;
90 memcached_return_t rc;
91
92 constexpr auto POOL_MIN = 5;
93 constexpr auto POOL_MAX = 10;
94
95 auto pool = memcached_pool_create(memc, POOL_MIN, POOL_MAX);
96 REQUIRE(pool);
97
98 array<memcached_st *, POOL_MAX> hold{};
99 for (auto &h : hold) {
100 h = memcached_pool_fetch(pool, nullptr, &rc);
101 REQUIRE_SUCCESS(rc);
102 REQUIRE(h);
103 }
104
105 SECTION("depleted") {
106 REQUIRE(nullptr == memcached_pool_fetch(pool, nullptr, &rc));
107 REQUIRE_RC(MEMCACHED_NOTFOUND, rc);
108 }
109
110 SECTION("usable") {
111 for (auto h : hold) {
112 auto s = to_string(reinterpret_cast<uintptr_t>(h));
113 REQUIRE_SUCCESS(memcached_set(h, s.c_str(), s.length(), s.c_str(), s.length(), 0, 0));
114 }
115 for (auto h : hold) {
116 auto s = to_string(reinterpret_cast<uintptr_t>(h));
117 Malloced val(memcached_get(h, s.c_str(), s.length(), nullptr, nullptr, &rc));
118 REQUIRE_SUCCESS(rc);
119 REQUIRE(*val);
120 REQUIRE(h == reinterpret_cast<memcached_st *>(stoul(*val)));
121 }
122
123 REQUIRE_SUCCESS(memcached_set(hold[0], S(__func__), "0", 1, 0, 0));
124 uint64_t inc = 0;
125 for (auto h : hold) {
126 uint64_t val;
127 REQUIRE_SUCCESS(memcached_increment(h, S(__func__), 1, &val));
128 CHECK(++inc == val);
129 }
130 }
131
132 SECTION("behaviors") {
133 uint64_t val;
134 REQUIRE_SUCCESS(memcached_pool_behavior_get(pool, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK, &val));
135 REQUIRE_FALSE(val == 9999);
136 REQUIRE_SUCCESS(memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK, 9999));
137 REQUIRE_SUCCESS(memcached_pool_behavior_get(pool, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK, &val));
138 REQUIRE(val == 9999);
139
140 for (auto &h : hold) {
141 REQUIRE_FALSE(9999 == memcached_behavior_get(h, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK));
142 REQUIRE_SUCCESS(memcached_pool_release(pool, h));
143 h = memcached_pool_fetch(pool, nullptr, &rc);
144 REQUIRE_SUCCESS(rc);
145 REQUIRE(h);
146 REQUIRE(9999 == memcached_behavior_get(h, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK));
147 REQUIRE_FALSE(9999 == memcached_behavior_get(h, MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK));
148 REQUIRE_SUCCESS(memcached_pool_release(pool, h));
149 }
150
151 REQUIRE_SUCCESS(memcached_pool_behavior_get(pool, MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK, &val));
152 REQUIRE_FALSE(val == 9999);
153 REQUIRE_SUCCESS(memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK, 9999));
154 REQUIRE_SUCCESS(memcached_pool_behavior_get(pool, MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK, &val));
155 REQUIRE(val == 9999);
156
157 for (auto &h : hold) {
158 h = memcached_pool_fetch(pool, nullptr, &rc);
159 REQUIRE_SUCCESS(rc);
160 REQUIRE(h);
161 REQUIRE(9999 == memcached_behavior_get(h, MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK));
162 }
163 }
164
165 for (auto h : hold) {
166 REQUIRE_SUCCESS(memcached_pool_release(pool, h));
167 auto again = memcached_pool_fetch(pool, nullptr, &rc);
168 REQUIRE_SUCCESS(rc);
169 REQUIRE(again);
170 REQUIRE_SUCCESS(memcached_pool_release(pool, again));
171 }
172
173 REQUIRE(memc == memcached_pool_destroy(pool));
174 }
175 }
176 }