1 #include "test/lib/common.hpp"
2 #include "test/lib/MemcachedCluster.hpp"
4 #include "libmemcached/instance.hpp"
5 #include "libmemcachedutil/common.h"
7 static memcached_return_t
ping_callback(const memcached_st
*, const memcached_instance_st
*instance
, void *) {
10 REQUIRE(libmemcached_util_ping(memcached_server_name(instance
), memcached_server_port(instance
), &rc
));
11 REQUIRE(rc
== MEMCACHED_SUCCESS
);
12 return MEMCACHED_SUCCESS
;
15 TEST_CASE("memcached_util") {
16 SECTION("version_check") {
17 auto test
= MemcachedCluster::mixed();
18 auto memc
= &test
.memc
;
20 REQUIRE_SUCCESS(memcached_version(memc
));
21 REQUIRE(libmemcached_util_version_check(memc
, 0, 0, 0));
22 REQUIRE_FALSE(libmemcached_util_version_check(memc
, 255, 255, 255));
24 auto instance
= memcached_server_instance_by_position(memc
, 0);
25 REQUIRE(libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, instance
->micro_version
));
26 if (instance
->micro_version
) {
27 REQUIRE(libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, instance
->micro_version
- 1));
28 if (instance
->micro_version
< 255) {
29 REQUIRE_FALSE(libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, instance
->micro_version
+ 1));
32 if (instance
->minor_version
) {
33 REQUIRE(libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
- 1, 255));
34 if (instance
->minor_version
< 255) {
35 REQUIRE_FALSE(libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
+ 1, 0));
38 if (instance
->major_version
) {
39 REQUIRE(libmemcached_util_version_check(memc
, instance
->major_version
- 1, 255, 255));
40 if (instance
->major_version
< 255) {
41 REQUIRE_FALSE(libmemcached_util_version_check(memc
, instance
->major_version
+ 1, 0, 0));
47 auto test
= MemcachedCluster::network();
48 memcached_return_t rc
;
50 for (auto &server
: test
.cluster
.getServers()) {
51 REQUIRE(server
.getPid() == libmemcached_util_getpid("localhost", get
<int>(server
.getSocketOrPort()), &rc
));
55 REQUIRE(-1 == libmemcached_util_getpid("localhost", 1, &rc
));
56 REQUIRE(memcached_fatal(rc
));
60 auto test
= MemcachedCluster::network();
61 auto memc
= &test
.memc
;
62 memcached_server_fn fptr
[] = {&ping_callback
};
64 REQUIRE_SUCCESS(memcached_server_cursor(memc
, fptr
, nullptr, 1));
68 auto test
= MemcachedCluster::network();
70 for (auto &server
: test
.cluster
.getServers()) {
71 memcached_return_t rc
;
72 REQUIRE(libmemcached_util_flush("localhost", get
<int>(server
.getSocketOrPort()), &rc
));
78 SECTION("deprecated") {
79 auto conf
= "--SERVER=host10.example.com --SERVER=host11.example.com --SERVER=host10.example.com --POOL-MIN=10 --POOL-MAX=32";
80 auto pool
= memcached_pool(S(conf
));
83 memcached_return_t rc
;
84 auto memc
= memcached_pool_pop(pool
, false, &rc
);
86 REQUIRE(MEMCACHED_SUCCESS
== rc
);
88 REQUIRE(MEMCACHED_SUCCESS
== memcached_pool_push(pool
, memc
));
89 REQUIRE(nullptr == memcached_pool_destroy(pool
));
93 auto test
= MemcachedCluster::mixed();
94 auto memc
= &test
.memc
;
95 memcached_return_t rc
;
97 constexpr auto POOL_MIN
= 5;
98 constexpr auto POOL_MAX
= 10;
100 auto pool
= memcached_pool_create(memc
, POOL_MIN
, POOL_MAX
);
103 array
<memcached_st
*, POOL_MAX
> hold
{};
104 for (auto &h
: hold
) {
105 h
= memcached_pool_fetch(pool
, nullptr, &rc
);
110 SECTION("depleted") {
111 REQUIRE(nullptr == memcached_pool_fetch(pool
, nullptr, &rc
));
112 REQUIRE_RC(MEMCACHED_NOTFOUND
, rc
);
116 for (auto h
: hold
) {
117 auto s
= to_string(reinterpret_cast<uintptr_t>(h
));
118 REQUIRE_SUCCESS(memcached_set(h
, s
.c_str(), s
.length(), s
.c_str(), s
.length(), 0, 0));
120 for (auto h
: hold
) {
121 auto s
= to_string(reinterpret_cast<uintptr_t>(h
));
122 Malloced
val(memcached_get(h
, s
.c_str(), s
.length(), nullptr, nullptr, &rc
));
125 REQUIRE(h
== reinterpret_cast<memcached_st
*>(stoul(*val
)));
128 REQUIRE_SUCCESS(memcached_set(hold
[0], S(__func__
), "0", 1, 0, 0));
130 for (auto h
: hold
) {
132 REQUIRE_SUCCESS(memcached_increment(h
, S(__func__
), 1, &val
));
137 SECTION("behaviors") {
139 REQUIRE_SUCCESS(memcached_pool_behavior_get(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, &val
));
140 REQUIRE_FALSE(val
== 9999);
141 REQUIRE_SUCCESS(memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, 9999));
142 REQUIRE_SUCCESS(memcached_pool_behavior_get(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, &val
));
143 REQUIRE(val
== 9999);
145 for (auto &h
: hold
) {
146 REQUIRE_FALSE(9999 == memcached_behavior_get(h
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
));
147 REQUIRE_SUCCESS(memcached_pool_release(pool
, h
));
148 h
= memcached_pool_fetch(pool
, nullptr, &rc
);
151 REQUIRE(9999 == memcached_behavior_get(h
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
));
152 REQUIRE_FALSE(9999 == memcached_behavior_get(h
, MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK
));
153 REQUIRE_SUCCESS(memcached_pool_release(pool
, h
));
156 REQUIRE_SUCCESS(memcached_pool_behavior_get(pool
, MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK
, &val
));
157 REQUIRE_FALSE(val
== 9999);
158 REQUIRE_SUCCESS(memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK
, 9999));
159 REQUIRE_SUCCESS(memcached_pool_behavior_get(pool
, MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK
, &val
));
160 REQUIRE(val
== 9999);
162 for (auto &h
: hold
) {
163 h
= memcached_pool_fetch(pool
, nullptr, &rc
);
166 REQUIRE(9999 == memcached_behavior_get(h
, MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK
));
170 for (auto h
: hold
) {
171 REQUIRE_SUCCESS(memcached_pool_release(pool
, h
));
172 auto again
= memcached_pool_fetch(pool
, nullptr, &rc
);
175 REQUIRE_SUCCESS(memcached_pool_release(pool
, again
));
178 REQUIRE(memc
== memcached_pool_destroy(pool
));